Skip to content

Commit 9fa8fdc

Browse files
Add support for a log bucket (#104)
* feat: add support for a log bucket * Auto Format * chore: add support for log bucket var config * Auto Format * chore: tighten up the bucket name integration * Auto Format * chore: fix incorrect local * Auto Format * chore: consolidate locals Co-authored-by: cloudpossebot <[email protected]>
1 parent d7a1067 commit 9fa8fdc

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ Available targets:
264264
| Name | Source | Version |
265265
|------|--------|---------|
266266
| <a name="module_dynamodb_table_label"></a> [dynamodb\_table\_label](#module\_dynamodb\_table\_label) | cloudposse/label/null | 0.25.0 |
267+
| <a name="module_log_storage"></a> [log\_storage](#module\_log\_storage) | cloudposse/s3-log-storage/aws | 0.26.0 |
267268
| <a name="module_this"></a> [this](#module\_this) | cloudposse/label/null | 0.25.0 |
268269

269270
## Resources
@@ -313,6 +314,10 @@ Available targets:
313314
| <a name="input_label_value_case"></a> [label\_value\_case](#input\_label\_value\_case) | Controls the letter case of ID elements (labels) as included in `id`,<br>set as tag values, and output by this module individually.<br>Does not affect values of tags passed in via the `tags` input.<br>Possible values: `lower`, `title`, `upper` and `none` (no transformation).<br>Set this to `title` and set `delimiter` to `""` to yield Pascal Case IDs.<br>Default value: `lower`. | `string` | `null` | no |
314315
| <a name="input_labels_as_tags"></a> [labels\_as\_tags](#input\_labels\_as\_tags) | Set of labels (ID elements) to include as tags in the `tags` output.<br>Default is to include all labels.<br>Tags with empty values will not be included in the `tags` output.<br>Set to `[]` to suppress all generated tags.<br>**Notes:**<br> The value of the `name` tag, if included, will be the `id`, not the `name`.<br> Unlike other `null-label` inputs, the initial setting of `labels_as_tags` cannot be<br> changed in later chained modules. Attempts to change it will be silently ignored. | `set(string)` | <pre>[<br> "default"<br>]</pre> | no |
315316
| <a name="input_logging"></a> [logging](#input\_logging) | Bucket access logging configuration. | <pre>object({<br> bucket_name = string<br> prefix = string<br> })</pre> | `null` | no |
317+
| <a name="input_logging_bucket_enabled"></a> [logging\_bucket\_enabled](#input\_logging\_bucket\_enabled) | Whether to create the s3 access log bucket. | `bool` | `false` | no |
318+
| <a name="input_logging_bucket_expiration_days"></a> [logging\_bucket\_expiration\_days](#input\_logging\_bucket\_expiration\_days) | Whether to create the s3 access log bucket. | `number` | `90` | no |
319+
| <a name="input_logging_bucket_glacier_transition_days"></a> [logging\_bucket\_glacier\_transition\_days](#input\_logging\_bucket\_glacier\_transition\_days) | Whether to create the s3 access log bucket. | `number` | `60` | no |
320+
| <a name="input_logging_bucket_standard_transition_days"></a> [logging\_bucket\_standard\_transition\_days](#input\_logging\_bucket\_standard\_transition\_days) | Whether to create the s3 access log bucket. | `number` | `30` | no |
316321
| <a name="input_mfa_delete"></a> [mfa\_delete](#input\_mfa\_delete) | A boolean that indicates that versions of S3 objects can only be deleted with MFA. ( Terraform cannot apply changes of this value; https://github.com/terraform-providers/terraform-provider-aws/issues/629 ) | `bool` | `false` | no |
317322
| <a name="input_name"></a> [name](#input\_name) | ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.<br>This is the only ID element not also included as a `tag`.<br>The "name" tag is set to the full `id` string. There is no tag with the value of the `name` input. | `string` | `null` | no |
318323
| <a name="input_namespace"></a> [namespace](#input\_namespace) | ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique | `string` | `null` | no |

docs/terraform.md

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
| Name | Source | Version |
2020
|------|--------|---------|
2121
| <a name="module_dynamodb_table_label"></a> [dynamodb\_table\_label](#module\_dynamodb\_table\_label) | cloudposse/label/null | 0.25.0 |
22+
| <a name="module_log_storage"></a> [log\_storage](#module\_log\_storage) | cloudposse/s3-log-storage/aws | 0.26.0 |
2223
| <a name="module_this"></a> [this](#module\_this) | cloudposse/label/null | 0.25.0 |
2324

2425
## Resources
@@ -68,6 +69,10 @@
6869
| <a name="input_label_value_case"></a> [label\_value\_case](#input\_label\_value\_case) | Controls the letter case of ID elements (labels) as included in `id`,<br>set as tag values, and output by this module individually.<br>Does not affect values of tags passed in via the `tags` input.<br>Possible values: `lower`, `title`, `upper` and `none` (no transformation).<br>Set this to `title` and set `delimiter` to `""` to yield Pascal Case IDs.<br>Default value: `lower`. | `string` | `null` | no |
6970
| <a name="input_labels_as_tags"></a> [labels\_as\_tags](#input\_labels\_as\_tags) | Set of labels (ID elements) to include as tags in the `tags` output.<br>Default is to include all labels.<br>Tags with empty values will not be included in the `tags` output.<br>Set to `[]` to suppress all generated tags.<br>**Notes:**<br> The value of the `name` tag, if included, will be the `id`, not the `name`.<br> Unlike other `null-label` inputs, the initial setting of `labels_as_tags` cannot be<br> changed in later chained modules. Attempts to change it will be silently ignored. | `set(string)` | <pre>[<br> "default"<br>]</pre> | no |
7071
| <a name="input_logging"></a> [logging](#input\_logging) | Bucket access logging configuration. | <pre>object({<br> bucket_name = string<br> prefix = string<br> })</pre> | `null` | no |
72+
| <a name="input_logging_bucket_enabled"></a> [logging\_bucket\_enabled](#input\_logging\_bucket\_enabled) | Whether to create the s3 access log bucket. | `bool` | `false` | no |
73+
| <a name="input_logging_bucket_expiration_days"></a> [logging\_bucket\_expiration\_days](#input\_logging\_bucket\_expiration\_days) | Whether to create the s3 access log bucket. | `number` | `90` | no |
74+
| <a name="input_logging_bucket_glacier_transition_days"></a> [logging\_bucket\_glacier\_transition\_days](#input\_logging\_bucket\_glacier\_transition\_days) | Whether to create the s3 access log bucket. | `number` | `60` | no |
75+
| <a name="input_logging_bucket_standard_transition_days"></a> [logging\_bucket\_standard\_transition\_days](#input\_logging\_bucket\_standard\_transition\_days) | Whether to create the s3 access log bucket. | `number` | `30` | no |
7176
| <a name="input_mfa_delete"></a> [mfa\_delete](#input\_mfa\_delete) | A boolean that indicates that versions of S3 objects can only be deleted with MFA. ( Terraform cannot apply changes of this value; https://github.com/terraform-providers/terraform-provider-aws/issues/629 ) | `bool` | `false` | no |
7277
| <a name="input_name"></a> [name](#input\_name) | ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.<br>This is the only ID element not also included as a `tag`.<br>The "name" tag is set to the full `id` string. There is no tag with the value of the `name` input. | `string` | `null` | no |
7378
| <a name="input_namespace"></a> [namespace](#input\_namespace) | ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique | `string` | `null` | no |

main.tf

+23-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ locals {
4545
})
4646

4747
bucket_name = var.s3_bucket_name != "" ? var.s3_bucket_name : module.this.id
48+
49+
logging_bucket_enabled = local.bucket_enabled && var.logging_bucket_enabled
50+
logging_bucket_name_default = try(var.logging["bucket_name"], "${local.bucket_name}-logs")
51+
logging_prefix_default = try(var.logging["prefix"], "logs/")
52+
logging_bucket_name = local.logging_bucket_enabled ? module.log_storage.bucket_id : local.logging_bucket_name_default
53+
logging_prefix = local.logging_bucket_enabled ? module.log_storage.prefix : local.logging_prefix_default
4854
}
4955

5056
data "aws_iam_policy_document" "prevent_unencrypted_uploads" {
@@ -132,6 +138,21 @@ data "aws_iam_policy_document" "prevent_unencrypted_uploads" {
132138
}
133139
}
134140

141+
module "log_storage" {
142+
source = "cloudposse/s3-log-storage/aws"
143+
version = "0.26.0"
144+
145+
enabled = local.logging_bucket_enabled
146+
access_log_bucket_prefix = local.logging_prefix_default
147+
acl = "log-delivery-write"
148+
expiration_days = var.logging_bucket_expiration_days
149+
glacier_transition_days = var.logging_bucket_glacier_transition_days
150+
name = local.logging_bucket_name_default
151+
standard_transition_days = var.logging_bucket_standard_transition_days
152+
153+
context = module.this.context
154+
}
155+
135156
resource "aws_s3_bucket" "default" {
136157
count = local.bucket_enabled ? 1 : 0
137158

@@ -176,8 +197,8 @@ resource "aws_s3_bucket" "default" {
176197
dynamic "logging" {
177198
for_each = var.logging == null ? [] : [1]
178199
content {
179-
target_bucket = var.logging["bucket_name"]
180-
target_prefix = var.logging["prefix"]
200+
target_bucket = local.logging_bucket_name
201+
target_prefix = local.logging_prefix
181202
}
182203
}
183204

variables.tf

+24
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,30 @@ variable "logging" {
153153
description = "Bucket access logging configuration."
154154
}
155155

156+
variable "logging_bucket_enabled" {
157+
type = bool
158+
default = false
159+
description = "Whether to create the s3 access log bucket."
160+
}
161+
162+
variable "logging_bucket_standard_transition_days" {
163+
type = number
164+
default = 30
165+
description = "Whether to create the s3 access log bucket."
166+
}
167+
168+
variable "logging_bucket_glacier_transition_days" {
169+
type = number
170+
default = 60
171+
description = "Whether to create the s3 access log bucket."
172+
}
173+
174+
variable "logging_bucket_expiration_days" {
175+
type = number
176+
default = 90
177+
description = "Whether to create the s3 access log bucket."
178+
}
179+
156180
variable "bucket_enabled" {
157181
type = bool
158182
default = true

0 commit comments

Comments
 (0)