
本記事の目的
前回の記事では、Cloud Shellを使って簡単にOracle Functionsのサンプルを動かしてみました。
今回はGitHubのoracle-functions-samplesに登録されているサンプルプログラムを利用して、Oracle Functionsから任意のCompute Instanceの管理(起動・停止)を実施する手順を確認してみました。
手順
今回は「oci-compute-control-python」を利用し。基本的に、リンク先にあるReadmeに沿って実施すれば良いはず。
事前準備
- A – Set up your tenancy
- B – Create application
- C – Set up your Cloud Shell dev environment
A,Cに関しては、前回の記事で既に設定済み。Bに関して、今回は新しいアプリケーション名で設定してみた。
さらに、ファンクションから他のサービスリソースを操作できるようにするために動的グループの設定を追加。
・Create or Update your Dynamic Group
In order to use other OCI Services, your function must be part of a dynamic group. For information on how to create a dynamic group, refer to the documentation.When specifying the Matching Rules, we suggest matching all functions in a compartment with:
ALL {resource.type = ‘fnfunc’, resource.compartment.id = ‘ocid1.compartment.oc1..aaaaaxxxxx’}
・Create or Update IAM Policies
Create a new policy that allows the dynamic group to manage compute instances. We will grant manage access to instances in the compartment.Allow dynamic-group <dynamic-group-name> to manage instances in compartment <compartment-name>
ファンクション登録準備
ここからは前回の記事の流れと同様に、ファンクション作成から実施していく。
$ fn init --runtime python oci-compute-control Creating function at: ./oci-compute-control Function boilerplate generated. func.yaml created.
⇒oci-compute-controlという名前でファンクションを作成。
$ cd oci-compute-control $ ls func.py func.yaml requirements.txt
⇒ファンクション名に指定した名前のフォルダ内にサンプルソースが作成されるので、GitHubにあるソースの内容に手動で書き換え。
Deploy the function
$ fn -v deploy -app oci-compute-control Deploying oci-compute-control to app: oci-compute-control (省略)
⇒アプリケーション名=oci-compute-controlに登録
※ファンクション名とアプリケーション名を同じにしてしまったが、変更した方が分かりやすそう。
動作確認
本ファンクションの実行コマンドは以下の通り。
echo '{"command":"<command>", "instance_ocid":"<instance-ocid>"}' | fn invoke <app-name> <function-name>
任意のコンピュート・サービスのOCIDを指定して、まずは起動してみる。
$ echo '{"command":"start", "instance_ocid":"ocid1.instance.oc1.ap-tokyo-1.xxxxxx"}' | fn invoke oci-compute-control oci-compute-control {"status": "STARTING"}
⇒status=STARTINGとなり、該当Compute Instanceの起動が開始。
サービス・コンソールで確認して、起動中となっていることを確認。その後、無事起動を確認。
続いて、停止処理の確認。
$ echo '{"command":"stop", "instance_ocid":"ocid1.instance.oc1.ap-tokyo-1.xxxxx"}' | fn invoke oci-compute-control oci-compute-control {"status": "STOPPING"}
⇒停止コマンドも実施できることを確認。
結論
・Oracle Functionsは、とても便利で使いやすい!
・仕組みを理解する上でも、サンプルプログラムをまず触ってみるのがお奨め。
TIPS
動的グループの設定をしなかった場合のエラー
$ echo '{"command":"stop", "instance_ocid":"ocid1.instance.oc1.xxxxx"}' | fn invoke oci-compute-control oci-compute-control Error invoking function. status: 502 message: function faile
⇒このエラーメッセージから原因を類推するのは難しい印象。
参考情報
・Accessing Other Oracle Cloud Infrastructure Resources from Running Functions
・Troubleshooting Oracle Functions
変更履歴
2021/12/1 GitHubのサンプルスクリプトのリンク修正
Leave a Reply