Instance Stop and Start Documents
AWS Lambda
As per AWS “AWS Lambda lets you run code without provisioning or managing
servers. You pay only for the compute time you consume.
With Lambda, you can run code for virtually any type of application or backend
service – all with zero administration. Just upload your code and Lambda takes care of
everything required to run and scale your code with high availability. You can set up
your code to automatically trigger from other AWS services or call it directly from
any web or mobile app.
AWS EC2
As per AWS “Amazon Elastic Compute Cloud (Amazon EC2) is a web service that
provides secure, resizable compute capacity in the cloud. It is designed to make web-
scale cloud computing easier for developers.”
Amazon EC2’s simple web service interface allows you to obtain and configure
capacity with minimal friction. It provides you with complete control of your
computing resources and lets you run on Amazon’s proven computing environment.
Pre-Requirements
1) Create a custom AWS Identity and Access Management (IAM) policy and
execution role for your Lambda function.
2) Create Lambda functions that stop and start your EC2 instances.
3) Test your Lambda functions
4) Create Cloud Watch Events rules that trigger your function on a schedule. For
example, you could create a rule to stop your EC2 instances at night, and
another to start them again in the morning.
IAM Policy Creation
[Link] PolicesCreate Policy
[Link] Select JSON and paste the below code
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Effect": "Allow",
"Action": [
"ec2:Start*",
"ec2:Stop*"
],
"Resource": "*"
}
]
}
Note:
[Link]
cloudwatch/
Review Policy and Type the Name InstancesStopStart-Policy
2) IAM Role Creation
Select RoleCreate Role
Select the AWS Service and select LambdaNext:Permission
Find the recently create the InstancesStopStart-Policy
Type the Role Name:InstanceStopStart-Role and Select Create role
3)Lambda Function creation(Stop and Start)
To create the InstancesStop policy
Select Function and select create function
Create the functionAuthor from scratchType the function name:
InstanceStop-PolicySelect run time: Python 3.7Permission: Select change default
execution role Use an existing roleFind InstanceStopandStart-PolicyCreate
functionAnd update the InstanceStop code and Save or Deploy
import boto3
region = 'ap-southeast-1'
instances = ['i-08d15778963a11c47']
ec2 = [Link]('ec2', region_name=region)
def lambda_handler(event, context):
ec2.stop_instances(InstanceIds=instances)
print('stopped your instances: ' + str(instances))
InstanceStart Policy
Create the functionAuthor from scratchType the function name: InstanceStop-
PolicySelect run time: Python 3.7Permission: Select change default execution
role Use an existing roleFind InstanceStopandStart-PolicyCreate
functionAnd update the InstanceStop code and Save or Deploy
import boto3
region = 'ap-southeast-1'
instances = ['i-08d15778963a11c47']
ec2 = [Link]('ec2', region_name=region)
def lambda_handler(event, context):
ec2.start_instances(InstanceIds=instances)
print('started your instances: ' + str(instances))
4)CloudWatch: Cron schedule
Select CloudWatch and create the Rules:
Instance Stop Schedule:
Instance Stop every day Run at 12:15 pm (UTC)
Refer Notes:
[Link]
Select Configure details
Instance Start every day Run at 10:45 AM (UTC)
Instance Start Schedule: