How to Monitor and Alert AWS Security Group Modifications in Slack.

How to Monitor and Alert AWS Security Group Modifications in Slack.

Security in the Public Cloud Platform is very important. Especially, when you have multiple users in IAM and everyone can modify the security groups, it is important to Monitor and Alert the event. When we talk about ChatOps, Slack is the first preference of most of the Corporates. In this article, we will discuss How to Monitor and Alert AWS Security Group Modifications in Slack. Also, we will discuss store the Logs in the Database for the security audits.

Plan of Action.

Before getting our hands dirty, let’s discuss how we are going to approach this solution. So, we are going to create a CloudWatch Event with will AWS API Call via CloudTrail source type. Then, we are going to create a Lambda function for the CloudWatch Event Target. Then, we are going to Post messages using Slack Incoming WebHook API. Refer to the below diagram.

Monitor and Alert AWS Security Group in Slack
Monitor and Alert AWS Security Group in Slack

As part of this, we are going to discuss how to create the services one by one below.

Create Lambda Function

Firstly, we will create Lambda function which will be needed to be pointed in the CloudWatch Event Target later. So, to create the same, simply follow the below steps.

  • Step 1: Search for Lambda Service
  • Step 2: Click Create function Button.
  • Step 3: Select Author from Scratch
  • Step 4: In the Basic Information section, Give Function Name. Here we use Monitor_SG_Lambda.
  • Step 5: In the same Basic Information section, Select Python 3.8 from the Drop-down.
  • Step 6: In Change default execution role section, Select Create a new role with basic Lambda permissions
  • Step 7: Click Create Function
  • Step 8: On the Next Page, scroll down to the Function code block and replace and Paste the below code.
  • Step 9: Then, click Deploy button.

We will discuss the complete explanation of the code in this article.

Create CloudWatch event

Secondly, we will create a Cloud watch Event that will call the AWS API via CloudTrail and feed the information to the CloudWatch Event. So, to create the same, simply follow the below steps.

  • Step 1: Search for CloudWatch Service
  • Step 2: Under Events (from Left Panel) click Rules
  • Step 3: Click Create rule Button
  • Step 4: Under Event Source select Event Pattern
  • Step 5: In the Drop-down Build event pattern to match…, select Event by Service
  • Step 6: Under the same, In Service Name drop-down, select EC2
  • Step 7: In Event Type drop-down, select AWS API Call via CloudTrail.
  • Step 8: Under the Targets Click Add Target button.
  • Step 9: Select Lambda Function in the drop-down.
  • Step 10: In the Function Select the Monitor_SG_Lambda (Created previously).
  • Step 11:  Make Sure the Configure input selected with Matched event.
  • Step 12: Click Configure details button.
  • Step 13: On next screen, Name the Rule.
  • Step 14: Finally Click Create rule button.

Create Slack Channel Webhook.

Before getting into the Slack Webhook creation, we will see how the Incoming webhook works. Slack messages can be posted to the Slack channel via API calls as the JSON payload. So, follow the below procedure to create the Incoming Webhook.

  • Step 1: Click Here and Click Create New App button
  • Step 2: In the Pop-Up, Give App Name. Here we used AWS-Webhook.
  • Step 3: In the same Pop-up, select your Development Slack Workspace where you want to post message.
  • Step 4: From the Features page, toggle Activate incoming webhooks on.
  • Step 5: Then Click Add new webhook to workspace.
  • Step 6: Select a channel that the app will post to, then click Authorize.
  • Step 7: Copy the incoming webhook URL That can be used to post a message from Lambda. 

Lambda Code Explanation.

Now, let’s see the Lambda python code by block by block.

  • By default, the Lambda function will look for lambda_handler method. So, all the primary logic should be written inside the lambda_handler method.
  • Line number 12 has the variable which collects the Slack Incoming WebHook.
slack_webHook_URL = <Your Slack Incoming Webhook>
  • From line number 13 to 20 will have needed values to construct the JSON Payload.
userName = str(event['detail']['userIdentity']['userName'])
awsRegion = str(event['detail']['awsRegion'])
groupId = str(event['detail']['requestParameters']['groupId'])
eventName = str(event['detail']['eventName'])
eventTime = str(event['detail']['eventTime'])
security_group_URL = "https://"+awsRegion+".console.aws.amazon.com/ec2/v2/home?region="+awsRegion+"#SecurityGroup:group-id="+groupId
security_group_URL_text = "<"+security_group_URL+"|"+groupId+">"
data = {"text": "*Security Group Modification Alert!*\n*UserName*:  "+userName+"\n*Security Group ID*:  "+security_group_URL_text+"\n*Event Name*:  "+eventName+"\n*Event Time*:  "+eventTime+"\n*Region*:  "+awsRegion}
  • From line number 23 to 24 is to call the API request.
http = urllib3.PoolManager()
req = http.request("POST",slack_webHook_URL, body = json.dumps(data), headers = {"Content-Type": "application/json"})
  • From line number 23 to 24 is to call the API request.

That’s all, we Go and change any of the Security Group Inbound/Outbound rule and you can see the Modification Notification in your slack channel as shown in the below image.

Security Group Modification Alert Slack Message.
Security Group Modification Alert Slack Message.

NOTE: AWS CloudTrail API will watch all the modification event in Security Groups available in the entire AWS account. We can Restrict the Security Groups to be monitored by using Tags and Python Boto Library. We will discuss how to restrict the Specific Security Group in our next article.

Conclusion

In this article, we have discussed How to Monitor and Alert AWS Security Group Modifications in Slack. Hope this article gives you enough idea of how to configure the CloudWatch Event and make use of Lambda Target. But we haven’t discussed much how to filter the sources and manipulate the AWS Services using Python Boto Library. We will discuss the same in our upcoming article. Stay tuned and subscribe DigitalVarys for more articles and study materials on DevOpsAgileDevSecOps and App Development.

3 thoughts on “How to Monitor and Alert AWS Security Group Modifications in Slack.”

Leave a Reply