Skip to content

Instantly share code, notes, and snippets.

@hairyhenderson
Last active January 26, 2016 21:05
Show Gist options
  • Save hairyhenderson/153d0e5c451df4e8c513 to your computer and use it in GitHub Desktop.
Save hairyhenderson/153d0e5c451df4e8c513 to your computer and use it in GitHub Desktop.
provider "aws" {
region = "us-east-1"
}
provider "aws" {
alias = "us-east-1"
region = "us-east-1"
}
provider "aws" {
alias = "eu-west-1"
region = "eu-west-1"
}
provider "aws" {
alias = "us-west-1"
region = "us-west-1"
}
variable "regions" {
default = "us-east-1,eu-west-1,us-west-1"
}
resource "aws_vpc" "vpc" {
count = 3
provider = "aws.${element(split(",",var.regions), count.index)}"
cidr_block = "10.1.${count.index}.0/24"
}
@pll
Copy link

pll commented Jan 26, 2016

variable "regions" {
default = "us-east-1,us-east-2,us-east-3"
}

resource "aws_vpc" "vpc" {
count = 3
provider = "${element(split(",",var.regions), count.index)}"
}

@hairyhenderson
Copy link
Author

$ terraform plan
provider.aws.${element(split(",",var.regions), count.index)}.region
  The region where AWS operations will take place. Examples
  are us-east-1, us-west-2, etc.

  Default: us-east-1
  Enter a value: 

@theY4Kman
Copy link

Basically, you'd setup a variable for each attribute being passed into the resource. The value for that variable will be a comma-separated list. Like this:

variable "regions" {
    default = "us-east-1,us-east-2,us-east-3"
}

variable "flavors" {
    default = "X,Y,Z"
}

resource "aws_vpc" "vpc" { 
    count = 3
    region = "${element(split(",",var.regions), count.index)}"
    # Just an example!
    flavor = "${element(split(",",var.flavors), count.index)}"
}

@pll
Copy link

pll commented Jan 26, 2016

This seems to compile without errors:

   variable "count" {
     default = 1
   }

   variable "regions" {
     default = {
        "0" = "us-east-1"
        "1" = "eu-west-2"
        "2" = "us-west-1"
   }

   provider "aws" {
     alias = "0"
     region = "us-east-1"
   }

   provider "aws" {
     alias = "1"
     region = "eu-west-1"
   }

   provider "aws" {
     alias = "2"
     region = "us-west-1" 
   } 

   resource "aws_vpc" "vpc" {
     count = 3
     provider = "aws.aws_${count.index}"
     cidr_block = "10.1.${count.index}.0/24"
   }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment