从 VPC 内部访问 AWS SecretsManager

问题描述 投票:0回答:1

我有一个 Lambda 函数,需要位于 VPC 中,并且需要从 SecretsManager 访问 Secret。虽然代码在 Lambda 位于 VPC 外部时可以工作,但在 VPC 内部时则不起作用。问题是无法从 VPC 访问 SecretsManager。安全组允许访问 SecretsManager 的规则应该是什么?当前的 CloudFormation 模板如下:

  CrossadVpc:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 192.168.0.0/21
      EnableDnsHostnames: true
      EnableDnsSupport: true
      InstanceTenancy: default

  VpcEndpoint:
    Type: AWS::EC2::VPCEndpoint
    Properties:
      PrivateDnsEnabled: true
      SecurityGroupIds:
        - !Ref LambdaSecurityGroup
      ServiceName: !Sub 'com.amazonaws.${AWS::Region}.secretsmanager'
      VpcEndpointType: Interface
      SubnetIds:
        - !Ref LambdaSubnet
      VpcId: !Ref CrossadVpc

  LambdaSubnet:
    Type: AWS::EC2::Subnet
    Properties:
      CidrBlock: 192.168.0.0/24
      VpcId: !Ref CrossadVpc

  LambdaSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: LambdaSecurityGroup
      GroupName: LambdaSecurityGroup
      SecurityGroupEgress:
        - CidrIp: 0.0.0.0/0
          CidrIpv6: ::/0
          Description: AllowAllOutbound
          IpProtocol: -1
          FromPort: -1
          ToPort: -1
      SecurityGroupIngress:
        - CidrIp: 0.0.0.0/0
          CidrIpv6: ::/0
          Description: AlllowAllInboundTraffic
          IpProtocol: -1
          FromPort: -1
          ToPort: -1
      VpcId: !Ref CrossadVpc
   
ProxyLambda:
    Type: AWS::Serverless::Function
    Properties:
      Architectures:
        - arm64
      CodeUri: proxy
      Handler: com.github.somegroup.crossad.proxy.Proxy::handleRequest
      MemorySize: 1798
      PackageType: Zip
      Role: !GetAtt LambdaRole.Arn
      Runtime: java17
      Timeout: 600
      Tracing: Active
      Environment:
        Variables:
          ALLOWED_ORIGIN: "*"
          DEFAULT_HOST: !Ref CrossAdHost
          CROSSAD_CREDENTIALS_SECRET_NAME: !Ref CrossAdCredentialsSecretName
      VpcConfig:
        SecurityGroupIds:
          - !GetAtt LambdaSecurityGroup.GroupId
        SubnetIds:
          - !GetAtt LambdaSubnet.SubnetId
      Events:
        GetQuery:
          Type: Api
          Properties:
            Auth:
              Authorizer: CognitoAuth
              Scopes: ["https://crossad.loremipsum.com/scopes/backend"]
            Path: /{proxy+}
            Method: get
            RestApiId: !Ref ApiGateway
        PostQuery:
          Type: Api
          Properties:
            Auth:
              Authorizer: CognitoAuth
              Scopes: [ "https://crossad.loremipsum.com/scopes/backend" ]
            Path: /{proxy+}
            Method: post
            RestApiId: !Ref ApiGateway

amazon-web-services vpc secretsmanager
1个回答
0
投票

部署在 VPC 内的 Lambda 函数可能有点难以处理;尽管您可能期望它们在连接方面的操作与 EC2 类似,但事实并非如此。

在您的场景中,您确实在公有子网中部署 Lambda,如模板中使用默认路由表所示,该路由表通过 Internet 网关路由流量。但是,Lambda 函数无法与 Internet 网关一起运行,因为它没有分配公共 IP 地址

要访问私有资源,请将您的函数连接到私有子网。 如果您的功能需要互联网访问,请使用网络地址 翻译(NAT)。将函数连接到公共子网不会 为其提供互联网访问权限或公共 IP 地址。

要纠正此问题,请创建一个私有子网并向其分配 VPC 终端节点和 Lambda 函数。

© www.soinside.com 2019 - 2024. All rights reserved.