6
1

More than 3 years have passed since last update.

Serverless FrameworkでTypeScriptを使い、Lambda Layersを使う方法

Last updated at Posted at 2019-10-21

serverless framework自体をインストールする

npm install -g serverless

プロジェクトを作成する

serverless create --template aws-nodejs-typescript --path my-service

プラグインインストール

serverless plugin install -n serverless-webpack
npm install

デプロイ

serverless deploy --aws-profile sandbox -v

デプロイされると、 endpoints にURLが表示されています。

そのURLにcurlでアクセスしてみます。

curl https://hsnfe8svli.execute-api.us-east-1.amazonaws.com/dev/hello | jq

実行すると下記の結果が得られます。(抜粋)

{
  "message": "Go Serverless Webpack (Typescript) v1.0! Your function executed successfully!"
}

実行してみる

serverless invoke -f hello -l --aws-profile sandbox

出力

{
    "statusCode": 200,
    "body": "{\n  \"message\": \"Go Serverless Webpack (Typescript) v1.0! Your function executed successfully!\",\n  \"input\": {}\n}"
}
--------------------------------------------------------------------
START RequestId: d2d32d7e-7c23-448d-ac57-45133fcf6422 Version: $LATEST
END RequestId: d2d32d7e-7c23-448d-ac57-45133fcf6422
REPORT RequestId: d2d32d7e-7c23-448d-ac57-45133fcf6422  Duration: 2.07 ms   Billed Duration: 100 ms Memory Size: 1024 MB    Max Memory Used: 76 MB

Layersプラグインを使う

npx sls plugin install --name serverless-layers

S3 Bucketを指定する

serverless info -v

で表示される、ServerlessDeploymentBucketName を、serverless.yamlのcustomセクションに追加

custom:
  serverless-layers:
    layersDeploymentBucket: 'ここにServerlessDeploymentBucketNameの値を入れる'

(いちいちこの方法を使わないでも直接参照したい…。だれか知っている人がいたら教えてください)

6
1
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
6
1