-
Notifications
You must be signed in to change notification settings - Fork 70
Develop Terraform Infrastructure #74
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?
Changes from all commits
b2fa1be
ae19606
0bd191d
ba97ed2
4007488
8d480c2
a69787b
9ee735f
6fef7ba
23b40a8
be6e2c7
9557b95
8a5afde
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,44 @@ | ||
| # Local .terraform directories | ||
| .terraform/ | ||
|
|
||
| # .tfstate files | ||
| *.tfstate | ||
| *.tfstate.* | ||
|
|
||
| # Crash log files | ||
| crash.log | ||
| crash.*.log | ||
|
|
||
| # Exclude all .tfvars files, which are likely to contain sensitive data, such as | ||
| # password, private keys, and other secrets. These should not be part of version | ||
| # control as they are data points which are potentially sensitive and subject | ||
| # to change depending on the environment. | ||
| *.tfvars | ||
| *.tfvars.json | ||
|
|
||
| # Ignore override files as they are usually used to override resources locally and so | ||
| # are not checked in | ||
| override.tf | ||
| override.tf.json | ||
|
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 module call provisions a storage account named |
||
| *_override.tf | ||
| *_override.tf.json | ||
|
|
||
| # Ignore transient lock info files created by terraform apply | ||
| .terraform.tfstate.lock.info | ||
|
|
||
| # Include override files you do wish to add to version control using negated pattern | ||
| # !example_override.tf | ||
|
|
||
| # Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan | ||
| # example: *tfplan* | ||
|
|
||
| # Ignore CLI configuration files | ||
| .terraformrc | ||
| terraform.rc | ||
|
|
||
| # Optional: ignore graph output files generated by `terraform graph` | ||
| # *.dot | ||
|
|
||
| # Optional: ignore plan files saved before destroying Terraform configuration | ||
| # Uncomment the line below if you want to ignore planout files. | ||
| # planout | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| terraform { | ||
| backend "azurerm" { | ||
| storage_account_name = "yourstorageaccount" | ||
|
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 backend configuration correctly uses |
||
| container_name = "tfstate" | ||
| key = "terraform.tfstate" | ||
| resource_group_name = "mate-azure-task-12" | ||
| use_oidc = true | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| terraform { | ||
| required_providers { | ||
| azurerm = { | ||
| source = "hashicorp/azurerm" | ||
| version = "4.80.0" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| provider "azurerm" { | ||
| features {} | ||
| } | ||
|
|
||
| resource "azurerm_resource_group" "example" { | ||
| name = var.resource_group_name | ||
| location = var.resource_group_location | ||
| } | ||
| module "backend_storage" { | ||
| source = "../modules/storage" | ||
| resource_group_name = azurerm_resource_group.example.name | ||
| resource_group_location = azurerm_resource_group.example.location | ||
| storage_account_name = "yourstorageaccount" | ||
| container_name = "tfstate" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # COMMON VARIABLES | ||
| variable "resource_group_name" { | ||
| description = "Name of the resource group" | ||
|
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. |
||
| type = string | ||
| default = "mate-azure-task-12" | ||
| } | ||
| variable "resource_group_location" { | ||
| description = "Location of the resource group" | ||
| type = string | ||
| default = "uksouth" | ||
| } | ||
| #______________________________________________________________________________| | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,11 +13,12 @@ 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/AndS9/devops_todolist_terraform_task.git | ||
|
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 script correctly installs dependencies, clones the Todo app repository, copies the app files into 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. For the assignment, this hard-coded clone URL still points to your own GitHub username. The task text asks to update it with your GitHub username; if this repo is meant to be reused by others, they’ll need to change it, but for your submission, this is fine as long as the URL matches the app repo you control. |
||
| chmod +x devops_todolist_terraform_task/app/start.sh | ||
| cp -r devops_todolist_terraform_task/app/* /app | ||
|
|
||
| # create a service for the app via systemctl and start the app | ||
| mv /app/todoapp.service /etc/systemd/system/ | ||
| systemctl daemon-reload | ||
| systemctl start todoapp | ||
| systemctl enable todoapp | ||
| systemctl enable todoapp | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| terraform { | ||
| required_providers { | ||
| azurerm = { | ||
| source = "hashicorp/azurerm" | ||
| version = "4.80.0" | ||
|
Comment on lines
+4
to
+5
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. In the compute module outputs, you reference |
||
| } | ||
| random = { | ||
| source = "hashicorp/random" | ||
| version = "3.9.0" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| provider "azurerm" { | ||
| features { | ||
|
|
||
|
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 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. Re-running this script when the |
||
| } | ||
| use_oidc = true | ||
| } | ||
| resource "random_string" "suffix" { | ||
| length = 4 | ||
| upper = false | ||
| special = false | ||
| } | ||
| /* | ||
| I CAN"T USE IT IN MAIN, BECAUSE IT MUST BE CREATED WITH STORAGE ACC | ||
| BEFORE BACKEND for TERRAFORM TO PROPERLY START | ||
|
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 assignment explicitly says the VM should be named |
||
|
|
||
| resource "azurerm_resource_group" "example" { | ||
| name = var.resource_group_name | ||
| location = var.resource_group_location | ||
|
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 assignment requires the VM size to be |
||
| } | ||
| */ | ||
|
|
||
| data "azurerm_resource_group" "main" { | ||
| 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 requirement states the VM image must be Ubuntu 22.04 (Ubuntu2204). While your |
||
|
|
||
|
|
||
| module "network" { | ||
| source = "./modules/network" | ||
| resource_group_name = data.azurerm_resource_group.main.name | ||
| resource_group_location = data.azurerm_resource_group.main.location | ||
| network_name = var.virtual_network_name | ||
| v_net_address_prefix = var.vnet_address_prefix | ||
| subnet_name = var.subnet_name | ||
| subnet_address_prefix = var.subnet_address_prefix | ||
| pip_name = var.public_ip_address_name | ||
| dns_label = "${var.dns_label}-${random_string.suffix.result}" | ||
| network_SG_name = var.network_security_group_name | ||
| security_rules = var.security_rules | ||
| } | ||
| module "compute" { | ||
| source = "./modules/compute" | ||
| resource_group_name = data.azurerm_resource_group.main.name | ||
| resource_group_location = data.azurerm_resource_group.main.location | ||
| vm_name = var.vm_name | ||
| vm_size = var.vm_size | ||
| subnet_id = module.network.subnet_id | ||
| public_ip_id = module.network.public_ip_id | ||
| NSG_id = module.network.NSG_id | ||
| extension_name = "CustomScript" | ||
| path_to_script = "install-app.sh" | ||
|
Comment on lines
+62
to
+63
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 module correctly configures a CustomScript extension pointing to
Comment on lines
+62
to
+63
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
Comment on lines
+62
to
+63
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 |
||
| SSH_key = "linuxboxsshkey" | ||
| ssh_public_key_content = var.ssh_key_public | ||
| } | ||
| module "storage" { | ||
| source = "./modules/storage" | ||
| resource_group_name = data.azurerm_resource_group.main.name | ||
| resource_group_location = data.azurerm_resource_group.main.location | ||
| storage_account_name = var.storage_account_name_for_artifacts | ||
| container_name = "task-artifacts" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| resource "azurerm_network_interface" "main" { | ||
| name = "${var.vm_name}-nic" | ||
|
Comment on lines
+1
to
+2
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 is correctly named
Comment on lines
+1
to
+2
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 output exposes the VM ID, which is consistent with how the root module references |
||
| location = var.resource_group_location | ||
| resource_group_name = var.resource_group_name | ||
|
|
||
| ip_configuration { | ||
| name = "ip_config" | ||
| subnet_id = var.subnet_id | ||
| private_ip_address_allocation = "Dynamic" | ||
| public_ip_address_id = var.public_ip_id | ||
| } | ||
| } | ||
|
|
||
| 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
+13
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. 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 "azurerm_ssh_public_key" "example" { | ||
| name = var.SSH_key | ||
| resource_group_name = var.resource_group_name | ||
| location = var.resource_group_location | ||
| public_key = var.ssh_public_key_content | ||
| } | ||
|
|
||
| resource "azurerm_linux_virtual_machine" "main" { | ||
| name = var.vm_name | ||
| location = var.resource_group_location | ||
| resource_group_name = var.resource_group_name | ||
| network_interface_ids = [azurerm_network_interface.main.id] | ||
| size = var.vm_size | ||
|
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 default
Comment on lines
+26
to
+31
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 uses |
||
|
|
||
|
|
||
| admin_username = "testadmin" | ||
| disable_password_authentication = true | ||
| computer_name = "todoapp-host" | ||
| source_image_reference { | ||
| publisher = "Canonical" | ||
| offer = "0001-com-ubuntu-server-jammy" | ||
| sku = "22_04-lts" | ||
|
Comment on lines
+37
to
+40
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 task requires the VM image to be |
||
| version = "latest" | ||
| } | ||
| os_disk { | ||
| name = "myosdisk1" | ||
| caching = "ReadWrite" | ||
| storage_account_type = "Standard_LRS" | ||
| } | ||
|
|
||
| admin_ssh_key { | ||
| username = "testadmin" | ||
| public_key = azurerm_ssh_public_key.example.public_key | ||
| } | ||
| } | ||
|
|
||
| resource "azurerm_virtual_machine_extension" "example" { | ||
| name = var.extension_name | ||
| 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.module}/../../${var.path_to_script}")) | ||
|
Comment on lines
+55
to
+63
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 |
||
| }) | ||
|
Comment on lines
+62
to
+64
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. For the Azure |
||
|
|
||
|
Comment on lines
+53
to
+65
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 configuration correctly wires the compute module and passes |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| output "vm_id" { | ||
| value = azurerm_linux_virtual_machine.main.id | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| variable "resource_group_name" { | ||
| description = "Name of the resource group" | ||
| type = string | ||
| } | ||
|
|
||
| variable "resource_group_location" { | ||
| description = "Location of the resource group" | ||
| type = string | ||
| } | ||
|
|
||
| variable "pip_name" { | ||
| description = "Name of public IP resource" | ||
| type = string | ||
| default = "linuxboxpip" | ||
| } | ||
|
|
||
| variable "vm_name" { | ||
| description = "Virtual Machine Name" | ||
| type = string | ||
|
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
+15
to
+19
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 public IP resource now uses |
||
| default = "matebox" | ||
| } | ||
| variable "vm_size" { | ||
| description = "Size of VM" | ||
| type = string | ||
| default = "Standard_B1s" | ||
|
Comment on lines
+22
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. The |
||
| } | ||
|
|
||
| variable "subnet_id" { | ||
| type = string | ||
| } | ||
|
|
||
| variable "public_ip_id" { | ||
| type = string | ||
| } | ||
|
|
||
| variable "NSG_id" { | ||
| type = string | ||
| } | ||
|
|
||
| variable "extension_name" { | ||
| type = string | ||
| default = "CustomScript" | ||
| } | ||
| variable "path_to_script" { | ||
| type = string | ||
| } | ||
|
|
||
| variable "SSH_key" { | ||
| type = string | ||
| default = "linuxboxsshkey" | ||
| } | ||
|
|
||
| variable "ssh_public_key_content" { | ||
| type = string | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| resource "azurerm_virtual_network" "main" { | ||
| name = var.network_name | ||
| address_space = ["10.0.0.0/16"] | ||
| location = var.resource_group_location | ||
| resource_group_name = var.resource_group_name | ||
| } | ||
|
|
||
| resource "azurerm_subnet" "internal" { | ||
| name = var.subnet_name | ||
| resource_group_name = var.resource_group_name | ||
| virtual_network_name = azurerm_virtual_network.main.name | ||
| address_prefixes = ["10.0.0.0/24"] | ||
|
Comment on lines
+1
to
+12
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 VNet and subnet are hardcoded to |
||
| } | ||
|
|
||
| resource "azurerm_public_ip" "mainIP" { | ||
| name = var.pip_name | ||
| resource_group_name = var.resource_group_name | ||
| location = var.resource_group_location | ||
| allocation_method = "Dynamic" | ||
|
Comment on lines
+15
to
+19
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 public IP now uses |
||
| sku = "Standard" | ||
| domain_name_label = var.dns_label | ||
| } | ||
|
|
||
| resource "azurerm_network_security_group" "main" { | ||
| name = var.network_SG_name | ||
|
Comment on lines
+22
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.
Comment on lines
+22
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. The |
||
| location = var.resource_group_location | ||
| resource_group_name = var.resource_group_name | ||
|
|
||
| dynamic "security_rule" { | ||
| for_each = var.security_rules | ||
| content { | ||
| name = security_rule.value.name | ||
| priority = security_rule.value.priority | ||
| direction = security_rule.value.direction | ||
| access = security_rule.value.access | ||
| protocol = security_rule.value.protocol | ||
| source_port_range = security_rule.value.source_port_range | ||
| destination_port_range = security_rule.value.destination_port_range | ||
| source_address_prefix = security_rule.value.source_address_prefix | ||
| destination_address_prefix = security_rule.value.destination_address_prefix | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| output "subnet_id" { | ||
| value = azurerm_subnet.internal.id | ||
| } | ||
|
|
||
| output "public_ip_id" { | ||
| value = azurerm_public_ip.mainIP.id | ||
| } | ||
| output "public_ip" { | ||
| value = azurerm_public_ip.mainIP.ip_address | ||
| } | ||
| output "public_domain" { | ||
| value = azurerm_public_ip.mainIP.domain_name_label | ||
|
Comment on lines
+3
to
+12
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 define |
||
| } | ||
| output "NSG_id" { | ||
| value = azurerm_network_security_group.main.id | ||
|
Comment on lines
+5
to
+15
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 public IP and NSG outputs are correctly exposed for use by the root module, enabling you to wire the compute module and outputs as required. |
||
| } | ||
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 backend configuration here correctly uses
storage_account_name = "yourstorageaccount",container_name = "tfstate", andresource_group_name = "mate-azure-task-12", which matches the task requirements. Just make sure the storage account with this exact name is actually created by your bootstrap or main configuration (see the bootstrap file).