LoginSignup
14
10

More than 5 years have passed since last update.

Serverless Framework 入門(1)

Last updated at Posted at 2017-01-27

はじめに

AWSでAPI Gatewayを利用してごにょごにょしたいのでServerlessを触ってみました。
まずはHello Worldまで。

前提(私の環境)

Windows 10 Pro (64bit)
Git for Windows (Git Bash)
Node.js (v6.9.4 LTS / Windows(x64))
AWS (とりあえずアカウントは作成してコンソールには入れる)

AWS CLIのインストール

下記を参考にAWS CLIをインストールします。
Install the AWS CLI Using the MSI Installer (Windows)

インストールしたらGitBashを立ち上げて確認します。

$ aws --version
aws-cli/1.11.44 Python/2.7.9 Windows/8 botocore/1.5.7

Serverless Frameworkのインストール

下記のコマンドでグローバルにインストールしておきます。

$ npm install -g serverless
$ serverless --version
1.5.1

IAMユーザの作成

アクセスキーを取得するには、まずIAMユーザを作成する必要があります。
アクセスキーについて

IAMコンソールからグループを作成します。

グループ名:Administrators
ポリシー名:AdministratorAccess

次にユーザを作成します。
ユーザ名:Administrator
アクセスの種類:

  • プログラムによるアクセス
  • AWS マネジメントコンソールへのアクセス

アクセス権限:
作成した「Administrators」グループにユーザを追加

作成に成功すると「アクセスキーID」と「シークレットアクセスキー」が表示されます。
「シークレットアクセスキー」は1度しか表示されないので、
控え忘れた場合はIAMコンソールの「ユーザー」>「認証情報」から再度アクセスキーを生成します。

AWS CLIの設定

下記コマンドで設定を行います。
リージョンは、今回は「アジアパシフィック (東京)」にしてみます。

$ aws configure
AWS Access Key ID [None]: 作成したアクセスキー
AWS Secret Access Key [None]: 作成したシークレットアクセスキー
Default region name [None]: ap-northeast-1
Default output format [None]: json

Serverless用 IAMユーザの作成

名前はserverless-adminとし、ポリシーは「AdministartorAccess」を付与します。
実運用ではポリシーをもう少し考えます。

ここではユーザをAWS CLIで作成してみます。

$ aws iam create-user --user-name serverless-admin
{
    "User": {
        "UserName": "serverless-admin",
        "Path": "/",
        "CreateDate": "2017-01-27T06:46:36.697Z",
        "UserId": "作成されたユーザーID",
        "Arn": "arn:aws:iam::901746173629:user/serverless-admin"
    }
}
$ aws iam add-user-to-group --user-name serverless-admin --group-name Administrators
$ aws iam create-access-key --user-name serverless-admin
{
    "AccessKey": {
        "UserName": "serverless-admin",
        "Status": "Active",
        "CreateDate": "2017-01-27T06:47:18.129Z",
        "SecretAccessKey": "作成されたシークレットアクセスキー",
        "AccessKeyId": "作成されたアクセスキー"
    }
}

Serverless用 認証プロファイルの追加

作成したユーザを「serverless」という認証プロファイル名で追加します。

$ serverless config credentials --provider aws --profile serverless --key 作成されたアクセスキー --secret 作成されたシークレットアクセスキー
Serverless: Setting up AWS...
Serverless: Saving your AWS profile in "~/.aws/credentials"...
Serverless: Success! Your AWS access keys were stored under the "serverless" profile.

GitHubのリポジトリ作成

GitHubでリポジトリを作成してからcloneします。

$ git clone https://github.com/DaisukeOtaka/hello-serverless.git
$ cd hello-serverless

プロジェクトの作成

node.jsテンプレートでhello-serverlessという名前のプロジェクトを作成します。
なお、serverlessコマンドにはslsというエイリアスが設定されているので
以降はslsを利用します。

$ sls create --template aws-nodejs --name hello-serverless
Serverless: Generating boilerplate...
 _______                             __
|   _   .-----.----.--.--.-----.----|  .-----.-----.-----.
|   |___|  -__|   _|  |  |  -__|   _|  |  -__|__ --|__ --|
|____   |_____|__|  \___/|_____|__| |__|_____|_____|_____|
|   |   |             The Serverless Application Framework
|       |                           serverless.com, v1.5.1
 -------'

Serverless: Successfully generated boilerplate for template: "aws-nodejs"
$ ls
handler.js  LICENSE  README.md  serverless.yml

Lamda Functionとなる「handler.js」と、設定ファイルである「serverless.yml」が生成されました。

serverless.yml の修正

設定内容を修正します。
リージョンとプロファイル設定を変更しておきます。

serverless.yml
# you can overwrite defaults here
-#  stage: dev
-#  region: us-east-1
+  stage: dev
+  region: ap-northeast-1
+  profile: serverless

デプロイ

deployコマンドを実行すると「serverless.yml」に従ってデプロイされます。

$ sls deploy
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
.....
Serverless: Stack create finished...
Serverless: Packaging service...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading service .zip file to S3 (1.44 KB)...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
................
Serverless: Stack update finished...
Service Information
service: hello-serverless
stage: dev
region: ap-northeast-1
api keys:
  None
endpoints:
  None
functions:
  hello-serverless-dev-hello: arn:aws:lambda:ap-northeast-1:601746173624:function:hello-serverless-dev-hello

実行

invokeコマンドでdeployしたfunctionを叩きます。
なお、公式ドキュメントの例ではdataの指定はしていないのですが、
指定しないと何も返ってきません(応答待ち?で止まる)でした。

$ sls invoke --function hello --data "hello!"
{
    "statusCode": 200,
    "body": "{\"message\":\"Go Serverless v1.0! Your function executed successfully!\",\"input\":\"hello!\"}"
}

ちゃんと返ってきました。

削除

お試しが成功したので、removeコマンドで消しておきます。

$ sls remove
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...

GitHub にコミット

ソースは以下にコミットしてあります。
DaisukeOtaka/hello-serverless

参考

14
10
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
14
10