Optimizing AWS Infrastructure: Leveraging Terraform for Low Coupling and High Cohesion

itsnarayan
4 min readJul 18, 2023

--

Cohesive Architectures with Low Coupling

Introduction

Managing and optimizing AWS infrastructure can be challenging as complexity grows. By adopting a strategic approach that embraces low coupling and high cohesion, you can achieve significant improvements in resource management and cost optimization.
In this blog, we explore how Terraform enables efficient, modular deployments that provide control and cost savings. We address the challenges of traditional deployments and discuss the step-by-step process of transitioning to a modularized architecture.

The Challenge: Complex and Interdependent Architecture

Initially, our infrastructure deployment approach was straightforward and monolithic. We deployed all microservices resources within a single module, as shown in the diagram below:

Low Cohesion Strong coupling 😏

At that time, with a small team, we initially chose not to create separate staging environments for individual developers. However, as our team grew and the need for concurrent development and testing arose, we recognized the value of providing dedicated staging environments for each developer as needed.

By creating separate staging environments, developers were able to deploy their changes and test them independently, without being blocked or delayed by the progress of other tasks. This practice facilitated parallel development and improved overall efficiency in the development process.

As the number of services and team members increased, managing staging environments presented significant challenges, including resource waste, increased costs, and added burden on DevOps teams. Let’s explore the difficulties we encountered:

  • Manual Resource Deletion: Staging environments deployed all web services, including unnecessary ones, requiring manual deletion of unused resources.
  • Redeployment of Manually Deleted Resources: Changes to task definitions, or variables resulted in the recreation of previously deleted resources(for cost-cutting), leading to repetitive and time-consuming tasks.
  • Cost Impact: Deployment of unnecessary resources increased infrastructure costs and hindered effective cost optimization efforts.
  • Burden on DevOps Teams: Manual cleanup efforts and redeployment tasks consumed valuable time and energy, diverting focus from more valuable activities like automation and optimization.

The Solution: Introducing Modularization

High Cohesion Low coupling 😍

To address the challenges mentioned earlier, we implemented a combined solution involving modularization and user input flags within our Terraform code framework.

1. Modularization
Isolated ECS service components into separate modules. Each service component, such as task definitions, log groups, RDS instances, and ECR repositories, resides in its own module. This modular structure enhances organization, encapsulation, and maintainability of the infrastructure code.

Production block invokes all service modules

2. User Input Flags for Staging Environments
Introduced user input flags specifically for staging environments. These flags serve as conditions when calling service-specific modules, enabling the selective inclusion or exclusion of services based on user input. This approach provides fine-grained control, ensuring that only necessary services are provisioned during staging environment deployments.

Staging block invokes modules based on conditional flags.

The following screenshot depicts the bitbucket pipeline options when creating or modifying the staging environment.

Staging Pipeline with user input flags

Benefits of the Combined Approach

The combination of modularization and user input flags brings several benefits:

Granular Control in Staging Environments: User input flags enable developers and operators to choose which services to include or exclude during staging environment deployments. This level of control ensures that only essential services are provisioned, reducing unnecessary resource allocation and facilitating targeted testing and validation.

Simplified Resource Management: By isolating service components into separate modules, we achieve clear separation of concerns. This simplifies resource management by reducing interdependencies and making it easier to modify, update, or scale individual components without impacting others.

Improved Cost Efficiency: Selective deployment of services in staging environments based on user input flags significantly reduces resource waste. Unnecessary resources are not provisioned, resulting in cost savings and optimized resource utilization.

It’s important to note that for production deployments, user input flags are not utilized, and all services are deployed as necessary to ensure a consistent and controlled deployment process in the production environment.

By leveraging the combined power of modularization and user input flags, we achieved a streamlined and efficient deployment process for staging environments, gaining greater control over our infrastructure and improving overall cost efficiency.

Conclusion

In conclusion, adopting a low coupling and high cohesion approach in AWS infrastructure management using Terraform, modularization, and user input flags yields significant benefits.

DevOps engineers are encouraged to embrace these strategies to gain better control over resource allocation, reduce wasteful provisioning, and improve cost efficiency.

In summary, optimizing AWS infrastructure with low coupling and high cohesion principles leads to efficient resource management and cost savings.

--

--

itsnarayan

DevOps Engineer | AWS SAA-C03 | CKA | CKAD | Kubernetes | Docker | Terraform | Git | Japanese N2 Certified