Deploying LocalStack ?? using Docker
Mohamed Sharfy
IT Systems Administrator at Bank of Khartoum | HRIS & HCM Consultant | ITIL? V4 | OCI | CompTIA Security+ | SFPC? | FCF CyberSecurity|
LocalStack is a fully functional local AWS cloud stack that enables you to develop and test your cloud and serverless apps offline. It provides an easy-to-use test/mocking framework for developing cloud applications and supports various AWS services
First of all we are going to pull the localstack image needed and then
This will start the docker container with LocalStack running inside it
# Pull the docker image
docker pull localstack/localstack
# Run the docker container
docker run -d -p 4566:4566 -p 4571:4571 localstack/localstack
Creating a Bucket in LocalStack
To interact with LocalStack, we’ll use the AWS CLI.
If you don't have it on your machine and your using Windows , you can install and run it from here: https://awscli.amazonaws.com/AWSCLIV2.msi
or you can access the deployed container and run it from in there by using this command (replace container id with your localstack container id)
docker exec -it <container-id> bash
However, we need to configure it to interact with LocalStack and not the real AWS services. Here’s how you can do it:
# Configure AWS CLI for LocalStack
aws configure --profile localstack
Enter https://localhost:4566 for the endpoint and us-east-1 for the region. You can leave access key, secret key, and session token as none
Now, let’s create a bucket:
# Create a bucket
aws --endpoint-url=https://localhost:4566 --profile localstack s3 mb s3://my-bucket
This will create a bucket named my-bucket.
Using Terraform to deploy our mock instances
So we are going to deploy our instances using this tf file so that you can see all your created instances on LocalStack
This will create 10 instances for example:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.27"
}
}
required_version = ">= 0.13.4"
}
provider "aws" {
access_key = "test"
secret_key = "test"
region = "us-east-1"
s3_force_path_style = false
skip_credentials_validation = true
skip_metadata_api_check = true
skip_requesting_account_id = true
endpoints {
apigateway = "https://localhost:4566"
apigatewayv2 = "https://localhost:4566"
cloudformation = "https://localhost:4566"
cloudwatch = "https://localhost:4566"
dynamodb = "https://localhost:4566"
ec2 = "https://localhost:4566"
es = "https://localhost:4566"
elasticache = "https://localhost:4566"
firehose = "https://localhost:4566"
iam = "https://localhost:4566"
kinesis = "https://localhost:4566"
lambda = "https://localhost:4566"
rds = "https://localhost:4566"
redshift = "https://localhost:4566"
route53 = "https://localhost:4566"
s3 = "https://s3.localhost.localstack.cloud:4566"
secretsmanager = "https://localhost:4566"
ses = "https://localhost:4566"
sns = "https://localhost:4566"
sqs = "https://localhost:4566"
ssm = "https://localhost:4566"
stepfunctions = "https://localhost:4566"
sts = "https://localhost:4566"
}
}
resource "aws_instance" "myserver" {
ami = "ami-830c94e3"
instance_type = "t2.micro"
count = 10
tags = {
Name = "Server${count.index + 1}",
}
}
To apply and create the mock instances using terraform
领英推荐
#initializes various local settings and data that will be used by subsequent commands
terraform init
#will show you a plan of what it will do and will prompt you for approval before making any changes
terraform apply
To check on your instances you can run the below:
aws --endpoint-url=https://localhost:4566 ec2 describe-instances --query 'Reservations[].Instances[].[InstanceId,InstanceType,PublicIpAddress,Tags[?Key==`Name`]| [0].Value]' --output table --region us-east-1
and your displayed result should look like this:
LocalStack is a great example to test locally your setups and applications as well as to get familiar with the AWS ecosystem and it can provide the following:
Remember, while LocalStack is a great tool for local development and testing, it’s not a replacement for testing in a real AWS environment before going live. Always ensure your applications work as expected in the actual AWS environment.
#LocalStack #Docker #AWS #CloudComputing #Serverless #S3Bucket
As always support the projects:
and his amazing channel on: