In continuation to the earlier post on mocks for clouds, this article does a deep dive into getting up & running with Localstack. This is a consolidation of the steps & best practices shared here, here & here. The Localstack set-up is on a Ubuntu-20.04, with Java-8x, Maven-3.8x, Docker-24.0x.
(I) Set-up
# Install awscli
sudo apt-get install awscli
# Install localstack ver 3.8
## Issue1: By default pip pulls in version 4.0, which gives an error:
## ERROR: Could not find a version that satisfies the requirement localstack-ext==4.0.0 (from localstack)
python3 -m pip install localstack==3.8.1
--
# Add to /etc/hosts
127.0.0.1 localhost.localstack.cloud
127.0.0.1 s3.localhost.localstack.cloud
--
# Configure AWS from cli
aws configure
aws configure set default.region us-east-1
aws configure set aws_access_key_id test
aws configure set aws_secret_access_key test
## Manually configure AWS
Add to ~/.aws/config
endpoint_url = http://localhost:4566
## Add mock credentials
Add to ~/.aws/credentials
aws_access_key_id = test
aws_secret_access_key = test
--
# Download docker images needed by the Lambda function
## Issue 2: Do this before hand, Localstack gets stuck
## at the download image stage unless it's already available
## Pull java:8.al2
docker pull public.ecr.aws/lambda/java:8.al2
## Pull nodejs (required for other nodejs Lambda functions)
docker pull public.ecr.aws/lambda/nodejs:18
## Check images downloaded
docker image ls
(II) Start Localstack
# Start locally
localstack start
# Start as docker (add '-d' for daemon)
## Issue 3: Local directory's mount should be as per sample docker-compose
docker-compose -f docker-compose-localstack.yaml up
# Localstack up on URL's
http://localhost:4566
http://localhost.localstack.cloud:4566
# Check Localstack Health
curl http://localhost:4566/_localstack/info
curl http://localhost:4566/_localstack/health
(III) AWS services on Localstack from CLI
(a) S3
# Create bucket named "test-buck"
aws --endpoint-url=http://localhost:4566 s3 mb s3://test-buck
# Copy item to bucket
aws --endpoint-url=http://localhost:4566 s3 cp a1.txt s3://test-buck
# List bucket
aws --endpoint-url=http://localhost:4566 s3 ls s3://test-buck
--
(b) Sqs
# Create queue named "test-q"
aws --endpoint-url=http://localhost:4566 sqs create-queue --queue-name test-q
# List queues
aws --endpoint-url=http://localhost:4566 sqs list-queues
# Get queue attribute
aws --endpoint-url=http://localhost:4566 sqs get-queue-attributes --queue-url http://sqs.us-east-1.localhost.localstack.cloud:4566/000000000000/test-q --attribute-names All
--
(c) Lambda
aws --endpoint-url=http://localhost:4566 lambda list-functions
# Create Java function
aws --endpoint-url=http://localhost:4566 lambda create-function --function-name test-j-div --zip-file fileb://original-java-basic-1.0-SNAPSHOT.jar --handler example.HandlerDivide::handleRequest --runtime java8.al2 --role arn:aws:iam::000000000000:role/lambda-test
# List functions
aws --endpoint-url=http://localhost:4566 lambda list-functions
# Invoke Java function
aws --endpoint-url=http://localhost:4566 lambda invoke --function-name test-j-div --payload '[200,9]' outputJ.txt
# Delete function
aws --endpoint-url=http://localhost:4566 lambda delete-function --function-name test-j-div
(IV) AWS services on Localstack from Java-SDK
# For S3 & Sqs - localstack-aws-sdk-examples, java sdk
# For Lambda - localstack-lambda-java-sdk-v1