forked from GoogleCloudPlatform/cloud-foundation-fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure.tf
165 lines (141 loc) · 5.22 KB
/
azure.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
/**
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
locals {
username = "azureuser"
app_name = "test-app"
}
provider "azurerm" {
features {}
}
data "azuread_client_config" "config" {}
resource "azuread_application" "app" {
display_name = local.app_name
identifier_uris = ["api://${local.app_name}"]
app_role {
allowed_member_types = ["Application"]
description = "User"
display_name = "User"
enabled = true
id = "3a9a28d5-f98d-47f5-ad0b-07d919533886"
value = "user"
}
}
resource "azuread_service_principal" "service_principal" {
application_id = azuread_application.app.application_id
app_role_assignment_required = true
}
resource "azurerm_resource_group" "resource_group" {
count = var.vm_test ? 1 : 0
name = "resourceGroup"
location = "West Europe"
}
resource "azurerm_virtual_network" "vnet" {
count = var.vm_test ? 1 : 0
name = "vnet"
address_space = ["10.0.0.0/16"]
resource_group_name = azurerm_resource_group.resource_group[0].name
location = azurerm_resource_group.resource_group[0].location
}
resource "azurerm_subnet" "subnet" {
count = var.vm_test ? 1 : 0
name = "subnet"
resource_group_name = azurerm_resource_group.resource_group[0].name
virtual_network_name = azurerm_virtual_network.vnet[0].name
address_prefixes = ["10.0.1.0/24"]
}
resource "azurerm_public_ip" "public_ip" {
count = var.vm_test ? 1 : 0
name = "vm"
resource_group_name = azurerm_resource_group.resource_group[0].name
location = azurerm_resource_group.resource_group[0].location
allocation_method = "Static"
sku = "Basic"
}
resource "azurerm_network_interface" "nic" {
count = var.vm_test ? 1 : 0
name = "nic"
resource_group_name = azurerm_resource_group.resource_group[0].name
location = azurerm_resource_group.resource_group[0].location
ip_configuration {
name = "ipconfig"
subnet_id = azurerm_subnet.subnet[0].id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.public_ip[0].id
}
}
resource "azurerm_network_security_group" "security_group" {
count = var.vm_test ? 1 : 0
name = "security-group"
resource_group_name = azurerm_resource_group.resource_group[0].name
location = azurerm_resource_group.resource_group[0].location
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 = "*"
}
}
resource "azurerm_network_interface_security_group_association" "security_group_association" {
count = var.vm_test ? 1 : 0
network_interface_id = azurerm_network_interface.nic[0].id
network_security_group_id = azurerm_network_security_group.security_group[0].id
}
resource "tls_private_key" "private_key" {
count = var.vm_test ? 1 : 0
algorithm = "RSA"
rsa_bits = 4096
}
resource "azurerm_linux_virtual_machine" "vm" {
count = var.vm_test ? 1 : 0
name = "vm"
resource_group_name = azurerm_resource_group.resource_group[0].name
location = azurerm_resource_group.resource_group[0].location
network_interface_ids = [azurerm_network_interface.nic[0].id]
size = "Standard_DS1_v2"
os_disk {
name = "disk"
caching = "ReadWrite"
storage_account_type = "Premium_LRS"
}
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
computer_name = "vm"
admin_username = "azureuser"
disable_password_authentication = true
custom_data = filebase64("${path.module}/setup.sh")
admin_ssh_key {
username = local.username
public_key = tls_private_key.private_key[0].public_key_openssh
}
identity {
type = "SystemAssigned"
}
}
resource "azuread_app_role_assignment" "app_role_assignment" {
count = var.vm_test ? 1 : 0
app_role_id = azuread_application.app.app_role_ids["user"]
principal_object_id = azurerm_linux_virtual_machine.vm[0].identity[0].principal_id
resource_object_id = azuread_service_principal.service_principal.object_id
}