thigley986 / lambda-aws-ses-send-email Goto Github PK
View Code? Open in Web Editor NEWSend an email from AWS Lambda through Amazon SES
License: Apache License 2.0
Send an email from AWS Lambda through Amazon SES
License: Apache License 2.0
import boto3
import collections
import datetime
import time
import sys
ses = boto3.client('ses')
ec = boto3.client('ec2', 'eu-west-1')
ec2 = boto3.resource('ec2', 'eu-west-1')
from datetime import datetime
from dateutil.relativedelta import relativedelta
#create date variables
date_after_month = datetime.now()+ relativedelta(days=7)
#date_after_month.strftime('%d/%m/%Y')
today=datetime.now().strftime('%d/%m/%Y')
def lambda_handler(event, context):
#Get instances with Owner Taggs and values Unknown/known
instance_ids = []
reservations = ec.describe_instances(
Filters=[
{'Name': 'tag:Owner', 'Values': ['Unknown', 'unknown']},
]
).get('Reservations', [])
for reservation in reservations:
instances = reservation['Instances']
for instance in instances:
instance_ids.append(instance['InstanceId'])
inst=format(','.join(instance_ids))
email_body=format(','.join(instance_ids))
tags = {}
for tag in instance['Tags']:
tags[tag['Key']] = tag['Value']
#Check if "TerminateOn" tag exists:
if 'TerminateOn' in tags:
#compare TerminteOn value with current date
if tags["TerminateOn"]==today:
#Check if termination protection is enabled
terminate_protection=ec.describe_instance_attribute(InstanceId =instance['InstanceId'] ,Attribute = 'disableApiTermination')
protection_value=(terminate_protection['DisableApiTermination']['Value'])
#if enabled disable it
if protection_value == True:
ec.modify_instance_attribute(InstanceId=instance['InstanceId'],Attribute="disableApiTermination",Value= "False" )
#terminate instance
ec.terminate_instances(InstanceIds=instance_ids)
#Send an email to engineering that the instance has been terminated. (as many emails as number of instances
#body=format(','.join(instance_ids))
response = ses.send_email(
Source = '[email protected]',
Destination={
'ToAddresses': [
'[email protected]',
],
},
Message={
'Subject': {
'Data': 'Instance will be removed'
},
'Body': {
'Text': {
'Data': email_body + ' Has been terminated'
}
}
}
)
else:
#Send an email to engineering that this instance will be removed X amount of days (calculate the date based on today's date and the termination date."
print email_body
response = ses.send_email(
Source = '[email protected]',
Destination={
'ToAddresses': [
'[email protected]',
],
},
Message={
'Subject': {
'Data': 'Instance will be removed'
},
'Body': {
'Text': {
'Data': email_body + "will be removed after 7 days"
}
}
}
)
else:
if not 'TerminateOn' in tags:#, create it
ec2.create_tags(Resources=instance_ids,Tags=[{'Key':'TerminateOn','Value':date_after_month.strftime('%d/%m/%Y')}])
ec.stop_instances(InstanceIds=instance_ids)
print "last "+format(','.join(instance_ids))
i want to pass email_body=format(','.join(instance_ids)) as variable to your function but it's not happening
here is what i'm getting:
'Data': email_body + "will be removed after 7 days"
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.