Skip to content

Instantly share code, notes, and snippets.

@charlires
Last active July 14, 2021 00:09
Show Gist options
  • Save charlires/d4b0c8c9efe566468e72eafde5766c6b to your computer and use it in GitHub Desktop.
Save charlires/d4b0c8c9efe566468e72eafde5766c6b to your computer and use it in GitHub Desktop.
Terraform

https://www.terraform.io/docs/language/syntax/style.html

Variables Names and Resource Identifiers Use snake case. e.g.

variable "this_is_a_variable_name" {} 
 
resource "aws_lambda_function" "replication_function" {}

https://www.terraform.io/docs/language/syntax/style.html

Define Environment variables We use terraform workspaces https://www.terraform.io/docs/language/state/workspaces.html#using-workspaces to define in which environment we are going to apply the plan.

The available environments must be: qa, stage and prod

Delimit the environments to only these 3

variable "env" {
    type = map(string)
    default = {
        qa = "qa"
        stage = "stage"
        prod = "prod"
    }
}

And whenever you use this variable get it using the workspace as

locals {
  env = var.env[terraform.workspace]
}

Resource Names Conventions All resources names we create should start with the environment as the name prefix e.g. qa-project-name a way to ensure that this happens could be something like the following

resource ... {
  name = "{local.env}-{local.project}-<resource>"
}

State TBD https://www.terraform.io/docs/language/settings/backends/s3.html

terraform {
  backend "s3" {
    bucket               = "<bucket-name>"
    workspace_key_prefix = "<infra/monitoring>/<app-name>/"
    key                  = "terraform.tfstate"
    // key - (Required) Path to the state file inside the S3 Bucket. When using a non-default workspace, the state path will be /workspace_key_prefix/workspace_name/key (see also the workspace_key_prefix configuration).
    region               = "us-east-1"
    dynamodb_table       = "<dynamodb-table>"
    encrypt              = true
  }
}

Troubleshooting Error: Invalid legacy provider address You must complete the Terraform 0.13 upgrade process before upgrading to later versions.

downgrade to terraform 13

brew install terraform@0.13
terraform -version 
# Terraform v0.13.6
# If this doesn't work you may need to do this extra step
# export PATH="/usr/local/opt/[email protected]/bin:$PATH"
# source ~/.zshrc

terraform 0.13upgrade .
terraform init

# You will have to do plan and apply with terraform 0.13

# go back to terraform 0.14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment