DEV Community

Cover image for Setting up AWS S3 bucket locally using Localstack and Docker
Ajeet Singh Raina for Docker

Posted on • Updated on

Setting up AWS S3 bucket locally using Localstack and Docker

With over 52,000 stars and 520+ contributors, LocalStack is an open-source tool that emulates a wide range of AWS services on a local machine. It's primarily used for testing and development purposes, allowing developers to run applications locally without needing to interact with the actual AWS cloud.

LocalStack is a cloud development platform that solves the challenge of costly and slow cloud resource usage during development and testing of serverless applications. By providing a local environment that replicates AWS services, LocalStack helps developers ensure that their applications work correctly before deploying them to a live environment.

Key features of LocalStack include:

  • Service Emulation: It emulates various AWS services such as S3, DynamoDB, Lambda, API Gateway, SQS, SNS, CloudFormation, and more.
  • Local Development: Developers can run AWS services locally, which speeds up development cycles and reduces costs associated with using AWS resources.
  • Consistency: Ensures that the development environment closely mirrors the production environment on AWS, reducing discrepancies and potential issues during deployment.
  • Automation and CI/CD Integration: LocalStack can be integrated into CI/CD pipelines, enabling automated testing of AWS-dependent code.
  • LocalStack can be run using Docker, making it easy to set up and use on various platforms.

Benefits of using Localstack:

  • Reduced Costs: By emulating AWS services locally, LocalStack eliminates the need to constantly deploy to the cloud, saving you money on cloud charges.
  • Faster Development: LocalStack lets you spin up services instantly on your machine, significantly speeding up development cycles compared to waiting for cloud deployments.

Getting Started

You can directly start the LocalStack container using the Docker CLI. This method requires more manual steps and configuration, but it gives you more control over the container settings.

The first time you start localstack, it requires Docker daemon to be up and running on your system.

localstack start -d

     __                     _______ __             __
    / /   ____  _________ _/ / ___// /_____ ______/ /__
   / /   / __ \/ ___/ __ `/ /\__ \/ __/ __ `/ ___/ //_/
  / /___/ /_/ / /__/ /_/ / /___/ / /_/ /_/ / /__/ ,<
 /_____/\____/\___/\__,_/_//____/\__/\__,_/\___/_/|_|

 ๐Ÿ’ป LocalStack CLI 3.5.0
 ๐Ÿ‘ค Profile: default

[09:22:54] starting LocalStack in Docker mode ๐Ÿณ                                                                                                    localstack.py:503
           preparing environment                                                                                                                    bootstrap.py:1283
ERROR: '['docker', 'ps']': exit code 1; output: b'Cannot connect to the Docker daemon at unix:///Users/ajeetsraina/.docker/run/docker.sock. Is the docker daemon running?\n'
โŒ Error: Docker could not be found on the system.
Please make sure that you have a working docker environment on your machine.
Enter fullscreen mode Exit fullscreen mode

Install Docker Desktop on your system and now this time you will find that Localstack gets started successfully.

localstack start -d

     __                     _______ __             __
    / /   ____  _________ _/ / ___// /_____ ______/ /__
   / /   / __ \/ ___/ __ `/ /\__ \/ __/ __ `/ ___/ //_/
  / /___/ /_/ / /__/ /_/ / /___/ / /_/ /_/ / /__/ ,<
 /_____/\____/\___/\__,_/_//____/\__/\__,_/\___/_/|_|

 ๐Ÿ’ป LocalStack CLI 3.5.0
 ๐Ÿ‘ค Profile: default

[09:23:54] starting LocalStack in Docker mode ๐Ÿณ                                                                                                    localstack.py:503
           preparing environment                                                                                                                    bootstrap.py:1283
           configuring container                                                                                                                    bootstrap.py:1291
           starting container                                                                                                                       bootstrap.py:1301
[09:23:55] detaching                                                                                                                                bootstrap.py:1305

Enter fullscreen mode Exit fullscreen mode

You can also start the Docker container simply by executing the following docker run command:

$ docker run --rm -it -p 4566:4566 -p 4510-4559:4510-4559 localstack/localstack
Enter fullscreen mode Exit fullscreen mode

List of AWS services that Localstack support

localstack status services
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”“
โ”ƒ Service                  โ”ƒ Status      โ”ƒ
โ”กโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ฉ
โ”‚ acm                      โ”‚ โœ” available โ”‚
โ”‚ apigateway               โ”‚ โœ” available โ”‚
โ”‚ cloudformation           โ”‚ โœ” available โ”‚
โ”‚ cloudwatch               โ”‚ โœ” available โ”‚
โ”‚ config                   โ”‚ โœ” available โ”‚
โ”‚ dynamodb                 โ”‚ โœ” available โ”‚
โ”‚ dynamodbstreams          โ”‚ โœ” available โ”‚
โ”‚ ec2                      โ”‚ โœ” available โ”‚
โ”‚ es                       โ”‚ โœ” available โ”‚
โ”‚ events                   โ”‚ โœ” available โ”‚
โ”‚ firehose                 โ”‚ โœ” available โ”‚
โ”‚ iam                      โ”‚ โœ” available โ”‚
โ”‚ kinesis                  โ”‚ โœ” available โ”‚
โ”‚ kms                      โ”‚ โœ” available โ”‚
โ”‚ lambda                   โ”‚ โœ” available โ”‚
โ”‚ logs                     โ”‚ โœ” available โ”‚
โ”‚ opensearch               โ”‚ โœ” available โ”‚
โ”‚ redshift                 โ”‚ โœ” available โ”‚
โ”‚ resource-groups          โ”‚ โœ” available โ”‚
โ”‚ resourcegroupstaggingapi โ”‚ โœ” available โ”‚
โ”‚ route53                  โ”‚ โœ” available โ”‚
โ”‚ route53resolver          โ”‚ โœ” available โ”‚
โ”‚ s3                       โ”‚ โœ” available โ”‚
โ”‚ s3control                โ”‚ โœ” available โ”‚
โ”‚ scheduler                โ”‚ โœ” available โ”‚
โ”‚ secretsmanager           โ”‚ โœ” available โ”‚
โ”‚ ses                      โ”‚ โœ” available โ”‚
โ”‚ sns                      โ”‚ โœ” available โ”‚
โ”‚ sqs                      โ”‚ โœ” available โ”‚
โ”‚ ssm                      โ”‚ โœ” available โ”‚
โ”‚ stepfunctions            โ”‚ โœ” available โ”‚
โ”‚ sts                      โ”‚ โœ” available โ”‚
โ”‚ support                  โ”‚ โœ” available โ”‚
โ”‚ swf                      โ”‚ โœ” available โ”‚
โ”‚ transcribe               โ”‚ โœ” available โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
Enter fullscreen mode Exit fullscreen mode

Creating a AWS S3 Bucket

Create an s3 bucket with LocalStack's awslocal CLI:

awslocal s3api create-bucket --bucket sample-bucket
{
    "Location": "/sample-bucket"
}
Enter fullscreen mode Exit fullscreen mode

Listing the S3 Bucket

awslocal s3api list-buckets
{
    "Buckets": [
        {
            "Name": "sample-bucket",
            "CreationDate": "2024-06-29T17:56:46.000Z"
        }
    ],
    "Owner": {
        "DisplayName": "webfile",
        "ID": "75aa57f09aa0c8caeab4f8c24e99d10f8e7faeebf76c078efc7c6caea54ba06a"
    }
}
Enter fullscreen mode Exit fullscreen mode

Listingthe items inside the bucket:

awslocal s3api list-objects --bucket sample-bucket
{
    "RequestCharged": null
}
Enter fullscreen mode Exit fullscreen mode

Running Localstack using Docker Compose

services:
  localstack:
    container_name: "${LOCALSTACK_DOCKER_NAME:-localstack-main}"
    image: localstack/localstack
    ports:
      - "127.0.0.1:4566:4566"            # LocalStack Gateway
      - "127.0.0.1:4510-4559:4510-4559"  # external services port range
    environment:
      # LocalStack configuration: https://docs.localstack.cloud/references/configuration/
      - DEBUG=${DEBUG:-0}
    volumes:
      - "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"
Enter fullscreen mode Exit fullscreen mode

Starting the container

docker compose up
[+] Running 1/0
 โœ” Container localstack_demo  Recreated                                                                                                                0.1s
Attaching to localstack-main
localstack-main  |
localstack-main  | LocalStack version: 3.5.1.dev
localstack-main  | LocalStack build date: 2024-06-24
localstack-main  | LocalStack build git hash: 9a3d238ac
localstack-main  |
localstack-main  | Ready.
Enter fullscreen mode Exit fullscreen mode

Creating a queue using SQS with LocalStack's awslocal CLI:

$ awslocal sqs create-queue --queue-name test-queue
Enter fullscreen mode Exit fullscreen mode

Result:

{
    "QueueUrl": "http://sqs.us-east-1.localhost.localstack.cloud:4566/000000000000/test-queue"
}
Enter fullscreen mode Exit fullscreen mode
$ awslocal sqs list-queues
Enter fullscreen mode Exit fullscreen mode

Result:

{
    "QueueUrls": [
        "http://sqs.us-east-1.localhost.localstack.cloud:4566/000000000000/test-queue"
    ]
}
Enter fullscreen mode Exit fullscreen mode

References:

Top comments (0)