This is my module for Linux VM in aws.
vm_linux.tf
provider "aws" {
version = "2.62.0"
region = var.region
}
data "aws_ami" "amazon_linux" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = [
"amzn-ami-hvm-*-x86_64-gp2",
]
}
filter {
name = "owner-alias"
values = [
"amazon",
]
}
}
resource "aws_network_interface" "main" {
count = 1
subnet_id = module.vpc.vp_id
}
resource "aws_instance" "main" {
count = var.instance_count
ami = data.aws_ami.amazon_linux.id
instance_type = var.instance_type
key_name = var.key_pair
vpc_security_group_ids = var.vpc_sg
subnet_id = module.vpc.sub_id
associate_public_ip_address = true
root_block_device {
volume_type = "gp2"
volume_size = 30
}
ebs_block_device {
device_name = "/dev/sdf"
volume_type = "gp2"
volume_size = 10
}
network_interface {
device_index = 0
network_interface_id = aws_network_interface.main[0].id
delete_on_termination = false
}
}
and this is the variable file for this module.
vars.tf
variable "vpc_id" {
description = "The ID of VPC in which to create ec2 instance."
default = ""
}
variable "instance_count" {
description = "Number of instances to be created"
default = "1"
}
variable "subnet" {}
variable "instance_type" {
default = "t2.micro"
}
variable "vpc_sg" {
description = "The ID of one or more security groups to associate with the ec2 instance"
default = []
}
variable "region" {
description = "AWS region to deploy testVM"
default = "us-east-1"
}
variable "key_pair" {
description = "SSH public key pair details"
default = ""
}
This is a vpc module file.
vpc.tf
data "aws_availability_zones" "available" {}
provider "aws" {
region = var.region
}
resource "aws_vpc" "vpc" {
cidr_block = var.vpc_cidr
enable_dns_hostnames = var.dns_hostnames
enable_dns_support = var.dns_support
tags = merge(
{
"Name" = format("%s", var.name)
},
var.tags,
var.vpc_tags,
)
}
resource "aws_subnet" "main" {
vpc_id = "${aws_vpc.vpc.id}"
cidr_block = "${var.subnet_cidr}"
tags = {
Name = "Main"
}
}
and finally, my main.tf file
main.tf
module "my_vpc" {
source = "../module/vpc"
org_name = "###"
subscription_name = "###"
#vpc_id = "${module.my_vpc.vpc_id}"
tags = {
name = "###"
}
}
module "linux_vm" {
source = "../module/vm_linux"
subnet_id = "${module.my_vpc.sub_id}"
}
ERROR
Error: Missing required argument
on main.tf line 14, in module "linux_vm":
14: module "linux_vm" {
The argument "subnet" is required, but no definition was found.
Error: Unsupported argument
on main.tf line 16, in module "linux_vm":
16: subnet_id = "${module.my_vpc.sub_id}"
An argument named "subnet_id" is not expected here.
I am not able to understand this error. terraform asking me for subnet_id and the same time it’s not expecting subnet_id here. can you plz help me with this issue. I want to understand this error.