Topic today for discussion
How to create an EC2 (Elastic Compute cloud) and try to manage automatic start/stop on schedule manner with below implementation sequence.
Create an AWS EC2 Instance
AWS Amazon Linux instance - free tier category.
Create an AWS Lambda Function
StartEC2Instances
import json
import boto3
region = 'us-east-1'
instances = ['i-039c057d3970794a5']
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
ec2.stop_instances(InstanceIds=instances)
print('stopped your instances: ' + str(instances))
return {
'statusCode': 200,
'body': json.dumps('stopped your instance')
}
StopEC2Instances
import json
import boto3
region = 'us-east-1'
instances = ['i-039c057d3970794a5']
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
ec2.stop_instances(InstanceIds=instances)
print('stopped your instances: ' + str(instances))
return {
'statusCode': 200,
'body': json.dumps('stopped your instance')
}
Create Rule
myEC2Start
myEC2Stop
Create Event/Rules using Cloudwatch
ec2start
Ec2stop