# Using Terraform with LocalStack

In our previous article, [Running AWS Lambda Functions Locally Using LocalStack](https://blog.raulnq.com/running-aws-lambda-functions-locally-using-localstack), we saw how simple it was to interact with AWS SAM and AWS CLI. However, we are not limited to those tools; several [integrations](https://docs.localstack.cloud/user-guide/integrations/) are available, and [Terraform](https://developer.hashicorp.com/terraform) is among the most popular.

## **Pre-requisites**

* Install the [**AWS CLI**](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html#cli-configure-quickstart-creds).
    
* Ensure that [**Docker Desktop**](https://docs.docker.com/desktop/install/windows-install/) is up and running.
    
* Install [**Terraform CLI**](https://learn.hashicorp.com/tutorials/terraform/install-cli).
    

## Running LocalStack

Create a `docker-compose.yml` file with the following content:

```yaml
version: "3.8"
services:
  localstack:
    container_name: "my-localstack"
    image: localstack/localstack
    ports:
      - "127.0.0.1:4566:4566"
      - "127.0.0.1:4510-4559:4510-4559"
    environment:
      - DEBUG=1
      - DOCKER_HOST=unix:///var/run/docker.sock
    volumes:
      - ".volume/tmp/localstack:/tmp/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"
```

Run `docker-compose up`. To verify the availability of all services, navigate to [`http://localhost:4566/health`](http://localhost:4566/health) in a web browser.

## Terraform Script

Create a `main.tf` file as follows:

```json
terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "4.22.0"
    }
  }
}

provider "aws"{
  region                      = "us-east-1"
  access_key                  = "mock_access_key"
  secret_key                  = "mock_secret_key"
  s3_use_path_style           = true
  skip_credentials_validation = true
  skip_metadata_api_check     = true
  skip_requesting_account_id  = true 
  endpoints {
    s3             = "http://localhost:4566"
  }
}

resource "aws_s3_bucket" "my-bucket" {
  bucket = "my-bucket"
}
```

The arguments `region`, `access_key`, and `secret_key` are mock credentials. Let's discuss the other arguments:

* `skip_credentials_validation`: Whether to skip credentials validation via the STS API.
    
* `skip_metadata_api_check`: Whether to skip the AWS Metadata API check.
    
* `skip_requesting_account_id`: Whether to skip requesting the account ID.
    
* `s3_use_path_style`: Whether to enable the request to use path-style addressing, i.e., [`https://s3.amazonaws.com/BUCKET/KEY`](https://s3.amazonaws.com/BUCKET/KEY)
    
* `endpoints`: Configuration block for customizing [service endpoints](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/guides/custom-service-endpoints). In our case, we are only using `s3`, but we might need to set up the rest of the services.
    

Run the following commands:

```powershell
terraform init
terraform plan -out app.tfplan
terraform apply 'app.tfplan'
```

And that’s it. Run `aws s3 ls --endpoint-url=`[`http://localhost:4566`](http://localhost:4566) to see the list of buckets. Using Terraform with LocalStack enables us to accelerate Infrastructure as Code (IaC) development, identify errors sooner, and eliminate expenses during testing. Thank you, and happy coding!
