Optimizing Cost and Performance in the Cloud: Strategies for Efficient Resource Management
Tags: aws, cloud, tutorial
Published: 2023-07-14 03:37:29


In the past few months, I have been learning and interacting with the Well Architected Tool in the AWS Console, this added to performing Well Architected Reviews on several customers has shown me the real impact and importance of efficiently managing resources in the cloud and how this is a critical aspect of successful cloud operations. Currently, organizations strive to strike a balance between optimizing costs and delivering high-performance services to meet customer demands. In this blog post, we will explore strategies and best practices for optimizing cost and performance in the cloud. By implementing these techniques, you can maximize resource utilization, reduce unnecessary expenses, and ensure optimal performance for your applications and workloads.

In this article I will first outline the significance of resource management in cloud environments, from cost reduction to ensuring optimal performance, and how it affects overall business growth.

Then, we will have a brief introduction to AWS various resource management tools, like AWS Cost Explorer, AWS Budgets, AWS Trusted Advisor, and AWS CloudWatch, which are instrumental in achieving cost and performance optimization.

Finally we will have some use cases for this services and how they can help us achieve substantial savings while providing performance improvement across our enviroments.

**Understanding the Importance of Efficient Resource Management**

- Cost Reduction: Efficient resource management in cloud environments is crucial for several reasons.
- Without effective resource management, companies might pay for unused or underutilized resources, leading to unnecessary costs.
- Ensuring Optimal Performance: Resource management is not just about cost; it's equally about performance.
- Business Agility: Efficient cloud resource management allows organizations to become more agile.
- Knowing the cost and utilization of resources helps businesses plan for future growth or contraction.
- Sustainability: By optimizing resource usage, businesses also contribute to environmental sustainability.

**AWS Resource Management Services**

AWS Cost Explorer: AWS Cost Explorer is a tool that enables users to view and analyze their costs and usage over time. With Cost Explorer, you can filter graphs by values such as API operation, Availability Zone, AWS service, custom cost allocation tag, and more.

AWS Budgets: This tool gives you the ability to set custom cost and usage budgets that alert you when your costs or usage exceed (or are forecasted to exceed) your budgeted amount.

AWS Trusted Advisor: This is an online resource to help you reduce cost, increase performance, and improve security by optimizing your AWS environment. Trusted Advisor provides real-time guidance to help you provision your resources following AWS best practices.

AWS CloudWatch: Amazon CloudWatch is a monitoring service for AWS resources and the applications you run on AWS. You can use CloudWatch to collect and track metrics, collect and monitor log files, set alarms, and automatically react to changes in your AWS resources.

**Practical Use Cases**

**_Right-Sizing_**
 
Calculating our application's actual demand can be tricky sometimes.The goal of right-sizing is to eliminate wasted resources and minimize costs while still meeting the application's performance requirements. For instance, you may start with an EC2 instance with high computing power.However, in time you may see with the help of other AWS tools as CloudWatch, CostExplorer or Compute Optimizer you can perform analysis on your usage patterns and discover that you can achieve the same performance with a less expensive instance type, therefore optimizing costs. 

For example I have an instance that has been running for some time, even though demand is consistent CloudWatch reveals that the average CPU usage during the last 3 months has not exceeded 35%.But we also should look at different metrics as RAM usage, EBS volume throughput, network troughtput to have a good understanding what metrics of our instance are good enough for our workload and which ones are oversized or undersized.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lnzwzkmg2o0sqsergwrl.png)

As we know RAM usage tracking is not available in the default metrics provided by EC2, to achieve this we have to install the CloudWatch Agent on this instance, which we I did a month ago to see if my instance is in fact oversized. Let's see how is the memory usage.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5tvmo74dl0mw07v5g4p0.png)

RAM usage did not exceed 30% usage in the last month.Using the metrics provided by CloudWatch we could say our instance is oversized, but let's take a look at another tool AWS provides to help rightsize our instances.

Compute Optimizer gathers all this metrics along with the price of our instance, and analyzing usage trends generates recommendations determining if our instance is over-provisioned or under-provisioned.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4v7br1ejo81d0n3oi3nl.png)

In this case, it has determined that my instances is over-provisioned. Another important feature of Compute Optimizer is Enhanced infraestructure metrics. This feature is a paid one, that will store up to 3 months worth of Cloud Watch metrics history and make the recommedations based on that period against the base version that only stores up to 14 days worth of metrics. The cost per instance comes up to $0.25 USD per month.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/30w4rds9f3w3nm6nkh70.png)

Compute Optimizer provides us with recomendations showing us the price we currently pay, in my case i have a m5.xlarge but with the utilization my instance currently has I could switch to a t3.xlarge and save up to 36.2% but lets notice that there may be medium performance risk on that option compared to the c6i.xlarge and the c5.xlarge options that the risk is very low. As always we should always consider how predictable or unpredictable our instance has and we can compare options with the metrics as shown in the next picture to take the best decision.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5bgn6va4r94parb52vq6.png)
 
Here you could think that there is just one line but is actually both current and option 1, so they will have the same CPU utilization against the same load.Option 2 would have slightly higher CPU utilization as shown in the next picture.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hljo5nc7zzqhn33vdh2d.png)

So it really comes to gathering all the metrics and data required to take the best decision and be able to optimize costs while maintaining the same performance.


**_Cost Allocation Tagging_**

Tagging, on the other hand, enables you to categorize and track your AWS costs. When you apply tags to your AWS resources (like EC2 instances or S3 buckets), AWS generates a cost allocation report with your usage costs broken down by tags. This granularity allows you to see exactly where your money is going and makes it easier to optimize in the future. For example, you can use tags to label resources by department or project, allowing you to accurately track and allocate costs accordingly.

This strategy can also be enforced on an organizational level using SCPs to enforce certain tags when creating resources and denying resource creation that does not comply with specific tag requeriments. 

This associated with a tag policy at an organizational level can provide with a strong tagging culture that will allow you to track costs granularly and also have responsibles for every resource that is created in your organization.

Remember to always test thoroughly SCPs and tagging policies before releasing them in productive enviroments as they can disrupt resource creation.

AWS provides with a little example to see a good tag allocation can look like to track your costs.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rbdsaaidp8xmwobudzfv.png)

To provide you with a simple example lets define a taxonomy for a fake organization, lets say we will require from now on our EC2 instances, EBS volumes, CloudFormations Stacks, RDS Databases and ECS Cluster to be tagged with 3 tags: ENVIRONMENT, PROJECT and OWNER. Remember, tags are case sensitive and while you can enforce uppercase tags it is best to have a tagging culture in your organization to prevent messy tags.

So first we have to define a Tagging Policy like the one in this image:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mad1f686ekubhwadt3ad.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bl8ivga1hnwqclm3ous0.png)

As you can see, is enforced for the resources we mentioned earlier, an also we can define some values that the tag will accept as in ENVIRONMENT only values like: DEV, QA and PROD.

And now to enforce this policy across our organization we can use a SCP to prevent resources from being launched without having these tags.The SCP will look like this:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6u0g87vqjqjq2346nhrv.png)

And when we try to create for example and EBS volume and no tag is entered we encounter the following error: 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/scho7og5jh448wt3403g.png)

As I mentioned earlier, we can enforce uppercase in tag values and certain values, for example DEV in lowercase is not accepted and not other value in ENVIRONMENT is accepted.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0hji8htlyfq4xtz2imyy.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iuvgi34nfs669k0kdg0j.png)

Only when we enter the accepted values for ENVIRONMENT the resource is created.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sz1mefps53fwhmpygbwq.png)


In conclusion, what we have seen today is two strategies that can help us achieve cost optimization while maintaining performance and having trackable resources making them more manageable.
These strategies used together can accomplish great effect in your organization and take your worload to the next level.

Next time we will keep digging on more strategies like the power of the savings plans or reserved instances and automating and scheduling tasks for optimization

See you on the next post! Happy architecting!