0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

IBM Cloud Functions を使って IBM Cloud Object Storage (ICOS) アップロードをメール通知する

Last updated at Posted at 2020-08-28

概要

こんなイメージです。
ICOS に適当なファイルをアップロードをするだけでもよいですが、
せっかくなので Power Systems Virtual Server on IBM Cloud で Image Capture を取得して ICOS に Export する機能を使って動作することを確認します。

Kobito.hqpRHK.png

事前準備

CLI を使って準備を進めます。

CLI プラグインインストール


ibmcloud plugin install cloud-functions
ibmcloud plugin show cloud-functions

CLI ログイン


export SOURCE_REGION="us-east"
ibmcloud login -a cloud.ibm.com -r $SOURCE_REGION

環境設定


export FN_NAMESPACE="khayama-fn-us-east"
export COS_INSTANCE_NAME="khayama-cos" # 既存の ICOS インスタンスを使います

Functions Namespace 設定

namespace を作成してプロパティをセットします。


ibmcloud fn namespace create $FN_NAMESPACE
ibmcloud fn namespace target $FN_NAMESPACE

Functions から ICOS へのアクセスを許可

ICOS の変更通知を受信してトリガーできるように以下の許可設定をおこないます。


ibmcloud iam authorization-policy-create \
functions \
--source-service-instance-name $FN_NAMESPACE \
cloud-object-storage \
--target-service-instance-name $COS_INSTANCE_NAME \
"Notifications Manager" 

Email Delivery, powered by Sendgrid の無料プランを購入

こちらのサービスを使ってメールを送信します。
IaaS を有効化済みのアカウントをお持ちであれば、25000 通まで無料の「Free Package」を活用できます。

Email Delivery, powered by Sendgrid - IBM Cloud : https://cloud.ibm.com/catalog/infrastructure/email-delivery
貼り付けた画像_2020_08_28_11_30.png

GitHub からダウンロード

GitHub にあるファイルをダウンロードします。


git clone https://github.com/khayama/fn-sendEmailCosWrite.git
cd fn-sendEmailCosWrite

今回の Functions デプロイ用 yaml では以下のように定義しています。
Functions 実行パラメータは、環境変数の値を取得して埋め込む形で定義しています。

sendEmailCosWrite.yml

packages:
  sendEmailCosWrite:
    version: 1.0
    license: Apache-2.0
    actions:
      sendEmailCosWrite:
        version: 1.0
        description: sendEmail with SL_USER, SL_APIKEY when cos object write
        docker: openwhisk/dockerskeleton
        function: sendEmailCosWrite.sh
        inputs:
          SL_USER: $SL_USER
          SL_APIKEY: $SL_APIKEY
          TO: $TO
          FROM: 
                default: no-reply@ibmfunctions.com
          SUBJECT: 
                default: test subject
          BODY: 
                default: test body 1\ntest body 2\ntest body 3

    triggers:
      cosWrite:
        feed: /whisk.system/cos/changes
        inputs:
          bucket: $MY_BUCKET # The COS bucket must be regional and exist in the same location as the namespace
          event_types: write # https://cloud.ibm.com/docs/openwhisk?topic=openwhisk-pkg_obstorage#pkg_obstorage_ev_ch_ref_trig

    rules:
      sendEmail-cosWrite:
        description: Action is triggered by object-write operation
        action: sendEmailCosWrite
        trigger: cosWrite

Functions デプロイ

環境変数セット

以下のように環境変数をセットします。


export SL_USER="xxx"
export SL_APIKEY="xxx"
export TO="宛先メールアドレス"
export MY_BUCKET="khayama-power"

yaml ファイルを指定してデプロイ


ibmcloud fn deploy --manifest sendEmailCosWrite.yml 
# 削除するには ibmcloud fn undeploy --manifest sendEmailCosWrite.yml

動作確認

Power Systems Virtual Server on IBM Cloud で Image Capture を取得して ICOS に Export してみました。

Capturing and exporting a virtual machine (VM)

貼り付けた画像_2020_08_28_13_57.png
貼り付けた画像_2020_08_27_16_30.png

実際に ICOS にファイルアップロードをおこなわれ、Functions がトリガーされたかは以下の Monitor 画面で確認できます。

https://cloud.ibm.com/functions/dashboard

貼り付けた画像_2020_08_27_16_33.png

また、ICOS のバケットを見ても、以下のようにファイルの存在と最終変更日時について確認できます。

貼り付けた画像_2020_08_27_16_31.png

このような形で無事設定された通知メールを受信できました。

貼り付けた画像_2020_08_27_16_31.png

さいごに

これでICOSアップロードを自動でメール通知できたことが確認できました。
Power Systems Virtual Server on IBM Cloud のような Image Capture を取得して ICOS に Export するような機能で無事プロセスが完了したことを通知することに活用できると思います。

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?