Content Introduction Prerequisites Virtual Host Redirect to HTTPS Conclusion Introduction To secure the data transfer redirecting the HTTP traffic to...
Read MoreIt is difficult to start or stop instances manually on a daily basis. We create a Lambda function with the CloudWatch rule through which it automatically starts or stops the instance as per the predefined time in CloudWatch rule. It is also called the scheduler for AWS EC2 instance. There are some Benefits on using scheduler on EC2:
Log in to the AWS console with root account or with IAM user who has permission to create Roles.
We want our function able to perform the following:
To create the above policy using a visual editor or by providing a JSON. You can use the below JSON policy.
{
"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": "*"
}
]
}
Navigate to the AWS Lambda Management Console from Services.
I m using Boto3. Boto3 is SDK for python, which provides programmatic connectivity to create, configure, and manage AWS services with python.
import boto3
region = 'us-west-1'
instances = ['i-12345cb6de4f78g9h', 'i-08ce9b2d7eccf6d26']
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
ec2.stop_instances(InstanceIds=instances)
print('stopped your instances: ' + str(instances))
Every time when the Lambda function will execute the instances are started (mention in the instance array).
The lambda function to start the EC2 instance is completed. You can test it by clicking the Test button.
The lambda function is created, and we use the CloudWatch rule to automate the lambda function.
The above settings to start an EC2 instance every day at 3:00 am (timezone GMT). You can use cron expression according to your requirement.
Targets are used to select the Lambda function for which the rule is used.
You can also use a Fixed rate of option to start EC2 instance if you want to start instance after a fixed number of minutes Hours and Days.
Navigate to the AWS Lambda Management Console from Services.
I m using Boto3. Boto3 is SDK for python, which provides programmatic connectivity to create, configure, and manage AWS services with python.
import boto3
region = 'us-west-1'
instances = ['i-0d2870e7767dc02ef']
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
ec2.stop_instances(InstanceIds=instances)
print('stopped your instances: ' + str(instances))
Every time when the Lambda function will execute the instances are stopeed (mention in the instance array).
The lambda function to stop the EC2 instance is completed. You can test it by clicking the Test button.
The lambda function is created, and we use the CloudWatch rule to automate the lambda function.
The above settings to stop an EC2 instance every day at 21:00 PM (timezone GMT). You can use cron expression according to your requirement.
Targets are used to select the Lambda function for which the rule is used.
You can also use a Fixed rate of option to start EC2 instance if you want to start instance after a fixed number of minutes Hours and Days.
In this article, we have created a Lambda function to Start and Stop the EC2 instance using the CloudWatch Rule. The cron expression is used from the CloudWatch to run the Lambda function.
Content Introduction Prerequisites Virtual Host Redirect to HTTPS Conclusion Introduction To secure the data transfer redirecting the HTTP traffic to...
Read MoreContent Introduction Requirement Getting Started Conclusion Introduction Angular is an open-source web application framework. It is a TypeScript-based free and development...
Read More