amazon-web-services - 通过 CloudFormation 创建跨 AWS 账户的 VPCPeeringConnection

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

在 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

How can I go about creating a VPCPeeringConnection to a VPC in another account via CloudFormation?

最佳答案

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

You can peer with a virtual private cloud (VPC) in another AWS account by using AWS::EC2::VPCPeeringConnection. This creates a networking connection between two VPCs that enables you to route traffic between them so they can communicate as if they were within the same network. A VPC peering connection can help facilitate data access and data transfer.

To establish a VPC peering connection, you need to authorize two separate AWS accounts within a single AWS CloudFormation stack.



资料来源: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"
      }
    }
  }
}

关于amazon-web-services - 通过 CloudFormation 创建跨 AWS 账户的 VPCPeeringConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42280709/

相关文章:

amazon-web-services - AWS CDK 中的 InterfaceVpcEndpoint 与 VpcEndpoint

amazon-web-services - AWS ECS Fargate ALB 错误(请求超时)

amazon-web-services - 如何检索超过 3 小时的 CloudWatch 指标

amazon-web-services - 从 Putty 连接到 Amazon Linux EC2 AWS 镜像时出现 "Server refused our key"问题

java - 在 AWS Amazon 上自动执行 api

amazon-web-services - AWS ELB - 多 VPC 负载平衡

amazon-web-services - 如何触发Terraform上传新的Lambda代码

MongoDB 无共享奴隶

amazon-web-services - AWS 中的网络 ACL 和路由表有什么区别?

amazon-web-services - cloudformation 模板中现有的动态 AWS 资源