Skip to content

Commit 2722627

Browse files
authored
use existing resource group and fmt (#37)
* use existing resource group and fmt * simplified variable
1 parent 354162a commit 2722627

File tree

16 files changed

+101
-85
lines changed

16 files changed

+101
-85
lines changed

modules/services/event-hub-data-source/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ No modules.
6969
| <a name="input_partition_count"></a> [partition\_count](#input\_partition\_count) | The number of partitions in the Event Hub | `number` | `4` | no |
7070
| <a name="input_region"></a> [region](#input\_region) | Datacenter where Sysdig-related resources will be created | `string` | n/a | yes |
7171
| <a name="input_resource_group_name"></a> [resource\_group\_name](#input\_resource\_group\_name) | Name of the resource group to be created | `string` | `"sysdig-resource-group"` | no |
72+
| <a name="input_resource_group"></a> [resource\_group](#input\_resource\_group) | Name of the existing resource group | `string` | n/a | no |
7273
| <a name="input_subscription_id"></a> [subscription\_id](#input\_subscription\_id) | Identifier of the subscription to be onboarded | `string` | n/a | yes |
7374
| <a name="input_sysdig_client_id"></a> [sysdig\_client\_id](#input\_sysdig\_client\_id) | Service client ID in the Sysdig tenant | `string` | n/a | yes |
7475
| <a name="input_throughput_units"></a> [throughput\_units](#input\_throughput\_units) | The number of throughput units to be allocated to the Event Hub | `number` | `1` | no |

modules/services/event-hub-data-source/main.tf

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,26 @@ resource "random_string" "random" {
2525
# Create service principal in customer tenant
2626
#---------------------------------------------------------------------------------------------
2727
resource "azuread_service_principal" "sysdig_service_principal" {
28-
client_id = var.sysdig_client_id
28+
client_id = var.sysdig_client_id
2929
use_existing = true
3030
lifecycle {
3131
prevent_destroy = true
3232
}
3333
}
3434

35+
#---------------------------------------------------------------------------------------------
36+
# Use an existing resource group for Sysdig resources
37+
#---------------------------------------------------------------------------------------------
38+
data "azurerm_resource_group" "existing" {
39+
count = var.resource_group != null ? 1 : 0
40+
name = var.resource_group
41+
}
42+
3543
#---------------------------------------------------------------------------------------------
3644
# Create a resource group for Sysdig resources
3745
#---------------------------------------------------------------------------------------------
3846
resource "azurerm_resource_group" "sysdig_resource_group" {
47+
count = var.resource_group == null ? 1 : 0
3948
name = "${var.resource_group_name}-${local.subscription_hash}"
4049
location = var.region
4150
}
@@ -44,12 +53,12 @@ resource "azurerm_resource_group" "sysdig_resource_group" {
4453
# Create an Event Hub Namespace for Sysdig
4554
#---------------------------------------------------------------------------------------------
4655
resource "azurerm_eventhub_namespace" "sysdig_event_hub_namespace" {
47-
name = "${var.event_hub_namespace_name}-${local.subscription_hash}"
48-
location = azurerm_resource_group.sysdig_resource_group.location
49-
resource_group_name = azurerm_resource_group.sysdig_resource_group.name
50-
sku = var.namespace_sku
51-
capacity = var.throughput_units
52-
auto_inflate_enabled = var.auto_inflate_enabled
56+
name = "${var.event_hub_namespace_name}-${local.subscription_hash}"
57+
location = var.resource_group != null ? data.azurerm_resource_group.existing[0].location : azurerm_resource_group.sysdig_resource_group[0].location
58+
resource_group_name = var.resource_group != null ? data.azurerm_resource_group.existing[0].name : azurerm_resource_group.sysdig_resource_group[0].name
59+
sku = var.namespace_sku
60+
capacity = var.throughput_units
61+
auto_inflate_enabled = var.auto_inflate_enabled
5362
maximum_throughput_units = var.maximum_throughput_units
5463
}
5564

@@ -59,7 +68,7 @@ resource "azurerm_eventhub_namespace" "sysdig_event_hub_namespace" {
5968
resource "azurerm_eventhub" "sysdig_event_hub" {
6069
name = "${var.event_hub_name}-${random_string.random.result}"
6170
namespace_name = azurerm_eventhub_namespace.sysdig_event_hub_namespace.name
62-
resource_group_name = azurerm_resource_group.sysdig_resource_group.name
71+
resource_group_name = var.resource_group != null ? data.azurerm_resource_group.existing[0].name : azurerm_resource_group.sysdig_resource_group[0].name
6372
partition_count = var.partition_count
6473
message_retention = var.message_retention_days
6574
}
@@ -71,7 +80,7 @@ resource "azurerm_eventhub_consumer_group" "sysdig_consumer_group" {
7180
name = var.consumer_group_name
7281
namespace_name = azurerm_eventhub_namespace.sysdig_event_hub_namespace.name
7382
eventhub_name = azurerm_eventhub.sysdig_event_hub.name
74-
resource_group_name = azurerm_resource_group.sysdig_resource_group.name
83+
resource_group_name = var.resource_group != null ? data.azurerm_resource_group.existing[0].name : azurerm_resource_group.sysdig_resource_group[0].name
7584
}
7685

7786
#---------------------------------------------------------------------------------------------
@@ -80,7 +89,7 @@ resource "azurerm_eventhub_consumer_group" "sysdig_consumer_group" {
8089
resource "azurerm_eventhub_namespace_authorization_rule" "sysdig_rule" {
8190
name = var.eventhub_authorization_rule_name
8291
namespace_name = azurerm_eventhub_namespace.sysdig_event_hub_namespace.name
83-
resource_group_name = azurerm_resource_group.sysdig_resource_group.name
92+
resource_group_name = var.resource_group != null ? data.azurerm_resource_group.existing[0].name : azurerm_resource_group.sysdig_resource_group[0].name
8493

8594
listen = true
8695
send = true
@@ -121,7 +130,7 @@ resource "azurerm_monitor_diagnostic_setting" "sysdig_diagnostic_setting" {
121130
resource "azurerm_monitor_aad_diagnostic_setting" "sysdig_entra_diagnostic_setting" {
122131
count = var.enable_entra ? 1 : 0
123132

124-
name = "${var.entra_diagnostic_settings_name}-${local.subscription_hash}"
133+
name = "${var.entra_diagnostic_settings_name}-${local.subscription_hash}"
125134
eventhub_authorization_rule_id = azurerm_eventhub_namespace_authorization_rule.sysdig_rule.id
126135
eventhub_name = azurerm_eventhub.sysdig_event_hub.name
127136

@@ -135,15 +144,15 @@ resource "azurerm_monitor_aad_diagnostic_setting" "sysdig_entra_diagnostic_setti
135144

136145
enabled_log {
137146
category = "SignInLogs"
138-
147+
139148
retention_policy {
140149
enabled = false
141150
}
142151
}
143152

144153
enabled_log {
145154
category = "NonInteractiveUserSignInLogs"
146-
155+
147156
retention_policy {
148157
enabled = false
149158
}
@@ -167,7 +176,7 @@ resource "azurerm_monitor_aad_diagnostic_setting" "sysdig_entra_diagnostic_setti
167176

168177
enabled_log {
169178
category = "ProvisioningLogs"
170-
179+
171180
retention_policy {
172181
enabled = false
173182
}
@@ -183,15 +192,15 @@ resource "azurerm_monitor_aad_diagnostic_setting" "sysdig_entra_diagnostic_setti
183192

184193
enabled_log {
185194
category = "RiskyUsers"
186-
195+
187196
retention_policy {
188197
enabled = false
189198
}
190199
}
191200

192201
enabled_log {
193202
category = "UserRiskEvents"
194-
203+
195204

196205
retention_policy {
197206
enabled = false

modules/services/event-hub-data-source/organizational.tf

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ data "azurerm_management_group" "onboarded_management_group" {
66
}
77

88
data "azurerm_management_group" "root_management_group" {
9-
count = var.is_organizational && length(var.management_group_ids) == 0 ? 1 : 0
9+
count = var.is_organizational && length(var.management_group_ids) == 0 ? 1 : 0
1010
display_name = "Tenant Root Group"
1111
}
1212

@@ -18,12 +18,12 @@ locals {
1818
}
1919

2020
data "azurerm_subscription" "onboarded_subscriptions" {
21-
for_each = var.is_organizational && length(local.all_mg_subscription_ids) > 0 ? toset(local.all_mg_subscription_ids) : toset([])
21+
for_each = var.is_organizational && length(local.all_mg_subscription_ids) > 0 ? toset(local.all_mg_subscription_ids) : toset([])
2222
subscription_id = each.value
2323
}
2424

25-
locals {
26-
enabled_subscriptions = var.is_organizational ? [for s in data.azurerm_subscription.onboarded_subscriptions : s if s.state == "Enabled"] : []
25+
locals {
26+
enabled_subscriptions = var.is_organizational ? [for s in data.azurerm_subscription.onboarded_subscriptions : s if s.state == "Enabled"] : []
2727
}
2828

2929
#---------------------------------------------------------------------------------------------
@@ -32,8 +32,8 @@ locals {
3232
resource "azurerm_monitor_diagnostic_setting" "sysdig_org_diagnostic_setting" {
3333
count = var.is_organizational ? length(local.enabled_subscriptions) : 0
3434

35-
name = "${var.diagnostic_settings_name}-${local.subscription_hash}"
36-
target_resource_id = local.enabled_subscriptions[count.index].id
35+
name = "${var.diagnostic_settings_name}-${local.subscription_hash}"
36+
target_resource_id = local.enabled_subscriptions[count.index].id
3737
eventhub_authorization_rule_id = azurerm_eventhub_namespace_authorization_rule.sysdig_rule.id
3838
eventhub_name = azurerm_eventhub.sysdig_event_hub.name
3939

modules/services/event-hub-data-source/outputs.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ output "event_hub_namespace" {
88
description = "Name of the newly created Event Hub Namespace"
99
}
1010
output "consumer_group_name" {
11-
value = azurerm_eventhub_consumer_group.sysdig_consumer_group.name
12-
description = "Name of the newly created Event Hub Consumer Group"
11+
value = azurerm_eventhub_consumer_group.sysdig_consumer_group.name
12+
description = "Name of the newly created Event Hub Consumer Group"
1313
}
1414

1515
output "subscription_alias" {
16-
value = data.azurerm_subscription.sysdig_subscription.display_name
16+
value = data.azurerm_subscription.sysdig_subscription.display_name
1717
description = "Display name of the subscription"
1818
}

modules/services/event-hub-data-source/variables.tf

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ variable "subscription_id" {
44
}
55

66
variable "sysdig_client_id" {
7-
type = string
7+
type = string
88
description = "Service client ID in the Sysdig tenant"
99
}
1010

@@ -61,33 +61,39 @@ variable "event_hub_name" {
6161
}
6262

6363
variable "resource_group_name" {
64-
type = string
64+
type = string
6565
description = "Name of the resource group to be created"
66-
default = "sysdig-resource-group"
66+
default = "sysdig-resource-group"
67+
}
68+
69+
variable "resource_group" {
70+
type = string
71+
description = "Name of the existing resource group"
72+
default = null
6773
}
6874

6975
variable "consumer_group_name" {
70-
type = string
76+
type = string
7177
description = "Name of the consumer group to be created"
72-
default = "sysdig-consumer-group"
78+
default = "sysdig-consumer-group"
7379
}
7480

7581
variable "eventhub_authorization_rule_name" {
76-
type = string
82+
type = string
7783
description = "Name of the authorization rule to be created"
78-
default = "sysdig-send-listen-rule"
84+
default = "sysdig-send-listen-rule"
7985
}
8086

8187
variable "diagnostic_settings_name" {
82-
type = string
88+
type = string
8389
description = "Name of the diagnostic settings to be created"
84-
default = "sysdig-diagnostic-settings"
90+
default = "sysdig-diagnostic-settings"
8591
}
8692

8793
variable "entra_diagnostic_settings_name" {
88-
type = string
94+
type = string
8995
description = "Name of the Entra diagnostic settings to be created"
90-
default = "sysdig-entra-diagnostic-settings"
96+
default = "sysdig-entra-diagnostic-settings"
9197
}
9298

9399
variable "is_organizational" {

modules/services/host-scanner/main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
provider "azurerm" {
2-
features { }
2+
features {}
33
}
44

55
data "azurerm_subscription" "primary" {
@@ -20,7 +20,7 @@ resource "azurerm_lighthouse_definition" "lighthouse_definition" {
2020
}
2121

2222
resource "azurerm_lighthouse_assignment" "lighthouse_assignment" {
23-
count = var.is_organizational ? 0 : 1
23+
count = var.is_organizational ? 0 : 1
2424
scope = "/subscriptions/${var.subscription_id}"
2525
lighthouse_definition_id = azurerm_lighthouse_definition.lighthouse_definition.id
2626
}

modules/services/host-scanner/organizational.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
#---------------------------------------------------------------------------------------------
44
# If no management group is present, then the root management group is used to onboard all the subscriptions
55
data "azurerm_management_group" "root_management_group" {
6-
count = var.is_organizational && length(var.management_group_ids) == 0 ? 1 : 0
6+
count = var.is_organizational && length(var.management_group_ids) == 0 ? 1 : 0
77
display_name = "Tenant Root Group"
88
}
99

1010
data "azurerm_management_group" "management_groups" {
1111
for_each = var.is_organizational && length(var.management_group_ids) > 0 ? var.management_group_ids : []
12-
name = each.value
12+
name = each.value
1313
}
1414

1515
locals {
1616
subscriptions = toset(var.is_organizational && length(var.management_group_ids) == 0 ? data.azurerm_management_group.root_management_group[0].subscription_ids :
17-
flatten([for m in data.azurerm_management_group.management_groups : m.subscription_ids]))
17+
flatten([for m in data.azurerm_management_group.management_groups : m.subscription_ids]))
1818
}
1919

2020
resource "azurerm_lighthouse_assignment" "lighthouse_assignment_for_tenant" {

modules/services/host-scanner/output.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ output "lighthouse_definition_display_id" {
44
}
55

66
output "subscription_alias" {
7-
value = data.azurerm_subscription.primary.display_name
7+
value = data.azurerm_subscription.primary.display_name
88
description = "Display name of the subscription"
99
}

modules/services/host-scanner/variables.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
variable "subscription_id" {
2-
type = string
2+
type = string
33
description = "Subscription ID in which to create a trust relationship"
44
}
55

66
variable "sysdig_tenant_id" {
7-
type = string
7+
type = string
88
description = "Sysdig Tenant ID"
99
}
1010

1111
variable "sysdig_service_principal_id" {
12-
type = string
12+
type = string
1313
description = "Service Principal ID in the Sysdig tenant"
1414
}
1515

modules/services/service-principal/main.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ resource "azurerm_role_definition" "sysdig_cspm_role" {
6565
# Custom role assignment for collecting authsettings
6666
#---------------------------------------------------------------------------------------------
6767
resource "azurerm_role_assignment" "sysdig_cspm_role_assignment" {
68-
scope = data.azurerm_subscription.primary.id
69-
role_definition_id = azurerm_role_definition.sysdig_cspm_role.role_definition_resource_id
70-
principal_id = azuread_service_principal.sysdig_sp.object_id
68+
scope = data.azurerm_subscription.primary.id
69+
role_definition_id = azurerm_role_definition.sysdig_cspm_role.role_definition_resource_id
70+
principal_id = azuread_service_principal.sysdig_sp.object_id
7171
}

0 commit comments

Comments
 (0)