Ends in
00
days
00
hrs
00
mins
00
secs
ENROLL NOW

Get any AWS Specialty Mock Test for FREE when you Buy 2 AWS Pro-Level Practice Tests – as LOW as $10.49 USD each ONLY!

Setting up Slack notifications on Tag Modifications for EC2 Instances

Home » Others » Setting up Slack notifications on Tag Modifications for EC2 Instances

Setting up Slack notifications on Tag Modifications for EC2 Instances

Monitoring resource configurations in a dynamic cloud environment is important, especially in production. One essential aspect of managing AWS resources, such as Amazon EC2 instances, is tracking tag modifications. Tags are not just labels but vital in organizing resources, enabling billing allocation, and enforcing compliance policies. Any unintentional tag modification can disrupt operations or mislead resource management, making timely alerts vital.

This article will guide you through setting up automated Slack notifications to alert your team whenever a tag modification occurs on production EC2 instances. Using a combination of Amazon EventBridge, AWS Lambda, and Slack API, you can create a real-time notification system that will instantly notify your team of changes, allowing immediate review and action. This notification will ensure the production environments stay organized, secure, and aligned with your tagging standards.

Steps to Set up Slack Notifications for Tag Modifications

Step 1: In the AWS Management Console, create a new function in AWS Lambda.

  • Choose “Author from Scratch

  • Function Name: ” slack-notification-for-EC2-tag-modifications

  • Runtime: “Python 3.12

Step 2: In the Code Source section, paste the code below and click Deploy.

import json
import urllib.request

SLACK_WEBHOOK_URL = "YOUR_WEBHOOK_URL"

def lambda_handler(event, context):
    event_name = event['detail']['eventName']
    resources_set = event['detail']['requestParameters'].get('resourcesSet', {}).get('items', [])
    tag_set = event['detail']['requestParameters'].get('tagSet', {}).get('items', [])
    instance_id = resources_set[0]['resourceId']
    user_identity = event['detail']['userIdentity']['userName']
    block_message = [
                		{
                			"type": "section",
                			"text": {
                				"type": "mrkdwn",
                				"text": f"*EC2 Instance Tag modified by {user_identity}*"
                			}
                		},
                		{
                			"type": "section",
                			"text": {
                				"type": "mrkdwn",
                				"text": f"*Instance ID:* `{instance_id}`"
                			}
                		},
                		{
                			"type": "section",
                			"text": {
                				"type": "mrkdwn",
                				"text": "*Tag changes:*"
                			}
                		}
            ]
    actions = []
    if tag_set:
        for tag in tag_set:
            key = tag.get('key', '')
            value = tag.get('value', '')
            tag_action = "added" if event_name == 'CreateTags' else "removed"
            # action = f"Tag {key} {tag_action} with value {value}"
            # actions.append(action)
            tag_block_action={
        			"type": "section",
        			"text": {
        				"type": "mrkdwn",
        				"text": f"Tag `{key}` {tag_action} with value `{value}`"
        			}
        		}
            block_message.append(tag_block_action)
  
    # Send message to Slack
    send_to_slack(blocks=block_message)

def send_to_slack(blocks):
    payload = {
        'blocks': blocks  # Use blocks for formatted messages
    }

    data = json.dumps(payload).encode('utf-8')

    # Make the HTTP request using urllib
    req = urllib.request.Request(SLACK_WEBHOOK_URL, data=data, method='POST')

    try:
        with urllib.request.urlopen(req) as response:
            response_data = response.read().decode('utf-8')
            if response_data != 'ok':
                print(f"Failed to send message to Slack: {response_data}")
    except Exception as e:
        print(f"Error sending message to Slack: {str(e)}")
 

This Lambda function monitors tag modifications on EC2 instances and sends notifications to a specific Slack channel. When triggered by an event, it retrieves details such as the creation or deletion of the following; the instance ID, the tags, and the username of the person who modified it. It then structures the data into a Slack message block format, specifying each tag modification (key-value pair and action). Finally, it sends the message to a defined Slack channel via a webhook URL, enabling near real-time tracking of tag changes on EC2 instances in Slack.

Tutorials dojo strip

Step 3: Go to Amazon EventBridge, under Buses, choose Rules, then click “Create rule“.

  • Provide a Name and Description, then click Next.

Step 4: Under Event Pattern :

  • AWS Service: “EC2
  • Event type: “AWS API Call via CloudTrail

  • Click Edit pattern and input the JSON as follows:
{
  "source": ["aws.ec2"],
  "detail-type": ["AWS API Call via CloudTrail"],
  "detail": {
    "eventSource": ["ec2.amazonaws.com"],
    "eventName": ["CreateTags", "DeleteTags"],
    "errorCode": [{
      "exists": false
    }],
    "requestParameters": {
      "resourcesSet": {
        "items": {
          "resourceId": [{
            "prefix": "i-"
          }]
        }
      }
    }
  }
}

Step 5: After the input for JSON, click “Next”. 

  • Under Step 3, select a target “Lambda Function
  • For function, target your previously created Lambda function

Step 6: Review your rule settings and create it

Step 7: Go back to your Lambda function and navigate to Configurations.

  • Under Triggers, choose “Add triggers”

  • Under General Configurations, set the timeout to “5 minutes” then click “Save“.

 

Testing the Slack Notifications

  • In Instances, modify the Name or Manage tags

  • A Slack notification should be sent after a few moments.

Final Remarks

We have successfully implemented Slack notifications for EC2 tag changes—a simple yet powerful way to monitor updates in your AWS environment. By adding Amazon EventBridge triggers to the AWS Lambda function, this notification ensures that anytime a tag on an EC2 instance is changed, a notification is sent to your team’s Slack channel that will give everyone a quick heads-up. This is especially helpful in production environments where tracking changes to your resources is important for maintaining organization and staying aligned with tagging policies.
With these notifications, you’ll know right away if something changes and who made the change, allowing you to catch and correct any unintended updates. This setup keeps your team informed in real-time, making it easier to ensure smooth operations and giving everyone visibility over important changes.

Get any AWS Specialty Mock Test for FREE when you Buy 2 AWS Pro-Level Practice Tests – as LOW as $10.49 USD each ONLY!

Tutorials Dojo portal

Learn AWS with our PlayCloud Hands-On Labs

Tutorials Dojo Exam Study Guide eBooks

tutorials dojo study guide eBook

FREE AWS Exam Readiness Digital Courses

Subscribe to our YouTube Channel

Tutorials Dojo YouTube Channel

FREE AWS, Azure, GCP Practice Test Samplers

Follow Us On Linkedin

Recent Posts

Written by: Irene Bonso

Irene Bonso is currently thriving as a Junior Software Engineer at Tutorials Dojo and also an active member of the AWS Community Builder Program. She is focused to gain knowledge and make it accessible to a broader audience through her contributions and insights.

AWS, Azure, and GCP Certifications are consistently among the top-paying IT certifications in the world, considering that most companies have now shifted to the cloud. Earn over $150,000 per year with an AWS, Azure, or GCP certification!

Follow us on LinkedIn, YouTube, Facebook, or join our Slack study group. More importantly, answer as many practice exams as you can to help increase your chances of passing your certification exams on your first try!

View Our AWS, Azure, and GCP Exam Reviewers Check out our FREE courses

Our Community

~98%
passing rate
Around 95-98% of our students pass the AWS Certification exams after training with our courses.
200k+
students
Over 200k enrollees choose Tutorials Dojo in preparing for their AWS Certification exams.
~4.8
ratings
Our courses are highly rated by our enrollees from all over the world.

What our students say about us?