You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
EC2 Automated Start & Stop simplifies instance management by scheduling the activation and deactivation of EC2 instances. By automating this process, developers can start up instances according to a schedule without manual intervention. This ensures resources are available when needed and stops instances during inactivity, saving time, reducing costs, and optimizing resource use — thus enhancing operational efficiency.
Architectural Diagram
Step 1: Create an EC2 instance
Step 2: Create Lambda IAM Role
In the IAM role console, create a Lambda IAM role that will allow Lambda to manage EC2 instances and provide proper permission. Select AmazonEC2FullAccess and AWSLambdaBasicExecutionRole.
Step 3: Create "Start" Lambda Function
Create a "Start" EC2 Lambda Function. Attach the Lamda IAM role. Use Python code below. Modify region and instance ID as needed.
import boto3
region = 'us-east-1' # Replace with your desired region
instances = ['i-213sdsfsfsdf'] # Replace with desired EC2 instance ID(s)
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
ec2.start_instances(InstanceIds=instances)
print('starting your instances: ' + str(instances))
Step 4: Create "Stop" Lambda Function
Create a "Stop" EC2 Lambda Function. Attach the Lambda IAM role. Use Python code below. Modify region and instance ID as needed.
import boto3
region = 'us-east-1' # Replace with your desired region
instances = ['i-3424432432'] # Replace with desired EC2 instance ID(s)
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
ec2.stop_instances(InstanceIds=instances)
print('stopped your instances: ' + str(instances))
37 - Minute (at 37th minute)
15 - Hour (at 3 PM)
11 - Day of the month (11th)
3 - Month (March)
* - Day of the week (any day)
? - any (for day of month)
* - any (other)
Stop Instance Schedule:
Head to CloudWatch and start creating a "Schedule" > Select Recurring Schedule > Under Cron-based schedule create desired schedule to stop instance > Select Off under Flexible time schedule allowing the process to take place immediately > Next > Select Invoke Lambda > Select the "EC2Stop" Lambda function
Start Instance Schedule:
Create another "Schedule" > Select Recurring Schedule > Under Cron-based schedule create desired schedule to stop instance > Select Off under Flexible time schedule allowing the process to take place immediately > Next > Select Invoke Lambda > Select the "EC2Start" Lambda function
Finish!
Congratulations! You've configured EC2 Automated Start and Stop using Lambda Functions and CloudWatch Events/EventBridge. Instance(s) will start up or stop according to schedule!
Notes
Make sure you have the correct Cron expression arrangement/configuration.
Make sure to disable schedules when no longer in use.
Reference
EXAMPLE CRON EXPRESSION:
37 - Minute (at 37th minute)
15 - Hour (at 3 PM)
11 - Day of the month (11th)
3 - Month (March)
* - Day of the week (any day)
? - any (for day of month)
* - any (other)
"Stop" EC2 Lambda Python Function
import boto3
region = 'us-east-1' # Replace with your desired region
instances = ['i-3424432432'] # Replace with desired EC2 instance ID(s)
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
ec2.stop_instances(InstanceIds=instances)
print('stopped your instances: ' + str(instances))
"Start" EC2 Lambda Python Function
import boto3
region = 'us-east-1' # Replace with your desired region
instances = ['i-213sdsfsfsdf'] # Replace with desired EC2 instance ID(s)
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
ec2.start_instances(InstanceIds=instances)
print('starting your instances: ' + str(instances))
About
EC2 Automated Start & Stop automates the starting and stopping of EC2 instances — Saving time, money, and resources by preventing an active service like EC2 from running at all times.