Tag images in AWS ECR (Elastic Container Registry)

(Hakan Lindestaf) #1

Is there a way to tag an existing image in AWS ECR when it gets deployed so the lifecycle policy (in ECR) won’t remove images that are in-use by a deployment? The lifecycle policies support set ups based on tags that can skip images with certain tags from getting removed, and the ECR will always exclusively have a single tag per repository, so if I were to assign a tag like “deploy-stage” and “deploy-prod” to an image then I could use. But I would need Octopus to actually set the tag during deployment.


Hi Hakan,

Thanks for getting in touch!

While I haven’t personally done this before, it definitely appears that it is possible to tag existing images in AWS ECR, you should be able to find the relevant information here.

As for how to do that in Octopus, you would need to use the Run an AWS CLI Script step (shown below).

You would then incorporate this with Octopus variables to provide the tags you are looking for.

Let me know if there is anything else that I can assist with,


(Hakan Lindestaf) #4

Thanks for pointing me in that direction, it almost works. The issue is that sometimes the image that I’m trying to tag is already tagged, so trying to tag it again throws an exception when writing the tag. So I’m trying to call Get-ECRImageMetadata which seems to have a bug so it can’t be called with ImageId. Any other suggestions?

(Hakan Lindestaf) #5

I was able to solve this. Here’s the code for reference:

$PackageVersion = $OctopusParameters["Octopus.Action.Package[XXXXXXXXXXXX].PackageVersion"]
$RepositoryName = "XXXXXXXXXXXXXX"
$DeployTag = "deploy-" + $OctopusParameters["Octopus.Environment.Name"]

Write-Output "Package version: $PackageVersion"

$Image = Get-ECRImageBatch -ImageId @{ imageTag=$PackageVersion } -RepositoryName $RepositoryName
$ImageDeploy = Get-ECRImageBatch -ImageId @{ imageTag=$DeployTag } -RepositoryName $RepositoryName

if($Image.Images[0].ImageId.ImageDigest -ne $ImageDeploy.Images[0].ImageId.ImageDigest) {
	Write-Output "Setting tag $DeployTag on image $($Image.Images[0].ImageId.ImageDigest)"
	$Manifest = $Image.Images[0].ImageManifest
	Write-ECRImage -RepositoryName $RepositoryName -ImageManifest $Manifest -ImageTag $DeployTag

Hi Hakan,

Glad you were able to get that sorted!

Let me know if there is anything else we can assist with.