-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacker.tf
219 lines (188 loc) · 6.64 KB
/
packer.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# Configure the Microsoft Azure Provider
provider "azurerm" {
subscription_id = "e0613b30-845d-4493-90d9-664f99761c94"
client_id = "57e2f82c-9801-4aa3-8c38-7d42afda7bdf"
client_secret = "3Sv8Q~a5NJJgTf46Wx3bjw44gMnorNqHjsrgKbOK"
tenant_id = "c62943a6-db7e-4acb-afde-ad89cc8162a2"
features {}
}
# Locate the existing resource group
data "azurerm_resource_group" "main" {
name = "Packer"
}
output "id" {
value = data.azurerm_resource_group.main.id
}
# Locate the existing custom image
data "azurerm_image" "main" {
name = "Sandbox"
resource_group_name = "Packer"
}
output "image_id" {
value = "/subscriptions/e0613b30-845d-4493-90d9-664f99761c94/resourceGroups/Packer/providers/Microsoft.Compute/images/Sandbox"
}
# Create subnet
resource "azurerm_subnet" "main" {
name = "my-subnet"
resource_group_name = data.azurerm_resource_group.main.name
virtual_network_name = azurerm_virtual_network.main.name
address_prefixes = ["10.0.2.0/24"]
}
# Create a Network Security Group with some rules
resource "azurerm_network_security_group" "main" {
name = "my-SG"
location = data.azurerm_resource_group.main.location
resource_group_name = data.azurerm_resource_group.main.name
security_rule {
name = "Allow-andrew-inbound"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "*"
source_port_range = "*"
destination_port_range = "*"
source_address_prefix = "71.196.25.4/32"
destination_address_prefix = "*"
}
security_rule {
name = "AllowVnetInBound"
priority = 102
direction = "Inbound"
access = "Allow"
protocol = "*"
source_port_range = "*"
destination_port_range = "*"
source_address_prefix = "VirtualNetwork"
destination_address_prefix = "VirtualNetwork"
}
security_rule {
name = "AllowAzureLoadBalancer"
priority = 105
direction = "Inbound"
access = "Allow"
protocol = "*"
source_port_range = "*"
destination_port_range = "*"
source_address_prefix = "AzureLoadBalancer"
destination_address_prefix = "*"
}
security_rule {
name = "DenyAllInBound"
priority = 110
direction = "Inbound"
access = "Deny"
protocol = "*"
source_port_range = "*"
destination_port_range = "*"
source_address_prefix = "*"
destination_address_prefix = "*"
}
security_rule {
name = "DenyMyIpAdressCustomAnyOutbound"
priority = 100
direction = "Outound"
access = "Allow"
protocol = "*"
source_port_range = "*"
destination_port_range = "*"
source_address_prefix = "*"
destination_address_prefix = "104.131.8.184"
}
security_rule {
name = "AllowVnetOutBound"
priority = 102
direction = "Outound"
access = "Allow"
protocol = "*"
source_port_range = "*"
destination_port_range = "*"
source_address_prefix = "VirtualNetwork"
destination_address_prefix = "VirtualNetwork"
}
security_rule {
name = "AllowInternetOutBound"
priority = 105
direction = "Outound"
access = "Allow"
protocol = "*"
source_port_range = "*"
destination_port_range = "*"
source_address_prefix = "*"
destination_address_prefix = "Internet"
}
security_rule {
name = "DenyAllOutBound"
priority = 110
direction = "Outound"
access = "Deny"
protocol = "*"
source_port_range = "*"
destination_port_range = "*"
source_address_prefix = "*"
destination_address_prefix = "*"
}
}
resource "azurerm_subnet_network_security_group_association" "subnet_asscociation_nsg"{
depends_on = [ azurerm_network_security_group.main ]
subnet_id = azurerm_subnet.main.id
network_security_group_id = azurerm_network_security_group.main.id
}
# Create virtual network
resource "azurerm_virtual_network" "main" {
name = "my-network"
address_space = ["10.0.0.0/16"]
location = data.azurerm_resource_group.main.location
resource_group_name = data.azurerm_resource_group.main.name
}
# Create public IP
resource "azurerm_public_ip" "main" {
name = "my-public-ip"
resource_group_name = data.azurerm_resource_group.main.name
location = data.azurerm_resource_group.main.location
allocation_method = "Static"
tags = {
environment = "Production"
}
}
# Create network interface
resource "azurerm_network_interface" "main" {
name = "my-nic"
location = data.azurerm_resource_group.main.location
resource_group_name = data.azurerm_resource_group.main.name
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.main.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.main.id
}
}
# Create a new Virtual Machine based on the custom Image
resource "azurerm_virtual_machine" "myVM2" {
name = "myVM2"
location = data.azurerm_resource_group.main.location
resource_group_name = data.azurerm_resource_group.main.name
network_interface_ids = ["${azurerm_network_interface.main.id}"]
vm_size = "Standard_DS12_v2"
delete_os_disk_on_termination = true
delete_data_disks_on_termination = true
storage_image_reference {
id = "${data.azurerm_image.main.id}"
}
storage_os_disk {
name = "myVM2-OS"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
os_profile {
computer_name = "APPVM"
admin_username = "devopsadmin"
admin_password = "Cssladmin#2019"
}
os_profile_linux_config {
disable_password_authentication = false
}
tags = {
environment = "Production"
}
}