I have asked this question on SO, but it hasn’t received (m)any answers, so I’m hoping TF gurus here might be able to help. If it’s not allowed to cross-post the questions I’m happy to delete this question.
I have a use case where I need two AWS providers for different resources. The default aws
provider is configured in the main module which uses another module that defines the additional aws
provider which might use different AWS credentials.
By default, I’d like both providers to use the same AWS credentials unless explicitly overridden.
I figured I could do something like this. In the main module:
locals {
foo_cloud_access_key = aws.access_key
foo_cloud_secret_key = aws.secret_key
}
variable "foo_cloud_access_key" {
type = string
default = local.foo_cloud_access_key
}
variable "foo_cloud_secret_key" {
type = string
default = local.foo_cloud_secret_key
}
where variable
s foo_cloud_secret_key
and foo_cloud_access_key
would be passed down to the child module like this:
module foobar {
...
foobar_access_key = var.foo_cloud_access_key
foobar_secret_key = var.foo_cloud_secret_key
...
}
Where module foobar
would then configure its additional was provide with these variables:
provider "aws" {
alias = "foobar_aws"
access_key = var.foobar_access_key
secret_key = var.foobar_secret_key
}
Note, that I need to “extract” the default aws
provider config variables and use them elsewhere i.e. the default aws
provider will be configured with access_key
and secret_key
and I need to use those (values) as default values in other variables.
When I run the init terraform
spits out this error (for both variables):
Error: Variables not allowed
on variables.tf line 66, in variable "foo_cloud_access_key":
66: default = local.foo_cloud_access_key
Variables may not be used here.
Is it possible to achieve something like this in terraform
or is there any other way to go about this?