【问题标题】:Create VPCPeeringConnection across AWS accounts via CloudFormation通过 CloudFormation 跨 AWS 账户创建 VPCPeeringConnection
【发布时间】:2017-07-05 23:07:04
【问题描述】:

在 AWS 中,我正在尝试通过 CloudFormation 在不同账户中的两个 VPC 之间创建 VPC 对等连接。

我可以通过 UI 手动创建对等连接,包括 4 个字段:

Name
Local VPC

Target Account ID
Target VPC ID

好像CLI also supports a target Account。

当尝试通过 CloudFormation 使用 AWS::EC2::VPCPeeringConnection 对象做同样的事情时,问题就出现了,问题是这个对象似乎只支持 3 个字段,Target Account not being one of them -

PeerVpcId
VpcId
Tags

我的代码导致

AttributeError: AWS::EC2::VPCPeeringConnection object does not support attribute PeerVpcOwner

如何通过 CloudFormation 在另一个账户中创建到 VPC 的 VPCPeeringConnection?

【问题讨论】:

    标签: amazon-web-services amazon-ec2 amazon-vpc


    【解决方案1】:

    是的,您可以在两个 AWS 账户之间使用 cloudformation 配置 VPC 对等。

    您可以与另一个 AWS 账户中的虚拟私有云 (VPC) 对等 通过使用 AWS::EC2::VPCPeeringConnection。这创建了一个网络 两个 VPC 之间的连接,使您能够在之间路由流量 以便他们可以像在同一个网络中一样进行通信。 VPC 对等连接有助于促进数据访问和数据 转移。

    要建立 VPC 对等连接,您需要授权两个 单个 AWS CloudFormation 堆栈中的独立 AWS 账户。

    来源:Walkthrough: Peer with an Amazon VPC in Another AWS Account

    第 1 步:创建 VPC 和跨账户角色

    {
      "AWSTemplateFormatVersion": "2010-09-09",
      "Description": "Create a VPC and an assumable role for cross account VPC peering.",
      "Parameters": {
        "PeerRequesterAccountId": {
          "Type": "String"
        }
      },
      "Resources": {
        "vpc": {
          "Type": "AWS::EC2::VPC",
          "Properties": {
            "CidrBlock": "10.1.0.0/16",
            "EnableDnsSupport": false,
            "EnableDnsHostnames": false,
            "InstanceTenancy": "default"
          }
        },
        "peerRole": {
          "Type": "AWS::IAM::Role",
          "Properties": {
            "AssumeRolePolicyDocument": {
              "Statement": [
                {
                  "Principal": {
                    "AWS": {
                      "Ref": "PeerRequesterAccountId"
                    }
                  },
                  "Action": [
                    "sts:AssumeRole"
                  ],
                  "Effect": "Allow"
                }
              ]
            },
            "Path": "/",
            "Policies": [
              {
                "PolicyName": "root",
                "PolicyDocument": {
                  "Version": "2012-10-17",
                  "Statement": [
                    {
                      "Effect": "Allow",
                      "Action": "ec2:AcceptVpcPeeringConnection",
                      "Resource": "*"
                    }
                  ]
                }
              }
            ]
          }
        }
      },
      "Outputs": {
        "VPCId": {
          "Value": {
            "Ref": "vpc"
          }
        },
        "RoleARN": {
          "Value": {
            "Fn::GetAtt": [
              "peerRole",
              "Arn"
            ]
          }
        }
      }
    }
    

    第 2 步:创建包含 AWS::EC2::VPCPeeringConnection 的模板

    {
      "AWSTemplateFormatVersion": "2010-09-09",
      "Description": "Create a VPC and a VPC Peering connection using the PeerRole to accept.",
      "Parameters": {
        "PeerVPCAccountId": {
          "Type": "String"
        },
        "PeerVPCId": {
          "Type": "String"
        },
        "PeerRoleArn": {
          "Type": "String"
        }
      },
      "Resources": {
        "vpc": {
          "Type": "AWS::EC2::VPC",
          "Properties": {
            "CidrBlock": "10.2.0.0/16",
            "EnableDnsSupport": false,
            "EnableDnsHostnames": false,
            "InstanceTenancy": "default"
          }
        },
        "vpcPeeringConnection": {
          "Type": "AWS::EC2::VPCPeeringConnection",
          "Properties": {
            "VpcId": {
              "Ref": "vpc"
            },
            "PeerVpcId": {
              "Ref": "PeerVPCId"
            },
            "PeerOwnerId": {
              "Ref": "PeerVPCAccountId"
            },
            "PeerRoleArn": {
              "Ref": "PeerRoleArn"
            }
          }
        }
      },
      "Outputs": {
        "VPCId": {
          "Value": {
            "Ref": "vpc"
          }
        },
        "VPCPeeringConnectionId": {
          "Value": {
            "Ref": "vpcPeeringConnection"
          }
        }
      }
    }
    

    【讨论】:

    • 一个简单的问题,我们如何自动引用另一个账户的 VPC 和角色(无需硬编码以确保我们可以跨不同堆栈复制它)?
    • 使用 ansible 之类的东西来查询这些信息,然后将值作为 CloudFormation 参数传递。
    • 我如何忽略 vpc 对等互连的接受,因为我想在没有跨帐户角色的对等帐户上手动执行此操作
    • 不幸的是有点一文不值,因为双方都需要添加路由才能使用 VPC 对等连接。感谢 AWS! :)
    • 对等只是一个连接,路由总是分开的,即使你创建了一个vpc,在你设置子网之前你不能创建路由。 AWS 没有添加依赖项,而只是网络的性质。
    猜你喜欢
    • 2018-06-17
    • 2015-05-11
    • 2019-06-30
    • 1970-01-01
    • 2018-11-26
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    相关资源
    最近更新 更多