Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BQ へのログ保存を行う #4

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Local .terraform directories
**/.terraform/*

# .tfstate files
**/*.tfstate
**/*.tfstate.*

# Crash log files
**/crash.log
**/crash.*.log

# Exclude all .tfvars files, which are likely to contain sensitive data, such as
# password, private keys, and other secrets. These should not be part of version
# control as they are data points which are potentially sensitive and subject
# to change depending on the environment.
**/*.tfvars
**/*.tfvars.json

# Ignore override files as they are usually used to override resources locally and so
# are not checked in
**/override.tf
**/override.tf.json
**/*_override.tf
**/*_override.tf.json

# Ignore transient lock info files created by terraform apply
**/.terraform.tfstate.lock.info

# Include override files you do wish to add to version control using negated pattern
# !example_override.tf

# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*

# Ignore CLI configuration files
**/.terraformrc
**/terraform.rc
14 changes: 14 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ repos:
"./modules/gcs2spanner/",
]

- repo: https://github.com/terraform-docs/terraform-docs
rev: "v0.18.0"
hooks:
- id: terraform-docs-go
name: terraform-docs-log2bq
args:
[
"markdown",
"table",
"--output-file",
"./README.md",
"./modules/log2bq/",
]

- repo: https://github.com/terraform-docs/terraform-docs
rev: "v0.18.0"
hooks:
Expand Down
2 changes: 1 addition & 1 deletion environments/dev/main.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
locals {
project_id = "project=id"
project_id = "project_id"
location = "location"
}
2 changes: 1 addition & 1 deletion environments/prod/main.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
locals {
project_id = "project=id"
project_id = "project_id"
location = "location"
}
2 changes: 1 addition & 1 deletion environments/stg/main.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
locals {
project_id = "project=id"
project_id = "project_id"
location = "location"
}
44 changes: 44 additions & 0 deletions modules/log2bq/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!-- BEGIN_TF_DOCS -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >=1.7 |
| <a name="requirement_archive"></a> [archive](#requirement\_archive) | >=2.4.0 |
| <a name="requirement_google"></a> [google](#requirement\_google) | >= 5.22.0 |
| <a name="requirement_google-beta"></a> [google-beta](#requirement\_google-beta) | >= 5.22.0 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_google"></a> [google](#provider\_google) | >= 5.22.0 |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [google_bigquery_dataset.main](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/bigquery_dataset) | resource |
| [google_bigquery_table.main](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/bigquery_table) | resource |
| [google_logging_project_sink.main](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/logging_project_sink) | resource |
| [google_project_iam_member.main](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/project_iam_member) | resource |
| [google_project_service.main](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/project_service) | resource |
| [google_project.main](https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/project) | data source |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_bigquery"></a> [bigquery](#input\_bigquery) | The bigquery settings | <pre>object({<br> dataset = string<br> table = string<br> view = string<br> expiration_days = number<br> })</pre> | n/a | yes |
| <a name="input_logging"></a> [logging](#input\_logging) | n/a | <pre>object({<br> target = string<br> filter = string<br> })</pre> | n/a | yes |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_enabled_apis"></a> [enabled\_apis](#output\_enabled\_apis) | Already enabled APIs list. |
<!-- END_TF_DOCS -->
23 changes: 23 additions & 0 deletions modules/log2bq/bigquery.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
resource "google_bigquery_dataset" "main" {
dataset_id = "cloud_logging_sinked_${var.logging.target}"
description = "Cloud Logging sinked dataset"
location = "US"
default_partition_expiration_ms = 60 * 1000 * 24 * var.bigquery.expiration_days
storage_billing_model = "PHYSICAL"
}

resource "google_bigquery_table" "main" {
dataset_id = google_bigquery_dataset.main.dataset_id
table_id = var.bigquery.view

view {
query = <<EOF
SELECT *
FROM `${google_bigquery_dataset.main.dataset_id}.${var.bigquery.table}`
WEHRE ${var.logging.target}
EOF
use_legacy_sql = false
}

require_partition_filter = false
}
17 changes: 17 additions & 0 deletions modules/log2bq/logging.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
resource "google_logging_project_sink" "main" {
name = "${var.logging.target}-to-bigquery-sink"
destination = "bigquery.googleapi.com/${google_bigquery_dataset.main.id}"
filter = var.logging.filter

bigquery_options {
use_partitioned_tables = true
}

unique_writer_identity = true
}

resource "google_project_iam_member" "main" {
project = data.google_project.main.project_id
role = "roles/bigquery.dataEditor"
member = google_logging_project_sink.main.writer_identity
}
16 changes: 16 additions & 0 deletions modules/log2bq/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
locals {
apis = toset([
"bigquery.googleapis.com",
"logging.googleapis.com",
])
}

data "google_project" "main" {}

resource "google_project_service" "main" {
for_each = local.apis

project = data.google_project.main.project_id
service = each.value
disable_on_destroy = false
}
4 changes: 4 additions & 0 deletions modules/log2bq/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "enabled_apis" {
description = "Already enabled APIs list."
value = [for api in google_project_service.main : api.service]
}
18 changes: 18 additions & 0 deletions modules/log2bq/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
terraform {
required_version = ">=1.7"

required_providers {
archive = {
source = "hashicorp/google"
version = ">=2.4.0"
}
google = {
source = "hashicorp/google"
version = ">= 5.22.0"
}
google-beta = {
source = "hashicorp/google"
version = ">= 5.22.0"
}
}
}
16 changes: 16 additions & 0 deletions modules/log2bq/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
variable "bigquery" {
description = "The bigquery settings"
type = object({
dataset = string
table = string
view = string
expiration_days = number
})
}

variable "logging" {
type = object({
target = string
filter = string
})
}