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.

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{% raw %}`
```


Check configuration : `$ aws configure list`

![aws configure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7wqxday3lkffzog4ezg4.png)

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

```

---

## Creation S3 through Terraform
Ensure, Terraform is running on your system.

```
% terraform --version
Terraform v1.2.2
on darwin_arm64
```

You can now either walk along with me or checkout this code from my repo
[Deploy S3 on LocalStack with Terraform ](https://github.com/rizways/tf-localstack-s3
)

### Time for some IaC
`main.tf`
```
resource "aws_s3_bucket" "s3-localstack" {
  bucket = "my-bucket-1"
}
```

`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"
  }
}

```

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 │
└──────────────────────────┴─────────────┘
```

Go ahead and test your scripts.
Thanks.

Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
navedrizv profile image
Sayed Naweed Rizvi

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