问题描述
我一直在尝试以可重复使用的方式重组terraform模块。 我要在此处解决的问题是避免创建仅具有一个或两个用于特定设置的新属性的新tf模块。
当前结构:
├── 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
我还一直在考虑使用terraform-aws-ec2-instance git repo作为我的terragrunt脚本中的源。这样,我完全不必管理tf模块。但是我不确定如何编写terragrunt.hcl以指向GitHub存储库并固定到某个版本。
这是推荐的做事方式吗?还是有一种更清洁的方法?
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"
}
}
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
}
尝试了上述设置,但面临缺少后端“ s3”块的问题
解决方法
尝试使用这种方式:
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"
}
,
在此链接的帮助下设法弄清楚了
https://github.com/gruntwork-io/terragrunt/issues/311
就我而言,即使我已在terragrunt根文件中定义了remote_state块,运行terragrunt命令后也不会在缓存文件夹中生成后端块。
需要做的是包括generate块,以告诉terragrunt将后端块生成到tf文件中以解决此问题。