1
votes

I have question about how use modules in 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. What do I need to do that attach ec2 to my vpc(subnet)? Thank you for you answers

2
Welcome to the community. To improve the chance of getting your question answered, please try to put your subject a bit more specific to reflect your question instead of being too generic. For example, this question subject line "terraform modules ec2 and vpc AWS" does not indicate your problem. It could have been better if you say "How to attach AWS VPC subnet to EKS in Terraform". For general guidance, please refer stackoverflow.com/help/how-to-askkrishg
Additionally, your Terraform code seems incomplete, which modules are you using to deploy your infrastructure?Nebulastic

2 Answers

0
votes

What do I need to do that attach ec2 to my vpc(subnet)?

aws_instance has subnet_id attribute. Thus to place your instance in a given subnet, you have to set the subnet_id.

Since you are using a module to create your aws_vpc, likely the module will output subnet IDs as well. 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.

0
votes

Thank you. I`ve got success resources in AWS. I forget to set in the module of ec2 a parameter subnet_id