概要
AWS
のlambda
はAWS Management Console
内で直接書くこともできますが、
git
でソースコードを管理して、CLIからデプロイしたいと思うことはありませんか?
当記事ではServerlessを使って
Node.js
で実装したサンプルをlambda
にデプロイする手順の例を示します。
筆者がCLIでのAWS操作にあまりなれていなかったのもあり、本記事ではAWS Management Console
での操作手順もやや掘り下げて記載していますが、
Serverless
の基本機能はServerlessのメンテナでもある@horike37さんの下記の記事により詳しくまとまっているので、そちらを参照いただくのが良いと思いました。
Serverless Frameworkの使い方まとめ
awscliのインストール
$ brew install awscli
デプロイユーザーのアクセスキーの取得
デプロイに利用するアカウントでawscliでログインするために、AWS Management Console
でユーザーの認証情報を取得します。
-
IAM
->Users
->デプロイに使うユーザー
->Security credentials
->Create access key
-
Access key ID
とSecret access key
を保管する。(Download .csv file
でCSVをダウンロードしておくのが良いでしょう。)
デプロイユーザーへの権限付与
serverlessでlambdaをデプロイするには、デプロイユーザーにデプロイ用の権限が必要です。
ここでは、ポリシーの設定手順の例を示します。
-
IAM
->Policies
->Create policy
をクリック ->JSON
タブをクリック - 下記を貼り付け
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"iam:*",
"apigateway:*",
"s3:*",
"logs:*",
"lambda:*",
"cloudformation:*"
],
"Resource": "*"
}
]
}
※Action
やResource
は、セキュリティの観点からは必要なポリシーやリソースをもっと限定的に指定するのが好ましいですが、数が多くて細部の洗い出しが大変だったため、ワイルドカードでざっくりと指定をしました。
3. ポリシー名は任意ですが、ここでは例としてlambdaDeployPolicy
としてポリシーを作成します。
4. IAM
-> Users
-> デプロイユーザーを選択 -> Add permissions
-> Attach existing policies directly
5. 3.
で作成したlambdaDeployPolicy
を選択し、Next: Review
-> Add permissions
でポリシーを追加
awscliにデプロイユーザーとしてログイン
$ aws configure
AWS Access Key ID [None]: <Access key IDを貼り付け>
AWS Secret Access Key [None]: <Secret access key> を貼り付け
Default region name [None]: ap-northeast1
Default output format [None]: <そのままエンター>
serverlessのインストール
$ npm i -g serverless
serverlessプロジェクトの生成
$ sls create --template aws-nodejs --path lambda-deploy-serverless
$ cd lambda-deploy-serverless
serverlessの設定ファイルを更新
デフォルトだとリージョンがus-east1
になっていたり、APIゲートウェイの設定は含まれていないので、下記のように指定を加えます
service: lambda-deploy-serverless
provider:
name: aws
runtime: nodejs8.10
stage: dev
region: ap-northeast-1 # ap-northeast-1リージョンにデプロイする
functions:
hello: # 関数名の指定
handler: handler.hello
events:
- http: # API Gatewayの指定
path: hello
method: get
その他の設定はこちらのリファレンスを参照:
https://serverless.com/framework/docs/providers/aws/guide/serverless.yml/
デプロイ
$ sls deploy
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service lambda-deploy-serverless.zip file to S3 (45.3 KB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
........................
Serverless: Stack update finished...
Service Information
service: lambda-deploy-serverless
stage: dev
region: ap-northeast-1
stack: lambda-deploy-serverless-dev
resources: 10
api keys:
None
endpoints:
GET - https://**********.execute-api.ap-northeast-1.amazonaws.com/dev/hello
functions:
hello: lambda-deploy-serverless-dev-hello
layers:
None
もしもエラーになる場合は、ログのエラー箇所を参照して、ポリシーが適切に設定されているか確認して下さい。
詳細ログを標準出力に出力させるには、下記のようにします
SLS_DEBUG=* serverless deploy