Automating ECR Image Notifications in Slack with EventBridge and Lambda.
Tags: lambda, slack, aws, devops
Published: 2024-02-17 03:51:00

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](https://api.slack.com/apps). Click on 'Create an App,' and choose the option to create it from scratch.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wb3bmd2gohoecoq8n1qf.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r7kmq986l9kx491xj2je.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j4z2t7hvkswovszbo471.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5vuho5ml581eyj28a71v.png)

-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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xvzzix9vvlof7x7k5xfa.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wrqaeiswcemcsf1il1tl.png)

-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!')
    }
```

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/377ze6dxp1vymianpcgh.png)

-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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jn3hhzctfjd414fg1gu4.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gmkjnb0t1z8fnldzp0fw.png)

-10-. Finally, deploy the function.

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c36m2yeiv1edv0sj6t0m.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vqx603n4nlxq4j3zh4cy.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/10z7un7dsne91onek5lm.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b20algozkfi6yfh9c516.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pmz48eb96f7mf2ikq0h4.png)

-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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tqsv2d9kq1atqs9uuy6a.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gtpwgcxtl15x2magmlfn.png)

-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.