简体   繁体   English

地形模块 ec2 和 vpc AWS

[英]terraform modules ec2 and vpc AWS

I have question about how use modules in terraform.我对如何在 terraform 中使用模块有疑问。 See below my code.请参阅下面我的代码。

module "aws_vpc"{
  source = "../modules/vpc"
  vpc_cidr_block = "192.168.0.0/16"
  name_cidr = "ec2-eks"
  name_subnet = "ec2-eks-subnet"
  subnet_cidr = ["192.168.1.0/25"]
}
module "ec2-eks" {
  source = "../modules/ec2"
  ami_id = "ami-07c8bc5c1ce9598c3"
  subnet_id = module.aws_vpc.aws_subnet[0]
  count_server = 1
}
output "aws_vpc" {
  value = module.aws_vpc.aws_subnet[0]
}

I`m creating a vpc and want the next step to attach ec2 by my created subnet.But terraform attached by VPC of default.我正在创建一个 vpc,并希望下一步通过我创建的子网附加 ec2。但是 terraform 由默认的 VPC 附加。 What do I need to do that attach ec2 to my vpc(subnet)?我需要做什么才能将 ec2 附加到我的 vpc(子网)? Thank you for you answers谢谢你的回答

What do I need to do that attach ec2 to my vpc(subnet)?我需要做什么才能将 ec2 附加到我的 vpc(子网)?

aws_instance has subnet_id attribute. aws_instance具有subnet_id属性。 Thus to place your instance in a given subnet, you have to set the subnet_id .因此,要将您的实例放置在给定的子网中,您必须设置subnet_id

Since you are using a module to create your aws_vpc , likely the module will output subnet IDs as well.由于您使用模块来创建aws_vpc ,因此该模块可能也会输出子网 ID。 Due to lack of details of the module, its difficult to provide an exact answer, but in a general scenario you would do something along these lines (example):由于缺乏模块的详细信息,很难提供确切的答案,但在一般情况下,您会按照以下方式做一些事情(示例):

resource "aws_instance" "web" {
  ami           = data.aws_ami.ubuntu.id
  instance_type = "t3.micro"

  subnet_id = module.aws_vpc.subnet_id

  tags = {
    Name = "HelloWorld"
  }
}

Obviously, the above depends on the implementation of your module.显然,以上取决于您的模块的实现。

Thank you.谢谢你。 I`ve got success resources in AWS.我在 AWS 中有成功资源。 I forget to set in the module of ec2 a parameter subnet_id我忘记在ec2的模块中设置一个参数subnet_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM