Serverless Frameworkの初期設定

Windowsクライアント環境にServerless Frameworkをインストールし、動作確認も兼ねてAWS上にLambdaファンクションを作成してみます。

環境

  • Windows10 (10.0.18362 N/A ビルド 18362)
  • Serverless Framework 1.65.0

インストール

コマンドプロンプトを管理者権限で起動して、chocolatey経由でインストールします。

> choco install serverless

Installing as a standalone binary

バージョン確認。

> sls --version
Framework Core: 1.65.0 (standalone)
Plugin: 3.4.1
SDK: 2.3.0
Components: 2.22.3

Serverless Framework用IAMユーザーの作成

Serverless Frameworkで利用するAWS IAMユーザーを作成します。

serverless docs - AWS - Credentials

ユーザーを作成するコマンド。

> aws iam create-user ^
--user-name serverless-framework 

aws iam create-user

作成したユーザーに、必要となるポリシーを付与します。ポリシーのJSONが下記で公開されているので、ローカルに IAMCredentials.json という名前でファイルを作成しておきます。

https://gist.github.com/ServerlessBot/7618156b8671840a539f405dea2704c8

作成したJSONファイルを読み込ませる形で、ポリシーを作成します。

> aws iam create-policy ^
--policy-name serverless-framework ^
--policy-document file://IAMCredentials.json

aws iam create-policy

作成したポリシーをユーザーにアタッチします。

> aws iam attach-user-policy ^
--user-name serverless-framework ^
--policy-arn arn:aws:iam::<aws-account-id>:policy/serverless-framework

aws iam attach-user-policy

プログラムアクセス用のアクセスキーを発行します。

> aws iam create-access-key ^
--user-name serverless-framework

aws iam create-access-key

通常のAWSクレデンシャルファイルに追記すれば良いそうなので、取得したアクセスキーとシークレットキーの情報を末尾に追記します。

(ホームディレクトリ)/.aws/credentials

[sls]
aws_access_key_id=xxxxx
aws_secret_access_key=xxxxx

Lambdaファンクションの作成

公式のExampleを参考に、サンプルのLambdaファンクションを作成してみます。

Hello World Python Example

以下のコマンドを実行します。

> sls create --template aws-python3 --path sample
Serverless: Generating boilerplate...
Serverless: Generating boilerplate in "C:\tmp\sample"
 _______                             __
|   _   .-----.----.--.--.-----.----|  .-----.-----.-----.
|   |___|  -__|   _|  |  |  -__|   _|  |  -__|__ --|__ --|
|____   |_____|__|  \___/|_____|__| |__|_____|_____|_____|
|   |   |             The Serverless Application Framework
|       |                           serverless.com, v1.65.0
 -------'

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

カレントディレクトリ配下の sample フォルダ内に、Serverless Frameworkのマニフェストファイル serverless.yml と、Lambdaに登録されるPythonソースコードのファイルが作成されています。

.
├── handler.py
└── serverless.yml

sampleフォルダに移動して、deployします。Lambdaファンクションが作成されます。

> cd sample
> sls deploy --aws-profile sls --region ap-northeast-1
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
.........
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service sample.zip file to S3 (390 B)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
.................
Serverless: Stack update finished...
Service Information
service: sample
stage: dev
region: ap-northeast-1
stack: sample-dev
resources: 6
api keys:
  None
endpoints:
  None
functions:
  hello: sample-dev-hello
layers:
  None
Serverless: Run the "serverless" command to setup monitoring, troubleshooting and testing.

作成されたLambdaファンクションを発火します。

> sls invoke -f hello --aws-profile sls --region ap-northeast-1
{
    "statusCode": 200,
    "body": "{\"message\": \"Go Serverless v1.0! Your function executed successfully!\", \"input\": {}}"
}

最後にファンクションを削除します。

> sls remove --aws-profile sls --region ap-northeast-1
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...