1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ServerlessFrameworkでAPI GatewayとLambda資材をgit管理する

Posted at

はじめに

  • プロジェクト名はとりあえず「test-lambda」にする
  • AWSでLambdaに設定するロールを作成する
  • 下記を参考にServerlessFreamworkのインストールとAWSの設定をする

やること

  • ディレクトリ構成を参考に必要なものを配置する
  • serverless.ymlを書く
  • 「sls deploy」でデプロイを行う
  • デプロイに問題なければgitに入れて終わり

ServerlessFreamworkのディレクトリ構成

test-lambda
├─lambda(このディレクトリ下にlambdaの各関数を配置)
| ├─user
| | └─index.js
| └─user-regist
|   └─index.js
├─model(APIGateway Request Validationで使用するモデルを配置)
| └─User.json
├─hoge(レイヤーの名称)
| └─nodejs(このディレクトリ下でレイヤーに追加するライブラリをインストールする)
└─serverless.yml

ServerlessFreamworkの設定ファイル

serverless.yml
service: test-lambda
frameworkVersion: '3'

provider:
  name: aws
  runtime: nodejs16.x
#  stage: ${opt:stage, 'dev'}
  stage: dev
  region: ap-northeast-1
  memorySize: 128
  architecture: arm64
  # API Gatewayの名称
#  apiName: ${self:service}-${sls:stage}
  apiName: ${self:service}
  endpointType: REGIONAL
  iam:
    role: "arn:aws:iam::xxxxxxxxxx:role/test-lambda-role"
#  environment:
#    variable1: value1

package:
  individually: true
  exclude:
    - .git/**
    - test/**
    - README.md
    - lambda/**
    - model/**
    - hoge/**
#  include:
#    - lambda/common/**
  excludeDevDependencies: true

# Lambda Layerの設定
layers:
  hoge:
    path: hoge
    compatibleRuntimes:
      - ${self:provider.runtime}

# Lambdaの設定
functions:
  # ユーザー情報取得
  user:
    # 関数の格納場所
    handler: lambda/user/index.handler
    # 関数名
    name: user
    # API Gatewayの設定
    events:
      - http:
          path: /user
          method: get
#          authorizer: aws_iam
          cors:
            origin:
              - '*'
            headers:
              - Content-Type
              - X-Amz-Date
              - Authorization
              - X-Api-Key
              - X-Amz-Security-Token
          # APIGatewayのRequest Validation(パラメータのチェック)
          request:
            parameters:
              querystrings:
                name: true
                email: true
    package:
      include:
        - lambda/user/**
  # ユーザー情報登録
  user-regist:
    # 関数の格納場所
    handler: lambda/user-regist/index.handler
    # 関数名
    name: user-regist
    # Lanbda Layerの設定
    layers:
      # レイヤー名は対象レイヤーのアッパーキャメルケース + 「LambdaLayer」
      - !Ref HogeLambdaLayer
    # API Gatewayの設定
    events:
      - http:
          path: /user
          method: post
#          authorizer: aws_iam
          cors:
            origin:
              - '*'
            headers:
              - Content-Type
              - X-Amz-Date
              - Authorization
              - X-Api-Key
              - X-Amz-Security-Token
          # APIGatewayのRequest Validation(body部のチェック)
          request:
            schemas:
              application/json:
                schema: ${file(model/User.json)}
                name: User
                description: 'Validation model'
    package:
      include:
        - lambda/user-regist/**

APIGateway Request Validationで使用するモデルのサンプル

User.json
{
    "title": "User",
    "type": "object",
    "properties": {
        "name": {
            "type": "string",
            "minLength": 1
        },
        "email_address": {
            "type": "string",
            "pattern": "^[a-zA-Z0-9_+-]+(\\.[a-zA-Z0-9_+-]+)*@{1}[A-Za-z0-9_.-]+.[A-Za-z0-9]+$"
        }
    },
    "required": ["name"]
}

※モデルJSONの参考サイト

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?