amazon-web-services - 如何通过 Cloudformation 将弹性 IP 连接到 Nat 网关

标签 amazon-web-services aws-cloudformation aws-cloudformation-custom-resource aws-nat-gateway

我正在尝试学习 Aws cloud Formation ,我正在尝试创建 VPC,如图所示。它包含三个公共(public)子网、私有(private)子网、natg​​ateway 和 Internetgateway,以及公共(public)和私有(private)路由表。我试图通过云形成来实现它,但获得弹性 IP 的异常(exception)。 I am trying to learn Aws cloud Formation , where I am trying to create VPC as shown in picture.我已经创建了模板,但是当我尝试在云形成上创建堆栈时,我收到错误

"The elastic-ip ID 'xx.xxx.xx.xxx' is malformed (Service: AmazonEC2; Status Code: 400; Error Code: InvalidElasticIpID.Malformed; Request ID: 2e3a9f8c-5a7e-482e-869c-8a0e46a08f27; Proxy: null)"

。我正在尝试将弹性 IP 连接到 NatGateway 并出现上述错误。请指导我该怎么做。

{

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": {
    "ExampleEc2Instance": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "InstanceType": "t2.micro",
        "ImageId" : "ami-047a51fa27710816e",
        "AvailabilityZone" : "us-east-1a",
        "SecurityGroupIds" : [{
           "Ref":"ExampleSecurityGroup"
        }],
        "SubnetId" : {
           "Ref":"public2A"
        }
      }
    },"ExampleEc2InstancePrivate": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "InstanceType": "t2.micro",
        "ImageId" : "ami-047a51fa27710816e",
        "AvailabilityZone" : "us-east-1a",
        "SecurityGroupIds" : [{
           "Ref":"ExampleSecurityGroup"
        }],
        "SubnetId" : {
           "Ref":"private2A"
        }
      }
    },
    "public2A":{
      "Type" : "AWS::EC2::Subnet",
      "Properties":{
       "AvailabilityZone" : "us-east-1a",
       "CidrBlock" : "10.0.2.0/24",
       "Tags" : [{"Key" : "public2A", "Value" : "public2A"}],
       "VpcId" : {
        "Ref":"ExampleVpcId"
       }
      }
    },"public2B":{
      "Type" : "AWS::EC2::Subnet",
      "Properties":{
       "AvailabilityZone" : "us-east-1a",
       "CidrBlock" : "10.0.3.0/24",
       "Tags" : [{"Key" : "public2B", "Value" : "public2B"}],
       "VpcId" : {
        "Ref":"ExampleVpcId"
       }
      }
    },
    "public2C":{
      "Type" : "AWS::EC2::Subnet",
      "Properties":{
       "AvailabilityZone" : "us-east-1a",
       "CidrBlock" : "10.0.1.0/24",
       "Tags" : [{"Key" : "public2C", "Value" : "public2C"}],
       "VpcId" : {
        "Ref":"ExampleVpcId"
       }
      }
    },"private2A":{
      "Type" : "AWS::EC2::Subnet",
      "Properties":{
       "AvailabilityZone" : "us-east-1a",
       "CidrBlock" : "10.0.5.0/24",
       "Tags" : [{"Key" : "private2A", "Value" : "private2A"}],
       "VpcId" : {
        "Ref":"ExampleVpcId"
       }
      }
    },"private2B":{
      "Type" : "AWS::EC2::Subnet",
      "Properties":{
       "AvailabilityZone" : "us-east-1a",
       "CidrBlock" : "10.0.6.0/24",
       "Tags" : [{"Key" : "private2B", "Value" : "private2B"}],
       "VpcId" : {
        "Ref":"ExampleVpcId"
       }
      }
    },
    "private2C":{
      "Type" : "AWS::EC2::Subnet",
      "Properties":{
       "AvailabilityZone" : "us-east-1a",
       "CidrBlock" : "10.0.7.0/24",
       "Tags" : [{"Key" : "private2C", "Value" : "private2C"}],
       "VpcId" : {
        "Ref":"ExampleVpcId"
       }
      }
    },
    "privateRT":{
      "Type" : "AWS::EC2::RouteTable",
      "Properties" : {
          "Tags" : [{"Key" : "privateRT", "Value" : "privateRT"}],
          "VpcId" : {
            "Ref":"ExampleVpcId"
            }
        }
    },
    "publicRT":{
      "Type" : "AWS::EC2::RouteTable",
      "Properties" : {
          "Tags" : [{"Key" : "publicRT", "Value" : "publicRT"}],
          "VpcId" : {
            "Ref":"ExampleVpcId"
            }
        }
    },
    "public2ARouteTableAssociation" : {
       "Type" : "AWS::EC2::SubnetRouteTableAssociation",
       "Properties" : {
          "SubnetId" : { "Ref" : "public2A" },
          "RouteTableId" : { "Ref" : "publicRT" }
        }   
    },
    "public2BRouteTableAssociation" : {
       "Type" : "AWS::EC2::SubnetRouteTableAssociation",
       "Properties" : {
          "SubnetId" : { "Ref" : "public2B" },
          "RouteTableId" : { "Ref" : "publicRT" }
        }   
    },
    "public2CRouteTableAssociation" : {
       "Type" : "AWS::EC2::SubnetRouteTableAssociation",
       "Properties" : {
          "SubnetId" : { "Ref" : "public2C" },
          "RouteTableId" : { "Ref" : "publicRT" }
        }   
    },
    "private2ARouteTableAssociation" : {
       "Type" : "AWS::EC2::SubnetRouteTableAssociation",
       "Properties" : {
          "SubnetId" : { "Ref" : "private2A" },
          "RouteTableId" : { "Ref" : "privateRT" }
        }   
    },
    "private2BRouteTableAssociation" : {
       "Type" : "AWS::EC2::SubnetRouteTableAssociation",
       "Properties" : {
          "SubnetId" : { "Ref" : "private2B" },
          "RouteTableId" : { "Ref" : "privateRT" }
        }   
    },
    "private2CRouteTableAssociation" : {
       "Type" : "AWS::EC2::SubnetRouteTableAssociation",
       "Properties" : {
          "SubnetId" : { "Ref" : "private2C" },
          "RouteTableId" : { "Ref" : "privateRT" }
        }   
    },
    "myVpcInternetGateWay":{
        "Type" : "AWS::EC2::InternetGateway",
        "Properties" : {
            "Tags" : [{"Key" : "myVpcInternetGateWay", "Value" : "myVpcInternetGateWay"}]
         }
    },
    "myVpcInternetGateWayRoute":{
        "Type" : "AWS::EC2::Route",
            "Properties" : {
              "DestinationCidrBlock" : "0.0.0.0/0",
              "GatewayId" : {"Ref":"myVpcInternetGateWay"},
              "RouteTableId" : {"Ref":"publicRT"}
            }
    },
    "myVpcInternetGateWayAttachement":{
        "Type" : "AWS::EC2::VPCGatewayAttachment",
        "Properties" : {
            "InternetGatewayId" : {"Ref":"myVpcInternetGateWay"},
            "VpcId" : {"Ref":"ExampleVpcId"}
         }
    },
    "myNatGateWay":{
        "Type" : "AWS::EC2::NatGateway",
        "Properties" : {
             "AllocationId" : {"Ref":"myElasticIP"},
             "SubnetId" :{"Ref":"public2A"},
             "Tags" : [{"Key" : "myNatGateWay", "Value" : "myNatGateWay"}]
        }
    },"myVpcNatGatWayRoute":{
        "Type" : "AWS::EC2::Route",
            "Properties" : {
              "DestinationCidrBlock" : "0.0.0.0/0",
              "GatewayId" : {"Ref":"myNatGateWay"},
              "RouteTableId" : {"Ref":"privateRT"}
            }
    },
    "myElasticIP":{
        "Type" : "AWS::EC2::EIP",
        "Properties" : {
              "Domain" : "VPC",
              "Tags" : [{"Key" : "myElasticIP", "Value" : "myElasticIP"}]
         }
    },
    "ExampleSecurityGroup":{
       "Type":"AWS::EC2::SecurityGroup",
       "Properties" : {
          "GroupDescription" : "Allow http to client host",
          "GroupName" : "templateSecuritygrp",
          "Tags" : [ {"Key" : "securityGroup", "Value" : "cloudformationSecurityGroup"} ],
          "VpcId" :  {
                "Ref":"ExampleVpcId"
            }
        }
    },
    "ExampleSecurityGroupEgress" : {
        "Type":"AWS::EC2::SecurityGroupEgress",
        "Properties":{
                "IpProtocol":"-1",
                "FromPort":"-1",
                "ToPort":"-1",
                "DestinationSecurityGroupId":{
                   "Ref":"ExampleSecurityGroup"
                },
                "GroupId":{
                   "Ref":"ExampleSecurityGroup"
                }
             }
         },
    "ExampleSecurityGroupIngress" :{
         "Type":"AWS::EC2::SecurityGroupIngress",
         "Properties":{
                "IpProtocol":"-1",
                "FromPort":"-1",
                "ToPort":"-1",
                "SourceSecurityGroupId":{
                   "Ref":"ExampleSecurityGroup"
                },
                "GroupId":{
                   "Ref":"ExampleSecurityGroup"
                }
            }
         },
    "ExampleVpcId":{
       "Type":"AWS::EC2::VPC",
       "Properties" : {
          "CidrBlock" : "10.0.0.0/16",
          "EnableDnsSupport" : "false",
          "EnableDnsHostnames" : "false",
          "InstanceTenancy" : "default",
          "Tags" : [ {"Key" : "tmpltVPC", "Value" : "firstVpc"}]
       }
    }
  }
}

}

最佳答案

在您的myNatGateWay中,您应该使用GetAtt来获取AllocationId:

    "myNatGateWay":{
        "Type" : "AWS::EC2::NatGateway",
        "Properties" : {
             "AllocationId" : { "Fn::GetAtt" : ["myElasticIP", "AllocationId"]},
             "SubnetId" :{"Ref":"public2A"},
             "Tags" : [{"Key" : "myNatGateWay", "Value" : "myNatGateWay"}]
        }
    }

您还需要DependsOn在EIP中:

    "myElasticIP":{
        "Type" : "AWS::EC2::EIP",
        "DependsOn":["myVpcInternetGateWayAttachement"] ,
        "Properties" : {
              "Domain" : "VPC",
              "Tags" : [{"Key" : "myElasticIP", "Value" : "myElasticIP"}]
         }
    }

最后myVpcNatGatWayRoute应该是:

"myVpcNatGatWayRoute":{
        "Type" : "AWS::EC2::Route",
            "Properties" : {
              "DestinationCidrBlock" : "0.0.0.0/0",
              "NatGatewayId" : {"Ref":"myNatGateWay"},
              "RouteTableId" : {"Ref":"privateRT"}
            }
    }

关于amazon-web-services - 如何通过 Cloudformation 将弹性 IP 连接到 Nat 网关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66131377/

相关文章:

ruby-on-rails - AWS Elasticbeanstalk 应用程序的 Rails : How to get puma 3. 11?

amazon-web-services - 将对象放入另一个帐户的 S3 存储桶中

amazon-web-services - 无法创建 AWS Cognito 身份池

amazon-web-services - 我可以手动将 EC2 实例添加到已运行的 cloudformation 堆栈中吗?

amazon-web-services - 通过 Cloud Formation 创建 Amazon Elasticsearch Service 时 CloudWatch 资源访问策略错误

amazon-web-services - AWS Cloudformation 快速创建链接不提供其他选项

java - 下载的 Amazon S3 文件不包含数据

javascript - AWS Lambda - 错误 : Cannot find module 'uuid/v4'

apache-spark - Pyspark EMR 中步骤的 Cloudformation 模板

aws-cloudformation - AWS SAM 模板中出现 Fn::GetAtt 错误