我正在尝试使用无服务器框架(v 0.5.6)从 Lambda 函数访问 AWS Elasticache 集群,而不会失去对 Dynamodb 的访问权限。我试过使用这个Gist没有运气。在 Lambda 函数中,我要做的第一件事是连接到 Redis 实例,但我一直在超时,我已经仔细检查了 CloudFormation 输出变量及其在函数中的可见性以及 VPC 的 Lambda 角色/策略,但仍然没有……我没有没有找到任何关于如何使用 CloudFormation 和无服务器创建 VPC 和安全组的指南,以便按照此处的建议创建公共和私有子网、NAT 和 Internet 网关。任何人都可以帮忙吗?
问问题
6205 次
2 回答
5
您必须将 Lambda 函数放置在 ElastiCache 集群所在的 VPC 中。当然,一旦您这样做了,Lambda 函数就只能访问 VPC 中存在的资源,因此它将无法再访问 DynamoDB。解决方案是向 VPC 添加一个 NAT 网关,这将允许 Lambda 函数访问 VPC 外部的资源。
我认为设置 VPC 和 NAT 网关会超出 Serverless 框架,但我不是该框架的专家。我建议考虑通过 AWS 控制台手动配置,或者通过 CloudFormation 之类的工具进行配置,然后简单地在无服务器框架配置中指定它需要使用的 VPC。
于 2016-10-04T15:18:31.377 回答
2
虽然没有正确记录,但您实际上可以直接在无服务器配置文件中配置 VPC(请参阅链接)
0.5版
# s-function.json
{
"name": "hello",
"runtime": "nodejs4.3",
"handler": "handler.hello”,
"endpoints": [],
"events": [],
"vpc": {
"securityGroupIds": ["sg-123456"],
"subnetIds": [
"subnet-abc1",
"subnet-abc2",
"subnet-abc3",
]
}
}
1.0 版
# serverless.yaml
service: aws-hello
provider: aws
runtime: nodejs4.3
vpc:
securityGroupIds:
— "sg-123456"
subnetIds:
— "subnet-abc1"
— "subnet-abc1"
— "subnet-abc1"
functions:
foo: # inherits the VPC config
handler: src/handler.foo
bar: # overwrites the VPC config
handler: src/handler.bar
vpc:
securityGroupIds:
— "sg-999999"
subnetIds:
— "subnet-zzz9"
于 2017-01-26T00:40:52.067 回答