Skip to content

Commit b3120da

Browse files
authored
Add stackdriver project sink support (hashicorp#432)
* Vendor cloud logging api * Add logging sink support * Remove typo * Set Filter simpler * Rename typ, typName to resourceType, resourceId * Handle notFoundError * Use # instead of // for hcl comments * Cleanup test code * Change testAccCheckLoggingProjectSink to take a provided api object * Fix whitespace change after merge conflict
1 parent 2436bb2 commit b3120da

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed
+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
---
2+
layout: "google"
3+
page_title: "Google: google_logging_project_sink"
4+
sidebar_current: "docs-google-logging-project-sink"
5+
description: |-
6+
Manages a project-level logging sink.
7+
---
8+
9+
# google\_logging\_project\_sink
10+
11+
Manages a project-level logging sink. For more information see
12+
[the official documentation](https://cloud.google.com/logging/docs/),
13+
[Exporting Logs in the API](https://cloud.google.com/logging/docs/api/tasks/exporting-logs)
14+
and
15+
[API](https://cloud.google.com/compute/docs/reference/latest/instances).
16+
17+
Note that you must have the "Logs Configuration Writer" IAM role (`roles/logging.configWriter`)
18+
granted to the credentials used with terraform.
19+
20+
## Example Usage
21+
22+
```hcl
23+
resource "google_logging_project_sink" "my-sink" {
24+
name = "my-pubsub-instance-sink"
25+
26+
# Can export to pubsub, cloud storage, or bigtable
27+
destination = "pubsub.googleapis.com/projects/my-project/topics/instance-activity"
28+
29+
# Log all WARN or higher severity messages relating to instances
30+
filter = "resource.type = gce_instance AND severity >= WARN"
31+
32+
# Use a unique writer (creates a unique service account used for writing)
33+
unique_writer_identity = true
34+
}
35+
```
36+
37+
A more complete example follows: this creates a compute instance, as well as a log sink that logs all activity to a
38+
cloud storage bucket. Because we are using `unique_writer_identity`, we must grant it access to the bucket. Note that
39+
this grant requires the "Project IAM Admin" IAM role (`roles/resourcemanager.projectIamAdmin`) granted to the credentials
40+
used with terraform.
41+
42+
```hcl
43+
# Our logged compute instance
44+
resource "google_compute_instance" "my-logged-instance" {
45+
name = "my-instance"
46+
machine_type = "n1-standard-1"
47+
zone = "us-central1-a"
48+
49+
boot_disk {
50+
initialize_params {
51+
image = "debian-cloud/debian-8"
52+
}
53+
}
54+
55+
network_interface {
56+
network = "default"
57+
58+
access_config {}
59+
}
60+
}
61+
62+
# A bucket to storage logs in.
63+
resource "google_storage_bucket" "log-bucket" {
64+
name = "my-unique-logging-bucket"
65+
}
66+
67+
# Our sink; this logs all activity related to our "my-logged-instance" instance
68+
resource "google_logging_project_sink" "instance-sink" {
69+
name = "my-instance-sink"
70+
destination = "storage.googleapis.com/${google_storage_bucket.log-bucket.name}"
71+
filter = "resource.type = gce_instance AND resource.labels.instance_id = \"${google_compute_instance.my-logged-instance.instance_id}\""
72+
73+
unique_writer_identity = true
74+
}
75+
76+
# Because our sink uses a unique_writer, we must grant that writer access to the bucket.
77+
resource "google_project_iam_binding" "log-writer" {
78+
role = "roles/storage.objectCreator"
79+
80+
members = [
81+
"${google_logging_project_sink.instance-sink.writer_identity}",
82+
]
83+
}
84+
85+
```
86+
87+
## Argument Reference
88+
89+
The following arguments are supported:
90+
91+
* `name` - (Required) The name of the logging sink.
92+
93+
* `destination` - (Required) The destination of the sink (or, in other words, where logs are written to). Can be a
94+
Cloud Storage bucket, a PubSub topic, or a BigQuery dataset. Examples:
95+
```
96+
"storage.googleapis.com/[GCS_BUCKET]"
97+
"bigquery.googleapis.com/projects/[PROJECT_ID]/datasets/[DATASET]"
98+
"pubsub.googleapis.com/projects/[PROJECT_ID]/topics/[TOPIC_ID]"
99+
```
100+
The writer associated with the sink must have access to write to the above resource.
101+
102+
* `filter` - (Optional) The filter to apply when exporting logs. Only log entries that match the filter are exported.
103+
See (Advanced Log Filters)[https://cloud.google.com/logging/docs/view/advanced_filters] for information on how to
104+
write a filter.
105+
106+
* `project` - (Optional) The project to create the sink in. If omitted, the project associated with the provider is
107+
used.
108+
109+
* `unique_writer_identity` - (Optional) Whether or not to create a unique identity associated with this sink. If `false`
110+
(the default), then the `writer_identity` used is `serviceAccount:[email protected]`. If `true`,
111+
then a unique service account is created and used for this sink. If you wish to publish logs across projects, you
112+
must set `unique_writer_identity` to true.
113+
114+
## Attributes Reference
115+
116+
In addition to the arguments listed above, the following computed attributes are
117+
exported:
118+
119+
* `writer_identity` - The identity associated with this sink. This identity must be granted write access to the
120+
configured `destination`.

google.erb

+9
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,15 @@
336336
</ul>
337337
</li>
338338

339+
<li<%= sidebar_current("docs-google-logging") %>>
340+
<a href="#">Google Stackdriver Logging Resources</a>
341+
<ul class="nav nav-visible">
342+
<li<%= sidebar_current("docs-google-logging-project-sink") %>>
343+
<a href="/docs/providers/google/r/logging_project_sink.html">google_logging_project_sink</a>
344+
</li>
345+
</ul>
346+
</li>
347+
339348
<li<%= sidebar_current("docs-google-storage") %>>
340349
<a href="#">Google Storage Resources</a>
341350
<ul class="nav nav-visible">

0 commit comments

Comments
 (0)