通过AWS CLI在AWS Lambda中向函数添加或删除Layer的方法在AWS CLI中,如何向AWS Lambda的函数中添加或删除Layer
因为在AWS CLI中添加或删除Lambda函数的Layer的方法不太清楚,所以写下了以下备忘录。
前提 (Qian Ti)
-
- 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层
假设存在用于验证的函数”use-lambda-layer”。
如果要指定Layer,请指定包含版本的LayerVersionArn,该LayerVersionArn可以从上述ARN中获取。如果要添加多个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层 – AWS Lambda
https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html
亚马逊云服务的AWS Lambda层是一种功能,用于将共享代码、库和资源从函数中分离出来,以便在多个Lambda函数中共享。Layers允许您在函数之间共享和管理通用功能,提高开发速度和灵活性。详细信息可参考上述链接。
发布层版本 — AWS CLI 1.16.182 命令参考
https://docs.aws.amazon.com/cli/latest/reference/lambda/publish-layer-version.html