
Reduce AWS EC2 Cloud Spend by Starting and Stopping Instances with Lambda Functions



Apr 29, 2025
Cloud costs can quickly grow out of control. Although cloud providers include built-in compute optimization tools, these solutions don't always offer the flexibility to adapt to specific business needs.
Native tools often lack detailed scheduling features, struggle to manage dynamic workloads, and may require additional licensing for advanced automation. For CEOs, this translates to wasted expenses and inefficient use of resources.
Companies can use AWS Lambda to automate the starting and stopping of EC2 instances based on tags. This customized approach ensures that compute resources are available when required without incurring costs during idle times. It also helps optimize cloud expenses, enhance operational efficiency, and enforce governance policies while maintaining agility and control over infrastructure.
In this post, we’ll explain how you can use AWS Lambda to start and stop EC2 instances.
Prerequisites
-
AWS Account with permissions to create Lambda functions, EC2 instances, and IAM roles.
-
EC2 instances with specific tags that will be used to start or stop them.
-
AWS CLI and AWS Management Console access.
Step 1: Define Tags for Your EC2 Instances
The Lambda function will identify EC2 instances based on their tags. Add tags to the EC2 instances you want to control. For example, you can tag instances with:
-
Key: AutoManage
-
Value: True
You can assign different values for different environments or use additional tags, such as Environment=Production
or Environment=QA
.
Step 2: Create an IAM Role for Lambda
Your Lambda function needs permission to interact with EC2 instances. To allow that, create an IAM role with appropriate permissions.
-
Go to the IAM Console and create a new role.
-
Choose AWS Lambda as the trusted entity.
-
Attach the following policies:
-
AmazonEC2ReadOnlyAccess
-
AmazonEC2FullAccess
-
-
Name the role something like
Lambda_EC2_Control_Role
and create the role.
Step 3: Create the Lambda Function
Now, let's create the Lambda function that will handle starting and stopping instances.
-
Go to AWS Lambda in the AWS Management Console and click Create function.
-
Select Author from scratch.
-
Name your function, e.g.,
EC2_Toggle_Function
. -
Choose Python 3.9 as the runtime, or any other supported language.
-
Choose the IAM role created earlier (
Lambda_EC2_Control_Role
). -
Click Create function.
Step 4: Add the Lambda Code
In the Lambda console, add the following Python code to start and stop EC2 instances based on tags.
import boto3
import os
# Initialize EC2 client
ec2 = boto3.client('ec2')
def lambda_handler(event, context):
action = event.get('action', 'start') # Default action is start
tag_key = 'AutoManage' # Tag key to filter instances
tag_value = 'True' # Tag value to filter instances
# Filter instances by tag
instances = ec2.describe_instances(
Filters=[
{'Name': f'tag:{tag_key}', 'Values': [tag_value]},
{'Name': 'instance-state-name', 'Values': ['stopped' if action == 'start' else 'running']}
]
)
instance_ids = []
for reservation in instances['Reservations']:
for instance in reservation['Instances']:
instance_ids.append(instance['InstanceId'])
if not instance_ids:
print(f"No instances to {action}.")
return {"status": "No instances to process"}
# Perform the action (start or stop)
if action == 'start':
ec2.start_instances(InstanceIds=instance_ids)
print(f"Starting instances: {instance_ids}")
elif action == 'stop':
ec2.stop_instances(InstanceIds=instance_ids)
print(f"Stopping instances: {instance_ids}")
return {"status": f"{action.capitalize()} instances", "instances": instance_ids}
Step 5: Add Environment Variables (Optional)
To make the Lambda function more flexible, you can add environment variables for tags and actions.
-
In the Configuration tab, under Environment variables, add:
-
TAG_KEY:
AutoManage
-
TAG_VALUE:
True
-
This will allow you to change tag values without needing to modify the code.
Step 6: Test the Lambda Function
Now, test the Lambda function to make sure it works as expected.
-
Click Test in the Lambda console.
-
Choose Create new test event.
-
Set the test event to pass an action to the function:
{ "action": "start" }
-
Save the test event and click Test.
The Lambda function will search for EC2 instances with the tag AutoManage=True and start them. You can change the action to "stop" to stop the instances.
Step 7: Automate with CloudWatch Events (Optional)
You can schedule this Lambda function to run automatically using Amazon CloudWatch Events.
-
Go to CloudWatch and click Rules under Events.
-
Click Create rule.
-
Set the event source as Schedule, and configure it using cron or rate expressions.
-
For the target, choose your Lambda function.
-
Configure input to pass the desired action (start or stop) to the Lambda function:
{ "action": "start" }
You can create two separate CloudWatch rules: one to start instances in the morning and another to stop them in the evening.
Step 8: Monitor and Log
Lambda automatically logs to Amazon CloudWatch Logs. You can monitor the execution of your Lambda function from there and verify that the instances are starting and stopping as expected.
Wrapping Up
Matching automation of cloud resources with actual business needs can save enterprises a lot of money and improve efficiency. The Lambda function can even be customized to handle different environments or actions, making it a versatile tool for optimizing costs and managing infrastructure.
Oshyn is a leading digital experience implementation agency and solution partner with Adobe, Optimizely, and Sitecore. We can help you manage cloud resources and continue delivering value for your customers. Contact us to learn how.
Related Insights
-
-
Leonardo Bravo
Optimizing Your AWS Bill
Rightsizing, Scaling, and Scheduling for Efficiency
-
Erick Espinoza
-
-