Pass Container image value to AWS cloudformation template dynamically from jenkins

Viewed 372

I have a jenkins pipeline where I am executing a cloud formation template that I have stored in Gitlab. But my cloudformation template contains an image parameter, that i need to pass on dynamically from jenkins file. can anybody help me with how to do the same. i have highlighted it in bold.

ContainerDefinitions" : [ { "Image":"amazon/amazon-ecs-sample", ],

1 Answers

To do this you would instead need to make the Image value a parameter passed into the CloudFormation stack.

In your CloudFormation stack create a parameter named "Image" such as below

"Parameters": {
  "Image": {
    "Type": String,
    "Description": "The image name to use within the container definition"
  }
}

Then in your code you can reference it using the Ref intrinsic function within the Container Definition like below

ContainerDefinitions" : [ { "Image": { "Ref": "Image" }, ]

When you create the stack using the CLI you would create like this below

aws cloudformation create-stack --stack-name myteststack --template-body file://sampletemplate.json --parameters ParameterKey=Image,ParameterValue=amazon/amazon-ecs-sample

If you're using a parameter in the Jenkins Pipeline you could replace the image name like the below

aws cloudformation create-stack --stack-name myteststack --template-body file://sampletemplate.json --parameters ParameterKey=Image,ParameterValue=${env.Image}
Related