Closed tkocemba closed 5 years ago
Hi @tkocemba. I think that this thread https://github.com/hashicorp/terraform/pull/2015 is for describe multiples providers. Not for use theses multiples provides in a resource/module. Based on terraform documentation(Resources, Providers), is possible use only one provider per resource. Here a example:
from terrascript import Terrascript
from terrascript import provider
from terrascript.aws.r import aws_instance
ts = Terrascript()
AWS_REGION_LIST = [
'us-east-1',
'ap-southeast-1',
'eu-central-1',
]
aws_providers_map = {}
for _region in AWS_REGION_LIST:
_provider = ts.add(provider(
'aws',
version="~> 1.38",
access_key='AWS_ACCESS_KEY',
secret_key='AWS_SECRET_KEY',
region=_region,
alias=_region,
))
aws_providers_map[_region] = _provider
# Default Provider
ts.add(provider(
'aws',
version="~> 1.38",
access_key='AWS_ACCESS_KEY',
secret_key='AWS_SECRET_KEY',
region='us-east-1',
))
instance_example1 = ts.add(aws_instance(
'example2', ami='ami-2757f631', instance_type='t2.micro',
provider=aws_providers_map['us-east-1'].id,
))
print(ts.dump())
Output:
{
"provider": {
"aws": {
"__DEFAULT__": {
"access_key": "AWS_ACCESS_KEY",
"region": "us-east-1",
"secret_key": "AWS_SECRET_KEY",
"version": "~> 1.38"
},
"ap-southeast-1": {
"access_key": "AWS_ACCESS_KEY",
"alias": "ap-southeast-1",
"region": "ap-southeast-1",
"secret_key": "AWS_SECRET_KEY",
"version": "~> 1.38"
},
"eu-central-1": {
"access_key": "AWS_ACCESS_KEY",
"alias": "eu-central-1",
"region": "eu-central-1",
"secret_key": "AWS_SECRET_KEY",
"version": "~> 1.38"
},
"us-east-1": {
"access_key": "AWS_ACCESS_KEY",
"alias": "us-east-1",
"region": "us-east-1",
"secret_key": "AWS_SECRET_KEY",
"version": "~> 1.38"
}
}
},
"resource": {
"aws_instance": {
"example2": {
"ami": "ami-2757f631",
"instance_type": "t2.micro",
"provider": "${provider.aws.us-east-1.id}"
}
}
}
}
@tkocemba do you have a working example in Terraform HCL language?
@tkocemba @nielsonsantana I think this is similar to this: https://www.terraform.io/docs/configuration/modules.html#passing-providers-explicitly
Hey,
Sorry for updating sooner, busy with current task. Thanks for this link this is exactly how we use it. Its best practice to pass providers to module.
I run slightly modified example
and library generated following output
structure for providers is not proper, references passed to module cannot be found. Its problematic because we intensively uses modules and multiple providers. Expected output is described here https://github.com/hashicorp/terraform/pull/2015
Cheers, Tomek