· 6 years ago · Jul 08, 2019, 04:04 PM
1##################################################################################
2# VARIABLES
3##################################################################################
4
5#variable "aws_access_key" {}
6#variable "aws_secret_key" {}
7#variable "private_key_path" {}
8#variable "key_name" {
9# default = ""
10#}
11
12#IMPORTED VARIABLES
13variable "azure_msdn_client_id" {}
14variable "azure_msdn_client_secret" {}
15variable "azure_msdn_tenant_id" {}
16variable "azure_msdn_subscription_id" {}
17
18#LOCAL STATIC VARIABLES
19variable "prefix" {
20 default = "firstrun"
21}
22
23##################################################################################
24# PROVIDERS
25##################################################################################
26
27#provider "aws" {
28# access_key = "${var.aws_access_key}"
29# secret_key = "${var.aws_secret_key}"
30# region = "us-east-1"
31#}
32
33
34# Configure the Azure Provider
35provider "azurerm" {
36 # whilst the `version` attribute is optional, we recommend pinning to a given version of the Provider
37 # version = "=1.28.0"
38 # version = "=2.0.0"
39 # version = "~> 1.x"
40
41 subscription_id = "${var.azure_msdn_subscription_id}"
42 client_id = "${var.azure_msdn_client_id}"
43 client_secret = "${var.azure_msdn_client_secret}"
44 tenant_id = "${var.azure_msdn_tenant_id}"
45}
46
47
48
49
50
51##################################################################################
52# RESOURCES
53##################################################################################
54
55# Create a resource group
56resource "azurerm_resource_group" "tf-rg" {
57 name = "${var.prefix}-resourcegroup"
58 location = "uksouth"
59}
60
61# Create a virtual network within the resource group
62resource "azurerm_virtual_network" "tf-vn" {
63 name = "${var.prefix}-network"
64 resource_group_name = "${azurerm_resource_group.tf-rg.name}"
65 location = "${azurerm_resource_group.tf-rg.location}"
66 address_space = ["192.168.0.0/16"]
67}
68
69# Create a subnet within the vnet
70resource "azurerm_subnet" "tf-sn" {
71 name = "${var.prefix}-subnet"
72 resource_group_name = "${azurerm_resource_group.tf-rg.name}"
73 virtual_network_name = "${azurerm_virtual_network.tf-vn.name}"
74 address_prefix = "192.168.254.0/24"
75}
76
77#Create a network interface for the VM
78resource "azurestack_network_interface" "tf-ni" {
79 name = "${var.prefix}-networkinterface"
80 location = "${azurerm_resource_group.tf-rg.location}"
81 resource_group_name = "${azurerm_resource_group.tf-rg.name}"
82
83 ip_configuration {
84 name = "${var.prefix}-ipconfiguration1"
85 subnet_id = "${azurerm_subnet.tf-sn.id}"
86 private_ip_address_allocation = "dynamic"
87 }
88}
89
90# Create a virtual machine within the subnet
91resource "azurerm_virtual_machine" "az-vm" {
92 name = "${var.prefix}-virtualmachine"
93 resource_group_name = "${azurerm_resource_group.tf-rg.name}"
94 location = "${azurerm_resource_group.tf-rg.location}"
95 vm_size = "Standard_B1ms"
96 network_interface_ids = ["${azurestack_network_interface.tf-ni.id}"]
97 delete_os_disk_on_termination = true
98 delete_data_disks_on_termination = true
99
100 storage_image_reference {
101 publisher = "Canonical"
102 offer = "UbuntuServer"
103 sku = "16.04-LTS"
104 version = "latest"
105 }
106
107 storage_os_disk {
108 name = "myosdisk1"
109 caching = "ReadWrite"
110 create_option = "FromImage"
111 managed_disk_type = "Standard_LRS"
112 }
113
114 os_profile {
115 computer_name = "hostname"
116 admin_username = "testadmin"
117 admin_password = "Password1234!"
118 }
119
120 os_profile_linux_config {
121 disable_password_authentication = false
122 }
123
124 tags = {
125 environment = "${var.prefix}"
126 }
127}
128
129#resource "aws_instance" "nginx" {
130# ami = "ami-c58c1dd3"
131# instance_type = "t2.micro"
132# key_name = "${var.key_name}"
133#
134# connection {
135# user = "ec2-user"
136# private_key = "${file(var.private_key_path)}"
137# }
138#
139# provisioner "remote-exec" {
140# inline = [
141# "sudo yum install nginx -y",
142# "sudo service nginx start"
143# ]
144# }
145#}
146
147##################################################################################
148# OUTPUT
149##################################################################################
150
151output "azurerm_virtual_instance" {
152# value = "${azurerm_virtual_machine.az-vm.private_ip}"
153 value = "finished"
154}