为存在的资源在云中获取 InvalidRouteTableID.NotFound

问题描述

我在运行时反复在云形成堆栈中得到一个 InvalidRouteTableID.NotFound

aws cloudformation create-stack --stack-name sample --template-body file://aws-network.yml

我不知道为什么。

这是我的 cloudformation 模板 aws-network.yml。它非常标准,它创建 VPC、子网、互联网网关、弹性 IP 地址、nat 网关、路由表和关联。

AWstemplateFormatVersion: 2010-09-09
# This CloudFormation template deploys a basic VPC / Network. 
Resources:
  vpc:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.0.0/16
      EnablednsHostnames: true
      EnablednsSupport: false 
      InstanceTenancy: default
      Tags:
        - Key: Name
          Value: !Join ['',[!Ref "AWS::StackName","-vpc"]]
  internetGateway:
    Type: AWS::EC2::InternetGateway
    DependsOn: vpc
    Properties:
      Tags:
        - Key: Name
          Value: !Join ['',"-igw"]]
  attachGateway:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref vpc
      InternetGatewayId: !Ref internetGateway
  publicsubnetA:
    Type: AWS::EC2::subnet
    Properties:
      VpcId: !Ref vpc
      CidrBlock: 10.0.10.0/24
      AvailabilityZone: !Select [ 0,!GetAZs ]
      Tags:
        - Key: Name
          Value: !Join ['',"-public-a"]]
  publicsubnetB:
    Type: AWS::EC2::subnet
    Properties:
      VpcId: !Ref vpc
      CidrBlock: 10.0.20.0/24
      AvailabilityZone: !Select [ 1,"-public-b"]]
  privatesubnetA:
    Type: AWS::EC2::subnet
    Properties:
      VpcId: !Ref vpc
      CidrBlock: 10.0.30.0/24
      AvailabilityZone: !Select [ 0,"-private-a"]]
  privatesubnetB:
    Type: AWS::EC2::subnet
    Properties:
      VpcId: !Ref vpc
      CidrBlock: 10.0.40.0/24
      AvailabilityZone: !Select [ 1,"-private-b"]]
  publicRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref vpc
      Tags:
        - Key: Name
          Value: !Join ['',"-public"]]
  publicRoute1:
    Type: AWS::EC2::Route
    DependsOn: attachGateway
    Properties:
      RouteTableId: !Ref publicRouteTable
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref internetGateway
  natGateway: # it has a cost https://aws.amazon.com/vpc/pricing/
    Type: AWS::EC2::NatGateway
    Properties:
      AllocationId: !GetAtt elasticIpAddress.AllocationId # gets the allocation Id from the elasticIpAddress resource
      subnetId: !Ref publicsubnetA # only associated to a public subnet to simplify and reduce costs
      Tags:
        - Key: Name
          Value: !Join ['',"-nat"]]
  elasticIpAddress:
    Type: AWS::EC2::EIP
    Properties:
      Domain: vpc
  privateRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref vpc
      Tags:
        - Key: Name
          Value: !Join ['',"-private"]]
  privateRoute1:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref privateRouteTable
      DestinationCidrBlock: 0.0.0.0/0
      NateGatewayId: !Ref natGateway
  publicsubnetARouteTableAssociation:
    Type: AWS::EC2::subnetRouteTableAssociation
    Properties:
      subnetId: !Ref publicsubnetA
      RouteTableId: publicRouteTable
  publicsubnetbrouteTableAssociation:
    Type: AWS::EC2::subnetRouteTableAssociation
    Properties:
      subnetId: !Ref publicsubnetB
      RouteTableId: publicRouteTable
  privatesubnetARouteTableAssociation:
    Type: AWS::EC2::subnetRouteTableAssociation
    Properties:
      subnetId: !Ref privatesubnetA
      RouteTableId: privateRouteTable
  privatesubnetbrouteTableAssociation:
    Type: AWS::EC2::subnetRouteTableAssociation
    Properties:
      subnetId: !Ref privatesubnetB
      RouteTableId: privateRouteTable

根据事件,这不应该发生,正如我所看到的:

2021-04-21 17:04:05 UTC+0200    privateRouteTable   
CREATE_COMPLETE -

2021-04-21 17:04:05 UTC+0200    publicRouteTable    
CREATE_COMPLETE -

2021-04-21 17:04:22 UTC+0200    privatesubnetbrouteTableAssociation 
CREATE_Failed   The routeTable ID 'privateRouteTable' does not exist (Service: AmazonEC2; Status Code: 400; Error Code: InvalidRouteTableID.NotFound; Request ID: b51b2b9c-af12-4376-b6e4-1698624f7522; Proxy: null)

2021-04-21 17:04:22 UTC+0200    publicsubnetbrouteTableAssociation  
CREATE_Failed   The routeTable ID 'publicRouteTable' does not exist (Service: AmazonEC2; Status Code: 400; Error Code: InvalidRouteTableID.NotFound; Request ID: 5cb26e14-13ca-4915-9973-109dd44c5b2e; Proxy: null)

2021-04-21 17:04:22 UTC+0200    attachGateway   
CREATE_Failed   Resource creation cancelled

2021-04-21 17:04:23 UTC+0200    privatesubnetARouteTableAssociation 
CREATE_Failed   Resource creation cancelled

2021-04-21 17:04:23 UTC+0200    publicsubnetARouteTableAssociation  
CREATE_Failed   Resource creation cancelled

2021-04-21 17:04:23 UTC+0200    natGateway  
CREATE_Failed   Resource creation cancelled

2021-04-21 17:04:24 UTC+0200    rubiko  
ROLLBACK_IN_PROGRESS    The following resource(s) Failed to create: [publicsubnetbrouteTableAssociation,attachGateway,privatesubnetbrouteTableAssociation,natGateway,publicsubnetARouteTableAssociation,privatesubnetARouteTableAssociation]. Rollback requested by user.

知道为什么找不到某些创建的资源吗?

谢谢

解决方法

解决了,我忘记了!Ref(我会收工..)

这是正确的模板

AWSTemplateFormatVersion: 2010-09-09
# This CloudFormation template deploys a basic VPC / Network. 
Resources:
  vpc:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.0.0/16
      EnableDnsHostnames: true
      EnableDnsSupport: false 
      InstanceTenancy: default
      Tags:
        - Key: Name
          Value: !Join ['',[!Ref "AWS::StackName","-vpc"]]
  internetGateway:
    Type: AWS::EC2::InternetGateway
    DependsOn: vpc
    Properties:
      Tags:
        - Key: Name
          Value: !Join ['',"-igw"]]
  attachGateway:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref vpc
      InternetGatewayId: !Ref internetGateway
  publicSubnetA:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref vpc
      CidrBlock: 10.0.10.0/24
      AvailabilityZone: !Select [ 0,!GetAZs ]
      Tags:
        - Key: Name
          Value: !Join ['',"-public-a"]]
  publicSubnetB:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref vpc
      CidrBlock: 10.0.20.0/24
      AvailabilityZone: !Select [ 1,"-public-b"]]
  privateSubnetA:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref vpc
      CidrBlock: 10.0.30.0/24
      AvailabilityZone: !Select [ 0,"-private-a"]]
  privateSubnetB:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref vpc
      CidrBlock: 10.0.40.0/24
      AvailabilityZone: !Select [ 1,"-private-b"]]
  publicRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref vpc
      Tags:
        - Key: Name
          Value: !Join ['',"-public"]]
  publicRoute1:
    Type: AWS::EC2::Route
    DependsOn: attachGateway
    Properties:
      RouteTableId: !Ref publicRouteTable
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref internetGateway
  natGateway: # it has a cost https://aws.amazon.com/vpc/pricing/
    Type: AWS::EC2::NatGateway
    Properties:
      AllocationId: !GetAtt elasticIpAddress.AllocationId # gets the allocation Id from the elasticIpAddress resource
      SubnetId: !Ref publicSubnetA # only associated to a public subnet to simplify and reduce costs
      Tags:
        - Key: Name
          Value: !Join ['',"-nat"]]
  elasticIpAddress:
    Type: AWS::EC2::EIP
    Properties:
      Domain: vpc
  privateRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref vpc
      Tags:
        - Key: Name
          Value: !Join ['',"-private"]]
  privateRoute1:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref privateRouteTable
      DestinationCidrBlock: 0.0.0.0/0
      NatGatewayId: !Ref natGateway
  publicSubnetARouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref publicSubnetA
      RouteTableId: !Ref publicRouteTable
  publicSubnetBRouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref publicSubnetB
      RouteTableId: !Ref publicRouteTable
  privateSubnetARouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref privateSubnetA
      RouteTableId: !Ref privateRouteTable
  privateSubnetBRouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref privateSubnetB
      RouteTableId: !Ref privateRouteTable

全部归功于迈克·阿特金森!