Get Email notification on AWS IAM user creation

{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "ses:SendEmail",
"Resource": "*"
}
]
}
import json
import boto3
import os
import re
ses_client=boto3.client("ses")def send_email(subject,body):
ses_client.send_email(Source=os.environ['Source'],Destination={'ToAddresses': [os.environ['Recipient']]},
Message={
'Subject': {
'Data': subject
},
'Body': {
'Text': {
'Data': body
}
}
}
)

def lambda_handler(event, context):
CreatedDate= event['detail']['responseElements']['user']['createDate']
Username = event['detail']['responseElements']['user']['userName']
CreatedBy=''
if event['detail']['userIdentity']['type']=='IAMUser':
CreatedBy=event['detail']['userIdentity']['userName']
elif event['detail']['userIdentity']['type']=='AssumedRole':
CreatedBy=event['detail']['userIdentity']['principalId']
pattern = '^exclude-user'
result = re.match(pattern,Username.lower().strip())
if not result:
if CreatedBy=='':
Data=' User ' +Username + ' got created on ' + CreatedDate
else:
Data=' User ' +Username + ' got created on ' + CreatedDate + ' by '+CreatedBy
send_email("IAM Notification",Data)

{
"source": [
"aws.iam"
],
"detail-type": [
"AWS API Call via CloudTrail"
],
"detail": {
"eventSource": [
"iam.amazonaws.com"
],
"eventName": [
"CreateUser"
]
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store