
本記事の目的
以下の記事で、Cloud Shellを使ってOracle Functionsを実行できるようになったことを知ったので、実際に試してみた。
・[OCI]Cloud Shellを使ってOracle Functionsサンプルを動かしてみた。
手順は上記の記事に画面ショット付きで分かりやすく解説されているので、本記事は主に自分用のメモ。
手順
事前準備
- 仮想クラウド・ネットワークの作成
- IAMポリシーの作成
サービスコンソール>開発者サービス>ファンクションと選択すると、「Function Prerequisites」の中に手順(以下、抜粋)が記載されているのでそれに沿って実施すればOK
Before you can start using Functions, your tenancy administrator should set up the following network and policies.
Step 1) Create VCN/Subnets
Create VCN/Subnets using Virtual Cloud Networks > Start VCN Wizard > VCN with Internet Connectivity option.
Step 2) Create Policies
Create a policy in the root compartment with the following statements:
2.1 Service Policies
- allow service FAAS to use virtual-network-family in tenancy
- allow service FAAS to read repos in tenancy
2.2 Non Admin User Policies
- allow group <non-admin-fn-devs> to manage repos in tenancy
- allow group <non-admin-fn-devs> to use virtual-network-family in tenancy
- allow group <non-admin-fn-devs> to manage functions-family in tenancy
- allow group <non-admin-fn-devs> to read metrics in tenancy
- allow group <non-admin-fn-devs> to use cloud-shell in tenancy
Launch Cloud Shell / アプリケーション作成とクラウドシェルの起動
サービスコンソール>開発者サービス>ファンクション>「アプリケーションの作成」
作成したアプリケーションから「ファンクションの作成」を選択すると、自分の環境にあわせたコマンド付のチュートリアルが表示される。この手順を参考に実施していく。
今回の作業はクラウドシェルを利用。
Use the context for your region / 利用リージョン設定
fn list context
fn use context ap-tokyo-1
Update the context with the function’s compartment ID / コンパートメント登録
fn update context oracle.compartment-id ocid1.compartment.xxxxx
Update the context with the location of the OCI Registry you want to use / リポジトリ登録
fn update context registry <region-key>.ocir.io/<tenancy-namespace>/<repo-name>
リポジトリを作ってない場合は、以下のマニュアルを参考に作成
Creating a Repository
リポジトリ名:repo-name
アクセス:プライベート
Generate an AuthToken / トークン作成
サービス・コンソール上のリンクからAuthTokenを作成
Log into OCIR using the Auth Token as your password / リポジトリへのログイン
docker login -u ‘<tenancy-namespace>/<user-name>’ <region-key>.ocir.io
Verify your setup by listing Function applications in the compartment / アプリケーション名の確認
fn list apps
⇒以下のようにSampleFunctionsが表示されることを確認。今回はこのfnを利用。
NAME ID
SampleFunctions ocid1.fnapp.oc1.ap-tokyo-1.xxxx
Generate a ‘hello-world’ boilerplate function / ファンクション作成(まずは箱だけ)
fn init –runtime java hello-java
Switch into the generated directory / ファンクションのフォルダに移動
cd hello-java
Deploy your function / アプリケーションへのファンクション登録
fn -v deploy –app SampleFunctions
Invoke your function / ファンクション実行
fn invoke SampleFunctions hello-java
⇒「Hello, world!」が表示されたらOK
TIPS
・fn invoke SampleFunctions hello-java実行時に以下のエラー発生
$ fn invoke SampleFunctions hello-java
Error invoking function. status: 502 message: dhcp options ocid1.dhcpoptions.oc1.ap-tokyo-1.xxxx does not exist or Oracle Functions is not authorized to use it
マニュアルに載ってる以下のポリシーをRootコンパートメント配下に追加して解決
Allow service FaaS to read repos in tenancy
Allow service FaaS to use virtual-network-family in tenancy
・2回目以降は、Cloud Shellを起動して「fn invoke SampleFunctions hello-java」で実行可能。
・開発者サービス>レジストリ内に今回作成した「hello-java」が登録されていた。
・ファンクションはOCI Monitoringに対応。実行回数やエラー回数などが確認可能
・作成したファンクションは、アプリケーション>ファンクションから削除可能
・Functionsの実行環境は以下の3つが用意されている。
Different Options for Function Development Environments
Option 1: Setting up Cloud Shell. (Recommended)
Option 2: Setting up a local machine
Option 3: Setting up an Oracle Cloud Infrastructure compute instance
参考情報
・[OCI]Cloud Shellを使ってOracle Functionsサンプルを動かしてみた。
・チュートリアル:Oracle Functions on Cloud Shell Set up, creation, and deployment
・My Quick Start with Oracle Functions in Fresh OCI Tenancy
Leave a Reply