Serverless Framework のサービス開始時によくやる作業をまとめてみた

Serverless Framework のサービス開始時によくやる作業を整理しました
2020.12.02

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

「サービス開始時によくやる作業って何だっけ?」

ってたまになりませんか?今回はいくつかサービスを作った経験を元に、Serverless Framework のサービス開始時によくやる作業を整理してみました。

前提条件

Serverless Framework を利用するにあたり、初回のみ設定が必要な作業は済んでいる前提で進めます。

  • Serveless Framework のインストール
  • Serveless Framework 用の AWS アカウントの設定

未実施の方は、以下のブログなどを参照して設定してください。

今から始めるServerless Frameworkで簡単Lambda開発環境の構築

やってみた

以下の流れで作業します。

  1. サービスの作成
  2. Git の設定
  3. プラグインのインストール
  4. パッケージのインストール
  5. state-machines.yml と serverless.yml の初期設定

最終的なファイル構成は以下です(一部省略)。

test-sls
├── .gitignore
├── Pipfile
├── Pipfile.lock
├── handler.py
├── includes
│   └── state-machines.yml
├── package-lock.json
├── package.json
└── serverless.yml

1.サービスの作成

Serverless Framework のサービスを作成します。 今回のサービス名は test-sls です。

$ serverless create --template aws-python3 --path test-sls
Serverless: Generating boilerplate...
Serverless: Generating boilerplate in "{省略}"
 _______                             __
|   _   .-----.----.--.--.-----.----|  .-----.-----.-----.
|   |___|  -__|   _|  |  |  -__|   _|  |  -__|__ --|__ --|
|____   |_____|__|  \___/|_____|__| |__|_____|_____|_____|
|   |   |             The Serverless Application Framework
|       |                           serverless.com, v2.8.0
 -------'

Serverless: Successfully generated boilerplate for template: "aws-python3"

2.Git の設定

ディレクトリを移動して、Git 設定をします。

$ cd test-sls

$ git init
Initialized empty Git repository in {省略}/test-sls/.git/

お好みのエディターで .gitignore を以下のように編集します。 プロジェクトの容量を無駄に大きくしない為に、node_modules などを commit 対象から外しています。

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg
.idea/
__pycache__/

# Serverless directories
.serverless
node_modules/

一旦、今までの編集内容を commit する

$ git add .gitignore handler.py serverless.yml
$ git commit -m "First Commit"

3.プラグインのインストール

プラグインをインストールします。 今回はよく使うプラグインを入れます。 プラグインを入れたら、変更内容を commit しておきます。

$ sls plugin install -n serverless-step-functions && sls plugin install -n serverless-python-requirements
Serverless: Creating an empty package.json file in your service directory
Serverless: Installing plugin "serverless-step-functions@latest" (this might take a few seconds...)
Serverless: Successfully installed "serverless-step-functions@latest"
Serverless: Installing plugin "serverless-python-requirements@latest" (this might take a few seconds...)
Serverless: Successfully installed "serverless-python-requirements@latest"

$ git add serverless.yml package-lock.json package.json
$ git commit -m "sls plugin install -n serverless-step-functions && sls plugin install -n serverless-python-requirements"

4.パッケージのインストール

パッケージをインストールします。 今回はよく使うパッケージを入れます。 パッケージを入れたら、変更内容を commit しておきます。

$ pipenv install requests boto3

$ git add Pipfile Pipfile.lock
$ git commit -m "pipenv install requests boto3"

5.state-machines.yml & serverless.yml の初期設定

AWS Step Functions を使うことも多いので、state-machines.yml を用意します。 個人的には includes というフォルダを作成してファイルを配置することが多いです。

$ mkdir includes
$ touch includes/state-machines.yml

state-machines.yml は仮で以下のように編集しておきます。

stateMachines:
  countfunc:
    name: TestSLS-${self:provider.stage}
    definition:
      StartAt: Hello
      States:
        Hello:
          Type: Task
          InputPath: "$"
          Resource:
            Fn::GetAtt: [hello, Arn]
          ResultPath: "$"
          Next: Done
        Done:
          Type: Pass
          End: true

serverless.yml は仮で以下のように編集しておきます。

service: test-sls

provider:
  name: aws
  runtime: python3.8
  stage: ${opt:stage, self:custom.defaultStage}
  region: ${opt:region, self:custom.defaultRegion}

custom:
  pythonRequirements:
    usePipenv: true
  defaultStage: dev
  defaultRegion: ap-northeast-1
  #environment:
    #dev:
    #prod:

functions:
  hello:
    handler: handler.hello
    #environment: ${self:custom.environment.${self:provider.stage}}

stepFunctions: ${file(includes/state-machines.yml)}

plugins:
  - serverless-step-functions
  - serverless-python-requirements

追加の変更内容を commit しておきます。

$ git add serverless.yml includes/
$ git commit -m "Initial Setup serveless.yml & includes/state-machines.yml"

終わりに

いかがでしたでしょうか。 あくまで自己流なので、必要に応じてカスタマイズいただければと思います。 Serverless Framework を使う皆さんに少しでも役に立っていれば幸いです。

以上、 筧(カケイ) がお送りしました。