terraform - 如何限制模块/服务在 terraform 中执行
问题描述
我有多个使用 terraform 配置的环境。我的问题是如何限制特定模块在特定环境中执行。
例如:我只需要在生产环境中部署实例服务和相关模块,而不需要在较低的环境中部署
我有以下带有服务的结构,每个服务调用模块。
结构体:
├── instances
│ ├── config.tfplan
│ ├── data.tf
│ ├── main.tf
│ └── variables.tf
├── modules
│ ├── buckets
│ │ ├── buckets.tf
│ │ ├── outputs.tf
│ │ └── variables.tf
│ ├── compartments
│ │ ├── compartments.tf
│ │ ├── output.tf
│ │ └── variables.tf
│ ├── iam
│ │ ├── groups.tf
│ │ ├── outputs.tf
│ │ ├── policies.tf
│ │ ├── users.tf
│ │ └── variables.tf
│ ├── instances
│ │ ├── instance.tf
│ │ ├── output.tf
│ │ └── variables.tf
解决方案
这在旧版本的 Terraform 中是不可能的,但在 0.12.6 版本中是不可能的。您可以for_each
在模块中添加。请在此处找到我如何使用的示例for_each
:
定义变量:
variable "cloudwatch_event" {
description = "Map of cloudwatch event configuration"
type = map(any)
default = {
default = {}
}
}
定义资源:
module "cloudwatch_event" {
source = "git::git@github.com:tomarv2/terraform-aws-cloudwatch-event.git?ref=v0.0.4"
for_each = var.cloudwatch_event
description = lookup(each.value, "description", null)
custom_input = lookup(each.value, "custom_input", null)
suffix = lookup(each.value, "suffix", "rule")
schedule = lookup(each.value, "schedule", null)
deploy_event_rule = var.deploy_cloudwatch_event_trigger
deploy_event_target = var.deploy_cloudwatch_event_trigger
target_arn = join("", aws_lambda_function.lambda.*.arn)
#-----------------------------------------------
# Note: Do not change teamid and prjid once set.
teamid = var.teamid
prjid = var.prjid
}
如您所见,如果没有cloudwatch_event
此资源将不会被部署。
请检查位于此处的模块存储库:https ://github.com/tomarv2/terraform-aws-lambda
Terraform 文档供您参考:https ://www.terraform.io/docs/language/meta-arguments/for_each.html
推荐阅读
- flutter - Flutter:如何使 ListView 对指针事件透明(但不是它的非透明内容)?
- python - 我可以将 dask 数据框作为输入传递给 scikit-learn 模型吗?
- rust - 如何从标准输入读取具有最大输入长度的行?
- java - 如何转换两个日历对象之间的确切时间(年、月、日、小时、分钟、秒)?
- python - 错误:flask_oidc:错误:无法获取令牌信息
- php - php日期到sql
- casting - Acumatica - 字段类型十进制得到转换错误
- python - 如何使用 Python 或 R 将 Excel 中带有替代文本的图形导出为 PDF?
- jquery - 检查电子邮件验证并插入输入字段
- javascript - 如何将外部 JSON 文件中的样式/CSS 数据导入 Cytoscape.js?