From be2f29e6bba701efa436c5028fd87fbc30b7a1b0 Mon Sep 17 00:00:00 2001 From: DanPrs Date: Fri, 27 Aug 2021 22:15:28 -0400 Subject: [PATCH] comit --- test/README.md | 3 + test/main.tf | 126 +++++++++++++++++++++++++++++++++++++++++- test/outputs.tf | 11 ---- test/terraform.tfvars | 2 + test/variables.tf | 35 +++++++++--- test/versions.tf | 12 ---- 6 files changed, 156 insertions(+), 33 deletions(-) create mode 100644 test/README.md delete mode 100644 test/outputs.tf create mode 100644 test/terraform.tfvars delete mode 100644 test/versions.tf diff --git a/test/README.md b/test/README.md new file mode 100644 index 0000000..33cd28b --- /dev/null +++ b/test/README.md @@ -0,0 +1,3 @@ +# Learn Terraform with Azure + +This is the companion code from the HashiCorp Learn tutorials for [getting started with Terraform on Azure](https://learn.hashicorp.com/collections/terraform/azure-get-started). diff --git a/test/main.tf b/test/main.tf index 8b6bdb6..67fd3ba 100644 --- a/test/main.tf +++ b/test/main.tf @@ -1,4 +1,128 @@ +# Configure the Microsoft Azure Provider. +terraform { + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = ">= 2.26" + } + } + + required_version = ">= 0.14.9" +} + provider "azurerm" { - # Configuration options + features {} +} + +# Create a resource group +resource "azurerm_resource_group" "rg" { + name = "${var.prefix}TFRG" + location = var.location + tags = var.tags +} + +# Create virtual network +resource "azurerm_virtual_network" "vnet" { + name = "${var.prefix}TFVnet" + address_space = ["10.0.0.0/16"] + location = var.location + resource_group_name = azurerm_resource_group.rg.name + tags = var.tags +} + +# Create subnet +resource "azurerm_subnet" "subnet" { + name = "${var.prefix}TFSubnet" + resource_group_name = azurerm_resource_group.rg.name + virtual_network_name = azurerm_virtual_network.vnet.name + address_prefixes = ["10.0.1.0/24"] +} + +# Create public IP +resource "azurerm_public_ip" "publicip" { + name = "${var.prefix}TFPublicIP" + location = var.location + resource_group_name = azurerm_resource_group.rg.name + allocation_method = "Dynamic" + tags = var.tags +} + +# Create Network Security Group and rule +resource "azurerm_network_security_group" "nsg" { + name = "${var.prefix}TFNSG" + location = var.location + resource_group_name = azurerm_resource_group.rg.name + tags = var.tags + + 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 = "*" + } } +# Create network interface +resource "azurerm_network_interface" "nic" { + name = "${var.prefix}NIC" + location = var.location + resource_group_name = azurerm_resource_group.rg.name + tags = var.tags + + ip_configuration { + name = "${var.prefix}NICConfg" + subnet_id = azurerm_subnet.subnet.id + private_ip_address_allocation = "dynamic" + public_ip_address_id = azurerm_public_ip.publicip.id + } +} + +# Create a Linux virtual machine +resource "azurerm_virtual_machine" "vm" { + name = "${var.prefix}TFVM" + location = var.location + resource_group_name = azurerm_resource_group.rg.name + network_interface_ids = [azurerm_network_interface.nic.id] + vm_size = "Standard_DS1_v2" + tags = var.tags + + storage_os_disk { + name = "${var.prefix}OsDisk" + caching = "ReadWrite" + create_option = "FromImage" + managed_disk_type = "Premium_LRS" + } + + storage_image_reference { + publisher = "Canonical" + offer = "UbuntuServer" + sku = lookup(var.sku, var.location) + version = "latest" + } + + os_profile { + computer_name = "${var.prefix}TFVM" + admin_username = var.admin_username + admin_password = var.admin_password + } + + os_profile_linux_config { + disable_password_authentication = false + } + +} + +data "azurerm_public_ip" "ip" { + name = azurerm_public_ip.publicip.name + resource_group_name = azurerm_virtual_machine.vm.resource_group_name + depends_on = ["azurerm_virtual_machine.vm"] +} + +output "os_sku" { + value = lookup(var.sku, var.location) +} diff --git a/test/outputs.tf b/test/outputs.tf deleted file mode 100644 index 99bc350..0000000 --- a/test/outputs.tf +++ /dev/null @@ -1,11 +0,0 @@ -output "instance_ami" { - value = aws_instance.ubuntu.ami -} - -output "instance_arn" { - value = aws_instance.ubuntu.arn -} - -output "" { - -} \ No newline at end of file diff --git a/test/terraform.tfvars b/test/terraform.tfvars new file mode 100644 index 0000000..e612b94 --- /dev/null +++ b/test/terraform.tfvars @@ -0,0 +1,2 @@ +location = "westus2" +prefix = "tf" diff --git a/test/variables.tf b/test/variables.tf index f6e9bbb..7702a4a 100644 --- a/test/variables.tf +++ b/test/variables.tf @@ -1,15 +1,32 @@ -variable "region" { - description = "AWS region" - default = "us-west-1" +variable "location" {} + +variable "admin_username" { + type = string + description = "Administrator user name for virtual machine" } -variable "instance_type" { - description = "Type of EC2 instance to provision" - default = "t2.micro" +variable "admin_password" { + type = string + description = "Password must meet Azure complexity requirements" } -variable "instance_name" { - description = "EC2 instance name" - default = "Provisioned by Terraform" +variable "prefix" { + type = string + default = "my" } +variable "tags" { + type = map + + default = { + Environment = "Terraform GS" + Dept = "Engineering" + } +} + +variable "sku" { + default = { + westus2 = "16.04-LTS" + eastus = "18.04-LTS" + } +} diff --git a/test/versions.tf b/test/versions.tf deleted file mode 100644 index b370a13..0000000 --- a/test/versions.tf +++ /dev/null @@ -1,12 +0,0 @@ -terraform { - required_providers { - azurerm = { - source = "hashicorp/azurerm" - version = "2.74.0" - } - } -} - -provider "azurerm" { - # Configuration options -} \ No newline at end of file