Set the AWS_REGION in a parameter

I’ve got a pipeline setup to build some AWS Infrastructure using terraform.

I need to set AWS_REGION as an environment variable, but if I do this in Project Settings > Environment Variables on the CircleCI console then I can’t read the value back.

So I had the idea that I could set it as a parameter in the config.yml file. Something like:

AWS_REGION=<< pipeline.parameters.aws_region >>

Then I could have aws_region default to my usual region, but override it in the job if I need to.

This seemed so simple, but I couldn’t see how to do this in the docs, and everything I try doesn’t work. I either get syntax errors or AWS_REGION is set literally to “<< pipeline.parameters.aws_region >>” which obviously causes terraform to explode.

I was wondering if anyone else has run into this problem, and if there’s any way around it?

To use pipeline variables within a shell you use the environment: command as shown in the example here

Thanks. I’ve been trying to use the environment: command but I can’t get it to work. The variable doesn’t get set properly so the terraform command won’t run. This is different from the example, which only echoes a string to the screen.

Can you post your code blocks, including the calling code?

If I just try to follow the example:

parameters:
  aws-region:
    description: AWS Region
    default: "eu-west-1"
    type: string
    environment:
       AWSREGION: << pipeline.parameters.aws-region >>
    steps:
      - checkout
      - run: echo ${AWSREGION}

The output from the echo command is " << pipeline.parameters.aws-region >>" and not “eu-west-1”.

I’m not sure why you are not getting what you are expecting, below is my test that does output the default assigned value

version: 2.1 # Use 2.1 to enable using orbs and other features.

parameters:
  aws-region:
    default: "eu-west-1"
    type: string

executors:
  base-system:
    machine:
      image: ubuntu-2004:current
    resource_class: medium
    
jobs:
  build:
    executor: base-system
    environment:
       AWSREGION: << pipeline.parameters.aws-region >>
    steps:
      - checkout
      - run: echo ${AWSREGION}

So the questions that come to mind are which version are you setting and what is your target OS environment?

Thank you very much!

I was using version 2.0. As soon as I switched to 2.1 it suddenly started to behave as expected.

Now I am going to see if I can get terraform to run, but with any luck the incorrect version setting was the only reason I was having difficulties…

Yep terraform runs fine now. Thanks again @rit1010

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.