如何通过 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
profile picture
专家
已提问 5 个月前31 查看次数
1 回答
0

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

profile picture
专家
已回答 5 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则