-
Notifications
You must be signed in to change notification settings - Fork 70
solution #45
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?
solution #45
Changes from all commits
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,8 @@ | ||
| terraform { | ||
| backend "azurerm" { | ||
| resource_group_name = "mate-azure-task-12" | ||
| storage_account_name = "yourstorageaccount" | ||
| container_name = "tfstate" | ||
| key = "terraform.tfstate" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| 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 "compute" { | ||
| source = "./modules/compute" | ||
| location = var.location | ||
| resource_group_name = azurerm_resource_group.main.name | ||
| subnet_id = module.network.subnet_id | ||
| public_ip_id = module.network.public_ip_id | ||
| vm_name = var.vm_name | ||
| vm_size = var.vm_size | ||
| ssh_key_public = var.ssh_key_public | ||
| } | ||
|
|
||
| module "storage" { | ||
| source = "./modules/storage" | ||
| location = var.location | ||
| resource_group_name = azurerm_resource_group.main.name | ||
| storage_account_name = "stor${random_integer.rand.result}" | ||
| } | ||
|
|
||
| resource "random_integer" "rand" { | ||
| min = 1000 | ||
| max = 9999 | ||
| } | ||
|
Comment on lines
+41
to
+44
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 To avoid duplication and improve consistency, it's better to define this resource only once here in the root |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| resource "azurerm_network_interface" "this" { | ||
| 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" "this" { | ||
| name = var.vm_name | ||
| location = var.location | ||
| resource_group_name = var.resource_group_name | ||
| size = var.vm_size | ||
| admin_username = "azureuser" | ||
|
|
||
| network_interface_ids = [azurerm_network_interface.this.id] | ||
|
|
||
| admin_ssh_key { | ||
| username = "azureuser" | ||
| 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" | ||
| } | ||
| } | ||
|
|
||
| # VM Extension to deploy ToDo app | ||
| resource "azurerm_virtual_machine_extension" "install_app" { | ||
| name = "install-app" | ||
| virtual_machine_id = azurerm_linux_virtual_machine.this.id | ||
| publisher = "Microsoft.Azure.Extensions" | ||
| type = "CustomScript" | ||
| type_handler_version = "2.1" | ||
|
|
||
| settings = <<SETTINGS | ||
| { | ||
| "fileUris": ["https://raw.githubusercontent.com/chornoknysh/devops_todolist_terraform_task/main/install-app.sh"], | ||
| "commandToExecute": "bash install-app.sh" | ||
| } | ||
| SETTINGS | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| output "vm_id" { | ||
| value = azurerm_linux_virtual_machine.this.id | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| variable "location" {} | ||
| variable "resource_group_name" {} | ||
| variable "subnet_id" {} | ||
| variable "public_ip_id" {} | ||
| variable "vm_name" {} | ||
| variable "vm_size" {} | ||
| variable "ssh_key_public" {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| resource "azurerm_virtual_network" "this" { | ||
| name = var.virtual_network_name | ||
| address_space = [var.vnet_address_prefix] | ||
| location = var.location | ||
| resource_group_name = var.resource_group_name | ||
| } | ||
|
|
||
| resource "azurerm_subnet" "this" { | ||
| 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" "this" { | ||
| name = var.network_security_group_name | ||
| location = var.location | ||
| resource_group_name = var.resource_group_name | ||
| } | ||
|
|
||
| resource "azurerm_public_ip" "this" { | ||
| name = var.public_ip_address_name | ||
| location = var.location | ||
| resource_group_name = var.resource_group_name | ||
| allocation_method = "Dynamic" | ||
| domain_name_label = "${var.dns_label}${random_integer.rand.result}" | ||
| } | ||
|
|
||
| resource "random_integer" "rand" { | ||
| min = 1000 | ||
| max = 9999 | ||
| } | ||
|
Comment on lines
+29
to
+32
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 resource correctly generates a random number for the DNS label. However, there's a similar To avoid duplication and centralize the random number generation, it's a best practice to remove this resource from the module. You can add a new variable to |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| output "subnet_id" { | ||
| value = azurerm_subnet.this.id | ||
| } | ||
|
|
||
| output "public_ip_id" { | ||
| value = azurerm_public_ip.this.id | ||
| } | ||
|
|
||
| output "nsg_id" { | ||
| value = azurerm_network_security_group.this.id | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| variable "location" {} | ||
| variable "resource_group_name" {} | ||
| variable "virtual_network_name" {} | ||
| variable "vnet_address_prefix" {} | ||
| variable "subnet_name" {} | ||
| variable "subnet_address_prefix" {} | ||
| variable "network_security_group_name" {} | ||
| variable "public_ip_address_name" {} | ||
| variable "dns_label" {} | ||
|
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. To follow the best practice of defining a resource only once, consider adding a new variable here to receive the random number generated in the root variable "random_suffix" {
type = number
}You could then pass |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| resource "azurerm_storage_account" "this" { | ||
| name = var.storage_account_name | ||
| resource_group_name = var.resource_group_name | ||
| location = var.location | ||
| account_tier = "Standard" | ||
| account_replication_type = "LRS" | ||
| } | ||
|
|
||
| resource "azurerm_storage_container" "this" { | ||
| name = "task-artifacts" | ||
| storage_account_name = azurerm_storage_account.this.name | ||
| container_access_type = "private" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| output "storage_account_id" { | ||
| value = azurerm_storage_account.this.id | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| variable "location" {} | ||
| variable "resource_group_name" {} | ||
| variable "storage_account_name" {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| location = "uksouth" | ||
| resource_group_name = "mate-azure-task-12" | ||
| virtual_network_name = "vnet" | ||
| vnet_address_prefix = "10.0.0.0/16" | ||
| subnet_name = "default" | ||
| subnet_address_prefix = "10.0.0.0/24" | ||
| network_security_group_name = "defaultnsg" | ||
| public_ip_address_name = "linuxboxpip" | ||
| vm_name = "matebox" | ||
| vm_size = "Standard_B1s" | ||
| ssh_key_public = "ssh-rsa AAAAB3Nz...yourkey" | ||
| dns_label = "matetask" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| variable "location" { | ||
| default = "uksouth" | ||
| } | ||
|
|
||
| variable "resource_group_name" { | ||
| default = "mate-azure-task-12" | ||
| } | ||
|
|
||
| variable "virtual_network_name" { | ||
| default = "vnet" | ||
| } | ||
|
|
||
| variable "vnet_address_prefix" { | ||
| default = "10.0.0.0/16" | ||
| } | ||
|
|
||
| variable "subnet_name" { | ||
| default = "default" | ||
| } | ||
|
|
||
| variable "subnet_address_prefix" { | ||
| default = "10.0.0.0/24" | ||
| } | ||
|
|
||
| variable "network_security_group_name" { | ||
| default = "defaultnsg" | ||
| } | ||
|
|
||
| variable "public_ip_address_name" { | ||
| default = "linuxboxpip" | ||
| } | ||
|
|
||
| variable "vm_name" { | ||
| default = "matebox" | ||
| } | ||
|
|
||
| variable "vm_size" { | ||
| default = "Standard_B1s" | ||
| } | ||
|
|
||
| variable "ssh_key_public" { | ||
| default = "ssh-rsa AAAAB3Nz...yourkey" | ||
| } | ||
|
|
||
| variable "dns_label" { | ||
| default = "matetask" | ||
| } |
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.
According to the task description, the Terraform configuration should create the Azure Resource Group. This resource definition is missing from the root module. You should add an
azurerm_resource_groupresource here and use its name when calling the modules below.