Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions .terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions backend.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
terraform {
backend "azurerm" {
storage_account_name = "yourstorageaccount"
container_name = "tfstate"
key = "terraform.tfstate"
resource_group_name = "mate-azure-task-12"
}
}
4 changes: 2 additions & 2 deletions install-app.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ apt-get update -yq
apt-get install python3-pip -yq

# Create a directory for the app and download the files.
mkdir /app
mkdir /app
# make sure to uncomment the line bellow and update the link with your GitHub username
# git clone https://github.com/<your-gh-username>/azure_task_12_deploy_app_with_vm_extention.git
git clone https://github.com/nichelangeloo/devops_todolist_terraform_task.git
cp -r devops_todolist_terraform_task/app/* /app

# create a service for the app via systemctl and start the app
Expand Down
50 changes: 50 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
terraform {
required_providers {
azurerm = {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The backend configuration must match checklist item #36: storage_account_name is required to be "yourstorageaccount", but here it is set to a different value. Update this to the specified name and ensure the corresponding storage account exists.

source = "hashicorp/azurerm"
version = "4.79.0"
}
}
}

provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "main" {
name = var.resource_group_name
location = var.location
}

module "network" {
source = "./modules/network"

location = var.location
resource_group_name = azurerm_resource_group.main.name
virtual_network_name = var.virtual_network_name
vnet_address_prefix = var.vnet_address_prefix
subnet_name = var.subnet_name
subnet_address_prefix = var.subnet_address_prefix
network_security_group_name = var.network_security_group_name
public_ip_address_name = var.public_ip_address_name
dns_label = var.dns_label
}

module "storage" {
source = "./modules/storage"

location = var.location
resource_group_name = azurerm_resource_group.main.name
}

module "compute" {
source = "./modules/compute"

location = var.location
resource_group_name = azurerm_resource_group.main.name
vm_name = var.vm_name
vm_size = var.vm_size
subnet_id = module.network.subnet_id
public_ip_id = module.network.public_address_ip_id
ssh_key_public = var.ssh_key_public
}
53 changes: 53 additions & 0 deletions modules/compute/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
resource "azurerm_network_interface" "main" {
name = "${var.vm_name}-nic"
location = var.location
resource_group_name = var.resource_group_name

ip_configuration {
name = "internal"
subnet_id = var.subnet_id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = var.public_ip_id
}
}

resource "azurerm_linux_virtual_machine" "main" {
name = var.vm_name
resource_group_name = var.resource_group_name
location = var.location
size = var.vm_size
admin_username = "adminuser"
network_interface_ids = [
azurerm_network_interface.main.id,
]

admin_ssh_key {
username = "adminuser"
public_key = var.ssh_key_public
}

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 +34 to +38

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This VM configuration correctly uses vm_name and vm_size to satisfy the required name (matebox) and size (Standard_B1s). The Ubuntu Jammy 22.04 image also matches the Ubuntu2204 requirement functionally, so you don’t need to change this unless your reviewers insist on a different image notation.

}
}

resource "azurerm_virtual_machine_extension" "main" {
name = "install-extension"
virtual_machine_id = azurerm_linux_virtual_machine.main.id
publisher = "Microsoft.Azure.Extensions"
type = "CustomScript"
type_handler_version = "2.0"

protected_settings = jsonencode({
"script" = base64encode(file("${path.root}/install-app.sh"))
})
Comment on lines +42 to +51

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the CustomScript extension with install-app.sh via protected_settings fulfills the VM extension requirements (type CustomScript and executing install-app.sh). Ensure the script path and contents remain consistent so the ToDo app deploys correctly.


}
11 changes: 11 additions & 0 deletions modules/compute/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
output "network_interface_id" {
value = azurerm_network_interface.main.id
}

output "virtual_machine_id" {
value = azurerm_linux_virtual_machine.main.id
}

output "virtual_machine_extension_id" {
value = azurerm_virtual_machine_extension.main.id
}
34 changes: 34 additions & 0 deletions modules/compute/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
variable "location" {
type = string
description = "The location of VM"
}

variable "resource_group_name" {
type = string
description = "The name of the resource group"
}

variable "vm_name" {
type = string
description = "The name of the VM"
}

variable "vm_size" {
type = string
description = "The size of the VM"
}

variable "ssh_key_public" {
type = string
description = "The content of the public ssh key"
}

variable "subnet_id" {
type = string
description = "The id of the subnet"
}

variable "public_ip_id" {
type = string
description = "The id of the public ip"
}
73 changes: 73 additions & 0 deletions modules/network/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
resource "azurerm_network_security_group" "main" {
name = var.network_security_group_name
location = var.location
resource_group_name = var.resource_group_name

security_rule {
name = "AllowSSH"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "22"
source_address_prefix = "*"
destination_address_prefix = "*"
}

security_rule {
name = "AllowHTTP"
priority = 110
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "80"
source_address_prefix = "*"
destination_address_prefix = "*"
}

security_rule {
name = "AllowApp8080"
priority = 120
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "8080"
source_address_prefix = "*"
destination_address_prefix = "*"
}
}

resource "azurerm_virtual_network" "main" {
name = var.virtual_network_name
location = var.location
resource_group_name = var.resource_group_name
address_space = [var.vnet_address_prefix]
}

resource "azurerm_subnet" "main" {
name = var.subnet_name
resource_group_name = var.resource_group_name
address_prefixes = [var.subnet_address_prefix]
virtual_network_name = azurerm_virtual_network.main.name
}

resource "random_integer" "dns-suffix" {
min = 1
max = 100
}

resource "azurerm_public_ip" "main" {
name = var.public_ip_address_name
resource_group_name = var.resource_group_name
location = var.location
allocation_method = "Dynamic"
domain_name_label = "${var.dns_label}-${random_integer.dns-suffix.result}"
}

resource "azurerm_subnet_network_security_group_association" "main" {
subnet_id = azurerm_subnet.main.id
network_security_group_id = azurerm_network_security_group.main.id
}
15 changes: 15 additions & 0 deletions modules/network/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
output "virtual_network_id" {
value = azurerm_virtual_network.main.id
}

output "virtual_network_security_id" {
value = azurerm_network_security_group.main.id
}

output "public_address_ip_id" {
value = azurerm_public_ip.main.id
}

output "subnet_id" {
value = azurerm_subnet.main.id
}
44 changes: 44 additions & 0 deletions modules/network/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
variable "location" {
type = string
description = "The location of VM"
}

variable "resource_group_name" {
type = string
description = "The name of the resource group"
}

variable "virtual_network_name" {
type = string
description = "The name of the virtual network"
}

variable "vnet_address_prefix" {
type = string
description = "The vnet address prefix"
}

variable "subnet_name" {
type = string
description = "The name of the subnet"
}

variable "subnet_address_prefix" {
type = string
description = "The subnet address prefix"
}

variable "network_security_group_name" {
type = string
description = "The name of the network security group"
}

variable "public_ip_address_name" {
type = string
description = "The name of the public ip address"
}

variable "dns_label" {
type = string
description = "The dns label"
}
13 changes: 13 additions & 0 deletions modules/storage/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
resource "azurerm_storage_account" "main" {
name = "nichelangeloost"
resource_group_name = var.resource_group_name
location = var.location
account_tier = "Standard"
account_replication_type = "LRS"
}

resource "azurerm_storage_container" "main" {
name = "task-artifacts"
storage_account_id = azurerm_storage_account.main.id
container_access_type = "private"
}
7 changes: 7 additions & 0 deletions modules/storage/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
output "storage_account_id" {
value = azurerm_storage_account.main.id
}

output "storage_container_id" {
value = azurerm_storage_container.main.id
}
9 changes: 9 additions & 0 deletions modules/storage/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
variable "location" {
type = string
description = "The location of VM"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This backend block is functionally valid, but checklist item #36 requires storage_account_name to be exactly "yourstorageaccount". Update this value to match the task description and ensure that storage account actually exists in the mate-azure-task-12 resource group.

}

variable "resource_group_name" {
type = string
description = "The name of the resource group"
}
3 changes: 3 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "resource_group_id" {
value = azurerm_resource_group.main.id
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This backend block works technically, but checklist item #36 requires storage_account_name = "yourstorageaccount". Update this value and ensure the corresponding storage account and tfstate container exist in Azure.

Loading
Loading