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.
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
.
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))
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)
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
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
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!
- Make sure you have the correct Cron expression arrangement/configuration.
- Make sure to disable schedules when no longer in use.
- 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))