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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.tf text eol=lf
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.

1 change: 1 addition & 0 deletions .terraform/modules/modules.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Modules":[{"Key":"","Source":"","Dir":"."},{"Key":"compute","Source":"./modules/compute","Dir":"modules/compute"},{"Key":"network","Source":"./modules/network","Dir":"modules/network"},{"Key":"storage","Source":"./modules/storage","Dir":"modules/storage"}]}
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" {
resource_group_name = "mate-azure-task-12"
storage_account_name = "yourstorageaccount"
container_name = "tfstate"
key = "terraform.tfstate"
}
}
60 changes: 60 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
terraform {
required_version = ">= 1.5.0"

required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "3.105.0"
}
random = {
source = "hashicorp/random"
version = "~> 3.6"
}
}
}

provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "this" {
name = var.resource_group_name
location = var.location
tags = var.tags
}

module "network" {
source = "./modules/network"
resource_group_name = azurerm_resource_group.this.name
location = azurerm_resource_group.this.location
tags = var.tags

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 "compute" {
source = "./modules/compute"
resource_group_name = azurerm_resource_group.this.name
location = azurerm_resource_group.this.location
tags = var.tags

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
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

According to the task requirements, a storage account needs to be created. You have a storage module prepared for this, but it's not being called in this file. Please add a module "storage" block to ensure all required resources are provisioned.


module "storage" {
source = "./modules/storage"
resource_group_name = azurerm_resource_group.this.name
location = azurerm_resource_group.this.location
tags = var.tags
}
2 changes: 1 addition & 1 deletion install-app.sh → modules/compute/install-app.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ apt-get install python3-pip -yq
# Create a directory for the app and download the files.
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/nizartem/azure_task_12_deploy_app_with_vm_extention.git
cp -r devops_todolist_terraform_task/app/* /app

# create a service for the app via systemctl and start the app
Expand Down
72 changes: 72 additions & 0 deletions modules/compute/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
locals {
install_script_b64 = filebase64("${path.module}/${var.install_script_path}")
}

resource "azurerm_ssh_public_key" "linuxboxsshkey" {
name = "linuxboxsshkey"
location = var.location
resource_group_name = var.resource_group_name
public_key = var.ssh_key_public

tags = var.tags
}

resource "azurerm_network_interface" "this" {
name = "${var.vm_name}-nic"
location = var.location
resource_group_name = var.resource_group_name
tags = var.tags

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

resource "azurerm_linux_virtual_machine" "this" {
name = var.vm_name
location = var.location
resource_group_name = var.resource_group_name
size = var.vm_size

admin_username = var.admin_username
disable_password_authentication = true

network_interface_ids = [
azurerm_network_interface.this.id
]

admin_ssh_key {
username = var.admin_username
public_key = azurerm_ssh_public_key.linuxboxsshkey.public_key
}

os_disk {
name = "${var.vm_name}-osdisk"
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}

source_image_reference {
publisher = "Canonical"
offer = "0001-com-ubuntu-server-jammy"
sku = "22_04-lts-gen2"
version = "latest"
}

tags = var.tags
}

resource "azurerm_virtual_machine_extension" "install_app" {
name = "${var.vm_name}-install-app"
virtual_machine_id = azurerm_linux_virtual_machine.this.id
publisher = "Microsoft.Azure.Extensions"
type = "CustomScript"
type_handler_version = "2.1"

settings = jsonencode({
commandToExecute = "bash -c 'echo ${local.install_script_b64} | base64 -d > /tmp/install-app.sh && chmod +x /tmp/install-app.sh && /tmp/install-app.sh'"
})
}
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 "nic_id" {
value = azurerm_network_interface.this.id
}

output "vm_id" {
value = azurerm_linux_virtual_machine.this.id
}

output "ssh_key_id" {
value = azurerm_ssh_public_key.linuxboxsshkey.id
}
51 changes: 51 additions & 0 deletions modules/compute/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
variable "resource_group_name" {
type = string
description = "Resource group name"
}

variable "location" {
type = string
description = "Azure location"
}

variable "tags" {
type = map(string)
description = "Common tags"
default = {}
}

variable "vm_name" {
type = string
description = "VM name (should be matebox for this task)"
default = "matebox"
}

variable "admin_username" {
type = string
description = "Admin username for the VM"
default = "azureuser"
}

variable "ssh_key_public" {
type = string
}

variable "vm_size" {
type = string
}

variable "subnet_id" {
type = string
description = "Subnet ID where NIC will be placed"
}

variable "public_ip_id" {
type = string
description = "Public IP ID to attach to the NIC"
}

variable "install_script_path" {
type = string
description = "Path (relative to the module) to install-app.sh"
default = "install-app.sh"
}
49 changes: 49 additions & 0 deletions modules/network/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
resource "azurerm_virtual_network" "this" {
name = var.virtual_network_name
location = var.location
resource_group_name = var.resource_group_name

address_space = [var.vnet_address_prefix]
tags = var.tags
}

resource "azurerm_subnet" "default" {
name = var.subnet_name
resource_group_name = var.resource_group_name
virtual_network_name = azurerm_virtual_network.this.name

address_prefixes = [var.subnet_address_prefix]
}

resource "azurerm_network_security_group" "default" {
name = var.network_security_group_name
location = var.location
resource_group_name = var.resource_group_name

tags = var.tags
}

resource "azurerm_subnet_network_security_group_association" "default" {
subnet_id = azurerm_subnet.default.id
network_security_group_id = azurerm_network_security_group.default.id
}

resource "random_integer" "dns" {
min = 1000
max = 9999
}

locals {
dns_label = lower(format("%s%d", var.dns_label, random_integer.dns.result))
}

resource "azurerm_public_ip" "linuxbox" {
name = var.public_ip_address_name
location = var.location
resource_group_name = var.resource_group_name

allocation_method = "Dynamic"
domain_name_label = local.dns_label

tags = var.tags
}
23 changes: 23 additions & 0 deletions modules/network/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
output "vnet_id" {
value = azurerm_virtual_network.this.id
}

output "subnet_id" {
value = azurerm_subnet.default.id
}

output "nsg_id" {
value = azurerm_network_security_group.default.id
}

output "public_ip_id" {
value = azurerm_public_ip.linuxbox.id
}

output "public_ip_fqdn" {
value = azurerm_public_ip.linuxbox.fqdn
}

output "public_ip_dns_label" {
value = azurerm_public_ip.linuxbox.domain_name_label
}
40 changes: 40 additions & 0 deletions modules/network/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
variable "resource_group_name" {
type = string
}

variable "location" {
type = string
}

variable "tags" {
type = map(string)
default = {}
}

variable "virtual_network_name" {
type = string
}

variable "vnet_address_prefix" {
type = string
}

variable "subnet_name" {
type = string
}

variable "subnet_address_prefix" {
type = string
}

variable "network_security_group_name" {
type = string
}

variable "public_ip_address_name" {
type = string
}

variable "dns_label" {
type = string
}
Loading