DEV Community

Cover image for My Journey with AWS LocalStack and Terraform: A Local AWS Environment Setup
marocz
marocz

Posted on

My Journey with AWS LocalStack and Terraform: A Local AWS Environment Setup

Introduction

In the realm of cloud computing, gaining hands-on experience is pivotal. However, it can sometimes be expensive or complex to do this on real AWS environments. That's where AWS LocalStack paired with Terraform comes into the picture. This setup allows you to mock AWS resources locally, providing a cost-effective and straightforward way to test your cloud applications. In this post, I'll share how I set up a local AWS environment using Terraform and LocalStack to manage resources like S3, EC2, and ECR.

Prerequisites

  • AWS CLI
  • Docker
  • Terraform
  • LocalStack

Step 1: Installing LocalStack

Ensure Docker is running on your machine, then install LocalStack using pip:

pip install localstack
Certainly! Below is your post formatted according to the Dev.to markdown styling:

Enter fullscreen mode Exit fullscreen mode

markdown

Step 2: Configuring LocalStack

Create a docker-compose.yaml file with the following content to configure the services you'll need:

version: '3.1'
services:
  localstack:
    image: localstack/localstack
    ports:
      - "4566:4566"
      - "4571:4571"
    environment:
      - SERVICES=s3,ec2,ecr
      - DEBUG=1
      - DATA_DIR=/tmp/localstack/data
    volumes:
      - "./tmp/localstack:/tmp/localstack"
Enter fullscreen mode Exit fullscreen mode

Step 3: Launching LocalStack

To launch LocalStack, run the following command:

docker-compose up
Enter fullscreen mode Exit fullscreen mode

Step 4: Prepping Terraform

Construct a main.tf file with the subsequent content to establish the AWS resources using Terraform:

provider "aws" {
  endpoints {
    s3      = "http://localhost:4566"
    ec2     = "http://localhost:4566"
    ecr     = "http://localhost:4566"
  }
  region                      = "us-east-1"
  skip_credentials_validation = true
  skip_metadata_api_check     = true
  skip_requesting_account_id  = true
}

resource "aws_s3_bucket" "my_bucket" {
  bucket = "my-bucket"
}

resource "aws_ec2_instance" "my_instance" {
  ami           = "ami-abcdef01"
  instance_type = "t2.micro"
}

resource "aws_ecr_repository" "my_repo" {
  name = "my-repo"
}
Enter fullscreen mode Exit fullscreen mode

Step 5: Applying Terraform Configuration

Now, initialize Terraform and apply the configuration:

terraform init
terraform apply
Enter fullscreen mode Exit fullscreen mode

Conclusion

This setup has become a part of my daily workflow, enabling me to test and develop cloud applications locally with ease.

The amalgamation of Terraform and LocalStack provides a robust platform to mock AWS resources, significantly smoothing the development and testing phases.

Feel free to adapt this setup to your needs and explore other AWS resources available in LocalStack. Happy coding!

Note: Ensure you replace placeholder values like "ami-abcdef01" and "my-bucket" with your specific values or references.

Image description

Top comments (0)