Skip to content

Commit 0a03623

Browse files
authored
feat!: Create project destination and remove log bucket destination for the centralized logging (#1148)
1 parent dd6c09c commit 0a03623

File tree

9 files changed

+208
-102
lines changed

9 files changed

+208
-102
lines changed

1-org/envs/shared/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
| domains\_to\_allow | The list of domains to allow users from in IAM. |
4040
| interconnect\_project\_id | The Dedicated Interconnect project ID |
4141
| interconnect\_project\_number | The Dedicated Interconnect project number |
42-
| logs\_export\_logbucket\_linked\_dataset\_name | The resource name of the Log Bucket linked BigQuery dataset created for Log Analytics. See https://cloud.google.com/logging/docs/log-analytics . |
43-
| logs\_export\_logbucket\_name | The log bucket for destination of log exports. See https://cloud.google.com/logging/docs/routing/overview#buckets . |
42+
| logs\_export\_project\_linked\_dataset\_name | The resource name of the Log Bucket linked BigQuery dataset for the project destination. |
43+
| logs\_export\_project\_logbucket\_name | The resource name for the Log Bucket created for the project destination. |
4444
| logs\_export\_pubsub\_topic | The Pub/Sub topic for destination of log exports |
4545
| logs\_export\_storage\_bucket\_name | The storage bucket for destination of log exports |
4646
| network\_folder\_name | The network folder name. |

1-org/envs/shared/log_sinks.tf

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,16 @@ module "logs_export" {
7272
}
7373

7474
/******************************************
75-
Send logs to Logbucket
75+
Send logs to Logging project
7676
*****************************************/
77-
logbucket_options = {
78-
logging_sink_name = "sk-c-logging-logbkt"
77+
project_options = {
78+
logging_sink_name = "sk-c-logging-prj"
7979
logging_sink_filter = local.logs_filter
80-
name = "logbkt-org-logs-${random_string.suffix.result}"
80+
log_bucket_id = "AggregatedLogs"
81+
log_bucket_description = "Project destination log bucket for aggregated logs"
8182
location = local.default_region
82-
enable_analytics = true
83-
linked_dataset_id = "ds_c_logbkt_analytics"
84-
linked_dataset_description = "BigQuery Dataset for Logbucket analytics"
83+
linked_dataset_id = "ds_c_prj_aggregated_logs_analytics"
84+
linked_dataset_description = "Project destination BigQuery Dataset for Logbucket analytics"
8585
}
8686
}
8787

1-org/envs/shared/outputs.tf

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,21 +114,21 @@ output "logs_export_storage_bucket_name" {
114114
description = "The storage bucket for destination of log exports"
115115
}
116116

117-
output "logs_export_logbucket_name" {
118-
value = module.logs_export.logbucket_destination_name
119-
description = "The log bucket for destination of log exports. See https://cloud.google.com/logging/docs/routing/overview#buckets ."
117+
output "logs_export_project_logbucket_name" {
118+
description = "The resource name for the Log Bucket created for the project destination."
119+
value = module.logs_export.project_logbucket_name
120+
}
121+
122+
output "logs_export_project_linked_dataset_name" {
123+
description = "The resource name of the Log Bucket linked BigQuery dataset for the project destination."
124+
value = module.logs_export.project_linked_dataset_name
120125
}
121126

122127
output "billing_sink_names" {
123128
value = module.logs_export.billing_sink_names
124129
description = "The name of the sinks under billing account level."
125130
}
126131

127-
output "logs_export_logbucket_linked_dataset_name" {
128-
value = module.logs_export.logbucket_linked_dataset_name
129-
description = "The resource name of the Log Bucket linked BigQuery dataset created for Log Analytics. See https://cloud.google.com/logging/docs/log-analytics ."
130-
}
131-
132132
output "tags" {
133133
value = local.tags_output
134134
description = "Tag Values to be applied on next steps."

1-org/modules/centralized-logging/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ module "logging_logbucket" {
6161
|------|-------------|------|---------|:--------:|
6262
| billing\_account | Billing Account ID used in case sinks are under billing account level. Format 000000-000000-000000. | `string` | `null` | no |
6363
| enable\_billing\_account\_sink | If true, a log router sink will be created for the billing account. The billing\_account variable cannot be null. | `bool` | `false` | no |
64-
| logbucket\_options | Destination LogBucket options:<br>- name: The name of the log bucket to be created and used for log entries matching the filter.<br>- logging\_sink\_name: The name of the log sink to be created.<br>- logging\_sink\_filter: The filter to apply when exporting logs. Only log entries that match the filter are exported. Default is "" which exports all logs.<br>- location: The location of the log bucket. Default: global.<br>- enable\_analytics: Whether or not Log Analytics is enabled. A Log bucket with Log Analytics enabled can be queried in the Log Analytics page using SQL queries. Cannot be disabled once enabled.<br>- linked\_dataset\_id: The ID of the linked BigQuery dataset. A valid link dataset ID must only have alphanumeric characters and underscores within it and have up to 100 characters.<br>- linked\_dataset\_description: A use-friendly description of the linked BigQuery dataset. The maximum length of the description is 8000 characters.<br>- retention\_days: The number of days data should be retained for the log bucket. Default 30. | <pre>object({<br> name = optional(string, null)<br> logging_sink_name = optional(string, null)<br> logging_sink_filter = optional(string, "")<br> location = optional(string, "global")<br> enable_analytics = optional(bool, true)<br> linked_dataset_id = optional(string, null)<br> linked_dataset_description = optional(string, null)<br> retention_days = optional(number, 30)<br> })</pre> | `null` | no |
6564
| logging\_destination\_project\_id | The ID of the project that will have the resources where the logs will be created. | `string` | n/a | yes |
6665
| logging\_project\_key | (Optional) The key of logging destination project if it is inside resources map. It is mandatory when resource\_type = project and logging\_target\_type = logbucket. | `string` | `""` | no |
66+
| project\_options | Destination Project options:<br>- logging\_sink\_name: The name of the log sink to be created.<br>- logging\_sink\_filter: The filter to apply when exporting logs. Only log entries that match the filter are exported. Default is "" which exports all logs.<br>- log\_bucket\_id: Id of the log bucket create to store the logs exported to the project.<br>- log\_bucket\_description: Description of the log bucket create to store the logs exported to the project.<br>- location: The location of the log bucket. Default: global.<br>- enable\_analytics: Whether or not Log Analytics is enabled in the \_Default log bucket. A Log bucket with Log Analytics enabled can be queried in the Log Analytics page using SQL queries. Cannot be disabled once enabled.<br>- retention\_days: The number of days data should be retained for the \_Default log bucket. Default 30.<br>- linked\_dataset\_id: The ID of the linked BigQuery dataset for the \_Default log bucket. A valid link dataset ID must only have alphanumeric characters and underscores within it and have up to 100 characters.<br>- linked\_dataset\_description: A use-friendly description of the linked BigQuery dataset for the \_Default log bucket. The maximum length of the description is 8000 characters. | <pre>object({<br> logging_sink_name = optional(string, null)<br> logging_sink_filter = optional(string, "")<br> log_bucket_id = optional(string, null)<br> log_bucket_description = optional(string, null)<br> location = optional(string, "global")<br> enable_analytics = optional(bool, true)<br> retention_days = optional(number, 30)<br> linked_dataset_id = optional(string, null)<br> linked_dataset_description = optional(string, null)<br> })</pre> | `null` | no |
6767
| pubsub\_options | Destination Pubsub options:<br>- topic\_name: The name of the pubsub topic to be created and used for log entries matching the filter.<br>- logging\_sink\_name: The name of the log sink to be created.<br>- logging\_sink\_filter: The filter to apply when exporting logs. Only log entries that match the filter are exported. Default is "" which exports all logs.<br>- create\_subscriber: Whether to create a subscription to the topic that was created and used for log entries matching the filter. If 'true', a pull subscription is created along with a service account that is granted roles/pubsub.subscriber and roles/pubsub.viewer to the topic. | <pre>object({<br> topic_name = optional(string, null)<br> logging_sink_name = optional(string, null)<br> logging_sink_filter = optional(string, "")<br> create_subscriber = optional(bool, true)<br> })</pre> | `null` | no |
6868
| resource\_type | Resource type of the resource that will export logs to destination. Must be: project, organization, or folder. | `string` | n/a | yes |
6969
| resources | Export logs from the specified resources. | `map(string)` | n/a | yes |
@@ -74,8 +74,8 @@ module "logging_logbucket" {
7474
| Name | Description |
7575
|------|-------------|
7676
| billing\_sink\_names | Map of log sink names with billing suffix |
77-
| logbucket\_destination\_name | The resource name for the destination Log Bucket. |
78-
| logbucket\_linked\_dataset\_name | The resource name of the Log Bucket linked BigQuery dataset. |
77+
| project\_linked\_dataset\_name | The resource name of the Log Bucket linked BigQuery dataset for the project destination. |
78+
| project\_logbucket\_name | The resource name for the Log Bucket created for the project destination. |
7979
| pubsub\_destination\_name | The resource name for the destination Pub/Sub. |
8080
| storage\_destination\_name | The resource name for the destination Storage. |
8181

1-org/modules/centralized-logging/main.tf

Lines changed: 106 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
*/
1616

1717
locals {
18-
value_first_resource = values(var.resources)[0]
19-
logbucket_sink_member = { for k, v in var.resources : k => v if k != var.logging_project_key }
20-
include_children = (var.resource_type == "organization" || var.resource_type == "folder")
18+
value_first_resource = values(var.resources)[0]
19+
include_children = (var.resource_type == "organization" || var.resource_type == "folder")
2120

2221
# Create an intermediate list with all resources X all destinations
2322
exports_list = flatten([
@@ -38,40 +37,41 @@ locals {
3837
log_exports = {
3938
for v in local.exports_list : "${v.res}_${v.type}" => v
4039
}
40+
4141
destinations_options = {
4242
pub = var.pubsub_options
4343
sto = var.storage_options
44-
lbk = var.logbucket_options
44+
prj = var.project_options
4545
}
4646

4747
logging_sink_name_map = {
4848
pub = try("sk-to-tp-logs-${var.logging_destination_project_id}", "sk-to-tp-logs")
4949
sto = try("sk-to-bkt-logs-${var.logging_destination_project_id}", "sk-to-bkt-logs")
50-
lbk = try("sk-to-logbkt-logs-${var.logging_destination_project_id}", "sk-to-logbkt-logs")
50+
prj = try("sk-to-prj-logs-${var.logging_destination_project_id}", "sk-to-prj-logs")
5151
}
5252

5353
logging_tgt_name = {
5454
pub = "${local.logging_tgt_prefix.pub}${random_string.suffix.result}"
5555
sto = "${local.logging_tgt_prefix.sto}${random_string.suffix.result}"
56-
lbk = "${local.logging_tgt_prefix.lbk}${random_string.suffix.result}"
56+
prj = ""
5757
}
5858

5959
destination_uri_map = {
6060
pub = try(module.destination_pubsub[0].destination_uri, "")
6161
sto = try(module.destination_storage[0].destination_uri, "")
62-
lbk = try(module.destination_logbucket[0].destination_uri, "")
62+
prj = try(module.destination_project[0].destination_uri, "")
6363
}
6464

6565
destination_resource_name = merge(
6666
var.pubsub_options != null ? { pub = module.destination_pubsub[0].resource_name } : {},
6767
var.storage_options != null ? { sto = module.destination_storage[0].resource_name } : {},
68-
var.logbucket_options != null ? { lbk = module.destination_logbucket[0].resource_name } : {}
68+
var.project_options != null ? { prj = module.destination_project[0].project } : {}
6969
)
7070

7171
logging_tgt_prefix = {
7272
pub = "tp-logs-"
7373
sto = try("bkt-logs-${var.logging_destination_project_id}-", "bkt-logs-")
74-
lbk = "logbkt-logs-"
74+
prj = ""
7575
}
7676
}
7777

@@ -83,7 +83,7 @@ resource "random_string" "suffix" {
8383

8484
module "log_export" {
8585
source = "terraform-google-modules/log-export/google"
86-
version = "~> 7.4"
86+
version = "~> 7.8"
8787

8888
for_each = local.log_exports
8989

@@ -96,7 +96,6 @@ module "log_export" {
9696
include_children = local.include_children
9797
}
9898

99-
10099
module "log_export_billing" {
101100
source = "terraform-google-modules/log-export/google"
102101
version = "~> 7.4"
@@ -118,52 +117,115 @@ resource "time_sleep" "wait_sa_iam_membership" {
118117
]
119118
}
120119

121-
#-------------------------#
122-
# Send logs to Log Bucket #
123-
#-------------------------#
124-
module "destination_logbucket" {
125-
source = "terraform-google-modules/log-export/google//modules/logbucket"
126-
version = "~> 7.7"
120+
#--------------------------#
121+
# Send logs to Log project #
122+
#--------------------------#
127123

128-
count = var.logbucket_options != null ? 1 : 0
124+
module "destination_project" {
125+
source = "terraform-google-modules/log-export/google//modules/project"
126+
version = "~> 7.8"
127+
count = var.project_options != null ? 1 : 0
129128

130-
project_id = var.logging_destination_project_id
131-
name = coalesce(var.logbucket_options.name, local.logging_tgt_name.lbk)
132-
log_sink_writer_identity = module.log_export["${local.value_first_resource}_lbk"].writer_identity
133-
location = var.logbucket_options.location
134-
enable_analytics = var.logbucket_options.enable_analytics
135-
linked_dataset_id = var.logbucket_options.linked_dataset_id
136-
linked_dataset_description = var.logbucket_options.linked_dataset_description
137-
retention_days = var.logbucket_options.retention_days
138-
grant_write_permission_on_bkt = false
129+
project_id = var.logging_destination_project_id
130+
log_sink_writer_identity = module.log_export["${local.value_first_resource}_prj"].writer_identity
139131
}
140132

141-
#-------------------------------------------#
142-
# Log Bucket Service account IAM membership #
143-
#-------------------------------------------#
144-
resource "google_project_iam_member" "logbucket_sink_member" {
145-
for_each = var.logbucket_options != null ? local.logbucket_sink_member : {}
133+
#---------------------------------------------#
134+
# Log Projects Service account IAM membership #
135+
#---------------------------------------------#
136+
137+
resource "google_project_iam_member" "project_sink_member" {
138+
for_each = var.project_options != null ? var.resources : {}
146139

147140
project = var.logging_destination_project_id
148-
role = "roles/logging.bucketWriter"
141+
role = "roles/logging.logWriter"
149142

150143
# Set permission only on sinks for this destination using
151144
# module.log_export key "<resource>_<dest>"
152-
member = module.log_export["${each.value}_lbk"].writer_identity
145+
member = module.log_export["${each.value}_prj"].writer_identity
146+
}
147+
148+
#----------------------------------------------#
149+
# Send logs to Log project - Internal Log sink #
150+
#----------------------------------------------#
151+
152+
module "internal_project_log_export" {
153+
source = "terraform-google-modules/log-export/google"
154+
version = "~> 7.8"
155+
count = var.project_options != null ? 1 : 0
156+
157+
destination_uri = "logging.googleapis.com/projects/${var.logging_destination_project_id}/locations/${var.project_options.location}/buckets/${coalesce(var.project_options.log_bucket_id, "AggregatedLogs")}"
158+
filter = var.project_options.logging_sink_filter
159+
log_sink_name = "${coalesce(var.project_options.logging_sink_name, local.logging_sink_name_map["prj"])}-la"
160+
parent_resource_id = var.logging_destination_project_id
161+
parent_resource_type = "project"
162+
unique_writer_identity = true
163+
}
164+
165+
module "destination_aggregated_logs" {
166+
source = "terraform-google-modules/log-export/google//modules/logbucket"
167+
version = "~> 7.8"
168+
count = var.project_options != null ? 1 : 0
169+
170+
project_id = var.logging_destination_project_id
171+
name = coalesce(var.project_options.log_bucket_id, "AggregatedLogs")
172+
log_sink_writer_identity = module.internal_project_log_export[0].writer_identity
173+
location = var.project_options.location
174+
enable_analytics = var.project_options.enable_analytics
175+
linked_dataset_id = var.project_options.linked_dataset_id
176+
linked_dataset_description = var.project_options.linked_dataset_description
177+
retention_days = var.project_options.retention_days
178+
grant_write_permission_on_bkt = false
179+
}
180+
181+
#-------------------------------------------------#
182+
# Send logs to Log project - update _Default sink #
183+
#-------------------------------------------------#
184+
185+
data "google_client_config" "default" {
153186
}
154187

155-
#------------------------------------------------------------------#
156-
# Log Bucket Service account IAM membership for log_export_billing #
157-
#------------------------------------------------------------------#
158-
resource "google_project_iam_member" "logbucket_sink_member_billing" {
159-
count = var.enable_billing_account_sink == true && var.logbucket_options != null ? 1 : 0
188+
resource "terracurl_request" "exclude_external_logs" {
189+
count = var.project_options != null ? 1 : 0
190+
191+
name = "exclude_external_logs"
192+
url = "https://logging.googleapis.com/v2/projects/${var.logging_destination_project_id}/sinks/_Default?updateMask=exclusions"
193+
method = "PUT"
194+
response_codes = [200]
195+
headers = {
196+
Authorization = "Bearer ${data.google_client_config.default.access_token}"
197+
Content-Type = "application/json",
198+
}
199+
request_body = <<EOF
200+
{
201+
"exclusions": [
202+
{
203+
"name": "exclude_external_logs",
204+
"filter": "-logName : \"/${var.logging_destination_project_id}/\""
205+
}
206+
],
207+
}
208+
EOF
209+
210+
lifecycle {
211+
ignore_changes = [
212+
headers,
213+
]
214+
}
215+
}
216+
217+
#---------------------------------------------------------------#
218+
# Project Service account IAM membership for log_export_billing #
219+
#---------------------------------------------------------------#
220+
resource "google_project_iam_member" "project_sink_member_billing" {
221+
count = var.enable_billing_account_sink == true && var.project_options != null ? 1 : 0
160222

161223
project = var.logging_destination_project_id
162-
role = "roles/logging.bucketWriter"
224+
role = "roles/logging.logWriter"
163225

164226
# Set permission only on sinks for this destination using
165227
# module.log_export_billing key "<resource>_<dest>"
166-
member = module.log_export_billing["lbk"].writer_identity
228+
member = module.log_export_billing["prj"].writer_identity
167229

168230

169231
depends_on = [
@@ -176,7 +238,7 @@ resource "google_project_iam_member" "logbucket_sink_member_billing" {
176238
#----------------------#
177239
module "destination_storage" {
178240
source = "terraform-google-modules/log-export/google//modules/storage"
179-
version = "~> 7.4"
241+
version = "~> 7.8"
180242

181243
count = var.storage_options != null ? 1 : 0
182244

@@ -217,7 +279,7 @@ resource "google_storage_bucket_iam_member" "storage_sink_member_billing" {
217279

218280

219281
depends_on = [
220-
google_project_iam_member.logbucket_sink_member_billing
282+
google_project_iam_member.project_sink_member_billing
221283
]
222284
}
223285

@@ -227,7 +289,7 @@ resource "google_storage_bucket_iam_member" "storage_sink_member_billing" {
227289
#----------------------#
228290
module "destination_pubsub" {
229291
source = "terraform-google-modules/log-export/google//modules/pubsub"
230-
version = "~> 7.4"
292+
version = "~> 7.8"
231293

232294
count = var.pubsub_options != null ? 1 : 0
233295

0 commit comments

Comments
 (0)