DEV Community

KILLALLSKYWALKER
KILLALLSKYWALKER

Posted on

Refactor the Terraform Script — Restoring Balance

Previously when i use terraform and localstack , i avoid to using the tflocal wrapper due to one 100% same way when running in real environment . However i notice that thats a lot of unnecessary thing that we need to add and add more maintenance .

As example from previous

provider "aws" {
  profile = "localstack"
  region  = "us-east-1"

  s3_use_path_style           = true
  skip_credentials_validation = true
  skip_metadata_api_check     = true

  endpoints {
    s3             = "http://s3.localhost.localstack.cloud:4566"
  }

  default_tags {
    tags = {
      Environment = "tutorial"
      Project     = "terraform-configure-providers"
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

We need to specify specific server endpoint and also few flaf like skip_credentials_validation and others . Which is we dont need it on real environment .

Tflocal

Localstack provide a wrapper to run Terraform against LocalStack . You can read more about it in here .

Once you setup everything , you just can clean up your tf file . The only difference that you will need to is when you want to run it .

To init
tflocal init

To apply
tflocal apply

Closing

Luckily, I realized this early and moved away from the dark side. By using tflocal, things become much simpler. The Terraform files stay clean and close to real AWS usage, without extra LocalStack settings inside the code.

LocalStack details are handled when running Terraform, not when writing it. This keeps the setup easy to understand, easier to maintain, and closer to how things should work in the real environment.

Top comments (0)