-
Notifications
You must be signed in to change notification settings - Fork 70
experimental-1 #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
experimental-1 #36
Changes from all commits
d890863
2746bfa
61dc799
909062e
8a7576f
6582c9b
22ae2a3
f873db6
1302248
e7e7404
21c60d7
bc725f9
7e63fd3
d9feb96
366f3ca
05264d8
0a38aeb
bdd6e51
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| .terraform/ | ||
| *.tfstate | ||
| *.tfstate.* | ||
| terraform.tfvars | ||
| override.tf | ||
| override.tf.json | ||
| crash.log |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| terraform { | ||
| backend "azurerm" { | ||
| resource_group_name = "tfstate" | ||
| storage_account_name = "terra5" | ||
| container_name = "tfstate" | ||
| key = "terraform.tfstate" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| terraform { | ||
| required_version = ">= 1.5.0, < 2.0.0" | ||
|
|
||
| required_providers { | ||
| azurerm = { | ||
| source = "hashicorp/azurerm" | ||
| version = "~> 3.75" | ||
| } | ||
| random = { | ||
| source = "hashicorp/random" | ||
| version = "~> 3.5" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| provider "azurerm" { | ||
| features {} | ||
| subscription_id = var.subscription_id | ||
| } | ||
|
|
||
| resource "random_integer" "dns_suffix" { | ||
| min = 1000 | ||
| max = 9999 | ||
| } | ||
|
|
||
| locals { | ||
| dns_label = "${var.dns_label}${random_integer.dns_suffix.result}" | ||
|
|
||
| common_tags = merge( | ||
| { | ||
| environment = var.environment | ||
| project = var.resource_group_name | ||
| managed_by = "terraform" | ||
| }, | ||
| var.tags | ||
| ) | ||
| } | ||
|
|
||
| resource "azurerm_resource_group" "rg" { | ||
| name = var.resource_group_name | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The resource group is created from |
||
| location = var.location | ||
| tags = local.common_tags | ||
| } | ||
|
|
||
| module "network" { | ||
| source = "./modules/network" | ||
|
|
||
| resource_group_name = azurerm_resource_group.rg.name | ||
| location = azurerm_resource_group.rg.location | ||
| virtual_network_name = var.virtual_network_name | ||
| vnet_address_prefix = var.vnet_address_prefix # fixed: was [var.vnet_address_prefix] | ||
| subnet_name = var.subnet_name | ||
| subnet_address_prefix = var.subnet_address_prefix # fixed: was [var.subnet_address_prefix] | ||
| network_security_group_name = var.network_security_group_name | ||
| public_ip_address_name = var.public_ip_address_name | ||
| dns_label = local.dns_label # fixed: now globally unique | ||
| } | ||
|
|
||
| module "storage" { | ||
| source = "./modules/storage" | ||
|
|
||
| resource_group_name = azurerm_resource_group.rg.name | ||
| location = azurerm_resource_group.rg.location | ||
| storage_account_name = var.storage_account_name # fixed: matched to variable name below | ||
| storage_container_name = var.storage_container_name # fixed: matched to variable name below | ||
| } | ||
|
|
||
| module "compute" { | ||
| source = "./modules/compute" | ||
|
|
||
| resource_group_name = azurerm_resource_group.rg.name | ||
| location = azurerm_resource_group.rg.location | ||
| vm_name = var.vm_name | ||
| vm_size = var.vm_size | ||
| ssh_key_public = var.ssh_key_public | ||
| subnet_id = module.network.subnet_id | ||
| public_ip_id = module.network.public_ip_id | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Compute module expects
Comment on lines
+76
to
+77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verify the network module implements the outputs that the compute module expects ( |
||
| nsg_id = module.network.nsg_id | ||
| script_url = module.storage.script_url | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| # Creates the Network Interface for the VM and associates the NSG. | ||
| resource "azurerm_network_interface" "main" { | ||
| name = "${var.vm_name}-nic" | ||
| location = var.location | ||
| resource_group_name = var.resource_group_name | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The NIC resource currently does not associate the Network Security Group. The checklist requires the NIC to be associated with the provided NSG. Add an NSG association (for example by adding |
||
| ip_configuration { | ||
| name = "internal" | ||
| subnet_id = var.subnet_id | ||
| private_ip_address_allocation = "Dynamic" | ||
| public_ip_address_id = var.public_ip_id | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The NIC's ip_configuration sets |
||
| } | ||
| } | ||
|
|
||
| # Associates the Network Security Group with the Network Interface. | ||
| resource "azurerm_network_interface_security_group_association" "main" { | ||
| network_interface_id = azurerm_network_interface.main.id | ||
| network_security_group_id = var.nsg_id | ||
|
Comment on lines
+16
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You associate the NSG to the NIC using |
||
| } | ||
|
|
||
| # Creates the SSH public key resource as required by the task. | ||
| resource "azurerm_ssh_public_key" "main" { | ||
| name = "linuxboxsshkey" | ||
| resource_group_name = var.resource_group_name | ||
| location = var.location | ||
| public_key = var.ssh_key_public | ||
| } | ||
|
|
||
| # Creates the Linux Virtual Machine. | ||
| resource "azurerm_linux_virtual_machine" "main" { | ||
| name = var.vm_name | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The VM |
||
| resource_group_name = var.resource_group_name | ||
| location = var.location | ||
| size = var.vm_size | ||
| admin_username = "azureuser" | ||
| network_interface_ids = [azurerm_network_interface.main.id] | ||
|
|
||
| admin_ssh_key { | ||
| username = "azureuser" | ||
| public_key = azurerm_ssh_public_key.main.public_key | ||
| } | ||
|
|
||
| os_disk { | ||
| caching = "ReadWrite" | ||
| storage_account_type = "Standard_LRS" | ||
| } | ||
|
|
||
| source_image_reference { | ||
| publisher = "Canonical" | ||
| offer = "0001-com-ubuntu-server-jammy" | ||
| sku = "22_04-lts" | ||
| version = "latest" | ||
| } | ||
|
Comment on lines
+48
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Double-check that the |
||
| } | ||
|
|
||
| # Creates the VM extension to download and execute the installation script. | ||
| resource "azurerm_virtual_machine_extension" "main" { | ||
| name = "${var.vm_name}-install-app" | ||
| virtual_machine_id = azurerm_linux_virtual_machine.main.id | ||
| publisher = "Microsoft.Azure.Extensions" | ||
| type = "CustomScript" | ||
| type_handler_version = "2.0" | ||
|
|
||
| settings = <<SETTINGS | ||
| { | ||
| "fileUris": ["${var.script_url}"], | ||
|
Comment on lines
+64
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The CustomScript extension uses |
||
| "commandToExecute": "bash install-app.sh" | ||
| } | ||
| SETTINGS | ||
|
Comment on lines
+64
to
+69
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Custom Script extension Ensure |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| output "vm_id" { | ||
| value = azurerm_linux_virtual_machine.main.id | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The referenced resources ( |
||
| description = "The ID of the virtual machine." | ||
|
Comment on lines
+1
to
+3
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This output references |
||
| } | ||
|
|
||
| # Corrected the value to get the private IP from the IP configuration. | ||
| output "vm_private_ip" { | ||
| value = azurerm_network_interface.main.ip_configuration[0].private_ip_address | ||
| description = "The private IP address of the virtual machine." | ||
|
Comment on lines
+7
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This output references
|
||
| } | ||
|
|
||
| # Added public IP output for easier verification. | ||
| output "vm_public_ip" { | ||
| value = azurerm_network_interface.main.ip_configuration[0].public_ip_address_id != null ? data.azurerm_public_ip.vm_pip.ip_address : "" | ||
|
Comment on lines
+13
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The ternary expression here depends on
|
||
| description = "The public IP address of the virtual machine." | ||
| } | ||
|
|
||
| data "azurerm_public_ip" "vm_pip" { | ||
| name = split("/", azurerm_network_interface.main.ip_configuration[0].public_ip_address_id)[8] | ||
| resource_group_name = var.resource_group_name | ||
| depends_on = [azurerm_linux_virtual_machine.main] | ||
|
Comment on lines
+18
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| variable "resource_group_name" { | ||
| description = "Name of the resource group" | ||
| type = string | ||
| } | ||
|
Comment on lines
+1
to
+4
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The variable |
||
|
|
||
| variable "location" { | ||
| description = "Azure region for resources" | ||
| type = string | ||
| } | ||
|
Comment on lines
+6
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
|
||
| variable "vm_name" { | ||
| description = "Name of the virtual machine" | ||
| type = string | ||
| } | ||
|
|
||
| variable "vm_size" { | ||
| description = "Size of the virtual machine" | ||
| type = string | ||
| } | ||
|
|
||
| variable "ssh_key_public" { | ||
| description = "Public SSH key for the virtual machine" | ||
| type = string | ||
| sensitive = true | ||
| } | ||
|
Comment on lines
+21
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| variable "subnet_id" { | ||
| description = "ID of the subnet to connect the VM to" | ||
| type = string | ||
| } | ||
|
Comment on lines
+27
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| variable "public_ip_id" { | ||
| description = "ID of the public IP address to associate with the VM" | ||
| type = string | ||
| } | ||
|
Comment on lines
+32
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| # Added variable for the Network Security Group ID. | ||
| variable "nsg_id" { | ||
| description = "ID of the Network Security Group to associate with the NIC" | ||
| type = string | ||
| } | ||
|
|
||
| variable "script_url" { | ||
| description = "URL of the script to execute on the VM" | ||
| type = string | ||
| } | ||
|
Comment on lines
+43
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The module accepts There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing required variable: add |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| resource "azurerm_virtual_network" "vnet" { | ||
| name = var.virtual_network_name | ||
| location = var.location | ||
| resource_group_name = var.resource_group_name | ||
| address_space = var.vnet_address_prefix | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| resource "azurerm_subnet" "subnet" { | ||
| name = var.subnet_name | ||
| resource_group_name = var.resource_group_name | ||
| virtual_network_name = azurerm_virtual_network.vnet.name | ||
| address_prefixes = var.subnet_address_prefix | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| resource "azurerm_network_security_group" "nsg" { | ||
| name = var.network_security_group_name | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The NSG name is taken from |
||
| location = var.location | ||
| resource_group_name = var.resource_group_name | ||
|
Comment on lines
+15
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Network Security Group: you defined inbound rules for SSH and HTTP which make the VM reachable — good. Note that the module does not attach the NSG to the subnet here; that’s acceptable because the compute module associates the NSG to the NIC via |
||
|
|
||
| security_rule { | ||
| name = "SSH" | ||
| priority = 1001 | ||
| direction = "Inbound" | ||
| access = "Allow" | ||
| protocol = "Tcp" | ||
| source_port_range = "*" | ||
| destination_port_range = "22" | ||
| source_address_prefix = "*" | ||
| destination_address_prefix = "*" | ||
| } | ||
| security_rule { | ||
| name = "HTTP" | ||
| priority = 1002 | ||
| direction = "Inbound" | ||
| access = "Allow" | ||
| protocol = "Tcp" | ||
| source_port_range = "*" | ||
| destination_port_range = "80" | ||
| source_address_prefix = "*" | ||
| destination_address_prefix = "*" | ||
| } | ||
| } | ||
|
|
||
| resource "azurerm_public_ip" "pip" { | ||
| name = var.public_ip_address_name | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Public IP |
||
| location = var.location | ||
| resource_group_name = var.resource_group_name | ||
| allocation_method = "Dynamic" | ||
| domain_name_label = "${var.dns_label}${random_integer.r.result}" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You correctly set There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| tags = { | ||
| environment = "dev" | ||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The root and compute module expect outputs like |
||
|
|
||
| resource "random_integer" "r" { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You created |
||
| min = 10000 | ||
| max = 99999 | ||
| } | ||
|
Comment on lines
+56
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # The subnet ID. | ||
| output "subnet_id" { | ||
| description = "The ID of the subnet." | ||
| value = azurerm_subnet.subnet.id | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This output references |
||
| } | ||
|
|
||
| # The Public IP Address ID. Required for NIC association. | ||
| output "public_ip_id" { | ||
| description = "The ID of the Public IP Address." | ||
| value = azurerm_public_ip.pip.id | ||
|
Comment on lines
+8
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good — |
||
| } | ||
|
|
||
| # The Network Security Group ID. Required for NIC association. | ||
| output "nsg_id" { | ||
| description = "The ID of the Network Security Group." | ||
| value = azurerm_network_security_group.nsg.id | ||
|
Comment on lines
+14
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This output uses |
||
| } | ||
|
|
||
| # The Public IP address of the Linux VM. | ||
| output "public_ip_address" { | ||
| description = "The Public IP Address of the Linux VM." | ||
| value = azurerm_public_ip.pip.ip_address | ||
|
Comment on lines
+20
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The compute/root configuration expects |
||
| } | ||
|
|
||
| # The FQDN of the Public IP Address. | ||
| output "public_ip_fqdn" { | ||
| description = "The FQDN of the Public IP Address." | ||
| value = azurerm_public_ip.pip.fqdn | ||
|
Comment on lines
+26
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Comment on lines
+20
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
terraform {}block is present here but the task requires a remote backend configured inbackend.tfwithstorage_account_name = "yourstorageaccount"(literal), containertfstate, resource groupmate-azure-task-12, and keyterraform.tfstate. Make sure you have a separatebackend.tffile with those exact values. If you have backend code elsewhere, confirmstorage_account_nameis exactlyyourstorageaccount(this is a high-priority literal requirement).