DEV Community

Cover image for How to cut down the Costs of Running AWS servers (Elastic Compute)
Gauthier Kwatatshey for AWS Community Builders

Posted on • Edited on

How to cut down the Costs of Running AWS servers (Elastic Compute)

Let's save some money by optimizing the use of any applications hosted in ec2

1. Introduction

It is painful to start and stop instances manually on a daily basis. Let's create Lambda functions leavraging python boto3 with the CloudWatch rule and event (EventBridge) which automatically starts and stops servers based on the predefined time in EventBridge rule

2. Resources

2.1. AWS LamBda Function

AWS Lambda is a serverless, event-driven computing solution that allows you to run code for almost any form of application or backend service without the need for server provisioning or management.

You may call Lambda from more than 200 AWS services and software-as-a-service (SaaS) applications, and you only pay for what you use.

When you deploy your code to AWS Lambda, an executable package is created that is executed whenever the Lambda function is called.

In our scenario, we will import the Boto3 Library to activate the Step Function.

2.2. Elastic Compute (EC2)

Amazon Elastic Compute Cloud (EC2) is a cloud computing platform provided by Amazon.com that allows users to rent virtual computers on which to run their own computer applications.

EC2 promotes scalable application deployment by providing a web service through which a user can boot an Amazon Machine Image (AMI) to configure a virtual machine, referred to as a "instance" by Amazon, containing any software desired.

The term "elastic" refers to the ability of a user to build, launch, and terminate server instances as needed, paying by the second for active servers. EC2 gives users flexibility over the geographical location of instances, allowing them to optimize latency and achieve high levels of redundancy.

2.3. Amazon EventBridge

Amazon EventBridge is a serverless, fully managed, and scalable event bus that allows you to integrate AWS services, SaaS, and your applications.

EventBridge was previously known as Amazon CloudWatch Events. The core service and API for Amazon CloudWatch Events and EventBridge are the same; however, EventBridge has many more features.

2.3. Additional resources

2.3.1. CloudTrail

Your new AWS account comes with CloudTrail enabled by default. An event on CloudTrail is created whenever something happens in your AWS account. Within the CloudTrail console, by selecting Event history, you may quickly view recent occurrences. Create a trail or an event data store to keep a running log of all activity and events in your AWS account.

2.3.2. DynamoDB

Terraform state locking using DynamoDB

Image description

2.3.3. S3

Amazon S3 store the state as a specified key in a specified bucket (Bakend store terraform remote state). The dynamodb_table parameter can be set to the name of an existing DynamoDB table to enable state locking and consistency checking using Dynamo DB, which is another feature supported by this backend. Multiple remote state files can be locked using a single DynamoDB database. The values of the bucket and important variables are included in the key names that Terraform creates.

3. Implementation

AWS Lambda Functions, AWS EventBridge, Python boto3 . Using this method, You can schedule the start and stop of your EC2 instances by using tags on target instances.

The following are some advantages of utilizing scheduler on EC2:

  • Access the instance as per the working hours.
  • Reduce costs.
  • Stop instance from loading up on weekends.
  • The time limit for employees for working on the instance.

4. Architecture

Image description

5. Logic of the Python Code

Returning All EC2 instances(InstanceID) tagged with the tag Auto-Start-Stop will be started each working day from Monday to Friday at 8:00 am UTC Which is 9:00 and will stopped each working day from Monday to Friday evening at 6:00 pm UTC

6. File Structure

.
├── README.md
├── backend.tf
├── cloudtrail.tf
├── dynamodb.tf
├── eventbridge.tf
├── iam.tf
├── images
│   └── Auto-Start-Stop-Architecture.drawio.png
├── lambda.tf
├── lambda_start
│   ├── lambda_handler.zip
│   └── src
│   └── main_lambda.py
├── lambda_stop
│   ├── lambda_handler_1.zip
│   └── src
│   └── main_lambda_1.py
├── providers.tf
├── tags.tf
├── terraform.tfstate
├── terraform.tfstate.backup
├── terraform.tfvars
└── variables.tf

6 directories, 18 files

6.1. Lambda Python preview

Image description

7. Adjustment

Since the solution is deployed using Terraform, the schedule time adjustment should also be carried out using Terraform. However, you can also adjust the schedule time directly on the AWS console, however this is not really advised. To avoid any inconsistency or and duplicate resources, it is recommended that Terraform be used to apply the changes.

8. Remediation

Simply remove the tag "Auto-Start-Stop" from the target EC2 instances manually.

9. Deployment pre-requisites

  • Terraform CLI installed
  • IAM Role/IAM User with the enough permissions.
  • AWS CLI installed
  • Git Installed

10. Deployment Steps

git clone https://github.com/Assassin010/auto-start-stop-instances.git

git clone git@github.com:Assassin010/auto-start-stop-instances.git

Switch to the following directory => auto-start-stop

terraform init

terraform plan

terraform apply --auto-approve
Enter fullscreen mode Exit fullscreen mode

To completely decommission the solution

terraform destroy --auto-approve 
Enter fullscreen mode Exit fullscreen mode

11. Summary

So far, we've seen the utility of Event-Driven infrastructure, how services respond to events, and a use case in which a serverless compute service runs based on storage events and notifies a user via email. In addition, we used Infrastructure as a Code (IaaC) by using terragrunt + terraform to create and destroy all resources.

12. References

https://aws.amazon.com/ec2/
https://aws.amazon.com/lambda/
https://www.terraform.io
https://aws.amazon.com/cloudtrail/
https://aws.amazon.com/s3/
https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-what-is.html


Happy coding!
END - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

If you like my work and want to support me…
The BEST way is following me on dev.to here

Feel free to give likes, or writing comments so I know how helpful this post was for you.

Gauthier Kwatatshey__
Connect with me on LinkdIn

Medium

Top comments (2)

Collapse
 
prasadkpd profile image
Prasad Lakshan

Thank you for sharing this valuable content

Collapse
 
assassin010 profile image
Gauthier Kwatatshey

Happy to help✌🏾