如何处理无法从 AWS 上的 CloudFormation 模板创建错误?

How to handle failed to create erros from CloudFormation template on AWS?

在使用 AWS CloudFormation 模板时遇到问题,我正在尝试 运行。为什么这个模板不起作用?

获取错误如下:

以下资源创建失败:[PrivateSubnet、PrivateRouteTable、InternetGatewayAttachment、DefaultPrivateRoute、WobblelandEc2Instance、VPC、PublicSubnet、WobblelandSecurityGroup、NatGateway]。用户请求回滚。

无效 ID:“InternetGateway”(应为“igw-...”)(服务:AmazonEC2;状态代码:400;错误代码:InvalidInternetGatewayId.Malformed;请求 ID:8d68ee2d- f68f-4c30-919d-9cab04fb5b99;代理:空)

elastic-ip ID 'NatGatewayEIP.AllocationId'格式错误(服务:AmazonEC2;状态码:400;错误码:InvalidElasticIpID.Malformed;请求ID:6c371787-644b-4e0b- 9e94-1ae78b9af120;代理:空)

下面是我正在尝试使用的模板

AWSTemplateFormatVersion: 2010-09-09
Description: "Wumbo Jumbo"
Parameters: 
  AvailabilityZone: 
    Type: "AWS::EC2::AvailabilityZone::Name"
  EnvironmentName: 
    Description: "An environment name that is prefixed to resource names"
    Type: String
  KeyName: 
    Default: mongodb
    Type: "AWS::EC2::KeyPair::KeyName"
  PrivateSubnetCIDR: 
    Default: 10.0.2.0/24
    Description: "Please enter the IP range (CIDR notation) for the private subnet in the first Availability Zone"
    Type: String
  PublicSubnetCIDR: 
    Default: 10.0.0.0/24
    Description: "Please enter the IP range (CIDR notation) for the public subnet in the first Availability Zone"
    Type: String
  VpcCIDR: 
    Default: 10.0.0.0/16
    Description: "Please enter the IP range (CIDR notation) for this VPC"
    Type: String
Resources: 
  WobblelandEc2Instance: 
    Properties: 
      ImageId: ami-04505e74c0741db8d
      InstanceType: t2.micro
      KeyName: KeyName
      SecurityGroupIds: 
        - WobblelandSecurityGroup
      UserData: 
        Fn::Sub: |
            #!/bin/bash
            wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
            echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
            sudo apt-get update
            sudo apt-get install -y mongodb-org
            
    Type: "AWS::EC2::Instance"
  WobblelandSecurityGroup: 
    Properties: 
      GroupDescription: "Allow HTTP/HTTPS and SSH inbound and outbound traffic"
      GroupName: 
        - "-"
        - 
          - Wobbleland-security-group
          - dev
      SecurityGroupIngress: 
        - 
          CidrIp: 0.0.0.0/0
          FromPort: 80
          IpProtocol: tcp
          ToPort: 80
        - 
          CidrIp: 0.0.0.0/0
          FromPort: 443
          IpProtocol: tcp
          ToPort: 443
        - 
          CidrIp: 0.0.0.0/0
          FromPort: 22
          IpProtocol: tcp
          ToPort: 22
    Type: "AWS::EC2::SecurityGroup"
  DefaultPrivateRoute: 
    Properties: 
      DestinationCidrBlock: 0.0.0.0/0
      NatGatewayId: NatGateway
      RouteTableId: PrivateRouteTable
    Type: "AWS::EC2::Route"
  DefaultPublicRoute: 
    DependsOn: InternetGatewayAttachment
    Properties: 
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: InternetGateway
      RouteTableId: PublicRouteTable
    Type: "AWS::EC2::Route"
  InternetGateway: 
    Properties: 
      Tags: 
        - 
          Key: Name
          Value: EnvironmentName
        - 
          Key: Env
          Value: EnvironmentName
    Type: "AWS::EC2::InternetGateway"
  InternetGatewayAttachment: 
    Properties: 
      InternetGatewayId: InternetGateway
      VpcId: VPC
    Type: "AWS::EC2::VPCGatewayAttachment"
  NatGateway: 
    Properties: 
      AllocationId: NatGatewayEIP.AllocationId
      SubnetId: PublicSubnet
    Type: "AWS::EC2::NatGateway"
  NatGatewayEIP: 
    DependsOn: InternetGatewayAttachment
    Properties: 
      Domain: vpc
    Type: "AWS::EC2::EIP"
  PrivateRouteTable: 
    Properties: 
      Tags: 
        - 
          Key: Name
          Value: "${EnvironmentName} Private Routes (AZ1)"
        - 
          Key: Env
          Value: EnvironmentName
      VpcId: VPC
    Type: "AWS::EC2::RouteTable"
  PrivateSubnet: 
    Properties: 
      AvailabilityZone: 
        - 0
      CidrBlock: PrivateSubnetCIDR
      MapPublicIpOnLaunch: false
      Tags: 
        - 
          Key: Name
          Value: "${EnvironmentName} Private Subnet (AZ1)"
        - 
          Key: Env
          Value: EnvironmentName
      VpcId: VPC
    Type: "AWS::EC2::Subnet"
  PublicRouteTable: 
    Properties: 
      Tags: 
        - 
          Key: Name
          Value: "${EnvironmentName} Public Routes"
        - 
          Key: Env
          Value: EnvironmentName
      VpcId: VPC
    Type: "AWS::EC2::RouteTable"
  PublicSubnet: 
    Properties: 
      AvailabilityZone: 
        - 0
      CidrBlock: PublicSubnetCIDR
      MapPublicIpOnLaunch: true
      Tags: 
        - 
          Key: Name
          Value: "${EnvironmentName} Public Subnet (AZ1)"
        - 
          Key: Env
          Value: EnvironmentName
      VpcId: VPC
    Type: "AWS::EC2::Subnet"
  VPC: 
    Properties: 
      CidrBlock: VpcCIDR
      EnableDnsHostnames: true
      EnableDnsSupport: true
      Tags: 
        - 
          Key: Name
          Value: EnvironmentName
        - 
          Key: Env
          Value: EnvironmentName
    Type: "AWS::EC2::VPC"

而不是:

GatewayId: InternetGateway

你应该

InternetGatewayId: !GetAtt InternetGateway.InternetGatewayId