LoginSignup
46
40

More than 5 years have passed since last update.

初心者のためのNode.jsでServerless Frameworkするやつ。

Posted at

概要

Serverless Framework を試すためにすることをまとめました。

はじめに

Serverless Framework を試すのに 3 点候補あげておきますが、この記事では「AWS Lambda」を扱います。

  • Amazon Web Services
    • AWS Lambda
  • Google Cloud Platform
    • Google Cloud Functions
  • Microsoft Azure
    • Azure Functions

前提条件

  • AWS アカウント作成済み
  • AWS マネジメントコンソールに入れること
  • IAM でアクセスキーを作成済み
  • IAM で作成したアクセスキーを確認できる状態にすること
  • Homebrew をインストール済み
  • Node.js をインストール済み
  • npm -v で問題なくバージョンが表示されること

やることリスト

  • awscli のインストール
  • AWS CLI の設定
  • Serverless Framework のインストール

awscli のインストール

AWS の機能をターミナル上から使えるようにするためのもの。

brew install awscli

AWS CLI の設定

  • accountName は任意の名前
  • --profile はどのアカウントを設定するのかを明示するもの
    • (複数の AWS アカウントを扱えるようにきちんと切り分ける)
aws configure --profile accountName

解説

IAM で作成したアクセスキーを見ながら入力する。

$ aws configure --profile accountName
AWS Access Key ID [None]: ここには Access key ID
AWS Secret Access Key [None]: ここには Secret access key
Default region name [None]: ap-northeast-1
Default output format [None]: json

Serverless Framework のインストール

npm install -g serverless

Serverless Framework で雛形を作成

  • --template は他にも指定できるものがあり、 serverless create --help で確認できます
  • --path で指定した名前のディレクトリが作成される
serverless create --template aws-nodejs --path quick-start

実行結果

"
 _______                             __
|   _   .-----.----.--.--.-----.----|  .-----.-----.-----.
|   |___|  -__|   _|  |  |  -__|   _|  |  -__|__ --|__ --|
|____   |_____|__|  \___/|_____|__| |__|_____|_____|_____|
|   |   |             The Serverless Application Framework
|       |                           serverless.com, v1.36.3
 -------'

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

作業ディレクトリに移動

cd quick-start

早速 AWS に デプロイする

  • --verbose は情報を詳細に出してくれる
  • --aws-profileaws configure --profile した時の名前を入れる
  • --region は東京(ap-northeast-1)を指定
serverless deploy --verbose --aws-profile accountName --region ap-northeast-1

実行結果

Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...

~~
~~

Serverless: Stack update finished...

Service Information
service: quick-start
stage: dev
region: ap-northeast-1
stack: quick-start-dev
api keys:
  None
endpoints:
  None
functions:
  hello: quick-start-dev-hello
layers:
  None

Stack Outputs
HelloLambdaFunctionQualifiedArn: ...
ServerlessDeploymentBucketName: ...

デプロイしたやつを AWS 上で確認する

AWS コンソールにログイン後

  • 東京リージョンになっていることを確認
  • サービス検索から「Lambda」を検索する

2019-01-27_22-58-51.png

Lambda の画面で

  • 左側に見える「関数」をクリック
  • 今回デプロイしたもの「quick-start-dev-hello」をクリック

2019-01-27_23-03-34.png

quick-start-dev-hello の画面

この画面を活用する方法は別記事で説明します。
デプロイしたものは、この様に確認できます。

2019-01-27_23-08-00.png

デプロイしたやつを実行する

  • --function は実行したい関数の名前
  • --log は実行した時のメモリ使用量や実⾏時間、課⾦対象時間などを表示
  • --aws-profile--region は前回の通り
serverless invoke --function hello --log --aws-profile accountName --region ap-northeast-1

実行結果

{
    "statusCode": 200,
    "body": "{\"message\":\"Go Serverless v1.0! Your function executed successfully!\",\"input\":{}}"
}

最後に

今回試しに動作させたものを消す

不要だったら捨てる。
必要ならまた serverless deploy しましょう。

serverless remove --aws-profile accountName --region ap-northeast-1

次回

Serverless Framework で API サーバーを構築するやつをやっていきましょう。

46
40
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
46
40