With my recent projects, I’ve been working more and more with terraform. LocalStack is a collection of AWS-compatible services you can run yourself. It’s meant for development and testing, not as a target to host real production instances of your application.
It doesn’t cover 100% of the AWS alphabet soup, but it implements many of the most popular services.
Today, I want to show you how to setup a few simple resources and deploy a lambda to Localstack using just your local setup.
All of the examples below are in the repository.
The first todo, was to configure and create a Localstack yml file.
services:
localstack:
image: localstack/localstack:0.11.4
ports:
- "4567-4597:4567-4597"
- "${PORT_WEB_UI-8080}:${PORT_WEB_UI-8080}"
environment:
- SERVICES=s3,iam,lambda
- DEBUG=${DEBUG- }
- DATA_DIR=${DATA_DIR- }
- PORT_WEB_UI=${PORT_WEB_UI- }
- LAMBDA_REMOTE_DOCKER=0
- LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR- }
volumes:
- "${TMPDIR:-/tmp/localstack}:/tmp/localstack"
- "/var/run/docker.sock:/var/run/docker.sock"
With this, we can start a local version of the s3, iam and lambda services. Just with one command: docker-compose up -d
.
The lambda itself is simple. One of my previous kata methods packaged into a zip file. It was a bit annoying though, that AWS lambda still does not support python 3.9+.
After zipping it, deploying to localstack is simple:
terraform init -input=false
terraform apply -auto-approve -input=false
It will deploy all definitions stored in local.tf file. All it takes is to properly configure the provider:
provider "aws" {
access_key = "mock_access_key"
region = "eu-west-1"
s3_force_path_style = true
secret_key = "mock_secret_key"
skip_credentials_validation = true
skip_metadata_api_check = true
skip_requesting_account_id = true
endpoints {
s3 = "http://localhost:4572"
lambda = "http://localhost:4574"
iam = "http://localhost:4593"
}
}
Once you’ll checkout the project, just go through make all
. And the resulting tests were so satisfactory:
aws --endpoint-url=http://localhost:4572 s3 ls
2021-07-17 15:12:23 example-bucket
aws --endpoint-url=http://localhost:4574 lambda list-functions
{
"Functions": [
{
"FunctionName": "lowest-common-ancestor-localstack",
"FunctionArn": "arn:aws:lambda:us-east-1:000000000000:function:lowest-common-ancestor-localstack",
"Runtime": "python3.8",
"Role": "arn:aws:iam::000000000000:role/example_lambda",
"Handler": "program.lambda_handler",
"CodeSize": 924,
"Description": "lowest-common-ancestor-localstack",
"Timeout": 3,
"MemorySize": 128,
"LastModified": "2021-07-17T14:12:23.922+0000",
"CodeSha256": "gToFEYreRpfgFMM7mMBq4qZSK2MWoloTh002s7l1fMQ=",
"Version": "$LATEST",
"VpcConfig": {},
"Environment": {
"Variables": {
"DATA_BUCKET": "example-bucket"
}
},
"TracingConfig": {
"Mode": "PassThrough"
},
"RevisionId": "fef04191-8ce8-4b87-9da4-f7c2f7b2d2e2",
"State": "Active",
"LastUpdateStatus": "Successful"
}
]
}
I meant to work a bit on principles.green this week. I failed. I will try to come back to this in some time. For now, I intend to work some more with localstack and python tests.