LoginSignup
26
29

More than 5 years have passed since last update.

Dockerで Serverless Framework 環境構築

Posted at

Serverless Framework とは?

AWS のサービスの、Lambda、API Gateway、DynamoDB、S3、etc….
作成、管理、デプロイ が可能なツールです

ローカルでもLambda関数を実行可能で(node.js、python、Java)
設定が.ymlなのでバージョン管理ができて、大量にFunctionがあっても管理が楽!
と、いいことがたくさんあるので、Lambda関数を作る場合はおすすめのツールです

環境構築

Serverless Framework を使おうと思ったとき、
いろいろ入れなきゃいけないもの(python,node,etc...)があって、環境も汚したくないなーと思い
今回は開発環境をDockerで構築しました
LambdaはPythonで書くので、Dockerのpython3.6のイメージをベースにdockerfileを作成しました

ディレクト構造は以下

|-- .env
|-- app
|-- docker-compose.yml
`-- serverless
    `-- Dockerfile
  • 各ファイルを作成・編集
docker-compose.yml
version: '3.5'
services:
  serverless:
    build: 
      context: ./serverless
      args: 
        - AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
        - AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
    tty: true
    stdin_open: true
    image: serverless
    working_dir: /app
    volumes:
      - .:/app
    container_name: serverless
    env_file:
      - .env
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"
Dockerfile
FROM python:3.6
ARG AWS_ACCESS_KEY_ID
ARG AWS_SECRET_ACCESS_KEY

# update apt-get
RUN apt-get update -y && apt-get upgrade -y

# Install Nodejs 8
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash -
RUN apt-get install -y nodejs

# install aws-cli
RUN pip install awscli

# install boto3
RUN pip install boto3

# install serverless framework
RUN npm install -g serverless

# set aws key 
RUN sls config credentials --provider aws --key $AWS_ACCESS_KEY_ID --secret $AWS_SECRET_ACCESS_KEY

# change work directory
RUN mkdir -p /app
WORKDIR /app/app

  • AWS のアクセスキー情報を以下に設定
#AWS keys
AWS_ACCESS_KEY_ID={Access key ID}
AWS_SECRET_ACCESS_KEY={Secret access key}
  • Docker イメージのビルド
$ docker-compose build --no-cache
  • Docker の起動
$ docker-compose up -d serverless
  • Serverless Framework の環境へログインする
$ docker-compose exec serverless /bin/bash
$ sls -v
1.34.0

Lambda 関数の作成とデプロイ

  • Serverless Framework を使ってテンプレートを作成
$ pwd 
/app/app
$ sls create -t aws-python3 -p py3test
  • 作成されたテンプレートファイルを編集
serverless.yml

service: py3test

provider:
  name: aws
  runtime: python3.6
  region: ap-northeast-1 #日本リージョン

functions:
  hello:
    handler: handler.hello #実行するLambda関数

    events:
      - http: #API Gatewayからのイベントで起動
          path: get_hello
          method: get
handler.py
import json


def hello(event, context):
    body = {
        "message": "Hello World"
    }

    response = {
        "statusCode": 200,
        "body": json.dumps(body)
    }

    return response
  • デプロイ
$ sls deploy -v
...
Service Information
service: py3test
stage: dev
region: ap-northeast-1
stack: py3test-dev
api keys:
  None
endpoints:
  GET - https://xxxxx.xxxxxxx.ap-northeast-1.amazonaws.com/dev/get_hello
functions:
  hello: py3test-dev-hello
layers:
  None

endpoints: に書かれているURLへリクエスト

{
    "message": "Hello World"
}

上のようなレスポンスが帰ってくればOK

AWS のコンソール画面で、Lambdaと、API Gatewayを確認するとそれぞれ作ったものが、
作成されているはずです

参考サイト

https://dev.classmethod.jp/cloud/aws/easy-deploy-of-lambda-with-serverless-framework/
https://qiita.com/Esfahan/items/736d09f732fa619d2410
https://blog.morizyun.com/blog/javascript-serverless-framework-aws-lambda-python-docker/index.html

26
29
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
26
29