如何隔离多个模块导入中创建的资源?

问题描述 投票:0回答:1

我制作了自己的 terraform 模块,用于创建 EC-2 实例及其角色:

resource "aws_instance" "instance" {
    ami=var.ami
    instance_type="t3a.micro"
    key_name = var.ssh_key
    iam_instance_profile = aws_iam_instance_profile.ec2_profile.name

    root_block_device {
        volume_size = 30
        volume_type = "gp3"
    }

    vpc_security_group_ids=var.ec2_security_groups

    # Ommitted for verbosity reduction
}

resource "aws_route53_record" "domain" {
  zone_id = var.domain_zone
  name    = local.domain
  type    = "CNAME"
  ttl     = 300
  records = [var.lb.dns_name]
}

# Load Balance

resource "aws_lb_target_group" "tg" {
  name     = "${local.name_with_env}-Tg"
  port     = 80
  protocol = "HTTP"
  target_type="instance"
  vpc_id=var.lb_tg_vpc
} 

它位于我的 terraform 项目中名为

ec2_lb
的模块中,具有以下文件结构:

- modules
-- ec2_lb
--- main.tf <<< File above
- client1
-- main.tf
- client2
-- main.tf

在每个客户端 (

clientX/main.tf
) 上,我想创建 2 个 ec2 实例,一个用于生产,一个用于登台:

module "test_staging_ec2_lb" {
  source = "../modules/ec2_lb"

 
  # Stuff ommitted for simplicity
}

module "test_production_ec2_lb" {
  source = "../modules/ec2_lb"

 
  # Stuff ommitted for simplicity
}

但我觉得模块导入

test_production_ec2_lb
将取代从
test_staging_ec2_lb
创建的资源。有没有一种方法可以对其进行命名,而无需为每个客户和每个环境创建重复的文件夹:

- modules
-- ec2_lb
--- main.tf <<< File above
- client1_production
-- main.tf
- client1_staging
-- main.tf
- client2_production
-- main.tf
- client2_staging
-- main.tf

amazon-web-services terraform
1个回答
0
投票

考虑探索 https://www.terraform-best-practices.com/examples 了解更多。

© www.soinside.com 2019 - 2024. All rights reserved.