Hi,
I’m attempting to build infrastructure described with terraform. My terraform configuration builds for multiple environments, for the case of this explination lets say: dev and prod.
So as mentioned, i have a single terraform configuration, into which I pass variables which determine the difference between a deployed environment. Within my terraform configuration I have a variables.tf
which, for example, declares a variable:
variable "rds_connect_string" {}
I would like to set the value of this variable depending on the environment I’m deploying to, so within my circleci environment variables page (in the ui) I’ve created two environment variables:
RDS_CONNECTION_STRING_DEV=xxxxxxx
and
RDS_CONNECTION_STRING_PROD=xxxxxx
Then in my .circleci/config.yml
I’m attempting to map them like this:
version: 2.1
orbs:
terraform: circleci/terraform@3.1
jobs:
dev:
executor: terraform/default
environment:
TF_VAR_rds_connection_string: $RDS_CONNECTION_STRING_DEV <---- here
steps:
- checkout
- terraform/fmt:
path: .
- terraform/validate:
path: .
- terraform/init:
backend_config: "bucket=$BUCKET,key=$SERVICE_NAME/dev/state.tfstate,region=$REGION"
path: .
- terraform/plan:
backend_config: "bucket=$BUCKET,key=$SERVICE_NAME/dev/state.tfstate,region=$REGION"
path: .
- terraform/apply:
backend_config: "bucket=$BUCKET,key=$SERVICE_NAME/dev/state.tfstate,region=$REGION"
path: .
prod:
executor: terraform/default
environment:
TF_VAR_rds_connection_string: $RDS_CONNECTION_STRING_PROD <--- here
steps:
- checkout
- terraform/fmt:
path: .
- terraform/validate:
path: .
- terraform/init:
backend_config: "bucket=$BUCKET,key=$SERVICE_NAME/dev/state.tfstate,region=$REGION"
path: .
- terraform/plan:
backend_config: "bucket=$BUCKET,key=$SERVICE_NAME/dev/state.tfstate,region=$REGION"
path: .
- terraform/apply:
backend_config: "bucket=$BUCKET,key=$SERVICE_NAME/dev/state.tfstate,region=$REGION"
path: .
note the lines:
TF_VAR_rds_connection_string: $RDS_CONNECTION_STRING_DEV
and
TF_VAR_rds_connection_string: $RDS_CONNECTION_STRING_PROD
My intent here is to override the variable terraform expects by setting TF_VAR_rds_connection_string
seperatly for each environment.
However, when I run my pipeline, terraform complains with something similar to this:
var.rds_connection_string is "$RDS_CONNECTION_STRING_DEV"
Which suggests to me circleci isn’t replacing the $RDS_CONNECTION_STRING_DEV
environment variable with the actual value.
I can achieve this functionality by simply replacing $RDS_CONNECTION_STRING_DEV
with the actual value rather than the environment variable reference but in doing this I’m forced to store a secure string in version control (this is not an option).
Is my syntax correct? Is this even possible? Or is there a better way to achieve this?
Thanks in advance
-Mark