Skip to content

Commit 1bb60e6

Browse files
committed
Added code for create-vm-scaleset-network-disks-using-packer-hcl
1 parent 8d4c157 commit 1bb60e6

File tree

5 files changed

+302
-0
lines changed

5 files changed

+302
-0
lines changed
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
terraform {
2+
required_providers {
3+
azurerm = {
4+
source = "hashicorp/azurerm"
5+
version = "~>2.0"
6+
}
7+
}
8+
}
9+
10+
provider "azurerm" {
11+
features {}
12+
}
13+
14+
resource "azurerm_resource_group" "vmss" {
15+
name = var.resource_group_name
16+
location = var.location
17+
18+
tags = {
19+
environment = "codelab"
20+
}
21+
}
22+
23+
resource "azurerm_virtual_network" "vmss" {
24+
name = "vmss-vnet"
25+
address_space = ["10.0.0.0/16"]
26+
location = var.location
27+
resource_group_name = azurerm_resource_group.vmss.name
28+
29+
tags = {
30+
environment = "codelab"
31+
}
32+
}
33+
34+
resource "azurerm_subnet" "vmss" {
35+
name = "vmss-subnet"
36+
resource_group_name = azurerm_resource_group.vmss.name
37+
virtual_network_name = azurerm_virtual_network.vmss.name
38+
address_prefix = "10.0.2.0/24"
39+
}
40+
41+
resource "azurerm_public_ip" "vmss" {
42+
name = "vmss-public-ip"
43+
location = var.location
44+
resource_group_name = azurerm_resource_group.vmss.name
45+
allocation_method = "Static"
46+
domain_name_label = azurerm_resource_group.vmss.name
47+
48+
tags = {
49+
environment = "codelab"
50+
}
51+
}
52+
53+
resource "azurerm_lb" "vmss" {
54+
name = "vmss-lb"
55+
location = var.location
56+
resource_group_name = azurerm_resource_group.vmss.name
57+
58+
frontend_ip_configuration {
59+
name = "PublicIPAddress"
60+
public_ip_address_id = azurerm_public_ip.vmss.id
61+
}
62+
63+
tags = {
64+
environment = "codelab"
65+
}
66+
}
67+
68+
resource "azurerm_lb_backend_address_pool" "bpepool" {
69+
resource_group_name = azurerm_resource_group.vmss.name
70+
loadbalancer_id = azurerm_lb.vmss.id
71+
name = "BackEndAddressPool"
72+
}
73+
74+
resource "azurerm_lb_probe" "vmss" {
75+
resource_group_name = azurerm_resource_group.vmss.name
76+
loadbalancer_id = azurerm_lb.vmss.id
77+
name = "ssh-running-probe"
78+
port = var.application_port
79+
}
80+
81+
resource "azurerm_lb_rule" "lbnatrule" {
82+
resource_group_name = azurerm_resource_group.vmss.name
83+
loadbalancer_id = azurerm_lb.vmss.id
84+
name = "http"
85+
protocol = "Tcp"
86+
frontend_port = var.application_port
87+
backend_port = var.application_port
88+
backend_address_pool_id = azurerm_lb_backend_address_pool.bpepool.id
89+
frontend_ip_configuration_name = "PublicIPAddress"
90+
probe_id = azurerm_lb_probe.vmss.id
91+
}
92+
93+
data "azurerm_resource_group" "image" {
94+
name = "myResourceGroup"
95+
}
96+
97+
data "azurerm_image" "image" {
98+
name = "myPackerImage"
99+
resource_group_name = data.azurerm_resource_group.image.name
100+
}
101+
102+
resource "azurerm_virtual_machine_scale_set" "vmss" {
103+
name = "vmscaleset"
104+
location = var.location
105+
resource_group_name = azurerm_resource_group.vmss.name
106+
upgrade_policy_mode = "Manual"
107+
108+
sku {
109+
name = "Standard_DS1_v2"
110+
tier = "Standard"
111+
capacity = 2
112+
}
113+
114+
storage_profile_image_reference {
115+
id=data.azurerm_image.image.id
116+
}
117+
118+
storage_profile_os_disk {
119+
name = ""
120+
caching = "ReadWrite"
121+
create_option = "FromImage"
122+
managed_disk_type = "Standard_LRS"
123+
}
124+
125+
storage_profile_data_disk {
126+
lun = 0
127+
caching = "ReadWrite"
128+
create_option = "Empty"
129+
disk_size_gb = 10
130+
}
131+
132+
os_profile {
133+
computer_name_prefix = "vmlab"
134+
admin_username = var.admin_user
135+
admin_password = var.admin_password
136+
}
137+
138+
os_profile_linux_config {
139+
disable_password_authentication = true
140+
141+
ssh_keys {
142+
path = "/home/azureuser/.ssh/authorized_keys"
143+
key_data = file("~/.ssh/id_rsa.pub")
144+
}
145+
}
146+
147+
network_profile {
148+
name = "terraformnetworkprofile"
149+
primary = true
150+
151+
ip_configuration {
152+
name = "IPConfiguration"
153+
subnet_id = azurerm_subnet.vmss.id
154+
load_balancer_backend_address_pool_ids = [azurerm_lb_backend_address_pool.bpepool.id]
155+
primary = true
156+
}
157+
}
158+
159+
tags = {
160+
environment = "codelab"
161+
}
162+
}
163+
164+
resource "azurerm_public_ip" "jumpbox" {
165+
name = "jumpbox-public-ip"
166+
location = var.location
167+
resource_group_name = azurerm_resource_group.vmss.name
168+
allocation_method = "Static"
169+
domain_name_label = "${azurerm_resource_group.vmss.name}-ssh"
170+
171+
tags = {
172+
environment = "codelab"
173+
}
174+
}
175+
176+
resource "azurerm_network_interface" "jumpbox" {
177+
name = "jumpbox-nic"
178+
location = var.location
179+
resource_group_name = azurerm_resource_group.vmss.name
180+
181+
ip_configuration {
182+
name = "IPConfiguration"
183+
subnet_id = azurerm_subnet.vmss.id
184+
private_ip_address_allocation = "dynamic"
185+
public_ip_address_id = azurerm_public_ip.jumpbox.id
186+
}
187+
188+
tags = {
189+
environment = "codelab"
190+
}
191+
}
192+
193+
resource "azurerm_virtual_machine" "jumpbox" {
194+
name = "jumpbox"
195+
location = var.location
196+
resource_group_name = azurerm_resource_group.vmss.name
197+
network_interface_ids = [azurerm_network_interface.jumpbox.id]
198+
vm_size = "Standard_DS1_v2"
199+
200+
storage_image_reference {
201+
publisher = "Canonical"
202+
offer = "UbuntuServer"
203+
sku = "16.04-LTS"
204+
version = "latest"
205+
}
206+
207+
storage_os_disk {
208+
name = "jumpbox-osdisk"
209+
caching = "ReadWrite"
210+
create_option = "FromImage"
211+
managed_disk_type = "Standard_LRS"
212+
}
213+
214+
os_profile {
215+
computer_name = "jumpbox"
216+
admin_username = var.admin_user
217+
admin_password = var.admin_password
218+
}
219+
220+
os_profile_linux_config {
221+
disable_password_authentication = true
222+
223+
ssh_keys {
224+
path = "/home/azureuser/.ssh/authorized_keys"
225+
key_data = file("~/.ssh/id_rsa.pub")
226+
}
227+
}
228+
229+
tags = {
230+
environment = "codelab"
231+
}
232+
}
233+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
output "vmss_public_ip_fqdn" {
2+
value = azurerm_public_ip.vmss.fqdn
3+
}
4+
5+
output "jumpbox_public_ip_fqdn" {
6+
value = azurerm_public_ip.jumpbox.fqdn
7+
}
8+
9+
output "jumpbox_public_ip" {
10+
value = azurerm_public_ip.jumpbox.ip_address
11+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Azure virtual machine scale set with jumpbox from Packer custom image
2+
3+
This template deploys an Azure virtual machine scale set with a jumpbox from a Packer custom image.
4+
5+
## Resources
6+
7+
| Terraform Resource Type | Description |
8+
| - | - |
9+
| `azurerm_resource_group` | The resource group all resources are deployed into |
10+
11+
## Variables
12+
13+
| Name | Description |
14+
|-|-|
15+
| `resource_group_name` | Name of the resource group in which the resources will be created |
16+
| `location` | Location where resources will be create |
17+
| `tags` | Map of the tags to use for the resources that are deployed |
18+
| `application_port` | Port that you want to expose to the external load balancer |
19+
| `admin_user` | User name to use as the admin account on the VMs that will be part of the VM scale set |
20+
| `admin_password` | Default password for admin account (NOTE: For security reasons, this value is not set in the plaintext variables.tf file.) |
21+
22+
## Example
23+
24+
To see how to run this example, see [Create an Azure virtual machine scale set from a Packer custom image by using Terraform
25+
](https://docs.microsoft.com/azure/developer/terraform/create-vm-scaleset-network-disks-using-packer-hcl#create-an-azure-image-by-using-packer).
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
variable "resource_group_name" {
2+
description = "Name of the resource group in which the resources will be created"
3+
default = "myResourceGroup"
4+
}
5+
6+
variable "location" {
7+
default = "eastus"
8+
description = "Location where resources will be created"
9+
}
10+
11+
variable "tags" {
12+
description = "Map of the tags to use for the resources that are deployed"
13+
type = map(string)
14+
default = {
15+
environment = "codelab"
16+
}
17+
}
18+
19+
variable "application_port" {
20+
description = "Port that you want to expose to the external load balancer"
21+
default = 80
22+
}
23+
24+
variable "admin_user" {
25+
description = "User name to use as the admin account on the VMs that will be part of the VM scale set"
26+
default = "azureuser"
27+
}
28+
29+
variable "admin_password" {
30+
description = "Default password for admin account"
31+
}

quickstart/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ This project has adopted the [Microsoft Open Source Code of Conduct](https://ope
2929
- [Azure Kubernetes Service with Log Analytics](./201-aks-log-analytics/)
3030
- [Azure Kubernetes Service with Helm](./201-aks-helm/)
3131
- [Azure Kubernetes Service with ACR](./201-aks-acr-identity/)
32+
- [Azure virtual machine scale set with jumpbox](./201-vmss-jumpbox)
33+
- [Azure virtual machine scale set with jumpbox from Packer custom image](./201-vmss-packer-jumpbox)
3234

3335
#### Advanced
3436
- [Azure Service Fabric](./301-service-fabric/)

0 commit comments

Comments
 (0)