How to stop and start Amazon EC2 instances using Lambda Functions and EventBridge/ CloudWatch
You will need the following to complete the process:
- I AM user credentials to complete the lab
- At least one Ec2 instance
- Create Policy and Roles
- 2 Lambda Functions that start and stop ec2 instances
- 2 EventBridge/ CloudWatch rules that trigger my Functions on a schedule
First we will Create an IAM role, which is will be needed to allow CloudWatch Events and Lambda to communicate.
Choose AWS Service and click on Lambda.
Click on Create Policy.
Next, select Create Policy and then the JSON tab. Copy the IAM Policy, paste in the JSON window and click next.
“Version”: “2012–10–17”,
“Statement”: [
{
“Effect”: “Allow”,
“Action”: [
“logs:CreateLogGroup”,
“logs:CreateLogStream”,
“logs:PutLogEvents”
],
“Resource”: “arn:aws:logs:*:*:*”
},
{
“Effect”: “Allow”,
“Action”: [
“ec2:DescribeInstances”,
“ec2:DescribeRegions”,
“ec2:StartInstances”,
“ec2:StopInstances”
],
“Resource”: “*”
}
]
}
Paste policy below and click next.
Give the Policy a name with meaning and click Create Policy
Next, go back to IAM and create roles.
I will choose the policy I created (Ec2_stop_start1)
Click next.
Below the Role will need a name. Keep all other default selections and click on create.
Next, we will create the Lambda function.
In the Management Console under Compute click on Lambda.
If you notice below I am in the Resources for US East (N. Virginia).
Click on Create function.
Here we will choose Author from scratch, give the lambda function a name, pick the runtime here I have python 3.8. Next select exiting role and I will choose the role created in the beginning. Click Create function.
Copy and paste the import boto3 Ec2_stop code in the Lambda function section below after deleting current entry. Then Press Deploy.
import boto3
region = ‘us-east-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))
Below is a screenshot of the Ec2_start lambda function.
With python code pasted to the lambda function.
import boto3
region = ‘us-east-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))
Next, click on configurations change timeout to 10 seconds below Click Save.
Now I will create my EventBridge/ CloudWatch event to trigger the Lambda function.
- Goto CloudWatch console
- Choose events and select Create rule
- Select schedule under Event source
Under cron expression I am selecting to stop my instance at 9pm with expression below.
(* 21 * * ? *) I choose a mountain time range. Choosing event pattern will give other options.
Next I will add my Target as Ec2_stop.
Give rule a name (Ec2_stop)
Select target (Ec2_start_instance)
I will now attempt to stop instance running a Test
Ec2 Instance has been stopped below.
Ec2 Instance will be started back up by running test code below.
Lambda has restarted the instance successfully as evidenced below.