Skip to content

Commit 2ad7826

Browse files
Merge pull request #240 from DataDog/mohamed.challal/fix-gcp-terraform-plan
[K9VULN-8670] fix(gcp): fix gcp terraform plan
2 parents 95598b0 + aa98a59 commit 2ad7826

File tree

10 files changed

+38
-33
lines changed

10 files changed

+38
-33
lines changed

gcp/examples/cross_project/README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ Use this **advanced** deployment model when:
7878
terraform init
7979
```
8080

81+
1. **Review the deployment plan**:
82+
```sh
83+
terraform plan \
84+
-var="scanner_project_id=my-scanner-project" \
85+
-var="datadog_api_key=$DD_API_KEY" \
86+
-var="datadog_site=datadoghq.com"
87+
```
88+
8189
1. **Deploy the scanner infrastructure**. You will need to:
8290
- Set your scanner project ID
8391
- Set your Datadog [API key](https://docs.datadoghq.com/account_management/api-app-keys/)
@@ -100,14 +108,19 @@ Use this **advanced** deployment model when:
100108

101109
1. Go to the `other_project` folder.
102110
1. Run `terraform init`.
103-
1. Run `terraform apply`.
111+
1. Run `terraform plan` to review the changes.
112+
1. Run `terraform apply` to deploy.
104113
1. Set the project ID to be scanned.
105114
1. Set both scanner service account emails from Step 1.
106115

107116
Example:
108117
```sh
109118
cd ../other_project
110119
terraform init
120+
terraform plan \
121+
-var="scanned_project_id=my-other-project" \
122+
-var="scanner_service_account_email_us=$SCANNER_SA_US" \
123+
-var="scanner_service_account_email_eu=$SCANNER_SA_EU"
111124
terraform apply \
112125
-var="scanned_project_id=my-other-project" \
113126
-var="scanner_service_account_email_us=$SCANNER_SA_US" \
@@ -153,7 +166,7 @@ Repeat Step 2 for each additional project you want to scan.
153166

154167
You have two options for providing the Datadog API key:
155168

156-
1. **Pass the API key directly** (shown in examples below):
169+
1. **Pass the API key directly** (shown in examples above):
157170
```bash
158171
-var="datadog_api_key=$DD_API_KEY"
159172
```

gcp/examples/cross_project/other_project/main.tf

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,10 @@ module "agentless_impersonated_service_account_us" {
2020
source = "git::https://github.com/DataDog/terraform-module-datadog-agentless-scanner//gcp/modules/agentless-impersonated-service-account?ref=7993939f19df2a39c981cbffbcd48a91c9fba214"
2121

2222
scanner_service_account_email = var.scanner_service_account_email_us
23-
unique_suffix = "${var.unique_suffix}us"
2423
}
2524

2625
module "agentless_impersonated_service_account_eu" {
2726
source = "git::https://github.com/DataDog/terraform-module-datadog-agentless-scanner//gcp/modules/agentless-impersonated-service-account?ref=7993939f19df2a39c981cbffbcd48a91c9fba214"
2827

2928
scanner_service_account_email = var.scanner_service_account_email_eu
30-
unique_suffix = "${var.unique_suffix}eu"
3129
}

gcp/examples/cross_project/other_project/variables.tf

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,3 @@ variable "scanner_service_account_email_eu" {
1212
description = "Email of the EU scanner service account from the scanner project (output from scanner_project deployment)"
1313
type = string
1414
}
15-
16-
variable "unique_suffix" {
17-
description = "Unique suffix to append to resource names. Must be alphanumeric only and maximum 6 characters (will be appended with region suffix)."
18-
type = string
19-
default = ""
20-
}

gcp/examples/cross_project/scanner_project/main.tf

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ module "datadog_agentless_scanner_us" {
2929
google = google.us
3030
}
3131

32-
site = var.datadog_site
33-
api_key = var.datadog_api_key
34-
vpc_name = "datadog-agentless-scanner-us"
35-
unique_suffix = ""
32+
site = var.datadog_site
33+
api_key = var.datadog_api_key
34+
vpc_name = "datadog-agentless-scanner-us"
3635
}
3736

3837
# Deploy the scanner infrastructure in EU region
@@ -43,8 +42,7 @@ module "datadog_agentless_scanner_eu" {
4342
google = google.eu
4443
}
4544

46-
site = var.datadog_site
47-
api_key = var.datadog_api_key
48-
vpc_name = "datadog-agentless-scanner-eu"
49-
unique_suffix = ""
45+
site = var.datadog_site
46+
api_key = var.datadog_api_key
47+
vpc_name = "datadog-agentless-scanner-eu"
5048
}

gcp/examples/single_region/README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This folder shows an example of Terraform code that uses the [datadog-agentless-scanner module](https://github.com/Datadog/terraform-module-datadog-agentless-scanner/tree/main/gcp) to deploy a Datadog Agentless scanner in your [GCP](https://cloud.google.com/) project.
44

5-
With this deployment, a single Agentless scanner is deployed in a single region with instances distributed across multiple zones for high availability. Datadog recommends this option for most use cases.
5+
With this deployment, a single Agentless scanner is deployed in a single region with instances distributed across multiple zones for high availability.
66

77
**Note**: The region is configured via the Google provider. In this example, `us-central1` is used, but you can change it to any GCP region.
88

@@ -35,6 +35,14 @@ To deploy a Datadog agentless scanner:
3535
terraform init
3636
```
3737

38+
1. **Review the deployment plan**:
39+
```sh
40+
terraform plan \
41+
-var="project_id=my-gcp-project" \
42+
-var="datadog_api_key=$DD_API_KEY" \
43+
-var="datadog_site=datadoghq.com"
44+
```
45+
3846
1. **Deploy the scanner**. You will need to:
3947
- Set your GCP project ID
4048
- Set your Datadog [API key](https://docs.datadoghq.com/account_management/api-app-keys/)
@@ -60,7 +68,7 @@ To deploy a Datadog agentless scanner:
6068

6169
You have two options for providing the Datadog API key:
6270

63-
1. **Pass the API key directly** (shown in examples below):
71+
1. **Pass the API key directly** (shown in examples above):
6472
```bash
6573
-var="datadog_api_key=$DD_API_KEY"
6674
```

gcp/examples/single_region/main.tf

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ provider "google" {
1717
module "datadog_agentless_scanner" {
1818
source = "git::https://github.com/DataDog/terraform-module-datadog-agentless-scanner//gcp?ref=7993939f19df2a39c981cbffbcd48a91c9fba214"
1919

20-
site = var.datadog_site
21-
api_key = var.datadog_api_key
22-
vpc_name = "datadog-agentless-scanner"
23-
unique_suffix = ""
20+
site = var.datadog_site
21+
api_key = var.datadog_api_key
22+
vpc_name = "datadog-agentless-scanner"
2423
}
25-

gcp/modules/agentless-impersonated-service-account/main.tf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ data "google_client_config" "current" {}
33
# Random ID for unique resource naming when unique_suffix is empty
44
resource "random_id" "deployment_suffix" {
55
byte_length = 4
6-
count = var.unique_suffix == "" ? 1 : 0
76
}
87

98
locals {
109
project_id = data.google_client_config.current.project
1110
# Use provided unique_suffix or generate random one
12-
effective_suffix = var.unique_suffix != "" ? var.unique_suffix : random_id.deployment_suffix[0].hex
11+
effective_suffix = var.unique_suffix != "" ? var.unique_suffix : random_id.deployment_suffix.hex
1312
}
1413

1514
# Custom role for creating snapshots

gcp/modules/agentless-scanner-service-account/main.tf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ data "google_client_config" "current" {}
33
# Random ID for unique resource naming when unique_suffix is empty
44
resource "random_id" "deployment_suffix" {
55
byte_length = 4
6-
count = var.unique_suffix == "" ? 1 : 0
76
}
87

98
locals {
109
project_id = data.google_client_config.current.project
1110
# Use provided unique_suffix or generate random one
12-
effective_suffix = var.unique_suffix != "" ? var.unique_suffix : random_id.deployment_suffix[0].hex
11+
effective_suffix = var.unique_suffix != "" ? var.unique_suffix : random_id.deployment_suffix.hex
1312
# Extract secret name from full path (projects/PROJECT_ID/secrets/SECRET_NAME -> SECRET_NAME)
1413
secret_name = regex("^projects/[a-zA-Z0-9-]+/secrets/([a-zA-Z0-9-]+)$", var.api_key_secret_id)[0]
1514
}

gcp/modules/instance/main.tf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ data "google_client_config" "current" {}
55
# Random ID for unique resource naming when unique_suffix is empty
66
resource "random_id" "deployment_suffix" {
77
byte_length = 4
8-
count = var.unique_suffix == "" ? 1 : 0
98
}
109

1110
locals {
1211
project_id = data.google_client_config.current.project
1312
region = data.google_client_config.current.region
1413
api_key_secret_id = var.api_key_secret_id != null ? var.api_key_secret_id : google_secret_manager_secret.api_key_secret[0].id
1514
# Use provided unique_suffix or generate random one
16-
effective_suffix = var.unique_suffix != "" ? var.unique_suffix : random_id.deployment_suffix[0].hex
15+
effective_suffix = var.unique_suffix != "" ? var.unique_suffix : random_id.deployment_suffix.hex
1716
# Validation for api_key XOR api_key_secret_id
1817
api_key_validation = (var.api_key != null && var.api_key_secret_id == null) || (var.api_key == null && var.api_key_secret_id != null)
1918
# Validation to ensure both SSH variables are provided or neither

gcp/modules/vpc/main.tf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ data "google_client_config" "current" {}
33
# Random ID for unique resource naming when unique_suffix is empty
44
resource "random_id" "deployment_suffix" {
55
byte_length = 4
6-
count = var.unique_suffix == "" ? 1 : 0
76
}
87

98
locals {
109
region = data.google_client_config.current.region
1110
# Use provided unique_suffix or generate random one
12-
effective_suffix = var.unique_suffix != "" ? var.unique_suffix : random_id.deployment_suffix[0].hex
11+
effective_suffix = var.unique_suffix != "" ? var.unique_suffix : random_id.deployment_suffix.hex
1312
vpc_name = "${var.name}-${local.effective_suffix}"
1413
}
1514

0 commit comments

Comments
 (0)