Skip to content

Commit 884f690

Browse files
authored
Merge pull request #261 from PerfectThymeTech/marvinbuss/update_azurerm_4
Upgrade azurerm to v4 and update configs
2 parents bf023c0 + d1820b7 commit 884f690

18 files changed

+361
-551
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
name: Terraform Destroy Template
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
environment:
7+
required: true
8+
type: string
9+
default: "dev"
10+
description: "Specifies the environment of the deployment."
11+
config:
12+
required: true
13+
type: string
14+
description: "Specifies the configuration folder for the deployment."
15+
terraform_version:
16+
required: true
17+
type: string
18+
description: "Specifies the terraform version."
19+
node_version:
20+
required: true
21+
type: number
22+
description: "Specifies the node version."
23+
working_directory:
24+
required: true
25+
type: string
26+
description: "Specifies the working directory."
27+
tenant_id:
28+
required: true
29+
type: string
30+
description: "Specifies the tenant id of the deployment."
31+
subscription_id:
32+
required: true
33+
type: string
34+
description: "Specifies the subscription id of the deployment."
35+
secrets:
36+
CLIENT_ID:
37+
required: true
38+
description: "Specifies the client id."
39+
MY_SAMPLE_SECRET:
40+
required: true
41+
description: "Specifies a sample secret."
42+
43+
permissions:
44+
id-token: write
45+
contents: read
46+
47+
jobs:
48+
deployment:
49+
name: Terraform Destroy
50+
runs-on: [self-hosted]
51+
continue-on-error: false
52+
environment: ${{ inputs.environment }}
53+
if: github.event_name == 'push' || github.event_name == 'release'
54+
concurrency:
55+
group: terraform-${{ inputs.config }}-${{ inputs.environment }}
56+
cancel-in-progress: false
57+
58+
env:
59+
ARM_TENANT_ID: ${{ inputs.tenant_id }}
60+
ARM_SUBSCRIPTION_ID: ${{ inputs.subscription_id }}
61+
ARM_CLIENT_ID: ${{ secrets.CLIENT_ID }}
62+
ARM_USE_OIDC: true
63+
64+
steps:
65+
# Setup Node
66+
- name: Setup Node
67+
id: node_setup
68+
uses: actions/setup-node@v4
69+
with:
70+
node-version: ${{ inputs.node_version }}
71+
72+
# Setup Terraform
73+
- name: Setup Terraform
74+
id: terraform_setup
75+
uses: hashicorp/setup-terraform@v3
76+
with:
77+
terraform_version: ${{ inputs.terraform_version }}
78+
terraform_wrapper: true
79+
80+
# Check Out Repository
81+
- name: Check Out Repository
82+
id: checkout_repository
83+
uses: actions/checkout@v4
84+
85+
# Terraform Init
86+
- name: Terraform Init
87+
working-directory: ${{ inputs.working_directory }}
88+
run: |
89+
terraform init -backend-config=../../config/${CONFIG}/azurerm.tfbackend
90+
env:
91+
CONFIG: ${{ inputs.config }}
92+
93+
# Terraform Destroy
94+
- name: Terraform Destroy
95+
working-directory: ${{ inputs.working_directory }}
96+
run: |
97+
terraform apply -var-file="../../config/${CONFIG}/vars.tfvars" -auto-approve -input=false -destroy
98+
env:
99+
CONFIG: ${{ inputs.config }}
100+
TF_VAR_my_secret: ${{ secrets.MY_SAMPLE_SECRET }}

.github/workflows/_terraformEnvironmentTemplate.yml

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ on:
3535
CLIENT_ID:
3636
required: true
3737
description: "Specifies the client id."
38-
CLIENT_SECRET:
39-
required: true
40-
description: "Specifies the client secret."
4138
MY_SAMPLE_SECRET:
4239
required: true
4340
description: "Specifies a sample secret."
@@ -101,15 +98,14 @@ jobs:
10198
environment: ${{ inputs.environment }}
10299
needs: [lint]
103100
concurrency:
104-
group: terraform-apply-${{ inputs.config }}-${{ inputs.environment }}
101+
group: terraform-${{ inputs.config }}-${{ inputs.environment }}
105102
cancel-in-progress: false
106103

107104
env:
108105
ARM_TENANT_ID: ${{ inputs.tenant_id }}
109106
ARM_SUBSCRIPTION_ID: ${{ inputs.subscription_id }}
110107
ARM_CLIENT_ID: ${{ secrets.CLIENT_ID }}
111-
ARM_CLIENT_SECRET: ${{ secrets.CLIENT_SECRET }}
112-
ARM_USE_OIDC: false
108+
ARM_USE_OIDC: true
113109

114110
steps:
115111
# Setup Node
@@ -196,18 +192,17 @@ jobs:
196192
runs-on: [self-hosted]
197193
continue-on-error: false
198194
environment: ${{ inputs.environment }}
199-
if: github.event_name == 'push' || github.event_name == 'release'
195+
# if: github.event_name == 'push' || github.event_name == 'release'
200196
needs: [plan]
201197
concurrency:
202-
group: terraform-apply-${{ inputs.config }}-${{ inputs.environment }}
198+
group: terraform-${{ inputs.config }}-${{ inputs.environment }}
203199
cancel-in-progress: false
204200

205201
env:
206202
ARM_TENANT_ID: ${{ inputs.tenant_id }}
207203
ARM_SUBSCRIPTION_ID: ${{ inputs.subscription_id }}
208204
ARM_CLIENT_ID: ${{ secrets.CLIENT_ID }}
209-
ARM_CLIENT_SECRET: ${{ secrets.CLIENT_SECRET }}
210-
ARM_USE_OIDC: false
205+
ARM_USE_OIDC: true
211206

212207
steps:
213208
# Setup Node

.github/workflows/terraform.yml

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,27 @@ jobs:
2727
with:
2828
environment: "dev"
2929
config: "PerfectThymeTech"
30-
terraform_version: "1.6.6"
31-
node_version: 18
30+
terraform_version: "1.9.6"
31+
node_version: 20
3232
working_directory: "./code/infra"
33-
tenant_id: "3556be79-2979-4b19-a1af-4dd4e6d9ed7e"
34-
subscription_id: "8f171ff9-2b5b-4f0f-aed5-7fa360a1d094"
33+
tenant_id: "37963dd4-f4e6-40f8-a7d6-24b97919e452"
34+
subscription_id: "1fdab118-1638-419a-8b12-06c9543714a0"
35+
secrets:
36+
CLIENT_ID: ${{ secrets.CLIENT_ID }}
37+
MY_SAMPLE_SECRET: ${{ secrets.MY_SAMPLE_SECRET }}
38+
39+
terraform_dev_destroy:
40+
uses: ./.github/workflows/_terraformDestroyTemplate.yml
41+
name: "Dev - Destroy"
42+
needs: [terraform_dev]
43+
with:
44+
environment: "dev"
45+
config: "PerfectThymeTech"
46+
terraform_version: "1.9.6"
47+
node_version: 20
48+
working_directory: "./code/infra"
49+
tenant_id: "37963dd4-f4e6-40f8-a7d6-24b97919e452"
50+
subscription_id: "1fdab118-1638-419a-8b12-06c9543714a0"
3551
secrets:
3652
CLIENT_ID: ${{ secrets.CLIENT_ID }}
37-
CLIENT_SECRET: ${{ secrets.CLIENT_SECRET }}
3853
MY_SAMPLE_SECRET: ${{ secrets.MY_SAMPLE_SECRET }}

code/infra/alerts.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
resource "azurerm_monitor_activity_log_alert" "monitor_activity_log_alert_service_health" {
22
name = "${local.prefix}-alert-servicehealth"
3+
location = "global"
34
resource_group_name = azurerm_resource_group.logging_rg.name
45
tags = var.tags
56

code/infra/applicationinsights.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module "application_insights" {
2+
source = "github.com/PerfectThymeTech/terraform-azurerm-modules//modules/applicationinsights?ref=main"
3+
providers = {
4+
azurerm = azurerm
5+
}
6+
7+
location = var.location
8+
resource_group_name = azurerm_resource_group.logging_rg.name
9+
tags = var.tags
10+
application_insights_name = "${local.prefix}-appi001"
11+
application_insights_application_type = "other"
12+
application_insights_log_analytics_workspace_id = var.log_analytics_workspace_id
13+
diagnostics_configurations = [] # local.diagnostics_configurations # Disabled to avoid duplicate logs in LAW and App Insights
14+
}

code/infra/appserviceplan.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module "app_service_plan" {
2+
source = "github.com/PerfectThymeTech/terraform-azurerm-modules//modules/appserviceplan?ref=main"
3+
providers = {
4+
azurerm = azurerm
5+
}
6+
7+
location = var.location
8+
resource_group_name = azurerm_resource_group.app_rg.name
9+
tags = var.tags
10+
service_plan_name = "${local.prefix}-asp001"
11+
service_plan_maximum_elastic_worker_count = null
12+
service_plan_os_type = "Linux"
13+
service_plan_per_site_scaling_enabled = false
14+
service_plan_sku_name = var.function_sku
15+
service_plan_worker_count = 1 # Update to '3' for production
16+
service_plan_zone_balancing_enabled = false # Update to 'true' for production
17+
diagnostics_configurations = local.diagnostics_configurations
18+
}

code/infra/function.tf

Lines changed: 22 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,3 @@
1-
resource "azurerm_service_plan" "service_plan" {
2-
name = "${local.prefix}-asp001"
3-
location = var.location
4-
resource_group_name = azurerm_resource_group.app_rg.name
5-
tags = var.tags
6-
7-
# maximum_elastic_worker_count = 20
8-
os_type = "Linux"
9-
per_site_scaling_enabled = false
10-
sku_name = var.function_sku
11-
worker_count = 1 # Update to '3' for production
12-
zone_balancing_enabled = false # Update to 'true' for production
13-
}
14-
15-
data "azurerm_monitor_diagnostic_categories" "diagnostic_categories_service_plan" {
16-
resource_id = azurerm_service_plan.service_plan.id
17-
}
18-
19-
resource "azurerm_monitor_diagnostic_setting" "diagnostic_setting_service_plan" {
20-
name = "logAnalytics"
21-
target_resource_id = azurerm_service_plan.service_plan.id
22-
log_analytics_workspace_id = azurerm_log_analytics_workspace.log_analytics_workspace.id
23-
24-
dynamic "enabled_log" {
25-
iterator = entry
26-
for_each = data.azurerm_monitor_diagnostic_categories.diagnostic_categories_service_plan.log_category_groups
27-
content {
28-
category_group = entry.value
29-
}
30-
}
31-
32-
dynamic "metric" {
33-
iterator = entry
34-
for_each = data.azurerm_monitor_diagnostic_categories.diagnostic_categories_service_plan.metrics
35-
content {
36-
category = entry.value
37-
enabled = true
38-
}
39-
}
40-
}
41-
421
resource "azapi_resource" "function" {
432
type = "Microsoft.Web/sites@2022-09-01"
443
parent_id = azurerm_resource_group.app_rg.id
@@ -65,7 +24,7 @@ resource "azapi_resource" "function" {
6524
redundancyMode = "None"
6625
reserved = true
6726
scmSiteAlsoStopped = true
68-
serverFarmId = azurerm_service_plan.service_plan.id
27+
serverFarmId = module.app_service_plan.service_plan_id
6928
storageAccountRequired = false
7029
vnetContentShareEnabled = true
7130
vnetImagePullEnabled = false # Set to 'true' when pulling image from private Azure Container Registry
@@ -91,7 +50,7 @@ resource "azapi_resource" "function" {
9150
appSettings = [
9251
{
9352
name = "APPLICATIONINSIGHTS_CONNECTION_STRING"
94-
value = azurerm_application_insights.application_insights.connection_string
53+
value = module.application_insights.application_insights_connection_string
9554
},
9655
{
9756
name = "AZURE_SDK_TRACING_IMPLEMENTATION"
@@ -123,23 +82,23 @@ resource "azapi_resource" "function" {
12382
},
12483
{
12584
name = "WEBSITE_OS_TYPE"
126-
value = azurerm_service_plan.service_plan.os_type
85+
value = module.app_service_plan.service_plan_os_type
12786
},
12887
{
12988
name = "WEBSITE_RUN_FROM_PACKAGE"
13089
value = "0"
13190
},
13291
{
13392
name = "AzureWebJobsStorage__accountName"
134-
value = azurerm_storage_account.storage.name
93+
value = module.storage_account.storage_account_name
13594
},
13695
{
13796
name = "AzureWebJobsSecretStorageType"
13897
value = "keyvault"
13998
},
14099
{
141100
name = "AzureWebJobsSecretStorageKeyVaultUri"
142-
value = azurerm_key_vault.key_vault.vault_uri
101+
value = module.key_vault.key_vault_uri
143102
},
144103
{
145104
name = "WEBSITES_ENABLE_APP_SERVICE_STORAGE" # Disable when not running a container
@@ -219,11 +178,8 @@ resource "azapi_resource" "function" {
219178
# "properties.siteConfig.appSettings"
220179
# ]
221180
depends_on = [
222-
azurerm_private_endpoint.key_vault_private_endpoint,
223-
azurerm_private_endpoint.storage_private_endpoint_blob,
224-
azurerm_private_endpoint.storage_private_endpoint_file,
225-
azurerm_private_endpoint.storage_private_endpoint_queue,
226-
azurerm_private_endpoint.storage_private_endpoint_table,
181+
module.key_vault.key_vault_setup_completed,
182+
module.storage_account.storage_setup_completed,
227183
]
228184
}
229185

@@ -234,7 +190,7 @@ data "azurerm_monitor_diagnostic_categories" "diagnostic_categories_function" {
234190
resource "azurerm_monitor_diagnostic_setting" "diagnostic_setting_function" {
235191
name = "logAnalytics"
236192
target_resource_id = azapi_resource.function.id
237-
log_analytics_workspace_id = azurerm_log_analytics_workspace.log_analytics_workspace.id
193+
log_analytics_workspace_id = var.log_analytics_workspace_id
238194

239195
dynamic "enabled_log" {
240196
iterator = entry
@@ -267,11 +223,20 @@ resource "azurerm_private_endpoint" "function_private_endpoint" {
267223
private_connection_resource_id = azapi_resource.function.id
268224
subresource_names = ["sites"]
269225
}
270-
subnet_id = azapi_resource.subnet_services.id
271-
private_dns_zone_group {
272-
name = "${azapi_resource.function.name}-arecord"
273-
private_dns_zone_ids = [
274-
var.private_dns_zone_id_sites
226+
subnet_id = azapi_resource.subnet_private_endpoints.id
227+
dynamic "private_dns_zone_group" {
228+
for_each = var.private_dns_zone_id_sites == "" ? [] : [1]
229+
content {
230+
name = "${azapi_resource.function.name}-arecord"
231+
private_dns_zone_ids = [
232+
var.private_dns_zone_id_sites
233+
]
234+
}
235+
}
236+
237+
lifecycle {
238+
ignore_changes = [
239+
private_dns_zone_group
275240
]
276241
}
277242
}

0 commit comments

Comments
 (0)