How To Get Lastest Image Version in AWS ECR

How To Get Lastest Image Version in AWS ECR

·

4 min read

- A python script to get latet image version of application from ECR.

- In some case, developers want to know the latest image version of application such master branch for deploying to staging and product.

- We can provide to them a tool (slackbot, ChatOps, etc.) to call the script.

What’s In This Document

🚀 Get application code

  • Get latest image version of master branch from ECR with prefix master- and lastest pushed at
import boto3
import re


def get_latest_master_image():
    """ Filter images with prefix master- and return the latest pushed one """
    client = boto3.client('ecr', region_name='ap-southeast-1')
    response = client.list_images(
        registryId='111111111111',
        repositoryName='repo/application',
        maxResults=1000
    )

    latest = None
    temp_tag = None

    for image in response['imageIds']:
        tag = image['imageTag']
        if re.search("^master-[0-9]+", tag):
            img = client.describe_images(
                registryId='111111111111',
                repositoryName='repo/application',
                imageIds=[
                    {
                        'imageTag': tag
                    },
                ]
            )
            pushed_at = img['imageDetails'][0]['imagePushedAt']
            if latest is None:
                latest = pushed_at
            else:
                if latest < pushed_at:
                    latest = pushed_at
                    temp_tag = tag
    return temp_tag, latest


version, pushed_at = get_latest_master_image()
print(f'app {version} pushed at {pushed_at}')

🚀 Run test

⚡ $ python getImageVersion.py 
app master-12163 pushed at 2020-12-31 10:10:53+07:00

🚀 A practice to use this script

  • Using slackbot

Alt Text

Mirror:

Read More

Blog · Web · Linkedin · Group · Page · Twitter