Summary

The tools you need to interact with localstack and links to the tutorials you need to start with.

localstack: Run aws infrastucture locally on your machine or during ci.

Requirements

  • localstack (paid or free)
  • docker
  • python3
  • node
  • npm

localstack-cli

I won’t go through how to setup localstack itself, they document in a clear and straight-forward way.

Go here and follow the install steps, if you have a paid account be sure to authorize your install.

When ready from the terminal:

localstack start

libvirt

If you decide to go down the vm route for ec2, you will need to pass the folder to the localstack docker container

export EC2_VM_MANAGER=libvirt
export DOCKER_FLAGS="-v /var/lib/libvirt:/var/lib/libvirt"

The Tools

To work with localstack as you would directly with aws you need local versions of the tools you’d normally use.

First step is to create a python virtual environment so you don’t interfere with your host.

cd ~
mkdir -p myaws && cd myaws
python3 -m venv venv
source venv/bin/activate

awslocal

Having initialised the virtual environment you can now install a local version of the aws cli

pip3 install awscli-local

If you have your AWS Credentials already on your profile you’ll want to change them to the localstack test account.

export AWS_ACCESS_KEY_ID=test
export AWS_SECRET_ACCESS_KEY=test

You can test it by running

awslocal s3api list-buckets
* Tip

I shorten awslocal to myaws, saves pressing a few keys

alias myaws=awslocal

terraform-local

Still with the python virtual environment enabled, run:

pip3 install terraform-local

Verify with

tflocal version
* Tip
alias mytf=tflocal

cdk-local

The cdk is not a python app, so the is where the node and npm come in

npm install -g aws-cdk-local aws-cdk

Test with

cdklocal --version
* Tip
alias mycdk=cdklocal

Working with localstack EC2

The easiest way to simulate ec2 is with docker, however you will need a pro licence for that.

It links to two ubuntu images, 20.04 and 22.04 and one amazonlinux 2025

For a lot of use cases that’s fine, however lets say you want your ec2 to be based on Debian what do you do. The answer is docker tag

docker pull debian:bookworm
docker tag debian:bookworm localstack-ec2/debian-bookworm:ami-bookworm

With the above you can now use ami-bookworm with either awslocal or tflocal

If you want to know which ami images you have available do the following:

docker images ls | grep localstack-ec2

tflocal

A few tips on running tflocal

tlocal plan -out my.plan

The above will execute a plan and show you any changes that may apply, you may need to modify a default variable which you can do with the following

tflocal plan -var ami_id=ami-df5de72bdb3b -out my.plan

To actual execute the plan do

tflocal apply my.plan

Tearing down

If you’re working with localstack you want to handle the terraform state a little different from production, in regards to wiping the state and starting fresh repeatedly.

Add the tf state files and even the .terraform to your .gitignore. When you’re ready to wipe do:

git clean -xdf

Hint: don’t run the above if you have uncommited files

Tutorials

I could rehash a tutorial from localstack but really, that’s a waste of time, the Localstack documentation has excellent tutorials.

The first tutorial I recommend working through is the S3 Static Website found here.

The next one would be the EC2 Tutorial found here but as of now doesn’t have a terraform step which I may correct myself.