Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

I need to pass variable to task-definition on Deploy to AWS-ECS

Deleted user July 22, 2022

I need to change ne name of IMAGE dynamically int the task definition. I tried in many ways but without success.

 

thats my pipe:

- step:

name: Push
image: atlassian/pipelines-awscli
# Pass the Docker service with 7 GB
services:
- docker-7g
oidc: true
# Raise the Step limit from 4 GB to 8 GB.
size: 2x
script:
- TAG=$BITBUCKET_COMMIT
- export AWS_REGION=****
- export AWS_ROLE_ARN=$AWS_ROLE_ARN_1
- export AWS_WEB_IDENTITY_TOKEN_FILE=$(pwd)/web-identity-token
- echo $BITBUCKET_STEP_OIDC_TOKEN > $(pwd)/web-identity-token
- IMAGE="**.dkr.ecr.us-east-1.amazonaws.com/am/account-manager:${TAG}"  <= IMAGE
- eval $(aws ecr get-login --no-include-email --region us-east-1| sed 's;https://;;g')
- docker build -t $IMAGE .
- docker push $IMAGE

 

==>   this step above works fine. The image is being sent to ECR with BITBUCKET_COMMIT as a TAG

 

- step:
name: deploy
image: atlassian/default-image:2
services:
- docker
oidc: true
script:
- TAG=$BITBUCKET_COMMIT
- export IMAGE="*****.dkr.ecr.us-east-1.amazonaws.com/am/account-manager:${TAG}"
- envsubst $IMAGE < pipeline/task-definition-template.json > pipeline/task-definition.json
- pipe: atlassian/aws-ecs-deploy:1.6.2
variables:
AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
AWS_OIDC_ROLE_ARN: $AWS_OIDC_ROLE_ARN_1
CLUSTER_NAME: 'am-production-Cluster-GBJBMXmKQYm3'
SERVICE_NAME: 'am-production-account-manager-Service-0lU6qXwcHbOr'
TASK_DEFINITION: 'pipeline/task-definition.json'  <=  Here is where i need a solution to change the TAG int the parameter IMAGE whit the variable that i created.
services:
- docker

 

task-definition.json

 

something esle before .....

],
"image": "",  <== <=  Here is where i need a solution to change the TAG int the parameter IMAGE whit the variable that i created.
"name": "account-manager"
}
],
.......something esle  after.....

 

 

 

2 answers

1 accepted

2 votes
Answer accepted
Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 28, 2022

Hello @[deleted] ,

Thank you for reaching out to Atlassian Support.

Looking at the YML file you shared, it seems like you already tried to use envsubs, and this would be my suggestion. From my understanding, you are building the image name and exporting it to a variable named $IMAGE, and as long as the variable $IMAGE is referenced in the file pipeline/task-definition-template.json , the envsubst command should work.

Just one thing to note is that you need to pass the variables to be replaced in single quotes, and envsubst just works for variables exported ( e.g. export MYVAR=123).

So, for example, if your pipeline/task-definition-template.json is like below :

[...]

{
"image"
"$IMAGE",  <== <=  Here is where i need a solution to change the TAG int the parameter IMAGE whit the variable that i created.

"name""account-manager"

}

[....]

Using the below envsubst command should work to replace any occurrence of $IMAGE in the file, by the value of that environment variable : 

envsubst '$IMAGE' < pipeline/task-definition-template.json > pipeline/task-definition.json

And then you can use the pipeline/task-definition.json as the pipe's task-definition.

You can give it a try with the above syntax and let us know how it goes. If it still not working, I would recommend adding a command to print the content of the file created from envsubst to confirm the variable was indeed replaced :

- cat pipeline/task-definition.json

Let me know in case you have any questions.

Thank you, @[deleted] .

Kind regards,

Patrik S

0 votes
Deleted user August 1, 2022

hey Patrick,

 

Thanks. It Worked.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PERMISSIONS LEVEL
Product Admin
TAGS
atlassian, ace, atlassian community event, donation, girls who code, women in tech, malala fund, plan international, kudos, community badge, badge, atlassian badge, International Women’s month, International Women’s Day, women's month, women's day

10 for Change at Atlassian Community Events

Show up and give back by attending an Atlassian Community Event: we’ll donate $10 for every event attendee in March!

Join an Atlassian Community Event!
AUG Leaders

Upcoming Bitbucket Events