AWS CLIからLambda関数にLayerを追加・削除する方法がわかりにくかったのでメモ。
前提
- AWSアカウントがある
- AWS CLIが利用可能
- 検証できるLambda関数がある
- なければ新規作成
Lambda Layerを作成
検証で利用するLayerを作成します。今回は関数内で参照しないので、ファイル内に//
だけ含んだファイルでLayerを作成します。空ファイルだとだめでした。
AWS CLIだとaws lambda publish-layer-version
コマンドで作成ができます。
> echo '//' layer.js
> zip layer.zip layer.js
> aws lambda publish-layer-version \
--layer-name test-layer-1 \
--zip-file fileb://layer.zip \
--compatible-runtimes nodejs8.10
{
"Content": {
"Location": "https://prod-04-2014-layers.s3.amazonaws.com/snapshots/(略)",
"CodeSha256": "T4pRG6jK1iy8rjqOxxP2t3R1YSb5hl61Q90xHYHkePE=",
"CodeSize": 168
},
"LayerArn": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:layer:test-layer-1",
"LayerVersionArn": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:layer:test-layer-1:1",
"Description": "",
"CreatedDate": "2019-06-20T07:05:45.061+0000",
"Version": 1,
"CompatibleRuntimes": [
"nodejs8.10"
]
}
> aws lambda publish-layer-version \
--layer-name test-layer-2 \
--zip-file fileb://layer.zip \
--compatible-runtimes nodejs8.10
{
"Content": {
"Location": "https://prod-04-2014-layers.s3.amazonaws.com/snapshots/(略)",
"CodeSha256": "T4pRG6jK1iy8rjqOxxP2t3R1YSb5hl61Q90xHYHkePE=",
"CodeSize": 168
},
"LayerArn": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:layer:test-layer-2",
"LayerVersionArn": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:layer:test-layer-2:1",
"Description": "",
"CreatedDate": "2019-06-20T07:07:28.453+0000",
"Version": 1,
"CompatibleRuntimes": [
"nodejs8.10"
]
}
Lambda Layerを関数に追加
検証用の関数use-lambda-layer
がある前提です。
Layerを指定する場合、上記にあるARNのうち、バージョンまで含まれているLayerVersionArn
を指定する必要があります。複数Layerを追加するにはスペース区切りで指定します。
# 1つ追加
> aws lambda update-function-configuration \
--function-name use-lambda-layer \
--layers \
"arn:aws:lambda:us-east-1:xxxxxxxxxxxx:layer:test-layer-1:1"
{
"FunctionName": "use-lambda-layer",
"FunctionArn": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:function:use-lambda-layer",
"Runtime": "nodejs8.10",
"Role": "arn:aws:iam::xxxxxxxxxxxx:role/service-role/xxxxxxxxxxxx",
"Handler": "index.handler",
"CodeSize": 262,
"Description": "",
"Timeout": 3,
"MemorySize": 128,
"LastModified": "2019-06-20T07:11:20.502+0000",
"CodeSha256": "F8AUyhyiHz5dsBaSl6+u86B5oiwWD3jtdi3IuvB0eKE=",
"Version": "$LATEST",
"VpcConfig": {
"SubnetIds": [],
"SecurityGroupIds": [],
"VpcId": ""
},
"TracingConfig": {
"Mode": "PassThrough"
},
"RevisionId": "f8ad4fd8-b8d2-4aba-ba12-9f28759df79d",
"Layers": [
{
"Arn": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:layer:test-layer-1:1",
"CodeSize": 168
}
]
}
# 2つ追加
> aws lambda update-function-configuration \
--function-name use-lambda-layer \
--layers \
"arn:aws:lambda:us-east-1:xxxxxxxxxxxx:layer:test-layer-1:1" \
"arn:aws:lambda:us-east-1:xxxxxxxxxxxx:layer:test-layer-2:1"
{
"FunctionName": "use-lambda-layer",
"FunctionArn": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:function:use-lambda-layer",
"Runtime": "nodejs8.10",
"Role": "arn:aws:iam::xxxxxxxxxxxx:role/service-role/xxxxxxxxxxxx",
"Handler": "index.handler",
"CodeSize": 262,
"Description": "",
"Timeout": 3,
"MemorySize": 128,
"LastModified": "2019-06-20T07:12:47.938+0000",
"CodeSha256": "F8AUyhyiHz5dsBaSl6+u86B5oiwWD3jtdi3IuvB0eKE=",
"Version": "$LATEST",
"VpcConfig": {
"SubnetIds": [],
"SecurityGroupIds": [],
"VpcId": ""
},
"TracingConfig": {
"Mode": "PassThrough"
},
"RevisionId": "8737bdfe-3b0d-404e-b3a0-c9fd57e971a9",
"Layers": [
{
"Arn": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:layer:test-layer-1:1",
"CodeSize": 168
},
{
"Arn": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:layer:test-layer-2:1",
"CodeSize": 168
}
]
}
Lambda Layerを関数から削除
Layerを削除というよりは利用するLayerを再指定することになります。
すべて削除するには--layers []
として、空配列
を指定する必要があります。わかりにくい
# 1つ削除
> aws lambda update-function-configuration \
--function-name use-lambda-layer \
--layers \
"arn:aws:lambda:us-east-1:xxxxxxxxxxxx:layer:test-layer-2:1"
{
"FunctionName": "use-lambda-layer",
"FunctionArn": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:function:use-lambda-layer",
"Runtime": "nodejs8.10",
"Role": "arn:aws:iam::xxxxxxxxxxxx:role/service-role/xxxxxxxxxxxx",
"Handler": "index.handler",
"CodeSize": 262,
"Description": "",
"Timeout": 3,
"MemorySize": 128,
"LastModified": "2019-06-20T07:13:27.749+0000",
"CodeSha256": "F8AUyhyiHz5dsBaSl6+u86B5oiwWD3jtdi3IuvB0eKE=",
"Version": "$LATEST",
"VpcConfig": {
"SubnetIds": [],
"SecurityGroupIds": [],
"VpcId": ""
},
"TracingConfig": {
"Mode": "PassThrough"
},
"RevisionId": "fa942867-332a-4d32-ae2d-31209ce9cc30",
"Layers": [
{
"Arn": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:layer:test-layer-2:1",
"CodeSize": 168
}
]
}
# 全部削除
> aws lambda update-function-configuration \
--function-name use-lambda-layer \
--layers []
{
"FunctionName": "use-lambda-layer",
"FunctionArn": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:function:use-lambda-layer",
"Runtime": "nodejs8.10",
"Role": "arn:aws:iam::xxxxxxxxxxxx:role/service-role/xxxxxxxxxxxx",
"Handler": "index.handler",
"CodeSize": 262,
"Description": "",
"Timeout": 3,
"MemorySize": 128,
"LastModified": "2019-06-20T07:13:53.065+0000",
"CodeSha256": "F8AUyhyiHz5dsBaSl6+u86B5oiwWD3jtdi3IuvB0eKE=",
"Version": "$LATEST",
"VpcConfig": {
"SubnetIds": [],
"SecurityGroupIds": [],
"VpcId": ""
},
"TracingConfig": {
"Mode": "PassThrough"
},
"RevisionId": "c060cba7-2cf4-48af-88d3-4e5f653965a2"
}
参考
AWS Lambda Layers - AWS Lambda
https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html
publish-layer-version — AWS CLI 1.16.182 Command Reference
https://docs.aws.amazon.com/cli/latest/reference/lambda/publish-layer-version.html