如何通过 SAM 配置Lambda的VPC?

0

【以下的问题经过翻译处理】 我正在尝试通过CloudFormation为lambda添加一个VPC。我们使用SAM,因此它是一个“AWS:: Serverless :: Function”。我根据文档添加了CF模板的VpcConfig部分,但VPC从未附加到lambda。没有错误,成功部署,但没有VPC。然后我可以通过控制台添加VPC(以及后来的EFS)配置。漂移检测显示实际和预期之间没有差异,无论我是否手动添加VPC。稍后再次部署,使用“sam deploy”,则会静默删除VPC配置。

下面是一个最小的CloudFormation模板,显示了这种行为。我尝试了我所能想到的一切,包括引用VPC和子网的“DependsOn”子句。我错过了什么?

AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: Test template for VPC/Lambda config

Resources:
  MyVPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: "10.0.0.0/24"
      EnableDnsHostnames: true
      EnableDnsSupport: true

  MyVPCSubnetMaster:
        Type: AWS::EC2::Subnet
        Properties:
            VpcId: !Ref MyVPC
            AvailabilityZone: !Select [0, !GetAZs ""]
            CidrBlock: "10.0.0.0/28"
            MapPublicIpOnLaunch: true

  MyVPCSubnetBackup:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref MyVPC
      AvailabilityZone: !Select [ 1, !GetAZs "" ]
      CidrBlock: "10.0.0.16/28"
      MapPublicIpOnLaunch: true

  MyLambda:
    Type: AWS::Serverless::Function
    VpcConfig:
      SecurityGroupIds:
        - !GetAtt MyVPC.DefaultSecurityGroup
      SubnetIds:
        - !GetAtt MyVPCSubnetMaster.SubnetId
        - !GetAtt MyVPCSubnetBackup.SubnetId
    Properties:
      FunctionName: "MyLambda"
      Runtime: "python3.8"
      Handler: "index.handler"
      CodeUri: test/MyLambda
1개 답변
0

【以下的回答经过翻译处理】 事实证明问题很简单:我的CF模板中的“VpcConfig”语句需要放在Lambda函数的“属性”配置部分下面。

profile picture
전문가
답변함 5달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인