DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Setup AWS S3 bucket locally with LocalStack
Sayed Naweed Rizvi
Sayed Naweed Rizvi

Posted on

Setup AWS S3 bucket locally with LocalStack

LocalStack is a great tool which Simulates the AWS services on your local system. You can run the CLI commands, Terraform scripts just as you would on Cloud.

LocalStack - A fully functional local cloud stack

Develop and test your cloud apps offline.

favicon localstack.cloud

In this quick read, I will show you how to setup LocalStack and spin up a S3 instance through CLI command and Terraform.Β 

Setup Requirements:

  • Python
  • pip
  • Docker
  • Terraform

Installation

pip install localstack

Startup

Before you start running localstack, ensure that Docker service is up & running.

Checkout - How you can use DockerDesktop to create containers locally.

Start localstack in docker mode, from a container

localstack start

Check Service status

% 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 β”‚
β”‚ secretsmanager           β”‚ βœ” available β”‚
β”‚ ses                      β”‚ βœ” available β”‚
β”‚ sns                      β”‚ βœ” available β”‚
β”‚ sqs                      β”‚ βœ” available β”‚
β”‚ ssm                      β”‚ βœ” available β”‚
β”‚ stepfunctions            β”‚ βœ” available β”‚
β”‚ sts                      β”‚ βœ” available β”‚
β”‚ support                  β”‚ βœ” available β”‚
β”‚ swf                      β”‚ βœ” available β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Enter fullscreen mode Exit fullscreen mode

You should now see localstack container running on DockerDesktop dashboard.

s3 aws localstack


Creating S3 through AWS CLI

Let's get into the localstack container and see what services are running.

Check if the AWS CLI is running
localstack container

Add AWS profile configuration

aws configure --profile default

Enter following values
Access key/Secret : test
Region: eu-west-2
profile : you can leave it blank`
Enter fullscreen mode Exit fullscreen mode

Check configuration : $ aws configure list

aws configure

Create s3 bucket
We will be deploying to localhost:4566

aws s3 mb s3://my-bucket --endpoint-url http://localhost:4566
make_bucket: my-bucket

Enter fullscreen mode Exit fullscreen mode

Creation S3 through Terraform

Ensure, Terraform is running on your system.

% terraform --version
Terraform v1.2.2
on darwin_arm64
Enter fullscreen mode Exit fullscreen mode

You can now either walk along with me or checkout this code from my repo
Deploy S3 on LocalStack with Terraform

Time for some IaC

main.tf

resource "aws_s3_bucket" "s3-localstack" {
  bucket = "my-bucket-1"
}
Enter fullscreen mode Exit fullscreen mode

provider.tf

provider "aws" {
  access_key = "test"
  secret_key = "test"
  region     = "us-east-1"

  skip_credentials_validation = true
  skip_requesting_account_id  = true
  skip_metadata_api_check     = true
  s3_use_path_style           = true 

  endpoints {
    s3 = "http://localhost:4566"
  }
}

Enter fullscreen mode Exit fullscreen mode

Run the regular Terraform commands

Initialise working directory : terraform init

Plan change, check : terraform plan

Apply changes : terraform apply

That's it

Check what services are runningΒ on


Β 

% 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                       β”‚ βœ” running   β”‚--> here we are
β”‚ s3control                β”‚ βœ” available β”‚
β”‚ secretsmanager           β”‚ βœ” available β”‚
β”‚ ses                      β”‚ βœ” available β”‚
β”‚ sns                      β”‚ βœ” available β”‚
β”‚ sqs                      β”‚ βœ” available β”‚
β”‚ ssm                      β”‚ βœ” available β”‚
β”‚ stepfunctions            β”‚ βœ” available β”‚
β”‚ sts                      β”‚ βœ” available β”‚
β”‚ support                  β”‚ βœ” available β”‚
β”‚ swf                      β”‚ βœ” available β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Enter fullscreen mode Exit fullscreen mode

Go ahead and test your scripts.
Thanks.

Top comments (1)

Collapse
 
rizways profile image
Sayed Naweed Rizvi

To check the s3 buckets list
aws s3 ls --endpoint-url=localhost:4566 --recursive --human-readable

Stop sifting through your feed.

Find the content you want to see.

Change your feed algorithm by adjusting your experience level and give weights to the tags you follow.