参数存储请求在 AWS Lambda 内部超时
Parameter Store request timing out inside of AWS Lambda
我正在尝试访问 AWS SSM 参数存储,就像 this article 一样。我已经在本地测试了 lambda 函数,它按预期工作。然而,当推送到 AWS 时,lambda 在尝试检索配置时失败了;超时:
{
"errorMessage": "2018-09-02T04:55:49.096Z 71a5006a-ae6c-11e8-9322-313ba5e28048 Task timed out after 6.01 seconds"
}
我的 serverless.yml
添加了以下权限。我已尽可能不受限制地尝试查找错误所在。此外,该参数只是一个字符串,因此它不使用 KMS。
service: pwaer-messages-service
provider:
name: aws
runtime: nodejs8.10
vpc:
securityGroupIds:
- sg-222f126f
subnetIds:
- subnet-756aef12
- subnet-130f8f3d
environment:
NODE_ENV: ${opt:stage, 'dev'}
iamRoleStatements:
- Effect: 'Allow'
Action: 'ssm:**'
Resource:
- 'Fn::Join':
- ':'
-
- 'arn:aws:ssm'
- Ref: 'AWS::Region'
- Ref: 'AWS::AccountId'
- 'parameter/*'
functions:
receiveText:
handler: dist/receive.handler
events:
- http:
path: sms/parse
method: post
我错过了什么?
由于提到的 Lambda 无法访问 public 互联网,要访问 AWS API,请设置 VPC endpoint。
根据描述 - "VPC endpoint enables you to privately connect your VPC to supported AWS services and VPC endpoint services"。
对于 AWS Systems Manager,请遵循此过程 - Setting Up VPC Endpoints for Systems Manager
如果您的 Lambda 在 VPC 中,就会发生这种情况。您需要做两件事:
- 将 aws ssm 服务公开为 VPC 端点(请参阅@Lech Migdal 的回答)
- VPC 端点的安全组必须与您希望允许连接的 lambda(或服务)的安全组相关联
- 将端口 443 的自入口规则添加到 lambda 的安全组
CDK 示例
const LambdaSecurityGroupIngressRule = new ec2.CfnSecurityGroupIngress(this, "LambdaSecurityGroupIngressRule", {
groupId: LambdaSecurityGroup.attrGroupId,
sourceSecurityGroupId: LambdaSecurityGroup.attrGroupId,
description: "Needed to connect to parameter store from lambda in VPC",
fromPort: 443,
ipProtocol: "tcp",
toPort: 443
})
LambdaSecurityGroupIngressRule.addDependsOn(S3IndexLambdasSecurityGroup)
我正在尝试访问 AWS SSM 参数存储,就像 this article 一样。我已经在本地测试了 lambda 函数,它按预期工作。然而,当推送到 AWS 时,lambda 在尝试检索配置时失败了;超时:
{
"errorMessage": "2018-09-02T04:55:49.096Z 71a5006a-ae6c-11e8-9322-313ba5e28048 Task timed out after 6.01 seconds"
}
我的 serverless.yml
添加了以下权限。我已尽可能不受限制地尝试查找错误所在。此外,该参数只是一个字符串,因此它不使用 KMS。
service: pwaer-messages-service
provider:
name: aws
runtime: nodejs8.10
vpc:
securityGroupIds:
- sg-222f126f
subnetIds:
- subnet-756aef12
- subnet-130f8f3d
environment:
NODE_ENV: ${opt:stage, 'dev'}
iamRoleStatements:
- Effect: 'Allow'
Action: 'ssm:**'
Resource:
- 'Fn::Join':
- ':'
-
- 'arn:aws:ssm'
- Ref: 'AWS::Region'
- Ref: 'AWS::AccountId'
- 'parameter/*'
functions:
receiveText:
handler: dist/receive.handler
events:
- http:
path: sms/parse
method: post
我错过了什么?
由于提到的 Lambda 无法访问 public 互联网,要访问 AWS API,请设置 VPC endpoint。
根据描述 - "VPC endpoint enables you to privately connect your VPC to supported AWS services and VPC endpoint services"。
对于 AWS Systems Manager,请遵循此过程 - Setting Up VPC Endpoints for Systems Manager
如果您的 Lambda 在 VPC 中,就会发生这种情况。您需要做两件事:
- 将 aws ssm 服务公开为 VPC 端点(请参阅@Lech Migdal 的回答)
- VPC 端点的安全组必须与您希望允许连接的 lambda(或服务)的安全组相关联
- 将端口 443 的自入口规则添加到 lambda 的安全组
CDK 示例
const LambdaSecurityGroupIngressRule = new ec2.CfnSecurityGroupIngress(this, "LambdaSecurityGroupIngressRule", {
groupId: LambdaSecurityGroup.attrGroupId,
sourceSecurityGroupId: LambdaSecurityGroup.attrGroupId,
description: "Needed to connect to parameter store from lambda in VPC",
fromPort: 443,
ipProtocol: "tcp",
toPort: 443
})
LambdaSecurityGroupIngressRule.addDependsOn(S3IndexLambdasSecurityGroup)