Setting the env variable for

In circleci/aws-ecs@4.0.0, when I’m using aws-ecs/deploy_service_update and trying to override the region with a value of $AWS_DEFAULT_REGION_SECONDARY env variable, it’s not working, region still holds $AWS_DEFAULT_REGION value.

What might be wrong?

Please find the example code:

- aws-ecs/deploy_service_update:
    context:
    - shared environment variables
    name: Deploy
    requires:
    - approval
    - Build And Push
    auth:
    - aws-cli/setup:
        aws_access_key_id: AWS_ACCESS_KEY_ID
        aws_secret_access_key: AWS_SECRET_ACCESS_KEY
        region: $AWS_DEFAULT_REGION_SECONDARY
    family: "$SERVICE_PREFIX-family"
    service_name: "$SERVICE_PREFIX-service"
    cluster: "arn:aws:ecs:$AWS_DEFAULT_REGION_SECONDARY:$AWS_ACCOUNT_ID:cluster/$SERVICE_PREFIX-cluster"
    region: AWS_DEFAULT_REGION_SECONDARY
    container_image_name_updates: "container=${SERVICE_PREFIX},tag=latest"

As it is the value of the environment variable you want you will need to use

${AWS_DEFAULT_REGION_SECONDARY}

Hi @rit1010 , thanks for the response, I did try this with the following values and combination, but still no luck.

region: AWS_DEFAULT_REGION_SECONDARY
region: “AWS_DEFAULT_REGION_SECONDARY”
region: “$AWS_DEFAULT_REGION_SECONDARY”
region: $AWS_DEFAULT_REGION_SECONDARY
region: ${AWS_DEFAULT_REGION_SECONDARY}
region: “${AWS_DEFAULT_REGION_SECONDARY}”

Below is an example with your given format, what I’m missing now?

- aws-ecs/deploy_service_update:
    context:
       - shared environment variables
    name: Deploy
    requires:
       - approval
       - Build And Push
    auth:
      - aws-cli/setup:
        aws_access_key_id: AWS_ACCESS_KEY_ID
        aws_secret_access_key: AWS_SECRET_ACCESS_KEY
        region: $AWS_DEFAULT_REGION_SECONDARY # This is working, because ~/.aws/config region is valid.
      - run:
                name: Check .env variables
                command: |
                  echo "AWS_DEFAULT_REGION: ${AWS_DEFAULT_REGION: -1}"
                  echo "AWS_DEFAULT_REGION_SECONDARY: ${AWS_DEFAULT_REGION: -1}"
                  region=$(awk -F' *= *' '/^\[.*\]$/ {in_section=0} /^\[default\]/ {in_section=1; next} in_section && /^region/ {print $2; exit}' ~/.aws/config)
                  echo "~/.aws/config region: ${region: -1}"
    family: "$SERVICE_PREFIX-family"
    service_name: "$SERVICE_PREFIX-service"
    cluster: "arn:aws:ecs:$AWS_DEFAULT_REGION_SECONDARY:$AWS_ACCOUNT_ID:cluster/$SERVICE_PREFIX-cluster"
    region: ${AWS_DEFAULT_REGION_SECONDARY} # How to access this inside `-run`, it's always AWS_DEFAULT_REGION
    container_image_name_updates: "container=${SERVICE_PREFIX},tag=latest"

Found the alternate solution to update the env variable in the circleci env-vars docs.

I already had env variables in Context with keys AWS_DEFAULT_REGION and AWS_DEFAULT_REGION_SECONDARY.

I had to remove AWS_DEFAULT_REGION and added a new one AWS_REGION

New Script Changes Which Is Working:

- run:
          name: "Setup custom environment variables"
          command: |
                  echo 'export AWS_DEFAULT_REGION ="$AWS_DEFAULT_REGION_SECONDARY"' >> "$BASH_ENV"
                  source "$BASH_ENV"