Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

220
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda