Reputation: 1
A common flow for setting up terraform/aws CI deployments is to use terraform to deploy your infrastructure and circle to handle deployments to AWS. Most examples I have seen using this flow will manually take the outputs from running terraform apply
and store those values in CircleCI Environment Variables which can then be referenced in CircleCI jobs.
Instead what I want to know is how I can run terraform output
to get the values I want and then use those values as parameters when passing into a circleCI orb.
First thing I looked for was an existing Terraform CircleCI orb. Hashicorp has an official orb which is nice but it doesn't support the output command. I could maybe try an apply job that targets nothing to get a similar result but instead I just created a circlCI job which will install and run the command I want.
retrieve-terraform-params:
parameters:
dir:
type: string
default: "."
service:
type: string
default: ""
executor: python
resource_class: small
environment:
DIR: << parameters.dir >>
steps:
- checkout
- run:
name: Parse Parameter and write to environment variable
command: |
cd ${DIR}
sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
wget -O- https://apt.releases.hashicorp.com/gpg | \
gpg --dearmor | \
sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
gpg --no-default-keyring \
--keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg \
--fingerprint
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \
https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \
sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update
sudo apt-get install terraform
terraform init
terraform output > tf.outputs
Now I have a CircleCI orb which takes in parameters for aws deployments which is looking for the following:
- retrieve-terraform-params:
dir: ./service/terraform/
- ecs/ecs-deploy:
name: deploy-stuff
launch-type: FARGATE
listener-arn: "arn:..."
blue-target-group: "arn:..."
green-target-group: "arn:..."
requires:
- retrieve-terraform-params
Those arns are in my tf.outputs I created with my retrieve-terraform-params job. What I want to know is whether or not there is a way of getting the values from tf.outputs in my first job used as parameters for my second job when the second job is an orb I did not write.
The approaches I have tried: First Approach) Parse the values of tf.outputs into environment variables and reference those environment variables in my workflow. Command for tf job:
...
cat tf.outputs | while read p;
do IN=$p;
arrIN=(${IN//=/ });
nvar="${arrIN[0]^^}";
export ${nvar}=${arrIN[1]};
echo "export ${nvar}='${arrIN[1]}'";
done
And then in my workflow:
- ecs/ecs-deploy:
name: deploy-stuff
launch-type: FARGATE
listener-arn: ${VAR1}
blue-target-group: ${VAR2}
green-target-group: ${VAR3}
requires:
- retrieve-terraform-params
Result: The value of my parameters VAR1,2,3 were always blank, I suspect this is due to searching circle for environment variables rather than using the ones created on the instance.
Second Approach) Parse the values of tf.outputs into circleci pipeline parameters which are used as input for my orb
parameters:
VAR1:
type: string
default: ""
...
- ecs/ecs-deploy:
name: deploy-stuff
launch-type: FARGATE
listener-arn: << pipeline.parameters.VAR1 >>
blue-target-group: << pipeline.parameters.VAR2 >>
green-target-group: << pipeline.parameters.VAR3 >>
requires:
- retrieve-terraform-params
Result: I couldn't a way of writing to those pipeline values from within a job. only accessing them.
So now I am stuck wondering if I instead need to create a new job which imitates the existing orb I am using. Any help would be appreciated.
Upvotes: 0
Views: 223