Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

219
Views
Sending alerts when an instance is created inside AWS

Need to receive emails when someone creates an EC2 instance in AWS. I have tried using cloudwatch but that doesn't seem to work for me. It says insufficient data. Is there a better way to do it?

Creating CloudWatch Alarms for CloudTrail Events

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Step1 : Create a Cloud watch Rule to notifiy the creation . As per the lifecycle of EC2 Instance when lauch button is pressed . Instance goes from Pending state to Running state . So create the Rule for Pending state

Create a Cloud watch Rule as specified in the image screenshot

Step2 : Create a Step function . Because cloud Trail logs all the event in the account with a delay of atleast 20 min . This step function is usefull if you want the name of user who has created the instance .

{
  "StartAt": "Wait",
  "States": {
    "Wait": {
      "Type": "Wait",
      "Seconds": 1800,
      "Next": "Ec2-Alert"
    },
   "Ec2-Alert":{
     "Type": "Task",
     "Resource":"arn:aws:lambda:ap-south-1:321039853697:function:EC2-Creation-Alert",
     "End": true  

  }
  }
}

Step3 : Create a SNS topic for notification

Step4 : Write a lambda function to fetch the log from cloud trail and get the user name who has created the instance .

import json
import os
import subprocess
import boto3


def lambda_handler(event, context):

    client = boto3.client('cloudtrail')
    client1 = boto3.client('sns')
    Instance=event["detail"]["instance-id"]     
    response = client.lookup_events(
    LookupAttributes=[
        {
            'AttributeKey': 'ResourceName',
            'AttributeValue': Instance
        },
    ],
    MaxResults=1)

    test=response['Events']

    st="".join(str(x) for x in test)
    print(st)
    user=st.split("Username")[1]
    finalname=user.split(",")
    Creator=finalname[0]
    #print(st[st.find("Username")])

    Email= "Hi All ,\n\n\n The User%s has created new EC2-Instance in QA account and the Instance id is %s \n\n\n Thank you \n\n\n Regard's lamda"%(Creator,Instance)



    response = client1.publish(
    TopicArn='arn:aws:sns:ap-south-1:321039853697:Ec2-Creation-Alert',
    Message=Email

)

    # TODO implement
    return {

        'statusCode': 200,
    }

Note: This code trigger an notification if the instance is changed from stop state to running state.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!