Help us understand the problem. What is going on with this article?

Serverless Frameworkを使ったAPI Gateway + Lambda + DynamoDBのセットアップ方法まとめ

More than 3 years have passed since last update.

概要

Serverless Frameworkを使って、API Gateway + Lambda + DynamoDBのアプリケーションセットアップ方法をまとめる

初期セットアップ

インストールからプロジェクトの作成まで

npmでServerlessをインストール

$ npm install serverless -g

新規プロジェクトの作成

$ serverless project create

スクリーンショット 2016-03-15 18.16.40.png

こういうファイル構成ができている

コンポーネントとファンクションの作成

$ serverless component create component1
$ serverless function create component1/function1

visibleというコンポーネントを作り、searchwordというファンクションを作る

[horike@horiketakahiro-no-MacBook-Pro elasticommerce-search-service]$serverless component create visible
Serverless: Installing "serverless-helpers" for this component via NPM...  
Serverless: -----------------  
serverless-helpers-js@0.1.0 node_modules/serverless-helpers-js
└── dotenv@1.2.0
Serverless: -----------------  
Serverless: Successfully created new serverless component: visible  
[horike@horiketakahiro-no-MacBook-Pro elasticommerce-search-service]$serverless function create visible/searchword
Serverless: Successfully created function: "visible/searchword" 
[horike@horiketakahiro-no-MacBook-Pro elasticommerce-search-service]$tree
.
├── README.md
├── _meta
│   ├── resources
│   │   └── s-resources-cf-dev-apnortheast1.json
│   └── variables
│       ├── s-variables-common.json
│       ├── s-variables-dev-apnortheast1.json
│       └── s-variables-dev.json
├── admin.env
├── package.json
├── s-project.json
├── s-resources-cf.json
└── visible
    ├── lib
    │   └── index.js
    ├── node_modules
    │   └── serverless-helpers-js
    │       ├── README.md
    │       ├── env
    │       │   └── index.js
    │       ├── index.js
    │       ├── node_modules
    │       │   └── dotenv
    │       │       ├── Contributing.md
    │       │       ├── README.md
    │       │       ├── config.js
    │       │       ├── dotenv.png
    │       │       ├── lib
    │       │       │   └── main.js
    │       │       ├── package.json
    │       │       └── test
    │       │           ├── config.js
    │       │           └── main.js
    │       └── package.json
    ├── package.json
    ├── s-component.json
    └── searchword
        ├── event.json
        ├── handler.js
        └── s-function.json

14 directories, 30 files

API Gateway

API Gatewayのリクエストの受け渡し周りの設定

/path/to/project_root/s-templates.jsonを設置。プロジェクト全体で受け取るリクエストの値を決めます。

/path/to/project_root/s-templates.json
{
  "apiRequestTemplate": {
    "application/json": {
      "httpMethod": "$context.httpMethod",
      "body": "$input.json('$')",
      "queryParams" : "$input.params().querystring",
      "headerParams" : "$input.params().header",
      "headerParamNames" : "$input.params().header.keySet()",
      "contentTypeValue" : "$input.params().header.get('Content-Type')"
    }
  }

/path/to/project_root/visible/searchwords-templates.jsonを設置

/path/to/project_root/visible/searchword/s-templates.json
{
  "apiRequestTemplate": {
    "application/json": {
      "searchword": "$input.params('searchword')"
    }
  }
}

/path/to/project_root/visible/searchwords-function.jsonを修正
endpointspathrequestTemplatesを修正する

s-function.json
"endpoints": [
    {
      "path": "searchword/{searchword}",
      "method": "GET",
      "type": "AWS",
      "authorizationType": "none",
      "apiKeyRequired": false,
      "requestParameters": {},
      "requestTemplates": "$${apiRequestTemplate}",

Lambda

初期設定状態では複数のファンクションが/path/to/project_root/lib/index.jsを参照するようになっています。この状態では使いにくいので、index.jsを分割します。

/path/to/project_root/visible/searchword配下のファンクションを分割します。
index.jsをコピーしてリネームします。

$ cp /path/to/project_root/index.js /path/to/project_root/searchword.js

/path/to/project_root/visible/searchword/handler.jsを以下のように編集します

handler.js
// Require Logic
var lib = require('../lib/searchword.js');

DynamoDB

CloudFormationから起動します。/path/to/project_root/s-resources-cf.jsonに以下を記述してテーブルを定義します。

s-resources-cf.json
"ProductSerarchTotalvalueDynamoDBTable" : {
      "Type" : "AWS::DynamoDB::Table",
      "Properties" : {
        "AttributeDefinitions" : [
          {
            "AttributeName" : "SearchWord",
            "AttributeType" : "S"   
          },
          {
            "AttributeName" : "Date",
            "AttributeType" : "S"
          }
        ],
        "KeySchema" : [
          {
            "AttributeName" : "SearchWord",
            "KeyType" : "HASH"
          },
          {
            "AttributeName" : "Date",
            "KeyType" : "RANGE"
          }
        ],
        "ProvisionedThroughput" : {
          "ReadCapacityUnits" : "5",
          "WriteCapacityUnits" : "5"
        },
        "TableName" : "${project}-${stage}-ProductSerarchTotalvalue"
      }
    }
  }

LambdaからDynamoDBにアクセスできるようにIAMポリシーも追加

s-resources-cf.json
"PolicyName": "${stage}-${project}-lambda",
"PolicyDocument": {
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "logs:CreateLogGroup",
        "logs:CreateLogStream",
        "logs:PutLogEvents"
      ],
      "Resource": "arn:aws:logs:${region}:*:*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "*"
      ],
      "Resource": "arn:aws:dynamodb:${region}:*:table/${project}-${stage}-ProductSerarchTotalvalue"
    }
  ]
},
"Roles": [
  {
    "Ref": "IamRoleLambda"
  }
]

AWSへのデプロイ

CloudFormationを以下のコマンドで実行してIAMとDynamoDBをAWS上に展開

$ serverless resources deploy

API Gateway、Lambdaをデプロイ

$ serverless dash deploy
horike37
CEO of Serverless Operations, Inc Serverless Framework Core Maintainer
https://serverless.co.jp
serverless-operations
AWSクラウド技術の豊富な知見を活かし、サーバーレスによる開発や運用の支援、コンサルティングまで行う会社です
https://serverless.co.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした