Skip to content

Commit b367a9b

Browse files
committed
feat(bicep): add tags parameter to various Bicep modules for resource tagging
1 parent 57f371f commit b367a9b

10 files changed

Lines changed: 112 additions & 5 deletions

infra/bicep/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Azure Bicep Infrastructure
2+
3+
This directory contains Bicep templates for deploying Azure infrastructure and is scanned for compliance using PSRule.
4+
5+
## PSRule for Azure
6+
7+
PSRule is a tool for validating Azure resources against best practices and compliance rules. It can be used to ensure that your Bicep templates adhere to organizational standards.
8+
9+
To manually run PSRule against the Bicep templates in this directory, you can use the following command:
10+
11+
```powershell
12+
$modules = @('PSRule.Rules.Azure')
13+
Install-Module -Name $modules -Scope CurrentUser -Force -ErrorAction Stop;
14+
Assert-PSRule -InputPath './infra/bicep/*.test.bicep' -Module $modules -Format File -ErrorAction Stop;
15+
```
16+
17+
Some rules have been suppressed due to known issues or specific requirements. You can find the list of suppressed rules in the [ps-rule.yaml](./ps-rule.yaml) file.
18+
19+
## Resources
20+
21+
- [PSRule for Azure](https://azure.github.io/PSRule.Rules.Azure/)
22+
- [PSRules for Azure Rule Reference](https://azure.github.io/PSRule.Rules.Azure/en/rules/)

infra/bicep/cosmosdb.bicep

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ param accountKind string
44
param identityPrincipalId string
55
param currentIpAddress string
66
param servicePrincipalId string
7+
param tags object
78

89
// https://github.com/Azure/bicep-registry-modules/tree/main/avm/res/document-db/database-account
910
module databaseAccount 'br/public:avm/res/document-db/database-account:0.11.3' = {
@@ -92,6 +93,7 @@ module databaseAccount 'br/public:avm/res/document-db/database-account:0.11.3' =
9293
principalType: 'ServicePrincipal'
9394
}
9495
]
96+
tags: tags
9597
}
9698
}
9799

infra/bicep/kubernetes.bicep

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ param metricsWorkspaceResourceId string
88
param currentUserObjectId string
99
param currentIpAddress string
1010
param configureMonitorSettings bool = false
11+
param tags object
1112

1213
// https://github.com/Azure/bicep-registry-modules/tree/main/avm/res/container-service/managed-cluster
1314
module managedCluster 'br/public:avm/res/container-service/managed-cluster:0.8.3' = {
@@ -30,6 +31,7 @@ module managedCluster 'br/public:avm/res/container-service/managed-cluster:0.8.3
3031
enableOidcIssuerProfile: true
3132
enableWorkloadIdentity: true
3233
enableKeyvaultSecretsProvider: true
34+
enableSecretRotation: true
3335
enableAzureMonitorProfileMetrics: configureMonitorSettings
3436
enableContainerInsights: configureMonitorSettings
3537
disablePrometheusMetricsScraping: !configureMonitorSettings
@@ -42,16 +44,49 @@ module managedCluster 'br/public:avm/res/container-service/managed-cluster:0.8.3
4244
systemAssigned: true
4345
}
4446
publicNetworkAccess: 'Enabled'
45-
// authorizedIPRanges: [
46-
// currentIpAddress
47-
// ]
47+
authorizedIPRanges: [
48+
currentIpAddress
49+
]
4850
roleAssignments: [
4951
{
5052
principalId: currentUserObjectId
5153
roleDefinitionIdOrName: 'Azure Kubernetes Service RBAC Cluster Admin'
5254
principalType: 'User'
5355
}
5456
]
57+
maintenanceConfigurations: [
58+
{
59+
maintenanceWindow: {
60+
durationHours: 4
61+
schedule: {
62+
weekly: {
63+
dayOfWeek: 'Sunday'
64+
intervalWeeks: 1
65+
}
66+
}
67+
startDate: '2025-06-11'
68+
startTime: '00:00'
69+
utcOffset: '+00:00'
70+
}
71+
name: 'aksManagedAutoUpgradeSchedule'
72+
}
73+
{
74+
maintenanceWindow: {
75+
durationHours: 4
76+
schedule: {
77+
weekly: {
78+
dayOfWeek: 'Sunday'
79+
intervalWeeks: 1
80+
}
81+
}
82+
startDate: '2025-06-11'
83+
startTime: '00:00'
84+
utcOffset: '+00:00'
85+
}
86+
name: 'aksManagedNodeOSUpgradeSchedule'
87+
}
88+
]
89+
tags: tags
5590
}
5691
}
5792

@@ -77,6 +112,7 @@ module registry 'br/public:avm/res/container-registry/registry:0.9.1' = if (depl
77112
principalType: 'ServicePrincipal'
78113
}
79114
]
115+
tags: tags
80116
}
81117
}
82118

@@ -95,6 +131,7 @@ resource dataCollectionEndpoint 'Microsoft.Insights/dataCollectionEndpoints@2022
95131
properties: {
96132
description: 'Data Collection Endpoint for Prometheus'
97133
}
134+
tags: tags
98135
}
99136

100137
resource dataCollectionRuleAssociationEndpoint 'Microsoft.Insights/dataCollectionRuleAssociations@2022-06-01' = if (configureMonitorSettings) {
@@ -148,6 +185,7 @@ resource dataCollectionRuleMSCI 'Microsoft.Insights/dataCollectionRules@2022-06-
148185
}
149186
]
150187
}
188+
tags: tags
151189
}
152190

153191
resource dataCollectionRuleAssociationMSCI 'Microsoft.Insights/dataCollectionRuleAssociations@2022-06-01' = if (configureMonitorSettings) {
@@ -193,6 +231,7 @@ resource dataCollectionRuleMSProm 'Microsoft.Insights/dataCollectionRules@2022-0
193231
}
194232
]
195233
}
234+
tags: tags
196235
}
197236

198237
resource dataCollectionRuleAssociationMSProm 'Microsoft.Insights/dataCollectionRuleAssociations@2022-06-01' = if (configureMonitorSettings) {
@@ -306,6 +345,7 @@ resource prometheusK8sRuleGroups 'Microsoft.AlertsManagement/prometheusRuleGroup
306345
}
307346
]
308347
}
348+
tags: tags
309349
}
310350

311351
resource prometheusNodeRuleGroups 'Microsoft.AlertsManagement/prometheusRuleGroups@2023-03-01' = if (configureMonitorSettings) {
@@ -367,6 +407,7 @@ resource prometheusNodeRuleGroups 'Microsoft.AlertsManagement/prometheusRuleGrou
367407
}
368408
]
369409
}
410+
tags: tags
370411
}
371412

372413
output id string = managedCluster.outputs.resourceId

infra/bicep/main.bicep

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,19 @@ param currentIpAddress string
6767
@description('value of source registry to use for image imports')
6868
param sourceRegistry string = 'ghcr.io/azure-samples'
6969

70+
@description('value of tags to apply to resources')
71+
param tags object = {
72+
environment: 'development'
73+
}
74+
7075
// generate a unique string based on the resource group id
7176
// this is used to ensure that each resource name is unique
7277
var name = '${appEnvironment}${take(uniqueString(subscription().id, appEnvironment), 4)}'
7378

7479
resource rg 'Microsoft.Resources/resourceGroups@2021-04-01' = {
7580
name: 'rg-${name}'
7681
location: location
82+
tags: tags
7783
}
7884

7985
module observability 'observability.bicep' = if (deployObservabilityTools) {
@@ -82,6 +88,7 @@ module observability 'observability.bicep' = if (deployObservabilityTools) {
8288
params: {
8389
nameSuffix: name
8490
currentUserObjectId: currentUserObjectId
91+
tags: tags
8592
}
8693
}
8794

@@ -98,6 +105,7 @@ module aks 'kubernetes.bicep' = {
98105
currentUserObjectId: currentUserObjectId
99106
currentIpAddress: currentIpAddress
100107
configureMonitorSettings: deployObservabilityTools
108+
tags: tags
101109
}
102110
}
103111

@@ -113,6 +121,7 @@ module workloadidentity 'workloadidentity.bicep' = if (deployAzureCosmosDB || de
113121
partialSubject: 'system:serviceaccount:${k8sNamespace}'
114122
}
115123
]
124+
tags: tags
116125
}
117126
}
118127

@@ -124,6 +133,7 @@ module servicebus 'servicebus.bicep' = if (deployAzureServiceBus) {
124133
currentUserObjectId: currentUserObjectId
125134
currentIpAddress: currentIpAddress
126135
servicePrincipalId: workloadidentity.outputs.principalId
136+
tags: tags
127137
}
128138
}
129139

@@ -136,6 +146,7 @@ module cosmosdb 'cosmosdb.bicep' = if (deployAzureCosmosDB) {
136146
identityPrincipalId: workloadidentity.outputs.principalId
137147
currentIpAddress: currentIpAddress
138148
servicePrincipalId: workloadidentity.outputs.principalId
149+
tags: tags
139150
}
140151
}
141152

@@ -160,6 +171,7 @@ module openai 'openai.bicep' = if (deployAzureOpenAI) {
160171
currentIpAddress: currentIpAddress
161172
servicePrincipalId: workloadidentity.outputs.principalId
162173
modelDeployments: modelDeployments
174+
tags: tags
163175
}
164176
}
165177

@@ -196,5 +208,5 @@ output AZURE_DATABASE_API string = cosmosDBAccountKind == 'MongoDB' ? 'mongodb'
196208
output AZURE_REGISTRY_NAME string = deployAzureContainerRegistry ? aks.outputs.registryName : ''
197209
output AZURE_REGISTRY_URI string = deployAzureContainerRegistry
198210
? aks.outputs.registryLoginServer
199-
: sourceRegistry
211+
: sourceRegistry
200212
output AZURE_TENANT_ID string = tenant().tenantId

infra/bicep/main.test.bicep

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// This file is for doing static analysis and contains sensible defaults
2-
// for PSRule to minimise false-positives and provide the best results.
2+
// for PSRule to minimize false-positives and provide the best results.
33

44
// This file is not intended to be used as a runtime configuration file.
55

66
targetScope = 'subscription'
77

8+
@description('value of deployment location for resources')
89
param location string = 'swedencentral'
910

1011
module main 'main.bicep' = {
@@ -14,5 +15,8 @@ module main 'main.bicep' = {
1415
location: location
1516
appEnvironment: 'test'
1617
currentIpAddress: '1.1.1.1'
18+
tags: {
19+
environment: 'development'
20+
}
1721
}
1822
}

infra/bicep/observability.bicep

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ param nameSuffix string
33
@description('The location of the resource.')
44
param location string = resourceGroup().location
55
param currentUserObjectId string
6+
param tags object
67

78
resource logWorkspace 'Microsoft.OperationalInsights/workspaces@2022-10-01' = {
89
name: 'logs-${nameSuffix}'
@@ -15,11 +16,13 @@ resource logWorkspace 'Microsoft.OperationalInsights/workspaces@2022-10-01' = {
1516
name: 'PerGB2018'
1617
}
1718
}
19+
tags: tags
1820
}
1921

2022
resource metricsWorkspace 'Microsoft.Monitor/accounts@2023-04-03' = {
2123
name: 'metrics-${nameSuffix}'
2224
location: location
25+
tags: tags
2326
}
2427

2528
resource grafanaDashboard 'Microsoft.Dashboard/grafana@2023-09-01' = {
@@ -32,6 +35,7 @@ resource grafanaDashboard 'Microsoft.Dashboard/grafana@2023-09-01' = {
3235
type: 'SystemAssigned'
3336
}
3437
properties: {
38+
grafanaMajorVersion: '11'
3539
grafanaIntegrations: {
3640
azureMonitorWorkspaceIntegrations: [
3741
{
@@ -40,6 +44,7 @@ resource grafanaDashboard 'Microsoft.Dashboard/grafana@2023-09-01' = {
4044
]
4145
}
4246
}
47+
tags: tags
4348
}
4449

4550
resource grafanaAdminRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = {

infra/bicep/openai.bicep

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ param currentUserObjectId string
55
param currentIpAddress string
66
param servicePrincipalId string
77
param modelDeployments array = []
8+
param tags object
89

910
// https://github.com/Azure/bicep-registry-modules/tree/main/avm/res/cognitive-services/account
1011
module cognitiveServicesAccount 'br/public:avm/res/cognitive-services/account:0.10.1' = {
@@ -52,6 +53,7 @@ module cognitiveServicesAccount 'br/public:avm/res/cognitive-services/account:0.
5253
principalType: 'ServicePrincipal'
5354
}
5455
]
56+
tags: tags
5557
}
5658
}
5759

infra/bicep/ps-rule.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
configuration:
2+
AZURE_BICEP_FILE_EXPANSION: true
3+
rule:
4+
exclude:
5+
# Ignore the following rules for all resources to keep cost down
6+
- Azure.Log.Replication # log storage would be too expensive
7+
- Azure.ACR.ContentTrust # requires ACR Premium SKU
8+
- Azure.ACR.Firewall # requires ACR Premium SKU
9+
- Azure.AKS.PoolScaleSet # run on a single node pool
10+
- Azure.AKS.NodeMinPods # run on a single node pool
11+
- Azure.AKS.AuditLogs # log storage would be too expensive
12+
- Azure.AKS.PlatformLogs # log storage would be too expensive
13+
- Azure.AKS.NetworkPolicy # using cilium instead which is recommended but not in the allowlist in this rule
14+
- Azure.AKS.DefenderProfile # log storage would be too expensive
15+
- Azure.Deployment.SecureParameter # uniqueKeyPolicyKeys needs to be secured here https://github.com/Azure/bicep-registry-modules/blob/main/avm/res/document-db/database-account/main.bicep

infra/bicep/servicebus.bicep

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ param nameSuffix string
33
param currentUserObjectId string
44
param currentIpAddress string
55
param servicePrincipalId string
6+
param tags object
67

78
// https://github.com/Azure/bicep-registry-modules/tree/main/avm/res/service-bus/namespace
89
module serviceBusNamespace 'br/public:avm/res/service-bus/namespace:0.13.2' = {
@@ -43,6 +44,7 @@ module serviceBusNamespace 'br/public:avm/res/service-bus/namespace:0.13.2' = {
4344
principalType: 'ServicePrincipal'
4445
}
4546
]
47+
tags: tags
4648
}
4749
}
4850

infra/bicep/workloadidentity.bicep

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@minLength(3)
22
param nameSuffix string
33
param federatedCredentials federatedCredential[]
4+
param tags object
45

56
type federatedCredential = {
67
audiences: string[]
@@ -20,6 +21,7 @@ module userAssignedIdentity 'br/public:avm/res/managed-identity/user-assigned-id
2021
subject: '${fc.partialSubject}:mi-${nameSuffix}'
2122
}
2223
]
24+
tags: tags
2325
}
2426
}
2527

0 commit comments

Comments
 (0)