Following article will describe how to start EC2 Instances on schedule using Lambda and send the public IP instantly to multiple recipients over email using SNS. This activity will help to schedule start EC2 instances on a particular time during weekdays and send the public IP of the instances to the development team who use those instances for some development purpose. Using this method you don’t have to allocate Elastic IP to the EC2 instances and also do not need any human intervention to send the public IP of the instances to users.
Step 1: Create following role for the EC2 Start Stop action:-
{
“Version”: “2012-10-17”,
“Statement”: [
{
“Effect”: “Allow”,
“Action”: [
“logs:CreateLogGroup”,
“logs:CreateLogStream”,
“logs:PutLogEvents”
],
“Resource”: “arn:aws:logs:*:*:*”
},
{
“Action”: “ec2:*”,
“Effect”: “Allow”,
“Resource”: “*”
}
]
}
Step 2: Create a lambda function with following Python 2.7 code:-
import boto3
region = ‘eu-central-1’
instances = [‘i- xxxxxxxxxxxx1′,’i- xxxxxxxxxxxx2′,’i- xxxxxxxxxxxx3’]
def lambda_handler(event, context):
ec2 = boto3.client(‘ec2’, region_name=region)
ec2.start_instances(InstanceIds=instances)
print ‘started Bastion and Store1 & Store2 servers’ + str(instances)
Step 3: Schedule the Lambda function from Cloudwatch -> rule -> schedule to run at 11AM IST from Monday to Friday
Step 4: Create another role in IAM with Full privilege on EC2 and on SNS (Not recommended for production. Be specific about granting privilages):-
Policy for EC2
{
“Version”: “2012-10-17”,
“Statement”: [
{
“Action”: “ec2:*”,
“Effect”: “Allow”,
“Resource”: “*”
}
]
}
Policy for SNS
{
“Version”: “2012-10-17”,
“Statement”: [
{
“Effect”: “Allow”,
“Action”: [
“sns:*”
],
“Resource”: “arn:aws:sns:eu-central-1:xxxxxxxxxx:fra_tcs_store_ips”
}
]
}
Step 5: Create new Lambda function with the above policy to fetch public IP of instances and send SNS notifications:-
from __future__ import print_function
import json
import boto3
ec2 = boto3.resource(‘ec2′, region_name=’eu-central-1’)
client = boto3.client(
“sns”,
aws_access_key_id=”**********************”,
aws_secret_access_key=”***********************”,
region_name=”eu-central-1″
)
topic = client.create_topic(Name=”storeipnotifications”)
topic_arn = topic[‘TopicArn’]
client.subscribe(TopicArn=topic_arn,Protocol=’email’,Endpoint=’email.1@xxxxx.com’)
client.subscribe(TopicArn=topic_arn,Protocol=’email’,Endpoint=’email.2@xxxxx.com’)
client.subscribe(TopicArn=topic_arn,Protocol=’email’,Endpoint=’email.3@xxxxxx.com’)
client.subscribe(TopicArn=topic_arn,Protocol=’email’,Endpoint=’email.4@xxxxxx.com’)
instances1 = ec2.instances.filter(Filters=[{‘Name’: ‘instance-id’, ‘Values’: [‘i-xxxxxxxxxxxx1’]}])
instances2 = ec2.instances.filter(Filters=[{‘Name’: ‘instance-id’, ‘Values’: [‘i- xxxxxxxxxxxx2’]}])
for instance in instances1:
store1IP = instance.public_ip_address
print(“Store1 public IP – “, instance.public_ip_address)
for instance in instances2:
store2IP = instance.public_ip_address
print(“Store2 public IP – “, instance.public_ip_address)
print(‘Loading function’)
def lambda_handler(event, context):
message = ‘Store1 IP:’+store1IP+’ , Store2 IP:’+store2IP
print(message)
client.publish(Message=message, TopicArn=topic_arn)
return message
Step 6: Now create the schedule to run this Lambda code at 11:05AM IST from Monday to Friday
So once the email subscription is confirmed by the recipients, the recipients will start receiving the IP address of the 2 Stores at 11:05AM IST from Monday to Friday.
I think other site proprietors should take this website as an model, very clean and great user genial style and design, let alone the content. You’re an expert in this topic!
I like what you guys are up also. Such clever work and reporting! Keep up the superb works guys I¡¦ve incorporated you guys to my blogroll. I think it’ll improve the value of my web site 🙂