Skip to content

This repository is intended to register an idea to bootstrap a study or QA terraform project using localstack docker container to simulate AWS Services. Also it uses local S3 bucket as backend and local DynamoDB to store state lock.

Notifications You must be signed in to change notification settings

edgar-anascimento/terraform-localstack-setup

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

terraform-localstack-setup

This repository is intended to register an idea to bootstrap a study or QA terraform project using LocalStack to simulate AWS.

The backend is configured to store tfstate in local S3 bucket and to store tfstate lock in local DynamoDB, both running in a LocalStack docker container.

First steps

Follow the next steps to create initial configuration and test if everything is setup correctly.


Install necessary software

To use this local setup you will need the following software available in your machine:

  • docker
  • docker-compose
  • terraform cli
  • awscli

Bring up localstack container

Change to localstack directory and run:
docker-compose up -d


Configure AWSCLI

Create a configuration for AWS with dummy creadentials.
The only important information to localstack is the region.
Credentials validation is actually disabled.
aws configure


Create s3 state bucket and DynamoDB table

Change to main directory and run terraform:
terraform init and terraform apply


Check if s3 bucket was created correctly:

aws --endpoint=http://localhost:4566 s3 ls

2021-01-25 22:05:18 terraform-state

Check if DynamoDB table was created correctly:

aws --endpoint=http://localhost:4566 aws dynamodb list-tables

{
    "TableNames": [
        "terraformlock"
    ]
}

Create a test resource, a tfstate and tfstate lock

Change to dev directory and run terraform:
terraform init and terraform apply


Check if sample resource was created correctly:

aws --endpoint=http://localhost:4566 sqs list-queues

{
    "QueueUrls": [
        "https://localhost:4566/000000000000/sample-queue"
    ]
}

Check if tfstate was stored correctly:

aws --endpoint=http://localhost:4566 s3 ls terraform-state/dev/

2021-01-25 22:17:34       1242 terraform.tfstate

Check if tfstate lock was stored correctly:

aws --endpoint=http://localhost:4566 dynamodb scan --table-name terraformlock --output json

{
    "Items": [
        {
            "Digest": {
                "S": "6295c01f8c53c7a74bfad08f566d20c4"
            },
            "LockID": {
                "S": "terraform-state/dev/terraform.tfstate-md5"
            }
        }
    ],
    "Count": 1,
    "ScannedCount": 1,
    "ConsumedCapacity": null
}

Extra hints

In order to not get annoyed on having to always provide --endpoint=http://localhost:4566 in your study machine, you can do the following:

  • Create an alias that will always point to the local enpoint as:
    alias awslocal='aws --endpoint-url http://localhost:4566'.
  • Put the alias in your .bashrc or .zshrc to have it permanent in your terminal session.
  • If you create the alias named as "aws" you can also make use of awscli autocomplete with your localstack endpoint.

About

This repository is intended to register an idea to bootstrap a study or QA terraform project using localstack docker container to simulate AWS Services. Also it uses local S3 bucket as backend and local DynamoDB to store state lock.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages