Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interpolation for provider field does not work #3072

Closed
jarosite opened this issue Aug 25, 2015 · 8 comments
Closed

Interpolation for provider field does not work #3072

jarosite opened this issue Aug 25, 2015 · 8 comments

Comments

@jarosite
Copy link

get error while trying to apply plan
Errors:

  • 1 error(s) occurred:
  • module root: 1 error(s) occurred:
  • aws_security_group.defaultAllOffice: resource depends on non-configured provider '${lookup(var.providers, count.index)}'
$terraform -v
Terraform v0.6.3

use case:
create same security group in all aws regions:

provider "aws" {
  alias = "us-east-1"
  region="us-east-1"
}
provider "aws" {
    alias = "us-west-2"
    region = "us-west-2"
}
variable "providers" {
  default = {
    "0" = "aws.us-west-2"
    "1" = "aws.us-east-1"
  }
}
resource "aws_security_group" "defaultAllOffice" {
    name = "defaultAllOffice ${lookup(var.providers, count.index)}"
    description = "Used in the terraform ${lookup(var.providers, count.index)}"
    count = "2"
    provider = "${lookup(var.providers, count.index)}"
/* some sg rules there*/
}

please let me know if there better solution to do cross region transformation

@jarosite
Copy link
Author

jarosite commented Sep 3, 2015

any idea for workaround?

@jarosite
Copy link
Author

jarosite commented Sep 7, 2015

as workaround created this simple script

#!/usr/bin/env bash

REGIONS=("us-east-1" "us-west-2" "us-west-1" "eu-west-1" "eu-central-1" "ap-southeast-1" "ap-southeast-2" "ap-northeast-1" "sa-east-1")

for RG in "${REGIONS[@]}"
do
  echo processing region $RG
  terraform $1 -state=state/$RG-sg.tfstate -var "region=$RG" 
done

@rosmo
Copy link

rosmo commented Sep 7, 2015

It would be great to have the interpolation of provider working.

@phinze
Copy link
Contributor

phinze commented Oct 12, 2015

This is something we should address. Closing to consolidate around #1819.

@phinze phinze closed this as completed Oct 12, 2015
@rodlogic
Copy link

rodlogic commented Nov 7, 2015

I see that there is an ongoing discussion on #1819, but that seems to take it to a deeper level.

Wouldn't it make sense to add interpolation here and at least allow one to dynamically select a provider in a module?

@NickLaMuro
Copy link

I was struggling with this today, and found an alternative workaround to the bash script. You could include in your module the provider with an interpolated region that then all of your resources within the module use.

As an example, here would be a module for an AWS VPC:

# module/vpc/vpc.tf

variable "aws_region" {}

provider "aws" {
  alias  = "aws_vpc"
  region = "${var.aws_region}"
}

resource "aws_vpc" "vpc" {
  provider = "aws.vpc_aws"

  # Other vpc config here
}

resource "aws_subnet" "private_subnet" {
  provider = "aws.vpc_aws"

  # Other subnet config here
}

And then if you need to create a cross region vpc in each region, you could then do in your main.tf:

# main.tf

module "vpc_east" {
  source "path/to/module/vpc"

  region "us-east-1"
}

module "vpc_west" {
  source "path/to/module/vpc"

  region "us-west-1"
}

Might not work in all cases, but this would allow you to reuse the bulk of configuration code and just make it region specific... or at least as far as I can tell. I haven't actually run an terraform apply with this, but the plan was at least compiling properly 😄

Update: Based on the upstream issue and comment #1819 (comment) , it is possible that this might not work currently. Again, this would have to be tested.

@mattwillsher
Copy link

From my tests you don't need the alias in the submodule - terraform uses the closest provider. This seems to be backed up in the terraform graph output.

In the above sample, the vpc.tf can be written as:

# module/vpc/vpc.tf

variable "aws_region" {}

provider "aws" {
  region = "${var.aws_region}"
}

resource "aws_vpc" "vpc" {
  # Other vpc config here
}

resource "aws_subnet" "private_subnet" {
  # Other subnet config here
}

@ghost
Copy link

ghost commented Apr 22, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Apr 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants