Serverless Frameworkを使ってTypeScriptなAWS Lambdaをセットアップ
マネジメントコンソール上でコードを書いていくのは辛い。
zipに固めてaws-cliでアップロードっていう方法もあるけれども、今回はServerless Frameworkを使います。
$ npm --version 6.10.3 $ npm install -g serverless ... ... $ sls --help Commands * You can run commands with "serverless" or the shortcut "sls" * Pass "--verbose" to this command to get in-depth plugin info * Pass "--no-color" to disable CLI colors * Pass "--help" after any <command> for contextual help ...
資格情報(credentials)を設定。もちろん事前にユーザーは作っておく。
$ sls config credentials --provider aws --key xxxxx --secret xxxxx Serverless: Setting up AWS... Serverless: Failed! ~/.aws/credentials already has a "default" profile. Use the overwrite flag ("-o" or "--overwrite") to force the update
おっと。設定済みだった。
$ cat ~/.aws/credentials [default] aws_access_key_id = dummy aws_secret_access_key = dummy ...
さっきのコマンドに --profile
オプションを追加して、デフォルトではなく別のプロファイルとして設定する。
$ sls config credentials --provider aws --key xxxxx --secret xxxxx --profile xxxxx
設定に成功したら ~/.aws/credentials
に追加されてる。
Hello World
$ sls create --template aws-nodejs-typescript --path helloLambda Serverless: Generating boilerplate... Serverless: Generating boilerplate in "/path/to/helloLambda" _______ __ | _ .-----.----.--.--.-----.----| .-----.-----.-----. | |___| -__| _| | | -__| _| | -__|__ --|__ --| |____ |_____|__| \___/|_____|__| |__|_____|_____|_____| | | | The Serverless Application Framework | | serverless.com, v1.52.0 -------' Serverless: Successfully generated boilerplate for template: "aws-nodejs-typescript" $ cd helloLambda $ sls deploy Serverless Error --------------------------------------- Serverless plugin "serverless-webpack" not found. Make sure it's installed and listed in the "plugins" section of your serverless config file. ...
なん、、だと、、
$ ls handler.ts package.json serverless.yml tsconfig.json webpack.config.js
node_modules
がない。
$ npm install $ ls handler.ts node_modules package-lock.json package.json serverless.yml tsconfig.json webpack.config.js $ sls deploy ... Serverless Error --------------------------------------- The security token included in the request is invalid. ...
おっと。多分デフォルトのプロファイルが使われてるな。。
どのプロファイルの資格情報を使うか、指定してあげないとだった。
$ vim serverless.yml ... provider: name: aws runtime: nodejs10.x # profileにプロファイル名を指定 profile: profile-name ...
$ sls deploy ... Serverless Error --------------------------------------- User: arn:aws:iam::xxxxx is not authorized to perform: cloudformation:CreateStack on resource: arn:aws:cloudformation:xxxxx ...
今度は権限がない、と。マネジメントコンソールから権限追加。
$ sls deploy ... Serverless: Stack update finished... Service Information service: hellolambda stage: dev region: us-east-1 stack: hellolambda-dev resources: 10 ...
できたけどリージョンが us-east-1
になってますな。
$ vim serverless.yml ... provider: name: aws runtime: nodejs10.x profile: profile-name # リージョン指定追加 region: ap-northeast-1 ...
もう一度 sls deploy
。
コンソールから無事東京リージョンにLambda関数を作成できていること確認。
別のリージョンに作ってしまった関数は削除。
作成時に出力されたエンドポイントに対してcurlを叩いてみる。
$ curl -X GET https://xxxxxxx.ap-northeast-1.amazonaws.com/xxxxx { "message": "Go Serverless Webpack (Typescript) v1.0! Your function executed successfully!", ...
ちゃんと動作している。
sls invoke
でもええよ。
$ sls invoke -f hello { "statusCode": 200, "body": "{\n \"message\": \"Go Serverless Webpack (Typescript) v1.0! Your function executed successfully!\",\n \"input\": {}\n}" }