AWS Lambda内的参数存储请求超时

20

我正在尝试访问AWS SSM参数商店,就像这篇文章一样。 我已经在本地测试了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

我错过了什么?


我不会将其连接到错误的权限,因为在这种情况下,您会立即收到IAM错误。它挂在哪个确切的函数调用上? - Lech Migdal
1
你尝试增加 Lambda 超时时间了吗,只是为了测试? - Lech Migdal
1
@LechMigdal 我遇到了这个问题。我刚刚发现删除 VPC 可以解决这个问题,但是我需要 VPC 来访问 RDS。aws-sdk 不应该能够在不访问公共互联网的情况下访问 aws 资源吗? - Ulad Kasach
2
啊,所以Lambda无法访问公共互联网?尝试为SSM设置VPC端点-https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-setting-up-vpc.html。 - Lech Migdal
1
不用客气,此外您可能想编辑问题的标题,使其更加通用/不指向IAM作为问题的根本原因 :) - Lech Migdal
显示剩余3条评论
2个回答

30

由于提到的Lambda无法访问公共互联网,要访问AWS API,请设置VPC端点

根据描述-"VPC端点使您能够私有地连接您的VPC与支持的AWS服务和VPC端点服务"

对于AWS系统管理器,请按照以下步骤进行操作- 为系统管理器设置VPC端点


有没有一种方法可以在serverless.yml文件中完成这个操作? - mikespitz_8
1
@mikespitz_8,您只需要创建一个端点,所有的 Lambda 函数都将使用它。除非您有一个包含所有 Lambda 函数的单个 Serverless 文件(我建议不要这样做,而是使用 Terraform 创建端点),否则您可能不应该在 Serverless 文件中创建它。 - Chad Greenburg

4

如果您的Lambda在VPC中,则会发生这种情况。您需要执行两个操作:

  1. 将AWS SSM服务公开为VPC终端节点(请参见@Lech Migdal的答案)
  • VPC终端节点的安全组必须与Lambda(或服务)的安全组相关联,以允许连接
  1. 向Lambda的安全组添加自我入站规则,以允许端口443的流量

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链接:https://aws.amazon.com/premiumsupport/knowledge-center/ec2-systems-manager-vpc-endpoints/ 我必须像@cosbor11建议的那样添加入站规则。 - Russell Keane

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接