Reputation: 1
Here is my tree command execute in terminal enter image description here
When i execute terraform plan i got below error and my issue i want to deploy a new VM in Azure/GCP/AWS using terraform only one file i.e., only i need to change just resource type, machine type, resource name and others in terraform files which is going to deploy in Azure/GCP/AWS. So for i got below terraform file to used it but got error dont know will all this files will work or not in Azure/GCP/AWS.
Im new to terraform struck below error from many hours search google didnt understand where i can learn more on modules
Here is my error
│ Error: Reference to undeclared resource
│
│ on output.tf line 5, in output "aws_ip":
│ 5: value = aws_instance.vm_instance.public_ip
│
│ A managed resource "aws_instance" "vm_instance" has not been declared in the root module.
╵
╷
│ Error: Reference to undeclared resource
│
│ on output.tf line 10, in output "aws_ssh_connection_string":
│ 10: value = modules.aws.ssh_connection_string
│
│ A managed resource "modules" "aws" has not been declared in the root module.
Here is my output.tf file
### AWS Outputs ###
output "aws_ip" {
description = "The public IP address of the AWS virtual machine"
# value = modules.aws.ip
value = aws_instance.vm_instance.public_ip
}
output "aws_ssh_connection_string" {
description = "The SSH command to connect to the AWS virtual machine"
value = modules.aws.ssh_connection_string
}
Here is my main.tf file
module "AWS" {
aws_region = var.aws_region
aws_creds_file = var.aws_creds_file
aws_profile = var.aws_profile
ssh_pub_key_file = var.ssh_pub_key_file
ssh_priv_key_file = var.ssh_priv_key_file
aws_instance = var.vm_instance.public_ip
source = "./modules/aws/"
# source = "./home/osboxes/Documents/Terraform-All-VMs/Onetime-Deploy/modules/aws"
}
#module "Azure" {
# azure_location = var.azure_location
# ssh_user = var.ssh_user
# ssh_pub_key_file = var.ssh_pub_key_file
# ssh_priv_key_file = var.ssh_priv_key_file
# source = "./modules/azure"
#}
Here is my variable.tf
### AWS Specific Variables ###
variable "aws_region" {
description = "The AWS region to deploy the resources to"
}
variable "aws_creds_file" {
description = "The full path to the .aws/credentials file"
}
variable "aws_profile" {
description = "The profile in the credentials tile to use"
}
### Azure Specific Variables ###
#variable "azure_location" {
# description = "The region of Azure resource group for this HitC Exercise"
# type = string
#}
### GCP Specific Variables ###
#variable "gcp_project" {
# description = "The GCP Project ID to deploy the VM"
#}
#variable "gcp_region" {
# description = "The GCP Region to deploy the VM"
#}
#variable "gcp_zone" {
# description = "The GCP Zone to deploy the VM"
#}
#variable "gcp_key_file" {
# description = "The path to the GCP Service Account"
#}
### Common Variables ###
variable "ssh_user" {
description = "The local user account to create on the virtual machine"
type = string
}
variable "ssh_pub_key_file" {
description = "The path to the public SSH key associated with the local user account"
type = string
}
variable "ssh_priv_key_file" {
description = "The path to the private SSH key associated with the local user account"
type = string
}
Here is my modules/aws/output.tf files
output "ip" {
description = "The public IP address of the virtual machine"
value = aws_instance.vm_instance.public_ip
}
output "ssh_connection_string" {
description = "The SSH command to connect to the virtual machine"
value = "ssh -i ${var.ssh_priv_key_file} ubuntu@${aws_instance.vm_instance.public_ip}"
}
i try to add some value = modules.aws.ip but couldnt understand how to add or change when comes to modules in output or in different files
as i said i want to deploy in aws/gcp/azure any one cloud at a time using above files, if files are not correct plse suggest to me how to write good output.tf and main.tf and variables.tf many more
please as im new in terraform
If u need any other file do let me will share it
Upvotes: 0
Views: 1564
Reputation: 74604
If you are intending to refer to your module "AWS"
block then the correct reference syntax would be module.AWS
.
Terraform thinks that modules.aws
is referring to a resource of type "modules" with the name "aws", because a resource type is the default interpretation of any top-level symbol that isn't a reserved word. The singular module
is the reserved word for referring to module calls, and the attributes of that pseudo-object are case-sensitive.
Upvotes: 0