#chiroito ’s blog

Java を中心とした趣味の技術について

AWS Lambda のファンクションを Fn へ変換する

Fn Project では AWS 社が提供する Lambda からファンクションをインポートできます。今回の例では、Lambda でファンクションを作成する際に記載されているコードを FnProject のファンクションへ変換します。 f:id:chiroito:20171014123039p:plain

インポートにはARR・AWSのリージョンが必要です。ARN はAWSのコンソールから確認できます。

Lambdaのインポート

AWS Lambda をファンクションとしてインポートするにはfn lambda aws-importコマンドを実行します。コマンドの引数はARN、リージョン、Dockerイメージ名を指定します。

今回の例では以下の設定値を使用します。

  • ARNはarn:aws:lambda:ap-northeast-1:875xxxxxx897:function:node_lambda
  • リージョンはap-northeast-1
  • Dockerイメージ名はchiroito/node_lambda
>fn lambda aws-import arn:aws:lambda:ap-northeast-1:875xxxxxx897:function:node_lambda ap-northeast-1 chiroito/node_lambda
Extracting 'index.js' to 'node_lambda\index.js'
Creating func.yaml ... OK

今回の例で作成されたファイルは以下の 2 つです。

>ls node_lambda
func.yaml  index.js

index.jsは同一になります。設定ファイルfunc.yamlは以下のとおりです。

>cat node_lambda\func.yaml
name: chiroito/node_lambda
version: 0.0.1
runtime: lambda-nodejs4.3
cmd: index.handler
path: /node_lambda

ファンクションのデプロイと実行

デプロイはインポートしてないファンクションと同様にfn deployを実行します。

>cd node_lambda
>fn deploy --local --app myapp
Deploying chiroito/node_lambda to app: myapp at path: /node_lambda
Bumped to version 0.0.1
Building image chiroito/node_lambda:0.0.1
Sending build context to Docker daemon  4.096kB
Step 1/4 : FROM funcy/lambda:node-4
 ---> 3a6ec020ee3a
Step 2/4 : WORKDIR /function
 ---> Using cache
 ---> 6a95ee7e1388
Step 3/4 : ADD . /function/
 ---> 6dc13dba226d
Removing intermediate container c6b8d8f5aa19
Step 4/4 : CMD index.handler
 ---> Running in c6fefb7d44fc
 ---> b2fa0eb924ff
Removing intermediate container c6fefb7d44fc
[Warning] One or more build-args [HTTPS_PROXY HTTP_PROXY] were not consumed
Successfully built b2fa0eb924ff
Successfully tagged chiroito/node_lambda:0.0.1
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
Updating route /node_lambda using image chiroito/node_lambda:0.0.1...

実行すると、Lambdaで実行した場合と同じ結果"Hello from Lambda"が得られます。

>fn call myapp /node_lambda
"Hello from Lambda"