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.
-2-. Provide an 'App Name,' choose your workspace created to receive notifications, and then click on 'Create App.'
-3-. On the left side menu bar, locate the 'Incoming Webhook' option and click on it.
-4-. Activate the incoming webhook by clicking on the 'On' toggle.
-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.
-6-. Once done, you will see a generated 'Webhook URL.' Copy this URL and save it on Notepad or any text editor.
-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.
-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.
-10-. Finally, deploy the function.
-11-. Now, go to 'EventBridge,' and click on 'Create Rule.'
-12-. Enter a name and description for the rule, then click on 'Next.'
-13-. Now, an important step is to configure the 'Event Pattern.' Select the value as 'my image' and click on 'Next.'
-14-. Choose the target as 'Lambda,' select your function, click 'Next,' and create the rule.
-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.
-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)