Search code examples
amazon-web-servicesterraformaws-security-groupaws-vpc

Using two different roles to build resources in one terraform code


Is it possible to create two security groups in two different accounts and reference one of them in the egress rules of other using Terraform in one code file?

For instance if we have two VPCs A and B, I want to create two security groups in each of the VPCs and reference security group A in egress of security group of B.

Main issue which I am facing is that I got two different deployer roles which I am not sure how to use in single terraform TF file.

PS: VPCs are peered.

Thanks in advance.


Solution

  • Yes, this is possible using multiple provider definitions and the alias attribute and referring to them when defining the resources:

    provider "aws" {
      region = "eu-west-1"
    }
    
    provider "aws" {
      region  = "us-east-1"
      profile = "other-account"
      alias = "other"
    }
    
    resource "aws_vpc" "this" {
      provider = "aws.other"
      // ..
    }