Setup AWS S3 bucket locally with LocalStack
Tags: localstack, aws, s3, terraform
Published: 2022-06-11 08:45:27



> `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.
{% embed https://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_.

{% embed https://youtu.be/CX8AnwTW2Zs %}


Start localstack in docker mode, from a container

![localstack start](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8zzwabzr1tl12yup1zou.png)


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

You should now see localstack `container` running on DockerDesktop dashboard.

![s3 aws localstack](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x98fscmwyi4s03ohoqfq.png)

---

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/roiye9osm3d2r8uuf9cz.png)

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


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.