Giter Club home page Giter Club logo

terraform-aws-vpc-peering-multi-account's Introduction

Project Banner

Latest ReleaseLast UpdatedSlack Community

Terraform module to create a peering connection between any two VPCs existing in different AWS accounts.

This module supports performing this action from a 3rd account (e.g. a "root" account) by specifying the roles to assume for each member account.

IMPORTANT: AWS allows a multi-account VPC Peering Connection to be deleted from either the requester's or accepter's side. However, Terraform only allows the VPC Peering Connection to be deleted from the requester's side by removing the corresponding aws_vpc_peering_connection resource from your configuration. Read more about this on Terraform's documentation portal.

Screenshots

vpc-peeringVPC Peering Connection in the AWS Web Console

Tip

๐Ÿ‘ฝ Use Atmos with Terraform

Cloud Posse uses atmos to easily orchestrate multiple environments using Terraform.
Works with Github Actions, Atlantis, or Spacelift.

Watch demo of using Atmos with Terraform
Example of running atmos to manage infrastructure from our Quick Start tutorial.

Usage

IMPORTANT: Do not pin to master because there may be breaking changes between releases. Instead pin to the release tag (e.g. ?ref=tags/x.y.z) of one of our latest releases.

For a complete example, see examples/complete

module "vpc_peering_cross_account" {
  source = "cloudposse/vpc-peering-multi-account/aws"
  # Cloud Posse recommends pinning every module to a specific version
  # version = "x.x.x"
  namespace        = "eg"
  stage            = "dev"
  name             = "cluster"

  requester_aws_assume_role_arn             = "arn:aws:iam::XXXXXXXX:role/cross-account-vpc-peering-test"
  requester_region                          = "us-west-2"
  requester_vpc_id                          = "vpc-xxxxxxxx"
  requester_allow_remote_vpc_dns_resolution = true

  accepter_aws_assume_role_arn             = "arn:aws:iam::YYYYYYYY:role/cross-account-vpc-peering-test"
  accepter_region                          = "us-east-1"
  accepter_vpc_id                          = "vpc-yyyyyyyy"
  accepter_allow_remote_vpc_dns_resolution = true
}

The arn:aws:iam::XXXXXXXX:role/cross-account-vpc-peering-test requester IAM Role should have the following Trust Policy:

Show Trust Policy
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::XXXXXXXX:root"
      },
      "Action": "sts:AssumeRole",
      "Condition": {}
    }
  ]
}

and the following IAM Policy attached to it:

NOTE: the policy specifies the permissions to create (with terraform plan/apply) and delete (with terraform destroy) all the required resources in the requester AWS account

Show IAM Policy
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "ec2:CreateRoute",
        "ec2:DeleteRoute"
      ],
      "Resource": "arn:aws:ec2:*:XXXXXXXX:route-table/*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "ec2:DescribeVpcPeeringConnections",
        "ec2:DescribeVpcs",
        "ec2:ModifyVpcPeeringConnectionOptions",
        "ec2:DescribeSubnets",
        "ec2:DescribeVpcAttribute",
        "ec2:DescribeRouteTables"
      ],
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "ec2:AcceptVpcPeeringConnection",
        "ec2:DeleteVpcPeeringConnection",
        "ec2:CreateVpcPeeringConnection",
        "ec2:RejectVpcPeeringConnection"
      ],
      "Resource": [
        "arn:aws:ec2:*:XXXXXXXX:vpc-peering-connection/*",
        "arn:aws:ec2:*:XXXXXXXX:vpc/*"
      ]
    },
    {
      "Effect": "Allow",
      "Action": [
        "ec2:DeleteTags",
        "ec2:CreateTags"
      ],
      "Resource": "arn:aws:ec2:*:XXXXXXXX:vpc-peering-connection/*"
    }
  ]
}

where XXXXXXXX is the requester AWS account ID.


The arn:aws:iam::YYYYYYYY:role/cross-account-vpc-peering-test accepter IAM Role should have the following Trust Policy:

Show Trust Policy
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::XXXXXXXX:root"
      },
      "Action": "sts:AssumeRole",
      "Condition": {}
    }
  ]
}

NOTE: The accepter Trust Policy is the same as the requester Trust Policy since it defines who can assume the IAM Role. In the requester case, the requester account ID itself is the trusted entity. For the accepter, the Trust Policy specifies that the requester account ID XXXXXXXX can assume the role in the accepter AWS account YYYYYYYY.

and the following IAM Policy attached to it:

NOTE: the policy specifies the permissions to create (with terraform plan/apply) and delete (with terraform destroy) all the required resources in the accepter AWS account

Show IAM Policy
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "ec2:CreateRoute",
        "ec2:DeleteRoute"
      ],
      "Resource": "arn:aws:ec2:*:YYYYYYYY:route-table/*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "ec2:DescribeVpcPeeringConnections",
        "ec2:DescribeVpcs",
        "ec2:ModifyVpcPeeringConnectionOptions",
        "ec2:DescribeSubnets",
        "ec2:DescribeVpcAttribute",
        "ec2:DescribeRouteTables"
      ],
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "ec2:AcceptVpcPeeringConnection",
        "ec2:DeleteVpcPeeringConnection",
        "ec2:CreateVpcPeeringConnection",
        "ec2:RejectVpcPeeringConnection"
      ],
      "Resource": [
        "arn:aws:ec2:*:YYYYYYYY:vpc-peering-connection/*",
        "arn:aws:ec2:*:YYYYYYYY:vpc/*"
      ]
    },
    {
      "Effect": "Allow",
      "Action": [
        "ec2:DeleteTags",
        "ec2:CreateTags"
      ],
      "Resource": "arn:aws:ec2:*:YYYYYYYY:vpc-peering-connection/*"
    }
  ]
}

where YYYYYYYY is the accepter AWS account ID.

For more information on IAM policies and permissions for VPC peering, see Creating and managing VPC peering connections.

Important

In Cloud Posse's examples, we avoid pinning modules to specific versions to prevent discrepancies between the documentation and the latest released versions. However, for your own projects, we strongly advise pinning each module to the exact version you're using. This practice ensures the stability of your infrastructure. Additionally, we recommend implementing a systematic approach for updating versions to avoid unexpected changes.

Makefile Targets

Available targets:

  help                                Help screen
  help/all                            Display help for all targets
  help/short                          This help short screen
  lint                                Lint terraform code

Requirements

Name Version
terraform >= 1.3
aws >= 2.0

Providers

Name Version
aws.accepter >= 2.0
aws.requester >= 2.0

Modules

Name Source Version
accepter cloudposse/label/null 0.25.0
requester cloudposse/label/null 0.25.0
this cloudposse/label/null 0.25.0

Resources

Name Type
aws_route.accepter resource
aws_route.requester resource
aws_vpc_peering_connection.requester resource
aws_vpc_peering_connection_accepter.accepter resource
aws_vpc_peering_connection_options.accepter resource
aws_vpc_peering_connection_options.requester resource
aws_caller_identity.accepter data source
aws_caller_identity.requester data source
aws_region.accepter data source
aws_region.requester data source
aws_route_table.requester data source
aws_route_tables.accepter data source
aws_route_tables.default_rts data source
aws_subnets.accepter data source
aws_subnets.requester data source
aws_vpc.accepter data source
aws_vpc.requester data source

Inputs

Name Description Type Default Required
accepter_allow_remote_vpc_dns_resolution Allow accepter VPC to resolve public DNS hostnames to private IP addresses when queried from instances in the requester VPC bool true no
accepter_aws_access_key Access key id to use in accepter account string null no
accepter_aws_assume_role_arn Accepter AWS Assume Role ARN string null no
accepter_aws_profile Profile used to assume accepter_aws_assume_role_arn string "" no
accepter_aws_secret_key Secret access key to use in accepter account string null no
accepter_aws_token Session token for validating temporary credentials string null no
accepter_enabled Flag to enable/disable the accepter side of the peering connection bool true no
accepter_region Accepter AWS region string n/a yes
accepter_subnet_tags Only add peer routes to accepter VPC route tables of subnets matching these tags map(string) {} no
accepter_vpc_id Accepter VPC ID filter string "" no
accepter_vpc_tags Accepter VPC Tags filter map(string) {} no
add_attribute_tag If true will add additional attribute tag to the requester and accceptor resources bool true no
additional_tag_map Additional key-value pairs to add to each map in tags_as_list_of_maps. Not added to tags or id.
This is for some rare cases where resources want additional configuration of tags
and therefore take a list of maps with tag key, value, and additional configuration.
map(string) {} no
attributes ID element. Additional attributes (e.g. workers or cluster) to add to id,
in the order they appear in the list. New attributes are appended to the
end of the list. The elements of the list are joined by the delimiter
and treated as a single ID element.
list(string) [] no
auto_accept Automatically accept the peering bool true no
aws_route_create_timeout Time to wait for AWS route creation specifed as a Go Duration, e.g. 2m string "5m" no
aws_route_delete_timeout Time to wait for AWS route deletion specifed as a Go Duration, e.g. 5m string "5m" no
context Single object for setting entire context at once.
See description of individual variables for details.
Leave string and numeric variables as null to use default value.
Individual variable settings (non-null) override settings in context object,
except for attributes, tags, and additional_tag_map, which are merged.
any
{
"additional_tag_map": {},
"attributes": [],
"delimiter": null,
"descriptor_formats": {},
"enabled": true,
"environment": null,
"id_length_limit": null,
"label_key_case": null,
"label_order": [],
"label_value_case": null,
"labels_as_tags": [
"unset"
],
"name": null,
"namespace": null,
"regex_replace_chars": null,
"stage": null,
"tags": {},
"tenant": null
}
no
delimiter Delimiter to be used between ID elements.
Defaults to - (hyphen). Set to "" to use no delimiter at all.
string null no
descriptor_formats Describe additional descriptors to be output in the descriptors output map.
Map of maps. Keys are names of descriptors. Values are maps of the form
{<br> format = string<br> labels = list(string)<br>}
(Type is any so the map values can later be enhanced to provide additional options.)
format is a Terraform format string to be passed to the format() function.
labels is a list of labels, in order, to pass to format() function.
Label values will be normalized before being passed to format() so they will be
identical to how they appear in id.
Default is {} (descriptors output will be empty).
any {} no
enabled Set to false to prevent the module from creating any resources bool null no
environment ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT' string null no
id_length_limit Limit id to this many characters (minimum 6).
Set to 0 for unlimited length.
Set to null for keep the existing setting, which defaults to 0.
Does not affect id_full.
number null no
label_key_case Controls the letter case of the tags keys (label names) for tags generated by this module.
Does not affect keys of tags passed in via the tags input.
Possible values: lower, title, upper.
Default value: title.
string null no
label_order The order in which the labels (ID elements) appear in the id.
Defaults to ["namespace", "environment", "stage", "name", "attributes"].
You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present.
list(string) null no
label_value_case Controls the letter case of ID elements (labels) as included in id,
set as tag values, and output by this module individually.
Does not affect values of tags passed in via the tags input.
Possible values: lower, title, upper and none (no transformation).
Set this to title and set delimiter to "" to yield Pascal Case IDs.
Default value: lower.
string null no
labels_as_tags Set of labels (ID elements) to include as tags in the tags output.
Default is to include all labels.
Tags with empty values will not be included in the tags output.
Set to [] to suppress all generated tags.
Notes:
The value of the name tag, if included, will be the id, not the name.
Unlike other null-label inputs, the initial setting of labels_as_tags cannot be
changed in later chained modules. Attempts to change it will be silently ignored.
set(string)
[
"default"
]
no
name ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.
This is the only ID element not also included as a tag.
The "name" tag is set to the full id string. There is no tag with the value of the name input.
string null no
namespace ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique string null no
regex_replace_chars Terraform regular expression (regex) string.
Characters matching the regex will be removed from the ID elements.
If not set, "/[^a-zA-Z0-9-]/" is used to remove all characters other than hyphens, letters and digits.
string null no
requester_allow_remote_vpc_dns_resolution Allow requester VPC to resolve public DNS hostnames to private IP addresses when queried from instances in the accepter VPC bool true no
requester_aws_access_key Access key id to use in requester account string null no
requester_aws_assume_role_arn Requester AWS Assume Role ARN string n/a yes
requester_aws_profile Profile used to assume requester_aws_assume_role_arn string "" no
requester_aws_secret_key Secret access key to use in requester account string null no
requester_aws_token Session token for validating temporary credentials string null no
requester_region Requester AWS region string n/a yes
requester_subnet_tags Only add peer routes to requester VPC route tables of subnets matching these tags map(string) {} no
requester_vpc_id Requester VPC ID filter string "" no
requester_vpc_tags Requester VPC Tags filter map(string) {} no
skip_metadata_api_check Don't use the credentials of EC2 instance profile bool false no
stage ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' string null no
tags Additional tags (e.g. {'BusinessUnit': 'XYZ'}).
Neither the tag keys nor the tag values will be modified by this module.
map(string) {} no
tenant ID element _(Rarely used, not included by default)_. A customer identifier, indicating who this instance of a resource is for string null no

Outputs

Name Description
accepter_accept_status Accepter VPC peering connection request status
accepter_connection_id Accepter VPC peering connection ID
accepter_subnet_route_table_map Map of accepter VPC subnet IDs to route table IDs
requester_accept_status Requester VPC peering connection request status
requester_connection_id Requester VPC peering connection ID

Related Projects

Check out these related projects.

  • terraform-aws-vpc - Terraform Module that defines a VPC with public/private subnets across multiple AZs with Internet Gateways
  • terraform-aws-vpc-peering - Terraform module to create a peering connection between two VPCs in the same AWS account
  • terraform-aws-kops-vpc-peering - Terraform module to create a peering connection between a backing services VPC and a VPC created by Kops

References

For additional context, refer to some of these links.

  • What is VPC Peering? - VPC peering connection is a networking connection between two VPCs that enables you to route traffic between them using private IPv4 addresses or IPv6 addresses.

Tip

Use Terraform Reference Architectures for AWS

Use Cloud Posse's ready-to-go terraform architecture blueprints for AWS to get up and running quickly.

โœ… We build it together with your team.
โœ… Your team owns everything.
โœ… 100% Open Source and backed by fanatical support.

Request Quote

๐Ÿ“š Learn More

Cloud Posse is the leading DevOps Accelerator for funded startups and enterprises.

Your team can operate like a pro today.

Ensure that your team succeeds by using Cloud Posse's proven process and turnkey blueprints. Plus, we stick around until you succeed.

Day-0: Your Foundation for Success

  • Reference Architecture. You'll get everything you need from the ground up built using 100% infrastructure as code.
  • Deployment Strategy. Adopt a proven deployment strategy with GitHub Actions, enabling automated, repeatable, and reliable software releases.
  • Site Reliability Engineering. Gain total visibility into your applications and services with Datadog, ensuring high availability and performance.
  • Security Baseline. Establish a secure environment from the start, with built-in governance, accountability, and comprehensive audit logs, safeguarding your operations.
  • GitOps. Empower your team to manage infrastructure changes confidently and efficiently through Pull Requests, leveraging the full power of GitHub Actions.

Request Quote

Day-2: Your Operational Mastery

  • Training. Equip your team with the knowledge and skills to confidently manage the infrastructure, ensuring long-term success and self-sufficiency.
  • Support. Benefit from a seamless communication over Slack with our experts, ensuring you have the support you need, whenever you need it.
  • Troubleshooting. Access expert assistance to quickly resolve any operational challenges, minimizing downtime and maintaining business continuity.
  • Code Reviews. Enhance your teamโ€™s code quality with our expert feedback, fostering continuous improvement and collaboration.
  • Bug Fixes. Rely on our team to troubleshoot and resolve any issues, ensuring your systems run smoothly.
  • Migration Assistance. Accelerate your migration process with our dedicated support, minimizing disruption and speeding up time-to-value.
  • Customer Workshops. Engage with our team in weekly workshops, gaining insights and strategies to continuously improve and innovate.

Request Quote

โœจ Contributing

This project is under active development, and we encourage contributions from our community.

Many thanks to our outstanding contributors:

For ๐Ÿ› bug reports & feature requests, please use the issue tracker.

In general, PRs are welcome. We follow the typical "fork-and-pull" Git workflow.

  1. Review our Code of Conduct and Contributor Guidelines.
  2. Fork the repo on GitHub
  3. Clone the project to your own machine
  4. Commit changes to your own branch
  5. Push your work back up to your fork
  6. Submit a Pull Request so that we can review your changes

NOTE: Be sure to merge the latest changes from "upstream" before making a pull request!

๐ŸŒŽ Slack Community

Join our Open Source Community on Slack. It's FREE for everyone! Our "SweetOps" community is where you get to talk with others who share a similar vision for how to rollout and manage infrastructure. This is the best place to talk shop, ask questions, solicit feedback, and work together as a community to build totally sweet infrastructure.

๐Ÿ“ฐ Newsletter

Sign up for our newsletter and join 3,000+ DevOps engineers, CTOs, and founders who get insider access to the latest DevOps trends, so you can always stay in the know. Dropped straight into your Inbox every week โ€” and usually a 5-minute read.

๐Ÿ“† Office Hours

Join us every Wednesday via Zoom for your weekly dose of insider DevOps trends, AWS news and Terraform insights, all sourced from our SweetOps community, plus a live Q&A that you canโ€™t find anywhere else. It's FREE for everyone!

License

License

Preamble to the Apache License, Version 2.0

Complete license is available in the LICENSE file.

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

  https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.

Trademarks

All other trademarks referenced herein are the property of their respective owners.


Copyright ยฉ 2017-2024 Cloud Posse, LLC

README footer

Beacon

terraform-aws-vpc-peering-multi-account's People

Contributors

actions-user avatar aknysh avatar benbentwo avatar chrisdotm avatar cloudpossebot avatar dboesswetter avatar dmitrijn avatar dmitriy-lukyanchikov avatar dtw45 avatar dylanbannon avatar gberenice avatar gowiem avatar hche608 avatar heruscode avatar jamestimms avatar max-lobur avatar maximmi avatar morremeyer avatar nitrocode avatar osterman avatar renovate[bot] avatar sgrimm avatar tommij avatar zaargh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

terraform-aws-vpc-peering-multi-account's Issues

Naming convention support for multi-project - multi-region - multi environment peering and multi-tenant VPCs

Have a question? Please checkout our Slack Community or visit our Slack Archive.

Slack Community

Describe the Feature

Support for multi-tenant peering / peering that spans projects, regions, environments, etc.

The naming convention created by https://registry.terraform.io/modules/cloudposse/label/null/latest -- in the way that has been set up in this repo -- it is assumed these are single tenant.

I am happy to make a pull request for this, as I've done this myself, but I'm not a complete terraform export and am not as familiar with your testing requirements / conventions as you might like.

Expected Behavior

Separately named resources for requester/accepter depending on project

Use Case

I have infrastructure that is shared between multiple projects, regions, and environments. One of these resources is our database VPC, which needs to peer with stage, prod, and a test resource (due to some limitations of instance sizes, pricing, and availability, it is a multi-tenant DB)

Describe Ideal Solution

If there was an option of setting:

  • namespace
  • environment
  • stage
  • name
  • delimiter
  • attributes
  • tags

for both requester and accepter, that would be very useful, as these are not always the same.
I can see supporting the current behavior and the new feature by:

  1. Create module.requester_context and module.accepter_context separately instead of using the module.this
  2. use the var.requester_X / var.accepter_X first, then default to var.X for the module.accepter_context / module.requester_context
  3. use module.requester_context and module.accepter _context instead of your current module.this

that should allow for this repo to support multi-tenant VPC peering, or projects that need to peer between projects/regions/etc.

Alternatives Considered

Set values that expand to both resources, however that breaks tag lookup

Additional Context

Add any other context or screenshots about the feature request here.

enabled = false doesn't work with [requester|accepter]_aws_assume_role_arn because of enabled ternary on the provider definition

Describe the Bug

when running enabled = false, terraform doesn't assume roles on account of the defined assume_role variable as it is not for_eached: [edit, garbled]

https://github.com/cloudposse/terraform-aws-vpc-peering-multi-account/blob/82ba0a4b1612aa7b5e93c188aeb799e1640601a5/requester.tf#L67C5-L67C88

for_each = local.enabled && var.accepter_aws_assume_role_arn != "" ? ["true"] : []

as a result, teardown is not possible if using either of:

  • requester_aws_assume_role_arn
  • accepter_aws_assume_role_arn

this causes: Error: reading EC2 VPC Peering Connection (pcx-vpc_peering_id): UnauthorizedOperation: You are not authorized to perform this operation. User: arn:aws:sts::0123456789:assumed-role/some_role is not authorized to perform: ec2:DescribeVpcPeeringConnections because no identity-based policy allows the ec2:DescribeVpcPeeringConnections action

Expected Behavior

setting enabled = false should plan/teardown successfully when using requester_aws_assume_role_arn | accepter_aws_assume_role_arn

Steps to Reproduce

  • start multi-account peering with requester_aws_assume_role_arn or accepter_aws_assume_role_arn
  • apply
  • set enabled = false
  • plan -> permission denied (assuming the role terraform is running as initially doesn't have permissions to either of the two accounts - and would depend on role chaining)

Screenshots

No response

Environment

No response

Additional Context

I believe the provider should always be enabled, including the role_arn part, so removing the enabled ternary part:

for_each = local.enabled && var.accepter_aws_assume_role_arn != "" ? ["true"] : []

to ->

for_each = var.[accepter|requester]_aws_assume_role_arn
should fix the issue. will likely draft a pr later.

Order of Operations issue?

Hey there, I am using your terraform-aws-vpc module in combination with this one to create and peer a vpc. I'm having what seems it may be an order of operations issue? Here is my main.tf:

module "vpc" {
  source     = "git::https://github.com/cloudposse/terraform-aws-vpc.git?ref=0.4.0"
  namespace  = "${var.namespace}"
  stage      = "${var.stage}"
  name       = "${var.app_name}"
  cidr_block = "${var.cidr_block}"
}

data "aws_availability_zones" "available" {}

module "dynamic_subnets" {
  source             = "git::https://github.com/cloudposse/terraform-aws-dynamic-subnets.git?ref=0.4.0"
  namespace          = "${var.namespace}"
  stage              = "${var.stage}"
  name               = "${var.app_name}"
  region             = "${var.region}"
  availability_zones = ["${data.aws_availability_zones.available.names}"]
  vpc_id             = "${module.vpc.vpc_id}"
  igw_id             = "${module.vpc.igw_id}"
  cidr_block         = "${var.cidr_block}"
}

module "vpc_peering" {
  source           = "git::https://github.com/cloudposse/terraform-aws-vpc-peering-multi-account.git?ref=0.4.0"
  namespace        = "${var.namespace}"
  stage            = "${var.stage}"
  name             = "${var.app_name}"
  auto_accept      = true

  accepter_aws_assume_role_arn             = "${var.vpn_account_arn}"
  accepter_region                          = "${var.vpn_region}"
  accepter_vpc_id                          = "${var.vpn_vpc_id}"
  accepter_allow_remote_vpc_dns_resolution = "true"

  requester_aws_assume_role_arn             = "${var.account_arn}"
  requester_region                          = "${var.region}"
  requester_vpc_id                          = "${module.vpc.vpc_id}"
  requester_allow_remote_vpc_dns_resolution = "true"
}

When I run terraform plan I am getting the following error:

Error: Error refreshing state: 1 error(s) occurred:

* module.vpc_peering.data.aws_route_table.requester: data.aws_route_table.requester: value of 'count' cannot be computed

My only guess as to what could be happening here is it's failing to get the route table data because the vpc does not yet exist? You'd think it would fail with an aws error rather than an error on the counter though. Hopefully someone might have some insight here.

I was able to get this working by splitting the peering into it's own tf file and running that after this one creates the vpc and passing forward the tfstate file. But ideally I'd like it all to share a tfstate file otherwise if I need to update this file it will wipe out the peering connection configuration.

Thanks

Module does not support for_each due to hard coded providers

Describe the Bug

The module is unable to support for_each since it contains provider configuration:

Module "foo" cannot be used with for_each because it contains a
nested provider configuration for "aws.requester", at
.terraform/modules/foo/requester.tf:60,10-15.

Expected Behavior

The module should support for_each loops.

Additional Context

I believe passing the providers into the module (instead of configuring the providers w/in the module) would do the trick. I labeled this as bug but it may be a feature request instead.

Module fails if subnet uses default "main" routing table

Describe the Bug

If one of the subnets in one of the VPCs is using the default "main" routing table, the module fails.

Steps to Reproduce

Steps to reproduce the behavior:

  1. Create a new VPC
  2. Create new subnets in the VPC without associating route tables with them
  3. Use the VPC as the "accepter"
  4. See error
Error: query returned no results. Please change your search criteria and try again

  on .terraform/modules/vpc_peering/accepter.tf line 64, in data "aws_route_table" "accepter":
  64: data "aws_route_table" "accepter" {

Additional Context

This bug was introduced by #44 which incorporated #31 but still did not really fix #30.

I propose eliminating the option to filter subnets because it never really worked. If someone wants it back, they can submit a PR to implement it properly.

Problems with filtering by subnet

Problem 1: Finding the route tables to modify

Sub-problem 1A: Looking up the route table for a subnet

You cannot find the route table for a subnet via

data "aws_route_table" "subnet" {
  subnet_id = local.subnet_id
}

because this will throw an error if the subnet does not have an associated route table and is instead using the default "main" table. That is the source of this bug.

Because there is no way to make this conditional, we simply cannot use data "aws_route_table".

Sub-problem 1B: Looking up the subnet for a route table

You could try looking up all the route tables and filtering them by associated subnets, but

  • this is hard, because you have to treat the "main" route table specially and match it to the subnets that have no associated route table, and
  • what do you do if you one route table is associated with both an included and an excluded subnet?
Conclusion: you might as well add routes to all the route tables, because you cannot promise better restrictions

After all, the entire VPC might have only one, default route table.

Problem 2: Why are we looking up subnets by tag, anyway?

The only meaningful restriction we can make is to limit routing to some portion of the VPC's CIDR, so we might as well pass explicit CIDRs for each VPC and just route those in all routing tables. It is not at all clear that selecting the CIDRs by subnet tag is more convenient than explicitly specifying them (or having the root module look them up any way it wants to).

Submodule to create `accepter_aws_assume_role_arn` peering role

Have a question? Please checkout our Slack Community or visit our Slack Archive.

Slack Community

Describe the Feature

Docs on how to create this https://github.com/cloudposse/terraform-aws-components/tree/master/modules/vpc-peering

It would be nice to create the accepter_aws_assume_role_arn IAM role using a submodule

locals {
  account_id = data.aws_caller_identity.current.account_id
}

data "aws_caller_identity" "current" {}

data "aws_iam_policy_document" "vpc_peering" {
  statement {
    sid       = ""
    effect    = "Allow"
    resources = ["arn:aws:ec2:*:${var.accepter_account}:route-table/*"]

    actions = [
      "ec2:CreateRoute",
      "ec2:DeleteRoute",
    ]
  }

  statement {
    sid       = ""
    effect    = "Allow"
    resources = ["*"]

    actions = [
      "ec2:DescribeVpcPeeringConnections",
      "ec2:DescribeVpcs",
      "ec2:ModifyVpcPeeringConnectionOptions",
      "ec2:DescribeSubnets",
      "ec2:DescribeVpcAttribute",
      "ec2:DescribeRouteTables",
    ]
  }

  statement {
    sid    = ""
    effect = "Allow"

    resources = [
      "arn:aws:ec2:*:${var.accepter_account}:vpc-peering-connection/*",
      "arn:aws:ec2:*:${var.accepter_account}:vpc/*",
    ]

    actions = [
      "ec2:AcceptVpcPeeringConnection",
      "ec2:DeleteVpcPeeringConnection",
      "ec2:CreateVpcPeeringConnection",
      "ec2:RejectVpcPeeringConnection",
    ]
  }

  statement {
    sid       = ""
    effect    = "Allow"
    resources = ["arn:aws:ec2:*:${var.accepter_account}:vpc-peering-connection/*"]

    actions = [
      "ec2:DeleteTags",
      "ec2:CreateTags",
    ]
  }
}

resource "aws_iam_role" "vpc_peering" {
  name               = "vpc-peering-role"

  assume_role_policy = jsonencode({
    Version = "2012-10-17"
    Statement = [
      {
        Action = "sts:AssumeRole"
        Effect = "Allow"
        Sid    = ""
        Principal = {
          AWS = [
            "arn:aws:iam::${var.requester_account}:root",
          ]
        }
      },
    ]
  })
}

resource "aws_iam_role_policy" "vpc_peering" {
  name = "vpc-peering-policy"
  role = aws_iam_role.vpc_peering.id

  policy = data.aws_iam_policy_document.vpc_peering.json
}

Add profile as provider option

The way we have set up our accounts is we have one auth account and then every other account in our org has an assume role that the auth account can assume. This means that when we use the aws cli, we have a base profile (that we don't set as default for security reasons) with credentials and then every other profile is an assume role with that base profile as the source profile. So when using this module, it does not work because I have no way of specifying the profile I want the provider to use. Can this be added?

So for example, this is how we set up our providers:

provider "aws" {
  alias           = "other-account"
  region          = "us-east-1"
  profile         = "ops"
  assume_role {
    role_arn      = "arn:aws:iam::XXXXXXXXXXXX:role/peering-assume-role"
  }
}

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: undefined. Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

Enabled set to false results in errors

Describe the Bug

When setting enabled to false you get errors in the aws_vpc_peering_connection_options.accepter and the aws_vpc_peering_connection_options.requester. The errors seem to be because these two resources are missing count.

Expected Behavior

When I set enabled to false I expect the module to do nothing.

Steps to Reproduce

Steps to reproduce the behavior:

  1. Set enabled to false with other variables filled with empty strings
  2. Run terraform plan (no errors should be shown)
  3. Run terraform apply (see errors)

Error Message

Error: error reading VPC Peering Connection (): InvalidVpcPeeringConnectionId.Malformed: Invalid id: "" (expecting "pcx-...")
        status code: 400, request id: c62fad7b-fc41-45d5-9fcb-6b97d7836638

  on .terraform/modules/vpc_peering_2/accepter.tf line 105, in resource "aws_vpc_peering_connection_options" "accepter":
 105: resource "aws_vpc_peering_connection_options" "accepter" {



Error: error reading VPC Peering Connection (): InvalidVpcPeeringConnectionId.Malformed: Invalid id: "" (expecting "pcx-...")
        status code: 400, request id: fc8948ec-92c9-4901-834d-9e6263055096

  on .terraform/modules/vpc_peering_2/requester.tf line 120, in resource "aws_vpc_peering_connection_options" "requester":
 120: resource "aws_vpc_peering_connection_options" "requester" {

Environment (please complete the following information):

Anything that will help us triage the bug will help. Here are some ideas:

  • OS: Ubuntu 18.04.4
  • Code Version: 0.7.0 (but I see the missing count in master)

Additional Context

I'm trying to loop through a list of VPC Peers but due to count in modules not being supported I've created three modules that I've set to enabled=false if the list of VPC Peers doesn't have a second or third element.

locals {
  # A dirty hack to support the below module hack
  vpc_peers = [for index in [0, 1, 2] : {
    peer_vpc_id    = (length(var.vpc_peers) > index) ? data.terraform_remote_state.aws_peer_vpcs[index].outputs.vpc_id : ""
    peer_region    = (length(var.vpc_peers) > index) ? data.terraform_remote_state.aws_peer_vpcs[index].outputs.region : ""
    peer_vpc_name  = (length(var.vpc_peers) > index) ? data.terraform_remote_state.aws_peer_vpcs[index].outputs.vpc_name : ""
    requester_role = (length(var.vpc_peers) > index) ? var.vpc_peers[index].requester_role : ""
    accepter_role  = (length(var.vpc_peers) > index) ? var.vpc_peers[index].accepter_role : ""
    enabled        = length(var.vpc_peers) > index
  }]
}

# A dirty hack as count and for_each is not supported for modules until 0.13 (currently 0.12)
module "vpc_peering_1" {
  source    = "git::https://github.com/cloudposse/terraform-aws-vpc-peering-multi-account.git?ref=0.7.0"
  namespace = terraform.workspace
  stage     = var.env
  name      = "${data.terraform_remote_state.this.outputs.vpc_name}-to-${local.vpc_peers[0].peer_vpc_name}"
  enabled   = local.vpc_peers[0].enabled

  requester_aws_assume_role_arn             = local.vpc_peers[0].requester_role
  requester_region                          = var.aws_region
  requester_vpc_id                          = data.terraform_remote_state.this.outputs.vpc_id
  requester_allow_remote_vpc_dns_resolution = true

  accepter_aws_assume_role_arn             = local.vpc_peers[0].accepter_role
  accepter_region                          = var.aws_region
  accepter_vpc_id                          = local.vpc_peers[0].peer_vpc_id
  accepter_allow_remote_vpc_dns_resolution = true
}

# A dirty hack as count and for_each is not supported for modules until 0.13 (currently 0.12)
module "vpc_peering_2" {
  source    = "git::https://github.com/cloudposse/terraform-aws-vpc-peering-multi-account.git?ref=0.7.0"
  namespace = terraform.workspace
  stage     = var.env
  name      = "${data.terraform_remote_state.this.outputs.vpc_name}-to-${local.vpc_peers[0].peer_vpc_name}"
  enabled   = local.vpc_peers[1].enabled

  requester_aws_assume_role_arn             = local.vpc_peers[1].requester_role
  requester_region                          = var.aws_region
  requester_vpc_id                          = data.terraform_remote_state.this.outputs.vpc_id
  requester_allow_remote_vpc_dns_resolution = true

  accepter_aws_assume_role_arn             = local.vpc_peers[1].accepter_role
  accepter_region                          = var.aws_region
  accepter_vpc_id                          = local.vpc_peers[1].peer_vpc_id
  accepter_allow_remote_vpc_dns_resolution = true
}

Terraform 0.12 Support

Currently the module is inoperable with Terraform 0.12 code, it would be great if it could support Terraform 0.12 :)

3.x AWS provider not supported

Describe the Bug

The module requires version 2.x of the AWS provider, which is no longer under active development.

Expected Behavior

If included in a project that uses version 3.x of the AWS provider, the module should work properly.

Steps to Reproduce

Reference the module from a project with a newer version of the AWS provider, e.g., one that has

terraform {
  required_providers {
    aws = {
      source  = "aws"
      version = "~> 3.10"
    }
  }
}

Running terraform init will print an error because it can't satisfy the version constraints of the project and the module at the same time.

Environment (please complete the following information):

Terraform 0.13.4, AWS provider 3.10.0.

Additional Context

I tried modifying a local copy of the module to relax the version constraint (changed it from ~> 2.0 to >= 2.0) and it appeared to work fine with provider version 3.10.0, though I didn't test every possible permutation of inputs.

error running plan

When I run terraform plan I get the below error

module.vpc_peering_cross_account: eval: *terraform.EvalLocal, err: Missing resource instance key: Because data.aws_route_tables.accepter has "count" set, its attributes must be accessed on specific instances.

For example, to correlate with indices of a referring resource, use:
data.aws_route_tables.accepter[count.index]

Feature request: allow disabling all accepter resources

I have a use case where I need to peer vpcs with a third-party company (and therefore cannot assume a role in the accepter account). I'd like to still be able to manage my side of these vpc peering connections in terraform, even if I can't manage the accepter infrastructure. Could cloudposse add a flag to enable/disable accepter resources here?

Note also that accepter resources should be enabled for auto_accept to be true.

Accepter incorrectly maps routes to subnets

Found a bug? Maybe our Slack Community can help.

Slack Community

Describe the Bug

When calculating the local value accepter_aws_rt_map the try condition always fails to default because it's attempting to access a set value using an index which is not possible.

Expected Behavior

The try condition properly accesses the set values when available to correctly build the map.

Steps to Reproduce

Steps to reproduce the behavior:

  1. Create a VPC with multiple route tables attached to different subnets as the accepter
  2. Run the terraform module with accepted and requester
  3. See that only one route table is mapped to the peering connection

Screenshots

If applicable, add screenshots or logs to help explain your problem.

Environment (please complete the following information):

Anything that will help us triage the bug will help. Here are some ideas:

  • Terraform Version 0.13
  • AWS Provider Version 4.0

Additional Context

Wrapping the set value in "tolist()" should resolve this, but I have yet to fully validate this solution.

Error: Invalid count argument

โ”‚ Error: Invalid count argument
โ”‚ 
โ”‚   on .terraform/modules/vpc-peering-multi-account/accepter.tf line 68, in data "aws_route_table" "accepter":
โ”‚   68:   count     = local.accepter_enabled ? local.accepter_subnet_ids_count : 0
โ”‚ 
โ”‚ The "count" value depends on resource attributes that cannot be determined
โ”‚ until apply, so Terraform cannot predict how many instances will be created.
โ”‚ To work around this, use the -target argument to first apply only the
โ”‚ resources that the count depends on.
โ•ต
โ•ท
โ”‚ Error: Invalid count argument
โ”‚ 
โ”‚   on .terraform/modules/vpc-peering-multi-account/requester.tf line 122, in data "aws_route_table" "requester":
โ”‚  122:   count     = local.enabled ? local.requester_subnet_ids_count : 0
โ”‚ 
โ”‚ The "count" value depends on resource attributes that cannot be determined
โ”‚ until apply, so Terraform cannot predict how many instances will be created.
โ”‚ To work around this, use the -target argument to first apply only the
โ”‚ resources that the count depends on.
โ•ต

Not assuming roles

Hi,

I am having problems with the module to assume the roles:

Error: Error refreshing state: 2 error(s) occurred:

* module.vpc_peering_cross_account.provider.aws.accepter: The role "arn:aws:iam::YYYYYYYYYYYYY:role/cross_account_role" cannot be assumed.

  There are a number of possible causes of this - the most common are:
    * The credentials used in order to assume the role are invalid
    * The credentials do not have appropriate permission to assume the role
    * The role ARN is not valid
* module.vpc_peering_cross_account.provider.aws.requester: The role "arn:aws:iam::XXXXXXXXXXX:role/cross_account_role" cannot be assumed.

  There are a number of possible causes of this - the most common are:
    * The credentials used in order to assume the role are invalid
    * The credentials do not have appropriate permission to assume the role
    * The role ARN is not valid

Using the same credentials and roles I can assume the roles using aws cli.

Any idea what can cause it?

Thanks

The given key does not identify an element in this collection value

Describe the Bug

We are getting the following error when running apply. We are in the process of upgrading from Terraform 0.11 to Terraform 0.12. Previously this was successful on Terraform 0.11 using the 0.11 branch.

Error: Invalid index

  on .terraform/modules/staging_peering/accepter.tf line 88, in resource "aws_route" "accepter":
  88:   route_table_id            = local.accepter_aws_route_table_ids[ceil(count.index / local.requester_cidr_block_associations_count)]
    |----------------
    | count.index is 5
    | local.accepter_aws_route_table_ids is list of string with 3 elements
    | local.requester_cidr_block_associations_count is 2

The given key does not identify an element in this collection value.

Expected Behavior

Plan runs without error.

Steps to Reproduce

Steps to reproduce the behavior:

  1. Configure the module as follows.
module "staging_peering" {
  source  = "cloudposse/vpc-peering-multi-account/aws"
  version = "0.11.3"

  namespace = "staging"
  stage     = "staging"
  name      = "staging-shared-services-peering"

  requester_aws_assume_role_arn             = "arn:aws:iam::xxxxxx:role/cross-account-vpc-peering"
  requester_region                          = "eu-west-1"
  requester_vpc_id                          = data.aws_vpc.peer_from_vpc.id
  requester_allow_remote_vpc_dns_resolution = false

  accepter_aws_assume_role_arn             = "arn:aws:iam::xxxxxx:role/pipeline"
  accepter_region                          = "us-east-2"
  accepter_vpc_id                          = data.aws_vpc.peer_to_staging_vpc.id
  accepter_allow_remote_vpc_dns_resolution = false
}
  1. Run 'terraform plan.'
  2. See error

Environment (please complete the following information):

Anything that will help us triage the bug will help. Here are some ideas:

  • Version 0.11.3
  • Terraform 0.12.30

accepter_subnet_tags doesn't have any effect

Describe the Bug

accepter_subnet_tags doesn't have any effect, data.aws_route_tables.accepter always contains the all route tables in accepter VPS (link to code)

requester_subnet_tags works as expected.

Expected Behavior

Just works as described in the readme.

Steps to Reproduce

Code to reproduce the behavior:

module "prod_to_stage" {
  source = "git::https://github.com/cloudposse/terraform-aws-vpc-peering-multi-account.git?ref=tags/0.11.0"

  namespace = "namespace"
  stage     = "stage"
  name      = "prod-to-stage"

  requester_aws_assume_role_arn             = "arn:aws:iam::${var.account_id}:role/cross-account-vpc-peering-test"
  requester_region                          = var.region
  requester_vpc_id                          = var.prod_vpc_id
  requester_allow_remote_vpc_dns_resolution = true
  requester_subnet_tags                     = {
    Role = "subnet-private"
  }

  accepter_aws_assume_role_arn             = "arn:aws:iam::${var.stage_account_id}:role/cross-account-vpc-peering-test"
  accepter_region                          = var.region
  accepter_vpc_id                          = var.stage_vpc_id
  accepter_allow_remote_vpc_dns_resolution = true
  accepter_subnet_tags                     = {
    "Role" = "subnet-private"
  }
}

Environment

Anything that will help us triage the bug will help. Here are some ideas:

  • terraform 0.12.29
  • terraform-aws-vpc-peering-multi-account 0.11.0

Need to run twice for a successful plan

Error: Error applying plan:

2 error(s) occurred:

* module.vpc_peering_syn_scout_dev.aws_vpc_peering_connection_options.requester: 1 error(s) occurred:

* aws_vpc_peering_connection_options.requester: Error modifying VPC Peering Connection Options: OperationNotPermitted: Peering pcx-0324fc216d5832fd3 is not active. Peering options can be added only to active peerings.
	status code: 400, request id: a0a31b88-9f31-4f02-a37f-5863bf16ec36
* module.vpc_peering_syn_scout_dev.aws_vpc_peering_connection_options.accepter: 1 error(s) occurred:

* aws_vpc_peering_connection_options.accepter: Error modifying VPC Peering Connection Options: OperationNotPermitted: Peering pcx-0324fc216d5832fd3 is not active. Peering options can be added only to active peerings.
	status code: 400, request id: f4fdc3cc-9a85-4d44-827b-2faa56b5a0ae

It seems as though the aws_vpc_peering_connection_options resource will not work until the peering connections are active. Is there any way for the aws_vpc_peering_connection_options resources to wait for the peers to be active before running them?

Find accepter VPC using a data source

Have a question? Please checkout our Slack Community or visit our Slack Archive.

Slack Community

Describe the Feature

Instead of using a hard coded requester_vpc_id, we can use a data source instead.

data "aws_vpc" "selected" {
  provider = aws.accepter

  tags = var.accepter_vpc_tags
}

where the tags are passed in

  accepter_vpc_tags = {
    Name = "yellowstone"
  }

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Ignored or Blocked

These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.

Detected dependencies

terraform
accepter.tf
  • cloudposse/label/null 0.25.0
examples/complete/main.tf
examples/complete/versions.tf
  • aws >= 2.0
  • hashicorp/terraform >= 1.3
examples/vpc-only/main.tf
  • cloudposse/dynamic-subnets/aws 2.4.1
  • cloudposse/vpc/aws 2.1.0
  • cloudposse/dynamic-subnets/aws 2.4.1
  • cloudposse/vpc/aws 2.1.0
examples/vpc-only/versions.tf
  • aws >= 2.0
  • local >= 1.3
  • null >= 2.0
  • template >= 2.2
  • hashicorp/terraform >= 1.3
requester.tf
  • cloudposse/label/null 0.25.0
versions.tf
  • aws >= 2.0
  • hashicorp/terraform >= 1.3

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.