Skip to content

Commit 9f42731

Browse files
committed
update to aws v4, ci, lifecycle policies
1 parent ae83ee1 commit 9f42731

File tree

9 files changed

+112
-52
lines changed

9 files changed

+112
-52
lines changed

.github/workflows/check.yml

Lines changed: 0 additions & 20 deletions
This file was deleted.

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
exclude: ".terraform"
22
repos:
33
- repo: https://github.com/antonbabenko/pre-commit-terraform
4-
rev: v1.72.1
4+
rev: v1.77.0
55
hooks:
66
- id: terraform_docs
77
always_run: true
@@ -56,7 +56,7 @@ repos:
5656
files: \.tf(vars)?$
5757
exclude: examples
5858
- repo: https://github.com/pre-commit/pre-commit-hooks
59-
rev: v4.2.0
59+
rev: v4.4.0
6060
hooks:
6161
- id: check-case-conflict
6262
- id: check-json

.terraform-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
latest:^1.1
1+
latest:^1

.terraform.lock.hcl

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module "vpcflowlog-bucket" {
2727
| Name | Version |
2828
|------|---------|
2929
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.4 |
30-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.8 |
30+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4 |
3131

3232
## Providers
3333

@@ -46,8 +46,13 @@ No modules.
4646
| [aws_kms_alias.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_alias) | resource |
4747
| [aws_kms_key.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource |
4848
| [aws_s3_bucket.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource |
49+
| [aws_s3_bucket_acl.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_acl) | resource |
50+
| [aws_s3_bucket_lifecycle_configuration.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_lifecycle_configuration) | resource |
51+
| [aws_s3_bucket_logging.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_logging) | resource |
4952
| [aws_s3_bucket_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_policy) | resource |
5053
| [aws_s3_bucket_public_access_block.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_public_access_block) | resource |
54+
| [aws_s3_bucket_server_side_encryption_configuration.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_server_side_encryption_configuration) | resource |
55+
| [aws_s3_bucket_versioning.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_versioning) | resource |
5156
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
5257
| [aws_iam_policy_document.key](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
5358
| [aws_iam_policy_document.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
@@ -58,6 +63,7 @@ No modules.
5863
| Name | Description | Type | Default | Required |
5964
|------|-------------|------|---------|:--------:|
6065
| <a name="input_allowed_account_ids"></a> [allowed\_account\_ids](#input\_allowed\_account\_ids) | Optional list of AWS Account IDs that are permitted to write to the bucket | `list(string)` | `[]` | no |
66+
| <a name="input_lifecycle_rules"></a> [lifecycle\_rules](#input\_lifecycle\_rules) | lifecycle rules to apply to the bucket | <pre>list(object(<br> {<br> id = string<br> enabled = optional(bool, true)<br> expiration = optional(number)<br> prefix = optional(number)<br> noncurrent_version_expiration = optional(number)<br> transition = optional(list(object({<br> days = number<br> storage_class = string<br> })))<br> }))</pre> | <pre>[<br> {<br> "id": "expire-noncurrent-objects-after-ninety-days",<br> "noncurrent_version_expiration": 90<br> },<br> {<br> "id": "transition-to-IA-after-30-days",<br> "transition": [<br> {<br> "days": 30,<br> "storage_class": "STANDARD_IA"<br> }<br> ]<br> },<br> {<br> "expiration": 2557,<br> "id": "delete-after-seven-years"<br> }<br>]</pre> | no |
6167
| <a name="input_logging_bucket"></a> [logging\_bucket](#input\_logging\_bucket) | S3 bucket to send request logs to the VPC flow log bucket to | `string` | n/a | yes |
6268
| <a name="input_region"></a> [region](#input\_region) | Region VPC flow logs will be sent to | `string` | n/a | yes |
6369
| <a name="input_tags"></a> [tags](#input\_tags) | Tags to include on resources that support it | `map(string)` | `{}` | no |

bin/install-ubuntu.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ cd ..
1515
rm -rf tmp
1616

1717
curl -L "$(curl -sL https://api.github.com/repos/terraform-linters/tflint/releases/latest | grep -o -E "https://.+?_linux_amd64.zip")" > tflint.zip && unzip tflint.zip && rm tflint.zip && sudo mv tflint /usr/bin/
18-
env GO111MODULE=on go get -u github.com/liamg/tfsec/cmd/tfsec
18+
go install github.com/aquasecurity/tfsec/cmd/tfsec@latest
1919
git clone https://github.com/tfutils/tfenv.git ~/.tfenv || true
2020
mkdir -p ~/.local/bin/
2121
. ~/.profile

main.tf

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,46 +13,65 @@ locals {
1313

1414
# Format account IDs into necessary resource lists.
1515
bucket_policy_put_resources = formatlist("${aws_s3_bucket.this.arn}/AWSLogs/%s/*", local.account_ids)
16-
kms_key_encrypt_resources = formatlist("arn:${local.partition}:cloudtrail:*:%s:trail/*", local.account_ids)
1716
}
1817

1918
resource "aws_s3_bucket" "this" {
2019
bucket = "${local.account_id}-${var.region}-vpcflowlog"
21-
acl = "private"
2220
tags = var.tags
2321

24-
lifecycle_rule {
25-
enabled = true
22+
lifecycle {
23+
prevent_destroy = true
24+
}
25+
}
2626

27-
transition {
28-
days = 30
29-
storage_class = "STANDARD_IA"
30-
}
27+
resource "aws_s3_bucket_acl" "this" {
28+
bucket = aws_s3_bucket.this.id
29+
acl = "private"
30+
}
3131

32-
}
32+
resource "aws_s3_bucket_lifecycle_configuration" "this" {
33+
count = var.lifecycle_rules == null ? 0 : 1
3334

34-
logging {
35-
target_bucket = var.logging_bucket
36-
target_prefix = "${local.account_id}-${var.region}-vpcflowlog/"
37-
}
35+
bucket = aws_s3_bucket.this.id
36+
37+
dynamic "rule" {
38+
iterator = rule
39+
for_each = var.lifecycle_rules
3840

39-
server_side_encryption_configuration {
40-
rule {
41-
apply_server_side_encryption_by_default {
42-
sse_algorithm = "aws:kms"
43-
kms_master_key_id = aws_kms_key.this.arn
41+
content {
42+
id = rule.value.id
43+
status = rule.value.enabled ? "Enabled" : "Disabled"
44+
45+
filter {
46+
prefix = lookup(rule.value, "prefix", null)
4447
}
45-
}
4648

47-
}
49+
expiration {
50+
days = lookup(rule.value, "expiration", 2147483647)
51+
}
4852

49-
versioning {
50-
enabled = true
51-
}
53+
noncurrent_version_expiration {
54+
noncurrent_days = lookup(rule.value, "noncurrent_version_expiration", 2147483647)
55+
}
5256

53-
lifecycle {
54-
prevent_destroy = true
57+
dynamic "transition" {
58+
for_each = coalesce(rule.value.transition, [])
59+
60+
content {
61+
days = transition.value.days
62+
storage_class = transition.value.storage_class
63+
}
64+
}
65+
}
5566
}
67+
68+
depends_on = [aws_s3_bucket_versioning.this]
69+
}
70+
71+
resource "aws_s3_bucket_logging" "this" {
72+
bucket = aws_s3_bucket.this.id
73+
target_bucket = var.logging_bucket
74+
target_prefix = "${local.account_id}-${var.region}-vpcflowlog/"
5675
}
5776

5877
resource "aws_s3_bucket_public_access_block" "this" {
@@ -63,6 +82,26 @@ resource "aws_s3_bucket_public_access_block" "this" {
6382
restrict_public_buckets = true
6483
}
6584

85+
resource "aws_s3_bucket_server_side_encryption_configuration" "this" {
86+
bucket = aws_s3_bucket.this.bucket
87+
88+
rule {
89+
apply_server_side_encryption_by_default {
90+
kms_master_key_id = aws_kms_key.this.arn
91+
sse_algorithm = "aws:kms"
92+
}
93+
}
94+
}
95+
96+
resource "aws_s3_bucket_versioning" "this" {
97+
bucket = aws_s3_bucket.this.id
98+
99+
versioning_configuration {
100+
status = "Enabled"
101+
}
102+
}
103+
104+
66105
data "aws_iam_policy_document" "this" {
67106
statement {
68107
actions = ["s3:GetBucketAcl"]

variables.tf

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,41 @@ variable "allowed_account_ids" {
44
type = list(string)
55
}
66

7+
variable "lifecycle_rules" {
8+
description = "lifecycle rules to apply to the bucket"
9+
10+
default = [
11+
{
12+
id = "expire-noncurrent-objects-after-ninety-days"
13+
noncurrent_version_expiration = 90
14+
},
15+
{
16+
id = "transition-to-IA-after-30-days"
17+
transition = [{
18+
days = 30
19+
storage_class = "STANDARD_IA"
20+
}]
21+
},
22+
{
23+
id = "delete-after-seven-years"
24+
expiration = 2557
25+
},
26+
]
27+
28+
type = list(object(
29+
{
30+
id = string
31+
enabled = optional(bool, true)
32+
expiration = optional(number)
33+
prefix = optional(number)
34+
noncurrent_version_expiration = optional(number)
35+
transition = optional(list(object({
36+
days = number
37+
storage_class = string
38+
})))
39+
}))
40+
}
41+
742
variable "logging_bucket" {
843
description = "S3 bucket to send request logs to the VPC flow log bucket to"
944
type = string

versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 3.8"
7+
version = ">= 4"
88
}
99
}
1010
}

0 commit comments

Comments
 (0)