36
32

More than 5 years have passed since last update.

AWS Lambdaデプロイ方法を求めて:Serverlessフレームワーク

Last updated at Posted at 2017-05-12

このドキュメントレベル:初めて学ぶ人向け

Lambdaの良いデプロイフローはないかと思って調べた記録です。
Lambdaのデプロイにはいくつか種類があるようです。

  • Serveless
  • Apex
  • Lamvery
  • LambCI
  • CodeShip

今回は「Serverless」について、どんなものかを触りだけ調べています。

Serverless

サーバーレスなアーキテクチャを容易に作成、管理できるフレームワークです
AWS Lambda, Apache OpenWhisk, Microsoft Azureなどをサポートしているようです。

デプロイイメージをつかむ

  • このnode.jsのサンプルを実際にやってみると感じがよくわかります。事前のAWSのCredentialsの設定をやっておきましょう

Hello World Node.js Example

Serverless Framework - AWS Lambda Guide - Credentials

豊富なサンプルコード

gutHubにサンプルコードがアップされているので、これを実行するだけでも感じがつかめます

serverless/examples: Serverless Examples – A collection of boilerplates and examples of serverless architectures built with the Serverless Framework and AWS Lambda

例)aws-node-rest-api-with-dynamodb/

cloneしてきて、deployしてみます

14:45:35 aws-node-rest-api-with-dynamodb/  $ ls
README.md   package.json    serverless.yml  todos


14:46:01 aws-node-rest-api-with-dynamodb/  $ npm install
aws-rest-with-dynamodb@1.0.0 /Users/bohebohechan/devel/src/gitlab.com/FirstFourNotes/serverless/aws-node-rest-api-with-dynamodb
└── uuid@2.0.3

npm WARN aws-rest-with-dynamodb@1.0.0 No repository field.

11:23:44 aws-node-rest-api-with-dynamodb/  $ sls deploy
Serverless: Packaging service...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
.....
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (22.45 KB)...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
......................................................................................................
Serverless: Stack update finished...
Service Information
service: serverless-rest-api-with-dynamodb
stage: dev
region: us-east-1
api keys:
  None
endpoints:
  POST - https://xxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/todos
  GET - https://xxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/todos
  GET - https://xxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/todos/{id}
  PUT - https://xxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/todos/{id}
  DELETE - https://xxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/todos/{id}
functions:
  create: serverless-rest-api-with-dynamodb-dev-create
  list: serverless-rest-api-with-dynamodb-dev-list
  get: serverless-rest-api-with-dynamodb-dev-get
  update: serverless-rest-api-with-dynamodb-dev-update
  delete: serverless-rest-api-with-dynamodb-dev-delete
11:25:19 aws-node-rest-api-with-dynamodb/  $

RestAPIができてしまったようです。

AWSコンソールで確認

実際にコンソールで確認してみましょう

> Lambda

lambda.png

> DynamoDB

キャプチャ撮り忘れたので割愛・・・

> API Gateway

apigateway.png

> S3

s3.png

> CloudFormation

cloudformation.png

> CloudWatch Logs

cloudwatchlogs.png

PostmanでAPIを実行してみます

> Post

postman-post.png

> Get

postman-list.png

後片付け

消しておきましょう

11:44:47 aws-node-rest-api-with-dynamodb/  $ sls remove
Serverless: Getting all objects in S3 bucket...
Serverless: Removing objects in S3 bucket...
Serverless: Removing Stack...
Serverless: Checking Stack removal progress...
..............................................................
Serverless: Stack removal finished...

このようなサンプルをカスタマイズしていくことで、自分の欲しい機能を簡単に作っていけそうです。

serverless.yml

先ほどの例)aws-node-rest-api-with-dynamodb/を参考にして、serverless.ymlの中身を紐解いてみましょう
解説はざっくりなので、詳細は公式ページで確認のことです。

AWSをプロバイダーにセットしたときに有効となるプロパティ一覧が載っています
Serverless Framework - AWS Lambda Guide - Serverless.yml Reference

  • service

プロジェクト名

  • frameworkVersion

フレームワークの対応バージョン

service: serverless-rest-api-with-dynamodb

frameworkVersion: ">=1.1.0 <2.0.0"
  • provider

AWS CloudFormation stack
サービスがデプロイされる対象について書きます。ここでは、AWSですよね 
 
- iamRoleStatements
How it works iamRoleStatements configuration section? - Serverless Framework - Serverless Forums

Lambda Functionで、AWSのリソースにアクションする際の許可をAWS IAM Roleで記述します。
「provider.iamRoleStatements」のプロパティに必要となる許可ステートメントを設定します。
今回は、Lambdaからdynamodbへの許可が必要ですね。

provider:
  name: aws
  runtime: nodejs6.10
  environment:
    DYNAMODB_TABLE: ${self:service}-${opt:stage, self:provider.stage}
  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:Query
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
      Resource: "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.DYNAMODB_TABLE}"

-Functions

Lambdaに作成するFunctionの定義を書きます

  • Events

Lambda Function の起動するトリガーを書きます
S3バケットへのアップロードや、SNSトピック受信や、HTTPのエンドポイントですね

サポートしているイベントの一覧はこちら
Serverless - AWS Lambda - Events

functions:
  create:
    handler: todos/create.create
    events:
      - http:
          path: todos
          method: post
          cors: true

  list:
    handler: todos/list.list
    events:
      - http:
          path: todos
          method: get
          cors: true

  get:
    handler: todos/get.get
    events:
      - http:
          path: todos/{id}
          method: get
          cors: true

  update:
    handler: todos/update.update
    events:
      - http:
          path: todos/{id}
          method: put
          cors: true

  delete:
    handler: todos/delete.delete
    events:
      - http:
          path: todos/{id}
          method: delete
          cors: true
  • Resources

AWS CloudFormation stackに追加することができます。
以下では、TodosDynamoDbTableを追加しています。
特定のCloudFormationのリソースに対して、値を上書きすることもできます

resources:
  Resources:
    TodosDynamoDbTable:
      Type: 'AWS::DynamoDB::Table'
      DeletionPolicy: Retain
      Properties:
        AttributeDefinitions:
          -
            AttributeName: id
            AttributeType: S
        KeySchema:
          -
            AttributeName: id

ワークフロー

Serverless Framework Guide - AWS Lambda - Workflow

Development Workflowとして以下の手順で回しましょう、といったことが書かれています。

  1. Write your functions
  2. Use serverless deploy only when you've made changes to serverless.yml and in CI/CD systems.
  3. Use serverless deploy function -f myFunction to rapidly deploy changes when you are working on a specific AWS Lambda Function.
  4. Use serverless invoke -f myFunction -l to test your AWS Lambda Functions on AWS.
  5. Open up a separate tab in your console and stream logs in there via serverless logs -f myFunction -t.
  6. Write tests to run locally.

参考になるドキュメント

36
32
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
36
32