Streamlining AWS Infrastructure with Terragrunt: Deploying EC2 and RDS for Dockerized Applications

Setting up and managing cloud infrastructure can be complex and time-consuming. Terragrunt, a thin wrapper for Terraform, offers a simplified approach to writing and maintaining your infrastructure as code. This article provides a comprehensive guide on using Terragrunt to deploy an Amazon EC2 instance running a Docker container, along with an RDS database, on AWS.

Prerequisites

Before diving into the Terragrunt setup, ensure you have the following prerequisites in place:

  1. AWS Account: Set up your AWS account and obtain your access and secret keys.
  2. Terraform: Install the latest version of Terraform on your local machine.
  3. Terragrunt: Install the latest version of Terragrunt.
  4. AWS CLI: Install and configure the AWS CLI with your credentials.
  5. Docker: Basic understanding of Docker containers and Dockerfiles.

Step 1: Install Terraform and Terragrunt

Terragrunt depends on Terraform. To install Terraform, follow the official Terraform installation guide. After installing Terraform, install Terragrunt by following the instructions on the Terragrunt GitHub repository.

Step 2: Set Up Your Terragrunt Directory Structure

Organize your Terragrunt configuration in a directory structure. For a simple setup, you might have:

/terraform-live
  /dev
    /vpc
      /terragrunt.hcl
    /ec2-instance
      /terragrunt.hcl
    /rds
      /terragrunt.hcl

Each terragrunt.hcl file will contain the configuration for its respective module.

Step 3: Configure Your Terragrunt Files

Within each module directory, create a terragrunt.hcl file. This file tells Terragrunt where to find the Terraform configurations for each module.

For the EC2 instance (/ec2-instance/terragrunt.hcl):

terraform {
  source = "git::https://github.com/terraform-aws-modules/terraform-aws-ec2-instance.git?ref=v2.0.0"
}

include {
  path = find_in_parent_folders()
}

inputs = {
  instance_count         = 1
  ami                   = "ami-0c55b159cbfafe1f0"
  instance_type         = "t2.micro"
  subnet_id             = "subnet-0123456789abcdef0"
  vpc_security_group_ids = ["sg-0123456789abcdef0"]
}

For the RDS instance (/rds/terragrunt.hcl):

terraform {
  source = "git::https://github.com/terraform-aws-modules/terraform-aws-rds.git?ref=v2.0.0"
}

include {
  path = find_in_parent_folders()
}

inputs = {
  identifier = "myappdb"
  engine     = "mysql"
  engine_version = "5.7"
  instance_class = "db.t2.micro"
  allocated_storage = 20
  username   = "admin"
  password   = "yourpassword"
  db_subnet_group_name = "my-db-subnet-group"
  vpc_security_group_ids = ["sg-0123456789abcdef0"]
}

Ensure you replace the placeholders with actual values from your AWS setup (AMI IDs, security group IDs, subnet IDs, etc.).

Step 4: Initialize and Apply Configuration

Navigate to each module’s directory in your CLI, and run the following Terragrunt commands:

terragrunt init
terragrunt apply

terragrunt init initializes the Terraform backend and modules, while terragrunt apply deploys your infrastructure.

Step 5: Verify Deployment

Once the apply command completes, verify the deployment in your AWS Management Console. You should see the new EC2 instance and RDS database instance running.

Conclusion

Terragrunt is an invaluable tool for teams looking to efficiently manage their cloud infrastructure. By following the steps outlined in this guide, you’ve learned how to use Terragrunt to deploy an EC2 instance paired with an RDS database—essential components for running dockerized applications on AWS.

Keywords

  • Terragrunt AWS setup
  • Deploy EC2 with Terragrunt
  • RDS database with Terragrunt
  • Docker on EC2 instance
  • Infrastructure as Code automation
  • AWS Terraform management

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest Comments

No comments to show.