DEV Community

Cover image for Deploying Multi-Cloud Infrastructure with Terraform Modules
Otu Udo
Otu Udo

Posted on

Deploying Multi-Cloud Infrastructure with Terraform Modules

Building an Infrastructure-as-Code Solution with Terraform Modules: Deploying EKS Clusters and Kubernetes Applications

Managing modern cloud infrastructures efficiently requires robust tools and practices. Terraform, with its support for reusable modules, empowers engineers to create scalable, maintainable, and version-controlled cloud resources. In this blog post, I’ll share how I used Terraform to deploy an Amazon Elastic Kubernetes Service (EKS) cluster and a Kubernetes application module into AWS, leveraging a default Virtual Private Cloud (VPC). This project highlights the advantages of modular Terraform configurations for infrastructure-as-code (IaC).


The Challenge

Deploying and managing Kubernetes clusters in the cloud is a complex task. It involves provisioning the control plane, configuring worker nodes, managing networking, and ensuring compatibility with application workloads. To streamline this process, I:

  1. Used Terraform’s EKS module to provision an Amazon EKS cluster.
  2. Created a Kubernetes application module for deploying workloads onto the EKS cluster.
  3. Integrated both modules with AWS’s default VPC, maintaining simplicity and scalability.

Why Terraform Modules?

Terraform modules help break down infrastructure code into smaller, reusable, and manageable units. Key benefits include:

  • Reusability: Modules encapsulate common configurations.
  • Consistency: Ensures resources are provisioned with standardized practices.
  • Simplified Maintenance: Updates to a module automatically propagate to all instances.

Architecture Overview

The infrastructure consists of:

  1. Terraform:

    • EKS Module: Provisions the Kubernetes cluster.
    • Kubernetes App Module: Deploys workloads onto the EKS cluster.
  2. AWS Cloud:

    • Default VPC with subnets and Internet Gateway.
    • Amazon EKS Cluster: Managed control plane and worker nodes.
    • Networking: Handles pod communication and external access.
  3. Kubernetes:

    • Workloads deployed using Kubernetes manifests.
    • Services exposing applications externally.

Implementation

1. Terraform EKS Module

The EKS module provisions the cluster, including:

  • Control Plane (managed by AWS).
  • Worker Nodes in an Auto Scaling Group.
  • Networking configuration to use the default VPC.
module "eks_cluster" {
  source = "path/to/eks_cluster_module"


  name = var.cluster_name

  min_size     = 1
  max_size     = 2
  desired_size = 1

  instance_types = ["t3.small"]
}
Enter fullscreen mode Exit fullscreen mode

2. Kubernetes App Module

The Kubernetes app module deploys a simple web application into the EKS cluster. It defines the number of replicas, container image, and environment variables.

module "simple_webapp" {
  source = "path/to/k8s_app_module"

  name           = "simple-webapp"
  image          = "training/webapp"
  replicas       = 2
  container_port = 5000

  environment_variables = {
    PROVIDER = "Terraform"
  }
}
Enter fullscreen mode Exit fullscreen mode

3. Default VPC

Using AWS’s default VPC eliminates the need for custom VPC configurations. The modules are configured to:

  • Use public subnets for worker nodes and load balancers.
  • Ensure pod-to-pod communication inside the cluster.
  • Route ingress/egress traffic via an Internet Gateway.

Diagram

diagram

Here’s a high-level view of the architecture:

  • Terraform provisions:
    • EKS cluster in the default VPC.
    • Kubernetes workloads using the Kubernetes app module.
  • AWS Components:
    • Default VPC with subnets and Internet Gateway.
    • EKS control plane and worker nodes.
  • Kubernetes Layer:
    • Pods, Deployments, and Services.

Deployment Steps

  1. Prepare the Environment:

    • Install Terraform and configure AWS credentials.
    • Ensure kubectl is installed and configured for EKS access.
  2. Provision Infrastructure:

    • Execute terraform init to initialize the modules.
    • Run terraform apply to create resources.

Image description

  1. Verify Deployment: Wait a little while for the web app to spin up and pass health checks, and then test out the service_endpoint:

Image description

  • Check EKS cluster status using kubectl get nodes.
  • Verify the Kubernetes application is running: kubectl get pods.

Challenges and Lessons Learned

  1. Kubernetes Version Compatibility:

    • Ensure the EKS module uses a supported Kubernetes version (e.g., 1.27).
  2. Image Deprecation:

    • Upgraded container images to comply with OCI standards.
  3. Networking Configurations:

    • Default VPC simplifies networking, but may need custom configurations for production-grade deployments.

Conclusion

This project highlights the power of Terraform modules in managing cloud infrastructure efficiently. By modularizing configurations for EKS and Kubernetes apps, I achieved a scalable, reusable, and maintainable setup. The approach is ideal for teams looking to standardize IaC practices while leveraging AWS and Kubernetes capabilities.

Have you implemented similar Terraform-based solutions? Share your experiences in the comments!

Top comments (0)