Lambda Layerにnode_modulesを含めてCloudFormationでLambdaデプロイする
Lambdaレイヤーにnode_modulesを配置してLambdaレイヤーとLambdaをデプロイします。
ディレクトリ構造
├─layer │ └─nodejs │ ├─package.json │ ├─package-lock.json │ └─node_modules │ └─luxon │ ├─node_modules ★local testしないなら不要 │ └─luxon │ ├─src │ └─index.js ├─cfn.yml ├─package.json └─package-lock.json
以下コマンドでプロジェクト作成します。
$ npm init -y $ npm i luxon ★local testする場合のみ $ cd layer/nodejs $ npm init -y $ npm i luxon
cfn.yml
AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Resources:
LambdaLayer:
Type: "AWS::Serverless::LayerVersion"
Properties:
LayerName: layername
ContentUri: layer/
CompatibleRuntimes:
- nodejs18.x
RetentionPolicy: Delete
LambdaA:
Type: "AWS::Serverless::Function"
Properties:
FunctionName: functionname
Handler: index.handler
Runtime: nodejs18.x
CodeUri: src/
Layers:
- !Ref LambdaLayer
package & deploy
aws cloudformation packageコマンド、aws cloudformation deployコマンドでデプロイします。
$ aws cloudformation package --template-file cfn.yml --s3-bucket <バケット名> --output-template-file cfn_out.yml Successfully packaged artifacts and wrote output template to file cfn_out.yml. Execute the following command to deploy the packaged template aws cloudformation deploy --template-file cfn_out.yml --stack-name $ aws cloudformation deploy --template-file cfn_out.yml --stack-name layer-test --capabilities CAPABILITY_IAM Waiting for changeset to be created.. Waiting for stack create/update to complete Successfully created/updated stack - layer-test
layer-testというスタックが作成されます。
functionnameというlambdaが作成されます。
layernameというlayerが作成されます。
Lambdaレイヤー確認
Lambdaレイヤーをダウンロードしzipを解凍して中身の構成を確認します。
プロジェクトのlayer配下と同じ構成になっていることが確認できます。

KHI入社して退社。今はCONFRAGEで正社員です。関西で140-170/80~120万から受け付けております^^
得意技はJS(ES6),Java,AWSの大体のリソースです
コメントはやさしくお願いいたします^^
座右の銘は、「狭き門より入れ」「願わくは、我に七難八苦を与えたまえ」です^^





コメント