DEV Community

πŸš€ Vu Dao πŸš€
πŸš€ Vu Dao πŸš€

Posted on

4 2

How To Get Lastest Image Version in AWS ECR

A Tool to get latet image version of application from ECR

- 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
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode




πŸš€ Run test




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

Enter fullscreen mode Exit fullscreen mode




πŸš€ A practice to use this script

  • Using slackbot

Alt Text

Mirror:

Read More

🌠 Blog · Web · Linkedin · Group · Page · Twitter 🌠

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here β†’

Top comments (0)

Billboard image

Try REST API Generation for Snowflake

DevOps for Private APIs. Automate the building, securing, and documenting of internal/private REST APIs with built-in enterprise security on bare-metal, VMs, or containers.

  • Auto-generated live APIs mapped from Snowflake database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay