A managed resource “aws_subnet” “pubsubnet” has not been declared in the root module.

Hi Techies,

We are going to see how to resolve ‘A managed resource “aws_subnet” “pubsubnet” has not been declared in the root module.’  error while using terraform to create 2 EC2 instance with VPC in AWS

code:

resource "aws_instance" "pub_instance" {
ami = "ami-033fabdd332044f06"
instance_type = "t2.micro"
availability_zone = "us-east-2a"
associate_public_ip_address = "true"
vpc_security_group_ids = [aws_security_group.PUBSG.id]
subnet_id = aws_subnet.pubsubnet.id
key_name = "Terraform_Srv"

tags = {
Name = "WEBSERVER"
}

}

resource "aws_instance" "pvt_instance" {
ami = "ami-033fabdd332044f06"
instance_type = "t2.micro"
availability_zone = "us-east-2b"
associate_public_ip_address = "true"
vpc_security_group_ids = [aws_security_group.PVTSG.id]
subnet_id = aws_subnet.pvtsubnet.id
key_name = "Terraform_Srv"

tags = {
Name = "APPSERVER"
}

Error:

[root@ip-172-31-7-226 terraform]# terraform apply
╷
│ Error: Reference to undeclared resource
│
│ on main.tf line 163, in resource "aws_instance" "pub_instance":
│ 163: subnet_id = aws_subnet.pubsubnet.id
│
│ A managed resource "aws_subnet" "pubsubnet" has not been declared in the root module.
╵
╷
│ Error: Reference to undeclared resource
│
│ on main.tf line 178, in resource "aws_instance" "pvt_instance":
│ 178: subnet_id = aws_subnet.pvtsubnet.id
│
│ A managed resource "aws_subnet" "pvtsubnet" has not been declared in the root module.

After checking the full code of script, found that I have declared the names like PUBSUB & PVTSUB for public and private subnet wiseversa.

But I wrongly mentioned like pubsubnet & pvtsubnet in the EC2 instance creation. So, after renaming it to correctly like below it is fixed.

subnet_id = aws_subnet.pubsubnet.id
subnet_id = aws_subnet.pubsubnet.id

modified like below based on the declared name inn previous code

subnet_id = aws_subnet.PUBSUB.id
subnet_id = aws_subnet.PVTSUB.id

Result:

After changing the error was fixed and find the below result

aws_instance.pvt_instance: Creating...
aws_instance.pub_instance: Creating...
aws_route_table.PVTRT: Modifying... [id=rtb-05ab30d4598210e59]
aws_route_table.PVTRT: Modifications complete after 0s [id=rtb-05ab30d4598210e59]
aws_instance.pub_instance: Still creating... [10s elapsed]
aws_instance.pvt_instance: Still creating... [10s elapsed]
aws_instance.pvt_instance: Still creating... [20s elapsed]
aws_instance.pub_instance: Still creating... [20s elapsed]
aws_instance.pub_instance: Still creating... [30s elapsed]
aws_instance.pvt_instance: Still creating... [30s elapsed]
aws_instance.pvt_instance: Creation complete after 31s [id=i-096c28fbbaeff8a42]
aws_instance.pub_instance: Creation complete after 31s [id=i-0f745c3aeca6327aa]

Apply complete! Resources: 2 added, 1 changed, 0 destroyed.
[root@ip-172-31-7-226 terraform]#

Error: creating EC2 Instance: operation error EC2: Run Instances, https response error StatusCode: 400, error InvalidSubnetID.NotFound: The subnet ID ‘aws_subnet.pubsubnet.id’ does not exist

Hi Techies!

Good day!

We are going to see how to troubleshoot issues in terraform(Alternate Ansible) script which will create VPC in AWS.

In my script the file name is main.tf and we need to execute “terraform init, terraform validate, terraform apply” commands one by one.

terraform init:   It will initialize the directory, which will contains the terraform configuration file(in our scenario main.tf)

terraform validate: It will to check the syntax and consistency of your Terraform configuration files without accessing remote services

terraform plan: It will help to create an execution plan and preview the changes to your infrastructure.

terraform apply: It will execute the proposed actions in a Terraform plan.

In our scenario I’m getting error while applying it (terraform apply)

Error:

Error: creating EC2 Instance: operation error EC2: RunInstances, https response error StatusCode: 400, RequestID: 5eea2384-64e8-4a57-ba1a-2ac955c799f9, api error InvalidSubnetID.NotFound: The subnet ID 'aws_subnet.pubsubnet.id' does not exist
│
│ with aws_instance.pub_instance,
│ on main.tf line 157, in resource "aws_instance" "pub_instance":
│ 157: resource "aws_instance" "pub_instance" {
│
╵
╷
│ Error: creating EC2 Instance: operation error EC2: RunInstances, https response error StatusCode: 400, RequestID: 64d5c412-e7db-49ad-a7aa-49b4f9345d89, api error InvalidSubnetID.NotFound: The subnet ID 'aws_subnet.pvtsubnet.id' does not exist
│
│ with aws_instance.pvt_instance,
│ on main.tf line 172, in resource "aws_instance" "pvt_instance":
│ 172: resource "aws_instance" "pvt_instance" {

Code in my script:

 resource "aws_instance" "pub_instance" {
ami                                     = "ami-033fabdd332044f06"
instance_type                           = "t2.micro"
availability_zone                       = "us-east-2a"
associate_public_ip_address             = "true"
vpc_security_group_ids                  = [aws_security_group.PUBSG.id]
subnet_id                               = "aws_subnet.PUBSUB.id"
key_name                                = "Terraform_Srv"

  tags = {
  Name = "WEBSERVER"
 }

}

resource "aws_instance" "pvt_instance" {
ami                                     = "ami-033fabdd332044f06"
instance_type                           = "t2.micro"
availability_zone                       = "us-east-2b"
associate_public_ip_address             = "true"
vpc_security_group_ids                  = [aws_security_group.PVTSG.id]
subnet_id                               = "aws_subnet.PVTSUB.id"
key_name                                = "Terraform_Srv"

  tags = {
  Name = "APPSERVER"
 }

}

Solution:

In may script I have used double quotes (“”) to mention subnet id. In my case I have removed double quotes and it fixed the issue.

resource "aws_instance" "pub_instance" {
ami                                    = "ami-033fabdd332044f06"
instance_type                          = "t2.micro"
availability_zone                      = "us-east-2a"
associate_public_ip_address            = "true"
vpc_security_group_ids                 = [aws_security_group.PUBSG.id]
subnet_id                              = aws_subnet.PUBSUB.id
key_name                               = "Terraform_Srv"

tags = {
Name = "WEBSERVER"
}

}

resource "aws_instance" "pvt_instance" {
ami                                    = "ami-033fabdd332044f06"
instance_type                          = "t2.micro"
availability_zone                      = "us-east-2b"
associate_public_ip_address            = "true"
vpc_security_group_ids                 = [aws_security_group.PVTSG.id]
subnet_id                              = aws_subnet.PVTSUB.id
key_name                               = "Terraform_Srv"

tags = {
Name = "APPSERVER"
}

}

Now while executing “terraform apply” it created ec2-instance along with VPC successfully.

Result:

#terraform apply

aws_instance.pvt_instance: Creating...
aws_instance.pub_instance: Creating...
aws_route_table.PVTRT: Modifying... [id=rtb-05ab30d4598210e59]
aws_route_table.PVTRT: Modifications complete after 0s [id=rtb-05ab30d4598210e59]
aws_instance.pub_instance: Still creating... [10s elapsed]
aws_instance.pvt_instance: Still creating... [10s elapsed]
aws_instance.pvt_instance: Still creating... [20s elapsed]
aws_instance.pub_instance: Still creating... [20s elapsed]
aws_instance.pub_instance: Still creating... [30s elapsed]
aws_instance.pvt_instance: Still creating... [30s elapsed]
aws_instance.pvt_instance: Creation complete after 31s [id=i-096c28fbbaeff8a42]
aws_instance.pub_instance: Creation complete after 31s [id=i-0f745c3aeca6327aa]
Apply complete! Resources: 2 added, 1 changed, 0 destroyed.
[root@ip-172-31-7-226 terraform]#

 

Linux Basic Interview Questions

Hi Everyone ! Will see Linux interview questions in this post. Many of them are getting difficulty on finding their job in Linux, without knowing the scenario-based question in Linux. So, we decided to help by sharing some Linux interview questions.

1. Explain Linux booting process: We already shared in another post regarding booting process in RHEL. Please make use of it to know brief about linux booting.

2. What is port number for http/https/nfs/dns/dhcp/telnet/ssh/ftp? https: 443, http: 80, nfs:111 and 2049, DNS: 245, dhcp: 546(client) 547 (server) 67  and 68

3. Explain architecture of ansible? Ansible architecture has been posted. Please read this post to know more.

4. What is split brain syndrome in vcs cluster? We already shred a post to know what is split brain syndrome in vcs. Please read that to know more.

5. How boot a linux host using old kernel, if new kernel update fails? Steps are shared in a post to know how to boot linux host with old kernel? Please read that post to know more.

6. How to enable EPEL Repository? Post contains steps to enable EPEL Repository in linux. Please read that.

7. How to increase xfs filesystem in LVM?
Please read the post Increasing LVM in xfs filesystem in our blog to know more.

8. Explain steps to configure LVM?
Please check the post which we shared earlier to know How to configure LVM?

9. What is the command to know whether one port is enabled or not in Linux? 
netstat command will help to know the listening port and need to use grep to filter
                       #netstat -tulpn | grep 8080

 

Will keep on update this post and if you have any question to know answer in Linux. Please