DEV Community

Cover image for Automating ECR Image Notifications in Slack with EventBridge and Lambda.
Shrihari Haridass
Shrihari Haridass

Posted on

Automating ECR Image Notifications in Slack with EventBridge and Lambda.

This blog covers how you can enable notifications in Slack whenever new Docker images are pushed into an AWS ECR repo. This is achieved with the assistance of AWS services such as EventBridge and Lambda. Additionally, to send notifications on Slack automatically, a Slack Webhook is also required.

-1-. As a first step, create an account on Slack, then navigate to the Slack API: Applications | Slack. Click on 'Create an App,' and choose the option to create it from scratch.

Image description

-2-. Provide an 'App Name,' choose your workspace created to receive notifications, and then click on 'Create App.'

Image description

-3-. On the left side menu bar, locate the 'Incoming Webhook' option and click on it.

Image description

-4-. Activate the incoming webhook by clicking on the 'On' toggle.

Image description

-5-. To add the webhook to the workspace, click on 'Add new webhook to workspace.' A pop-up will appear with Slack channel permission window; click 'Allow' to enable the reception of incoming webhooks.

Image description

-6-. Once done, you will see a generated 'Webhook URL.' Copy this URL and save it on Notepad or any text editor.

Image description

-7-. Next, log in to your AWS account and navigate to 'AWS Lambda.' Create a new function, choose the runtime as 'Python 3.12,' and then create the function. Since Lambda doesn't support the 'request' package, you'll need to write the Python code on your local machine where Python is already installed. Alternatively, you can use the provided code below:

import json
import requests

def lambda_handler(event, context):
    # Print the entire event to CloudWatch Logs for debugging
    print(json.dumps(event))

    # Access imageDigest and repositoryName dynamically
    image_digest = event.get('imageDigest') or event.get('detail', {}).get('imageDigest')
    repo_name = event.get('repositoryName') or event.get('detail', {}).get('repositoryName')

    if not image_digest or not repo_name:
        return {
            'statusCode': 400,
            'body': json.dumps('Invalid event structure. Missing required keys.')
        }

    slack_webhook_url = 'Your Slack URL'

    message = f"New image pushed to ECR: {repo_name}:{image_digest}"
    requests.post(slack_webhook_url, json={'text': message})

    return {
        'statusCode': 200,
        'body': json.dumps('Notification sent to Slack!')
    }
Enter fullscreen mode Exit fullscreen mode

-8-. After running the command 'pip install requests,' multiple files will be generated. Zip all these files, excluding the Slack webhook file.

Image description

-9-. Navigate back to the Lambda function, and on the right-hand side, click on 'Upload from' and choose '.zip.' Select your zipped file and upload your code. In the test event, set the key-value pairs accordingly.

Image description

Image description

-10-. Finally, deploy the function.

-11-. Now, go to 'EventBridge,' and click on 'Create Rule.'

Image description

-12-. Enter a name and description for the rule, then click on 'Next.'

Image description

-13-. Now, an important step is to configure the 'Event Pattern.' Select the value as 'my image' and click on 'Next.'

Image description

-14-. Choose the target as 'Lambda,' select your function, click 'Next,' and create the rule.

Image description

Image description

-15-. Now, go to Amazon ECR, create a public or private repository, push an image into that repository, and you will see a notification received in your Slack workspace as configured.

Image description

Image description

-16-. So, from now on, whenever you push a new image to ECR using EventBridge and Lambda, it will automatically send a notification to your Slack group.

Conclusion:

So, this is how you can configure notifications for ECR images. By leveraging EventBridge and Lambda along with Slack webhooks, you can receive automatic notifications in your Slack workspace whenever a new image is pushed to your ECR repository.

Top comments (0)