LoginSignup
5
3

More than 5 years have passed since last update.

serverlessでCLIからlambdaをデプロイする

Last updated at Posted at 2019-04-13

概要

AWSlambdaAWS 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でユーザーの認証情報を取得します。

  1. IAM->Users -> デプロイに使うユーザー -> Security credentials -> Create access key image.png
  2. Access key IDSecret access key を保管する。(Download .csv fileでCSVをダウンロードしておくのが良いでしょう。) image.png

デプロイユーザーへの権限付与

serverlessでlambdaをデプロイするには、デプロイユーザーにデプロイ用の権限が必要です。
ここでは、ポリシーの設定手順の例を示します。

  1. IAM->Policies->Create policy をクリック ->JSON タブをクリック
  2. 下記を貼り付け
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "iam:*",
                "apigateway:*",
                "s3:*",
                "logs:*",
                "lambda:*",
                "cloudformation:*"
            ],
            "Resource": "*"
        }
    ]
}

ActionResourceは、セキュリティの観点からは必要なポリシーやリソースをもっと限定的に指定するのが好ましいですが、数が多くて細部の洗い出しが大変だったため、ワイルドカードでざっくりと指定をしました。
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ゲートウェイの設定は含まれていないので、下記のように指定を加えます

serverless.yml(更新)
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

このようにLambdaがデプロイされていれば成功です
image.png

もしもエラーになる場合は、ログのエラー箇所を参照して、ポリシーが適切に設定されているか確認して下さい。

詳細ログを標準出力に出力させるには、下記のようにします

SLS_DEBUG=* serverless deploy
5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3