Automating AWS Monitoring with CloudWatch, SNS, and Lambda

Automating AWS Monitoring with CloudWatch, SNS, and Lambda
  • CloudWatch -

  1. Monitor AWS resources such as EC2 instances, RDS databases and S3 bucket.

  2. Collect and analyse logs from AWS services and applications.

  3. You can set alarms and notifications based on metric and log data.

  4. Visualize data using dashboard and charts.

  5. Troubleshoot issues using detailed metrics and logs.

  • Steps to create CloudWatch-
  1. Create dashboard for monitoring

  2. Create instance

  3. Give id to dashboard

  4. Select parameters like cpu utilization etc

    1. Cloud watch - Dashboard

  1. Create dashboard

  1. Give name

  1. Duplicate tab - Create an EC2 instance

  1. On cloud dashboard- Add widget- select required format - e.g.,Number

  1. Select required parameters- eg., NetworkOut, NetworkIn and CPU utilisation

  1. Here you can see data if instance to monitor- we can set limits in hour

  1. You can add multiple widgets. Add another widget if required

  1. Search instance Id - select metrics

  1. Here you can see data in respective formats

  • SNS - (Simple Notification Service)

  1. Publish messages to topics.

  2. Subscribe to topics sing various protocols (ex SMS, emails)

  3. Filter messages based on attributes, allowing for target notification.

  4. Here we can set alerts like if cpu utilities above 60% instance should stop

  • Steps to create SNS-
  1. Create topic

  2. Create subscription

    1. SNS - Dashboard

  1. Topics- Create topic

  1. Standard - Name and display name

  1. Here created topic can be seen

  1. Create subscription

    Subscription - Create subscription

  1. Select topic you create-required protocol eg., email - give email address

  1. Here subscription is pending

    You will receive mail to subscribe

  1. After you subscribe mail, status will be confirmed

  1. Create Alarm

    Alarms - In Alarm - Create alarm

  1. Select metric - next

  1. Give instance id - select metrics

  1. Select required parameters eg., CPU utilisation

  1. Default period is 5 min , you can select as per your needs

  1. Conditions - Static - Greater/Equal -60%

    So when you cpu utilises 60% your instance will be stopped

  1. Select topic

  1. EC2 instance - Stop this instance

  1. Give alarm name

  1. Insufficient data because we just initiated process

  1. After some time it will be in Ok state

  1. Connect EC2 instance on your gitbash - give following command to utilise cpu fast for testing

  1. Check CPU consumption

  1. After 60% utilisation you get below message

  1. In alarm alert is activated

  1. And your EC2 instance is stopped automatically

  • Lambda -

AWS Lambda is a server-less computing service provided by Amazon Web Services that allows you to run code without provisioning or managing servers.

Lambda functions are triggered by events and can execute in response to changes in data, state, or resource updates.

Lambda is an ideal compute service for application scenarios that need to scale up rapidly, and scale down to zero when not in demand. For example, you can use Lambda for:

  • File processing

  • Stream processing

  • Web applications

  • IoT backends

  • Mobile backends

Lambda function is a small, stateless code snippet that runs in response to events:

  • API gateway request

  • Database updates

  • File Uploads

  • Schedule

Lambda Functions are:

  • Serverless - No server management needed.

  • Event Driven - Runs only when triggered

Steps to create lambda function-

  1. Lambda - Create a function

  1. Name - language on which you’re running script

  1. Duplicate tab

    Create IAM role as admin

    IAM - Roles - Create role

  1. Select AWS service

  1. Use case Lambda

  1. Give admin permission

  1. Give role name

  1. On lambda tab - Existing role - select IAM you created

  1. Test below script to stop instance

Now your instance will be stopped automatically.