Gaurang Shah
Gaurang Shah

Reputation: 12920

Terragrunt - avoid code duplicity for multiple environments

I am coming from application development so my thought process is bit different. what normally happens with application development is, you have single code base. and based on your branching strategy your pipeline deploys it to multiple environment.

However, with what I am reading for terraform and terrgrunt. people are using different code base for different environment. which would give you a flexibility to have different version of (different memeory, cpu, disk) infra on different environment. However, it's a lot of code duplicity.

this is what I am reading as standard practice for Google.

── common
│   └── terraform.hcl
├── environments
│   ├── dev
│   │   ├── project-a
│   │   │   ├── function-1
│   │   │   │   └── terragrunt.hcl
│   │   │   ├── function-2
│   │   │   │   └── terragrunt.hcl
│   │   │   └── function-3
│   │   │       └── terragrunt.hcl
│   ├── qa
│   │   ├── project-a
│   │   │   ├── function-1
│   │   │   │   └── terragrunt.hcl
│   │   │   ├── function-2
│   │   │   │   └── terragrunt.hcl
│   │   │   └── function-3
│   │   │       └── terragrunt.hcl

should there be just single code base, regardless of environment and environment variable should replace the environment specific variables. and now based on which branch I am merging this code (i.e. dev/qa/staging/prod) it would deploy resources on that branch.

I mean something like this.

── common
│   └── terraform.hcl
│     ├── project-a
│     │   ├── function-1
│     │   │   └── terragrunt.hcl
│     │   ├── function-2
│     │   │   └── terragrunt.hcl
│     │   └── function-3
│     │       └── terragrunt.hcl

is this even possible ? if I try to setup what is the challenges I would face ?

Upvotes: 0

Views: 108

Answers (1)

ha36d
ha36d

Reputation: 1107

should there be just single code base, regardless of environment and environment variable should replace the environment specific variables.

There should be only one code base, and then different environments and therefore environment variables that base been defined usually in terragrunt.hcl file. example:

├─ code
│   └── main.tf
├── environments
│   ├── dev
│   │   ├── terragrunt.hcl
│   ├── qa
│   │   ├── terragrunt.hcl

code/main.tf

module "ec2_instance" {
  source  = "terraform-aws-modules/ec2-instance/aws"

  name = var.name

  instance_type          = var.instance_type
  key_name               = "user1"
  monitoring             = true
  vpc_security_group_ids = ["sg-12345678"]
  subnet_id              = "subnet-eddcdzz4"

  tags = {
    Terraform   = "true"
    Environment = "dev"
  }
}
variable "name" {}
variable "instance_type" {}

environments/dev/terragrunt.hcl

terraform {
source = ../..//code
}
inputs = {
  name = "MyName"
  instance_type  = "m4.large"
}

and now based on which branch I am merging this code (i.e. dev/qa/staging/prod) it would deploy resources on that branch.

The code should be always be run in master or main branch, because it will create a lot of drift and distraction, if you try to apply the code in different branches. Therefore, you will have different directories for different environments and the pull/merge request will be approved and merged and executed in the main branch.

Upvotes: 1

Related Questions