使用VPC子模块的subnet-id在另一个ec2子模块中。

huangapple go评论57阅读模式
英文:

Terraform-AWS-Modules-using subnet-id of a VPC child module in another ec2 child module

问题

我将从感谢你的时间开始,我一直在Google和阅读,可能只是忽视了一些非常简单的东西。我已经尽力阅读了一些Medium上的文章和Terraform文档。

所以,我的问题是...
我有一个看起来像这样的根模块

module "VPC" {
    source = "/home/jamie/Terraform_Project/modules/vpc/"
}

module "Key_Pair" {
    source = "/home/jamie/Terraform_Project/modules/key_pair/"
}

module "EC2_VPN" {
    source = "/home/jamie/Terraform_Project/modules/ec2_vpn/"
}

和三个子模块,你可以看到。我无法在我的EC2模块中引用VPC模块中的“Public_Subnet_ID”。我将在下面展示我的main.tfs和output.tfs。我认为值得一提的是,我尝试了一些我在Google上找到的各种方法,似乎没有任何进展,以下是我的最新尝试。我看到了StackOverflow上的其他答案,但它们对我没有用,或者我仍然在做一些错误的事情。

VPC - main.tf (仅显示子网部分)

/* 公共子网 */
resource "aws_subnet" "public_subnet" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.1.0/24"
  map_public_ip_on_launch = true

  tags = {
    Name = "Public"
    Project = var.project
    Architect = var.architect
  }
}

VPC - output.tf

output "public_subnet_id" {
    value = aws_subnet.public_subnet.id
}

EC2 - main.tf (问题部分)

resource "aws_instance" "web" {
  ami           = data.aws_ami.ubuntu.id
  instance_type = "t3.micro"
  subnet_id = var.public_subnet_id
  key_name = "${var.project}_Key"

  tags = {
    Name = "VPN_Server"
    Project = var.project
    Architect = var.architect
  }
}

我还尝试了带有变量的上述方法(也许是从另一个线程/指南中错了)

我的第一个错误是“module.1_VPC.Public_Subnet_id”没有被引用,但我设法解决了这个问题,但现在它最终出现了以下错误

错误: 创建EC2实例: InvalidSubnetID.NotFound: 子网ID 'module.1_VPC.Public_Subnet_id' 不存在
│ 状态码: 400,请求ID: 00fa3944-4ea3-450b-9fd4-39645785269f

│ 在module.EC2_VPN.aws_instance.web上,.terraform/modules/EC2_VPN/main.tf的第17行,位于resource "aws_instance" "web":
│ 17: resource "aws_instance" "web" {

再次感谢您的时间,我正在学习,并试图在构建/学习的过程中不仅仅是复制和粘贴其他模板。

尝试了各种指南/ Terraform文档(大多数参考模块但在同一个文件夹中而不是分开的文件夹)

我只需要能够导出一个资源ID以便在其他子模块中使用。一旦我能做到这一点,我就能够为安全组和其他我需要引用的任何其他内容复制。

英文:

I will start by thanking you for your time and I have been googling and reading and maybe just overlooking something very simple. I have tried my best with some articles on medium and the terraform documentation.

So, my problem is..
I have a root module that looks like this

module "VPC" {
    source = "/home/jamie/Terraform_Project/modules/vpc/"
}

module "Key_Pair" {
    source = "/home/jamie/Terraform_Project/modules/key_pair/"
}

module "EC2_VPN" {
    source = "/home/jamie/Terraform_Project/modules/ec2_vpn/"
}

and three child modules as you can see. I cannot reference the "Public_Subnet_ID" from my VPC module in my EC2 module. I will show my main.tfs and my output.tfs below. I think its worth mentioning that I have tried various things I have found on google and don't seem to get anywhere below is my latest attempt. i have seen other answers on stackoverflow but they have not worked for me or i am still doing something wrong.

VPC - main.tf (will show subnet bit only)

/* Public Subnet */
resource "aws_subnet" "public_subnet" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.1.0/24"
  map_public_ip_on_launch = true

  tags = {
    Name = "Public"
    Project = var.project
    Architect = var.architect
  }
}

VPC - output.tf

output "public_subnet_id" {
    value = aws_subnet.public_subnet.id
}

**EC2 - main.tf (problem bit)

resource "aws_instance" "web" {
  ami           = data.aws_ami.ubuntu.id
  instance_type = "t3.micro"
  subnet_id = var.public_subnet_id
  key_name = "${var.project}_Key"

  tags = {
    Name = "VPN_Server"
    Project = var.project
    Architect = var.architect
  }
}

i also tried the above with variable (maybe wrong from another thread/guide)

my first errors where the "module.1_VPC.Public_Subnet_id" wasnt referenced but managed to get that bit but now it just ends up with

Error: creating EC2 Instance: InvalidSubnetID.NotFound: The subnet ID 'module.1_VPC.Public_Subnet_id' does not exist
│ status code: 400, request id: 00fa3944-4ea3-450b-9fd4-39645785269f

│ with module.EC2_VPN.aws_instance.web,
│ on .terraform/modules/EC2_VPN/main.tf line 17, in resource "aws_instance" "web":
│ 17: resource "aws_instance" "web" {

Again thankyou for taking the time, I am learning and trying to build / learn as I go not just copy and paste other templates.

tried various guides / terraform docs (most ref modules but in same file not separated folders)

i just need to be able to export a resourse_id for use in another child modules. once i can do this i will be able to duplicate for security groups and anything else i need to ref.

答案1

得分: 0

DISCLAIMER: 模块开发非常动态,有时主观性很强。以下是一些起点和支持您查询的要点。

在您的模块中似乎有很多需要解决的问题,但我会尽量分享您需要修复和初始化的最少信息。

1. 在您的 VPC 子模块中,通常不需要 subnet_id 属性。

module "VPC" {
    source = "/home/jamie/Terraform_Project/modules/1_VPC/";

    # subnet_id = "module.VPC.Public_Subnet_id"
}
  • 因为您在子模块中创建了子网本身,所以不需要此属性。
  • 只需要在这里定义必需的输入,简而言之,在调用子模块时需要定义那些没有设置默认值或您想要覆盖的变量定义。
  • subnet_id 和其他属性的输出应该被定义,以便其他模块可以使用。

2. 参数化 EC2 子模块

resource "aws_instance" "web" {
  ami           = data.aws_ami.ubuntu.id
  instance_type = "t3.micro"
  subnet_id     = var.subnet_id
  key_name      = "${var.project}_Key"

  tags = {
    Name      = "VPN_Server"
    Project   = "${var.project}"
    Architect = "${var.architect}"
  }
}
  • 您可以/应该参数化所有动态输入,就像在任何模块开发中一样,以便在需要时可以在根模块上进行覆盖/设置。

  • 一个小提示,"${var.project}" 并不是强制要求的,对于最近的 Terraform 版本,var.project 完全可以使用。

在这种情况下,我已经更新了 subnet_id = var.subnet_id

3. 调整具有新更改的 EC2 根模块。

module "EC2_VPN" {
    source = "/home/jamie/Terraform_Project/modules/3_EC2_VPN/";

    subnet_id = module.VPC.subnet_id
}
  • 正如第 2 点所述,您需要在 EC2 子模块中定义一个变量,并在 VPC 子模块中定义 subnet_id 的输出,以传递您需要的 subnet_id 值。

有用的文档

英文:

>> DISCLAIMER: Module development is very dynamic and sometimes subjective. Below are some points to start with and to support with your query.

There seem to be many things to be addressed in your modules but I would try to share the minimal info you need to fix and initialize this.

1. In your VPC child module you usually do not need the subnet_id attribute.

module "VPC" {
    source = "/home/jamie/Terraform_Project/modules/1_VPC/"


    # subnet_id = "module.VPC.Public_Subnet_id"
}
  • Because you are creating the subnet itself in the child module it is not required.
  • Only required inputs are needed to be defined here, in simple words variables definitions whose default values are not set or which you want to override are needed in the root module while calling the child module.
  • Output for subnet_id and any other attribute which makes sense to be used by other modules should be defined.

2. Parameterize the EC2 child module

resource "aws_instance" "web" {
  ami           = data.aws_ami.ubuntu.id
  instance_type = "t3.micro"
  subnet_id     = var.subnet_id
  key_name      = "${var.project}_Key"

  tags = {
    Name      = "VPN_Server"
    Project   = "${var.project}"
    Architect = "${var.architect}"
  }
}
  • You can/should parameterize all the inputs which are dynamic just like in any module development so that it can be overridden/set on the root module if required.

  • One side note that "${var.project}" is not mandatory var.project is totally fine with recent terraform versions.

In this case, I have updated subnet_id = var.subnet_id

3. Adapt the EC2 root module with new changes.

module "EC2_VPN" {
    source = "/home/jamie/Terraform_Project/modules/3_EC2_VPN/"

    subnet_id = module.VPC.subnet_id
}
  • As mentioned in 2. point you need a variable defined in your child module of EC2 and output for subnet_id in your VPC child module with the value of subnet_id you need here to pass.

Helpful documentation

huangapple
  • 本文由 发表于 2023年2月16日 04:45:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定