Wondering how to use AWS Lambda to Turn off EC2? We can help you.
Recently, one of our customers approached us to reduce his Amazon EC2 usage by stopping and starting the EC2 instances automatically.
Here, at Bobcares, we assist our customers with several AWS queries as part of our AWS Support Services.
Today, let us see how our techs perform the same.
AWS Lambda to Turn off EC2
First and foremost, we need to get the IDs of the EC2 instances that we want to stop and start.
Then, to perform the task at regular intervals using Lambda, we do the following:
-
Create an IAM policy and execution role for the Lambda function
1. We create an IAM policy using the JSON policy editor.
Then we copy and paste the following JSON policy document into the policy editor:
{ "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": "*" } ] }
2. After that, we create an IAM role for Lambda.
In addition, while we attach a permissions policy to Lambda, we make sure to choose the IAM policy we just created.
-
Create Lambda functions that stop and start the EC2 instances
1. Here, we select the Create function from the AWS Lambda console.
2. Then we select the Author from scratch.
3. Under Basic information, we add the following:
a) For Function name, we enter a name that identifies it as the function used to stop the EC2 instances. For example, “StopEC2Instances”.
b) Then for Runtime, we select Python 3.8.
c) Under Permissions, we expand Choose or create an execution role.
d) Then for the Execution role, we select Use an existing role.
e) Under the Existing role, we select the IAM role we created.
4. After that, we select the Create function.
5. Under Function code, we add the following code into the editor pane in the code editor (lambda_function).
This will stop the EC2 instances that we identify.
Example function code to stop EC2 instances:
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))
Here, we need to change the region and instance, to our preference.
6. Under Basic settings, we set Timeout to 10 seconds.
7. Then we select, Deploy.
8. In order to create another function, we repeat steps 1-7.
However, we need to perform the following differently:
In step 3, we enter a different Function name. For example, “StartEC2Instances”.
In step 5, we copy and paste the following code into the editor pane in the code editor:
Example function code – starting EC2 instances
import boto3 region = 'us-west-1' instances = ['i-12345cb6de4f78g9h', 'i-08ce9b2d7eccf6d26'] ec2 = boto3.client('ec2', region_name=region) def lambda_handler(event, context): ec2.start_instances(InstanceIds=instances) print('started your instances: ' + str(instances))
-
Test the Lambda functions
1. To do so, on the AWS Lambda console, we select Functions.
2. Here, we can select one of the functions that we created.
3. Then we select, Test.
4. In the Configure test event dialog box, we select, Create new test event.
5. We can enter an Event name. Then, select Create.
6. Later, we select Test to run the function.
7. After that, we repeat steps 1-6 for the other function.
-
Create CloudWatch Events rules that trigger the Lambda functions
1. First, we open the Amazon CloudWatch console.
2. Then, under Events, we select Rules.
3. In here, we need to select, Create rule.
4. Similarly, under Event Source, we select Schedule.
5. Later we perform either of the following:
For Fixed rate of, we enter an interval of time in minutes, hours, or days.
For Cron expression, we enter an expression that tells Lambda when to stop the instances.
6. Under Targets, we select Add target.
7. Then we go to the Lambda function.
8. For Function, we select the function that stops the EC2 instances.
9. After that, we select, Configure details.
10. Under Rule definition, we do the following:
For Name, we enter a name to identify the rule.
For State, we select the Enabled check box.
11. Eventually, we select the Create rule option.
12. Finally, we need to repeat steps 1-11 to create a rule to start the EC2 instances.
However, in step 5, for Cron expression, we enter an expression that tells Lambda when to start the instances.
Then in step 8, for Function, we select the function that starts the EC2 instances.
And in step 10, under Rule definition, we enter a Name, such as “StartEC2Instances”.
[Stuck in between? We’d be glad to walk you out]
Conclusion
In short, we saw how our Support Techs use AWS Lambda to Turn off EC2.
PREVENT YOUR SERVER FROM CRASHING!
Never again lose customers to poor server speed! Let us help you.
Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.
0 Comments