Creating Slack Notifications for CloudWatch Alarms

Length: 00:11:34

Lesson Summary:

Configure Webhooks in Slack

  1. Create a Slack app:
  2. Search for and select Incoming Webhooks.
  3. Set Activate Incoming Webhooks to On.
  4. Select Add New Webhook to Workspace.
  5. Choose the default channel where messages will be sent and click Authorize.
  6. Note the webhook URL from the Webhook URLs for Your Workspace section. For example:

  7. Test the webhook:

     curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello, World!"}' $WEBHOOK_URL

Create an SNS Topic

aws sns create-topic --name high-cpu-alarm

Note the TopicArn.

Create a CloudWatch Alarm

  1. Send notifications to the SNS topic when CPU utilization > 40%:

     aws cloudwatch put-metric-alarm \
         --alarm-name cpu-mon \
         --alarm-description "Alarm when CPU exceeds 40%" \
         --metric-name CPUUtilization \
         --namespace AWS/EC2 \
         --statistic Average \
         --period 60 \
         --evaluation-periods 1 \
         --threshold 40 \
         --comparison-operator GreaterThanThreshold \
         --dimensions Name=InstanceId,Value=i-12345678901234567 \
         --alarm-actions arn:aws:sns:us-east-1:123456789012:high-cpu-alarm \
         --unit Percent

Create an SSM Parameter

aws ssm put-parameter --cli-input-json '{"Type": "SecureString", "KeyId": "alias/aws/ssm", "Name": "SlackWebHookURL", "Value": "'"$WEBHOOK_URL"'"}'

Create a Lambda Execution Role

  1. Attach the following managed policies:

    • AmazonSSMFullAccess
    • AWSLambdaBasicExecutionRole

Create a Lambda Function

  1. Use the SNS topic as a trigger.

Stress the CPU

# Install Extra Packages for Enterprise Linux
sudo amazon-linux-extras install epel
# Install stress
sudo yum install -y stress
# Beat it up for 5 mins
stress --cpu 2 --timeout 300s

This lesson is only available to Linux Academy members.

Sign Up To View This Lesson

Or Log In

Looking For Team Training?

Learn More