ApexでLambdaをデプロイする

awsnode.js

ApexでLambdaをデプロイする。 とても簡単に使えるし、変なこともしないので良い感じ。


Serverless Frameworkだとeventの設定までカバーできてより便利。

Serverless FrameworkでLambdaをデプロイする - sambaiz-net


インストール。ダウンロードして実行できるようにしている。

$ curl https://raw.githubusercontent.com/apex/apex/master/install.sh | sh
  • IAMFullAccess
  • AWSLambdaFullAccess

を付けたIAMのプロファイルを登録しておく。

$ aws configure --profile apex
$ aws configure list --profile apex
      Name                    Value             Type    Location
      ----                    -----             ----    --------
   profile                     apex           manual    --profile
access_key     ****************OVGQ shared-credentials-file    
secret_key     ****************oi5t shared-credentials-file    
    region           ap-northeast-1      config-file    ~/.aws/config

apex init してnameとdescriptionを入れるとIAMが登録され、 ディレクトリ構造が作られる。

$ apex init --profile apex
Project name: try-apex
Project description: test  
[+] creating IAM try-apex_lambda_function role
[+] creating IAM try-apex_lambda_logs policy
[+] attaching policy to lambda_function role.
[+] creating ./project.json
[+] creating ./functions

package.jsonで環境変数などの設定ができる。

$ ls -R
functions	project.json

./functions:
hello

./functions/hello:
index.js

$ cat project.json 
{
  "name": "try-apex",
  "description": "test",
  "memory": 128,
  "timeout": 5,
  "role": "arn:aws:iam::524580158183:role/try-apex_lambda_function",
  "environment": {}
}

apex deploy するとlambdaが作られる。–dry-run もできる。 バージョン管理されているのでrollbackもできる。

$ apex deploy hello --profile apex
   • creating function         env= function=hello
   • created alias current     env= function=hello version=1
   • function created          env= function=hello name=try-apex_hello version=1

$ apex list

  hello
    runtime: nodejs6.10
    memory: 128mb
    timeout: 5s
    role: arn:aws:iam::*****:role/try-apex_lambda_function
    handler: index.handle
    arn: arn:aws:lambda:ap-northeast-1:*****:function:try-apex_hello:current
    aliases: current@v1

マネジメントコンソールではここでバージョンが確認できる。

バージョンの確認

apex invoke で実行。標準入力でイベントを渡せる。

$ apex invoke hello
{"hello":"world"}

パッケージに含めないファイルは.apexignoreに書く。