-
Notifications
You must be signed in to change notification settings - Fork 8
/
packer.tf
172 lines (135 loc) · 4.69 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
terraform {
required_version = ">= 1.1.6"
required_providers {
azuread = {
source = "azuread"
version = ">= 2.53.0"
}
azurerm = {
source = "azurerm"
version = ">= 4.0.1"
}
github = {
source = "integrations/github"
version = ">= 6.2.3"
}
}
}
provider "azurerm" {
features {}
}
variable "location" {
type = string
description = "Azure region for Packer resources."
default = "Switzerland North"
}
# Packer Resource Groups
resource "azurerm_resource_group" "packer_artifacts" {
name = "packer-artifacts-rg"
location = var.location
}
resource "azurerm_resource_group" "packer_build" {
name = "packer-build-rg"
location = var.location
}
# Service Principal Used By Packer
resource "azuread_application" "packer" {
display_name = "packer-sp-app"
}
resource "azuread_service_principal" "packer" {
client_id = azuread_application.packer.client_id
}
resource "azuread_service_principal_password" "packer" {
service_principal_id = azuread_service_principal.packer.id
}
# RBAC
# Grant service principal `Reader` role scoped to subscription
# Grant service principal `Contributor` role scoped to Packer resource groups
data "azurerm_subscription" "subscription" {}
resource "azurerm_role_assignment" "subscription_reader" {
scope = data.azurerm_subscription.subscription.id
role_definition_name = "Reader"
principal_id = azuread_service_principal.packer.id
}
resource "azurerm_role_assignment" "packer_build_contributor" {
scope = azurerm_resource_group.packer_build.id
role_definition_name = "Contributor"
principal_id = azuread_service_principal.packer.id
}
resource "azurerm_role_assignment" "packer_artifacts_contributor" {
scope = azurerm_resource_group.packer_artifacts.id
role_definition_name = "Contributor"
principal_id = azuread_service_principal.packer.id
}
# Export Variables For Packer
data "github_repository" "packer_windows_avd" {
full_name = "schnerring/packer-windows-avd"
}
# Azure CLI Authentication
resource "github_actions_secret" "github_actions_azure_credentials" {
repository = data.github_repository.packer_windows_avd.name
secret_name = "AZURE_CREDENTIALS"
plaintext_value = jsonencode(
{
clientId = azuread_application.packer.client_id
clientSecret = azuread_service_principal_password.packer.value
subscriptionId = data.azurerm_subscription.subscription.subscription_id
tenantId = data.azurerm_subscription.subscription.tenant_id
}
)
}
# Packer Authentication
resource "github_actions_secret" "packer_client_id" {
repository = data.github_repository.packer_windows_avd.name
secret_name = "PACKER_CLIENT_ID"
plaintext_value = azuread_application.packer.client_id
}
resource "github_actions_secret" "packer_client_secret" {
repository = data.github_repository.packer_windows_avd.name
secret_name = "PACKER_CLIENT_SECRET"
plaintext_value = azuread_service_principal_password.packer.value
}
resource "github_actions_secret" "packer_subscription_id" {
repository = data.github_repository.packer_windows_avd.name
secret_name = "PACKER_SUBSCRIPTION_ID"
plaintext_value = data.azurerm_subscription.subscription.subscription_id
}
resource "github_actions_secret" "packer_tenant_id" {
repository = data.github_repository.packer_windows_avd.name
secret_name = "PACKER_TENANT_ID"
plaintext_value = data.azurerm_subscription.subscription.tenant_id
}
# Packer Resource Groups
resource "github_actions_secret" "packer_artifacts_resource_group" {
repository = data.github_repository.packer_windows_avd.name
secret_name = "PACKER_ARTIFACTS_RESOURCE_GROUP"
plaintext_value = azurerm_resource_group.packer_artifacts.name
}
resource "github_actions_secret" "packer_build_resource_group" {
repository = data.github_repository.packer_windows_avd.name
secret_name = "PACKER_BUILD_RESOURCE_GROUP"
plaintext_value = azurerm_resource_group.packer_build.name
}
# Outputs to run Packer locally
output "packer_artifacts_resource_group" {
value = azurerm_resource_group.packer_artifacts.name
}
output "packer_build_resource_group" {
value = azurerm_resource_group.packer_build.name
}
output "packer_client_id" {
value = azuread_application.packer.client_id
sensitive = true
}
output "packer_client_secret" {
value = azuread_service_principal_password.packer.value
sensitive = true
}
output "packer_subscription_id" {
value = data.azurerm_subscription.subscription.subscription_id
sensitive = true
}
output "packer_tenant_id" {
value = data.azurerm_subscription.subscription.tenant_id
sensitive = true
}