1
votes

I've been trying to restructure my terraform modules in a way that things can be reused. The issue I'm trying to resolve here is to avoid creating new tf modules with just one or two new attributes that are meant for certain setup.

The current structure:

├── services
│   ├── ec2
│   │   └── terragrunt.hcl
│   ├── ec2_with_fixed_ip
│   │   └── terragrunt.hcl (2)
│   └── terragrunt.hcl (1)
├── tf_moodules
    └── ec2
    │   └── main.tf
    └── ec2_with_fixed_ip
    │   └── main.tf
    └── ec2_with_root_block_device
        └── main.tf

I've also been looking at using the terraform-aws-ec2-instance git repo as the source in my terragrunt script. This way, I won't have to manage the tf modules at all. But I'm not sure how I should write my terragrunt.hcl to point to the GitHub repo and peg to a certain version.

Is this a recommended way of doing things? or is there a cleaner way to do it?

Content inside the terragrunt.hcl (1)

remote_state {
  backend = "s3"

  config = {
    encrypt        = true
    bucket         = "my_bucket"
    key            = "${path_relative_to_include()}/terraform.tfstate"
    region         = "ap-southeast-1"
    dynamodb_table = "tf-locks"
  }
}

Content inside the terragrunt.hcl (2)

terraform {
  source = "git::https://github.com/terraform-aws-modules/terraform-aws-ec2-instance.git//?ref=v2.15.0"
}

include {
  path = find_in_parent_folders()
}

inputs = {
  ami = "ami-0123456789abcd"
  instance_type = "t3.medium"
  disable_api_termination = false
}

Tried the above setup, but facing the issue with missing backend "s3" block

2
You're already doing it the right way! You could consider using a git source (git::[email protected]:terraform-aws-modules/terraform-aws-ec2-instance.git//?ref=v2.15.0) but that's mostly a matter of preference.Ben Whaley

2 Answers

0
votes

Try to use this way:

terragrunt = {
    terraform {
    source = "git::[email protected]:org/repo.git//lambda?ref=v0.6.2"
  }
}

    backend "s3" {
      bucket         = "stage-terraform"
      key            = "app/terraform.tfstate"
      region         = "us-east-1"
      encrypt        = false
      dynamodb_table = "stage-terraform-lock-table"
    }
0
votes

Managed to figure out more with the help of this link

https://github.com/gruntwork-io/terragrunt/issues/311

In my case, even though i've defined the remote_state block in my terragrunt root file, the backend block is not generated in the cache folder after running the terragrunt command.

What needs to be done is to include the generate block to tell terragrunt to generate the backend block into a tf file to resolve this problem.