Introduction

If you, like us, have a development environment where you test your application before releasing it into production, then this article is for you.
Find out how to save 60% of the cost of the development environment.

The problem

A development environment is a parallel environment to the production one, where the developers can test that the application is working and that the newly developed changes have not broken anything.

It tends to be an environment that developers need during the workday. And here a silly question may arise in itself, does it make sense to keep the development environment active 24 hours a day? What if you only activate it during the workday?

The solution

The answer is simple: if the development team uses the development environment in the same time slot, then it makes sense to turn on the development environment only during the working day, for example, from 7 a.m. to 7 p.m. (staying wide, considering that some developers may start earlier and others may finish after classic working hours). If your company does not work on Saturdays and Sundays, you can turn off the environment on these days as well.

We will shortly give an example on how to do this with AWS.

But how much do you save concretely?

Let’s do some simple maths together: let’s consider a 30-days month, with 4 weekends (so 4 Saturdays and 4 Sundays), and let’s assume that the development environment costs 1€ per day (left running 24 hours a day).

Then without the solution described above, the monthly cost is: 1 €/day * 30 days = 30 €.

Now suppose we activate the development environment from 7 am to 7 pm, so for a total of 12 hours, exactly half of a day. In this case, the daily cost of the development environment drops to €0.50.

Suppose we do not activate the development environment on Saturdays and Sundays, since no developer works. Then from 30 days per month we remove 4 Saturdays and 4 Sundays, for a total of 22 working days.

So the final cost with the above solution results: 0.50 €/day * 22 days = 11 €

We went from 30 € to 11 € with a savings of 63 %, saving 19 €.

I want it now, how is it done?

Take AWS in analysis, the same solution is applicable for all other cloud service providers using their equivalent services.

What do we need?

Let’s make a shopping list and see what we need to realize this solution:

  1. We definitely need something to turn on the various services on AWS and definitely something to turn them off;
  2. We need something to automatically schedule what we created in step 1, so that every morning it turns services on and every evening it turns everything off.

What services can I use?

To meet our requirements, we need only two simple services provided by AWS:

  • We can use AWS Lambda to turn various services on and off;
  • We can use Amazon EventBridge to schedule lambda functions and thus turn services on in the morning and off in the evening.

Enough talk, let’s do an example!

Let’s take an example assuming we have an EC2 instance.

For other AWS services, only the code related to the individual service changes, but the logic behind it is the same.

EC2 autostart

First we need to create a role that has permissions to access the EC2 instance to then assign to the lambda function.

Creating a role for the lambda function

Once logged into the AWS console we use the IAM service and then click on Roles. Then on Create Role.

After that we select AWS Service as the Trusted entity type and Lambda as the Use Case. Then we click on Next.

Create IAM Role

We then choose AmazonEC2FullAccess as the policy to associate with the role.

And finally we associate a name with the role, let’s say example-role.

Role finalization

Creating the lambda function

We use the AWS Lambda service and create a new function by clicking Create Function. We create a function with the template Author from scratch, then specify a name (for example example-lambda-autostart) and choose Python 3.9 as the runtime. We then click on Change default execution role and use the role created earlier (in our case example-role). Finally we click on Create Function. Below is an example:

Lambda creation

Then we add the following code:

import boto3
import os

def start_ec2():
    region=os.environ['REGION']
    dev_instance=os.environ['EC2_DEV_INSTANCE']
    
    client = boto3.client("ec2", region_name=region)
    
    # start dev instance
    client.start_instances(
        InstanceIds = [dev_instance]
    )

def lambda_handler(event, context):
    start_ec2()

And we click on Deploy.

In order to configure environment variables we click on the Configuration tab and then Environment variables. Then we add the following variables:

  • REGION = [name of your region].
  • EC2_DEV_INSTANCE = [id of your ec2 instance (Instance ID)]

Configuring EventBridge

To be able to automatically run the lambda function at a certain time on a certain day, you can use EventBridge, adding a schedule.

So we go to the EventBridge service. Then we click on Create rule.

We enter a name and select the Schedule mode. We click on Next.

Event Bridge rule creation

We define how often to run the lambda function. Suppose we run the function every morning at 7 a.m., only on weekdays (so Monday through Friday). Then we enter in cron the following value: 0 7 ? * MON,TUE,WED,THU,FRI *. We then click on Next.

NB: The cron will run at UTC time, so consider a possible time zone.

Event Bridge rule schedule

We now select which type of service to run each morning at 7 a.m. In our case the lambda function created earlier. Then we select the Lambda Function service and indicate the name of our function. Then we click Next.

Lambda setting in Event Bridge rule

In the section on tags we click on Next.

Setting Event Bridge rule tags

And finally we click on Create Rule.

Event Bridge rule finalization

Great! At this point, every morning at 7 a.m. from Monday through Friday, the lambda function we indicated will be executed.

Autostop of EC2

Lambda function

The process is identical to the one just described except for the EventBridge code and time. So let’s create a new lambda function (with the same role created earlier). The code to be executed is as follows:

import boto3
import os

def stop_ec2():
    region=os.environ['REGION']
    dev_instance=os.environ['EC2_DEV_INSTANCE']
    
    client = boto3.client("ec2", region_name=region)
    
    # stop dev instance
    client.stop_instances(
        InstanceIds = [dev_instance]
    )

def lambda_handler(event, context):
    stop_ec2()

EventBridge

We then create a new rule on EventBridge, this time instead we set the time to be 7 p.m. Monday through Friday. So in the cron field we enter: 0 19 ? * MON,TUE,WED,THU,FRI *.

Conclusion

We have seen a simple example on how to turn on and off an EC2 instance in a way that saves money regarding the development environment. Chiaramente è possibile applicare lo stesso concetto a tanti altri servizi di AWS, per esempio ECS, RDS, LoadBalancer, ElasticCache, etc…

If you would like to see more examples of other AWS services write to us in the comments!

Also write us what you think about it and if you have also tried to save 60% of the development environment costs.

Extra

If you want to apply the same concept seen above to the RDS service, you can find this article on the AWS blog where they explain very well how to do it.

The link is to the article is: Schedule Amazon RDS stop and start using AWS Lambda