Experience: is what you get soon after you need it.

Experience: is what you get soon after you need it.

****************Got the opportunity to pen a Book on Database Cloud Services.......More details to follow.***************

Title : Database Cloud Revealed
Publisher : Apress
Release Date : Jan-2020

**********************************************************************************


Rasul Allah (sal Allahu alaihi wa sallam) said: "Restore the trusts of those who trust you, and deal not falsely with him who deals falsely with you." [Abu Dawud, Tirmidhi]


My Cloud Certifications:

AWS Certified Solutions Architect Associate

Azure Certified Administrator

Oracle Cloud Infrastructure 2018 Certified Architect Associate.

Oracle Cloud Infrastructure Classic 2018 Certified Architect Associate.

Oracle Database Cloud Administrator Certified Professional.

Oracle Database Cloud Service Operations Certified Associate.

Search This Blog

Saturday, June 1, 2019

AWS Auto Stop Start EC2 Instances

In the below post we will create a simple Lambda function to Auto STOP/START EC2 instances.

Step-1
Before we create a lambda function we need to have a role that has access to our EC2 instances.

Go ahead and create a IAM role with appropriate permissions to stop/start the EC2 instances, you can either use the built-in policies or create a custom policy for this role.

 


Step-2
Now create a lambda function, choose option author from scratch as we will use custom code to stop/start the instances.











copy/paste the below code into the lmbda function, remember to replace the region & instance ids.


import boto3
region = 'us-east-2'
instances = ['i-00XXXXXX14f6f']

def lambda_handler(event, context):
    ec2 = boto3.client('ec2', region_name=region)
    ec2.start_instances(InstanceIds=instances)
    print 'started your instances: ' + str(instances)


Under roles select the IAM role you created for this function and change the timeout to at least 20 seconds.










Save and test the function.

You should see a successful message.























Now create a stop function to stop the EC2 instances. Follow step-2 except the in the code you will replace start command with stop command


import boto3
region = 'us-east-2'
instances = ['i-00XXXXXX14f6f']

def lambda_handler(event, context):
    ec2 = boto3.client('ec2', region_name=region)
    ec2.stop_instances(InstanceIds=instances)
    print 'stopped your instances: ' + str(instances)


Step-3:
Now  create Cloud Watch events that will trigger the lambda functions that were created in Step2.






















Create a new rule for the stop function :

















Here I have selected cron expression, you can select which ever is convenient for you.











More about schedule expressions can be found here:
https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html


****Remember scheduled events in CloudWatch uses default timezone as UTC




Repeat step-4 for start function so that the ec2 instances are started at a desired interval:

Validation:

Instance is stopped at the specified time:














No comments: