Skip to content

Commit 77b3b57

Browse files
Merge pull request #27 from rackspace-infrastructure-automation/feature/fix-deprecations
Fix deprecations and issues
2 parents 4b29f78 + 5a4a54f commit 77b3b57

File tree

6 files changed

+179
-143
lines changed

6 files changed

+179
-143
lines changed

.terraform-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.13.2
1+
0.13.7

README.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ The following module variables were updated to better meet current Rackspace sty
5151

5252
| Name | Version |
5353
|------|---------|
54-
| terraform | >= 0.12 |
55-
| aws | >= 2.7.0 |
54+
| terraform | >= 0.13 |
55+
| aws | ~> 3.0 |
5656

5757
## Providers
5858

5959
| Name | Version |
6060
|------|---------|
61-
| aws | >= 2.7.0 |
61+
| aws | ~> 3.0 |
6262

6363
## Modules
6464

@@ -68,32 +68,38 @@ No Modules.
6868

6969
| Name |
7070
|------|
71-
| [aws_s3_bucket](https://registry.terraform.io/providers/hashicorp/aws/2.7.0/docs/resources/s3_bucket) |
72-
| [aws_s3_bucket_public_access_block](https://registry.terraform.io/providers/hashicorp/aws/2.7.0/docs/resources/s3_bucket_public_access_block) |
71+
| [aws_s3_bucket](https://registry.terraform.io/providers/hashicorp/aws/3.0/docs/resources/s3_bucket) |
72+
| [aws_s3_bucket_acl](https://registry.terraform.io/providers/hashicorp/aws/3.0/docs/resources/s3_bucket_acl) |
73+
| [aws_s3_bucket_cors_configuration](https://registry.terraform.io/providers/hashicorp/aws/3.0/docs/resources/s3_bucket_cors_configuration) |
74+
| [aws_s3_bucket_logging](https://registry.terraform.io/providers/hashicorp/aws/3.0/docs/resources/s3_bucket_logging) |
75+
| [aws_s3_bucket_public_access_block](https://registry.terraform.io/providers/hashicorp/aws/3.0/docs/resources/s3_bucket_public_access_block) |
76+
| [aws_s3_bucket_server_side_encryption_configuration](https://registry.terraform.io/providers/hashicorp/aws/3.0/docs/resources/s3_bucket_server_side_encryption_configuration) |
77+
| [aws_s3_bucket_versioning](https://registry.terraform.io/providers/hashicorp/aws/3.0/docs/resources/s3_bucket_versioning) |
7378

7479
## Inputs
7580

7681
| Name | Description | Type | Default | Required |
7782
|------|-------------|------|---------|:--------:|
78-
| allowed\_headers | Specifies which headers are allowed. | `list(string)` | `[]` | no |
79-
| allowed\_methods | (Required) Specifies which methods are allowed. Can be GET, PUT, POST, DELETE or HEAD. | `list(string)` | `[]` | no |
80-
| allowed\_origins | (Required) Specifies which origins are allowed. | `list(string)` | `[]` | no |
83+
| abort\_incomplete\_multipart\_upload\_days | Abort Incomplete Multipart Upload Days i.e. 7 \| 0 | `number` | `7` | no |
8184
| block\_public\_access | Block various forms of public access on a per bucket level | `bool` | `false` | no |
8285
| block\_public\_access\_acl | Related to block\_public\_access. PUT Bucket acl and PUT Object acl calls will fail if the specified ACL allows public access. PUT Object calls will fail if the request includes an object ACL. | `bool` | `true` | no |
8386
| block\_public\_access\_ignore\_acl | Related to block\_public\_access. Ignore public ACLs on this bucket and any objects that it contains. | `bool` | `true` | no |
8487
| block\_public\_access\_policy | Related to block\_public\_access. Reject calls to PUT Bucket policy if the specified bucket policy allows public access. | `bool` | `true` | no |
8588
| block\_public\_access\_restrict\_bucket | Related to block\_public\_access. Only the bucket owner and AWS Services can access this buckets if it has a public policy. | `bool` | `true` | no |
8689
| bucket\_acl | Bucket ACL. Must be either authenticated-read, aws-exec-read, log-delivery-write, private, public-read or public-read-write. For more details https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl | `string` | `"private"` | no |
90+
| bucket\_key\_enabled | Whether or not to use Amazon S3 Bucket Keys for SSE-KMS. | `bool` | `false` | no |
8791
| bucket\_logging | Enable bucket logging. Will store logs in another existing bucket. You must give the log-delivery group WRITE and READ\_ACP permissions to the target bucket. i.e. true \| false | `bool` | `false` | no |
92+
| cors | Enable CORS Rules. Rules must be defined in the variable cors\_rules | `bool` | `false` | no |
93+
| cors\_rule | List of maps containing rules for Cross-Origin Resource Sharing. | `any` | `[]` | no |
8894
| environment | Application environment for which this network is being created. must be one of ['Development', 'Integration', 'PreProduction', 'Production', 'QA', 'Staging', 'Test'] | `string` | `"Development"` | no |
89-
| expose\_headers | Specifies expose header in the response. | `list(string)` | `[]` | no |
95+
| expected\_bucket\_owner | The account ID of the expected bucket owner | `string` | `null` | no |
9096
| force\_destroy\_bucket | A boolean that indicates all objects should be deleted from the bucket so that the bucket can be destroyed without error. These objects are not recoverable. | `bool` | `false` | no |
9197
| kms\_key\_id | The AWS KMS master key ID used for the SSE-KMS encryption. This can only be used when you set the value of sse\_algorithm as aws:kms. | `string` | `""` | no |
9298
| lifecycle\_enabled | Enable object lifecycle management. i.e. true \| false | `bool` | `false` | no |
9399
| lifecycle\_rule\_prefix | Object keyname prefix identifying one or more objects to which the rule applies. Set as an empty string to target the whole bucket. | `string` | `""` | no |
94100
| logging\_bucket\_name | Name of the existing bucket where the logs will be stored. | `string` | `""` | no |
95101
| logging\_bucket\_prefix | Prefix for all log object keys. i.e. logs/ | `string` | `""` | no |
96-
| max\_age\_seconds | Specifies time in seconds that browser can cache the response for a preflight request. | `number` | `600` | no |
102+
| mfa\_delete | Specifies whether MFA delete is enabled in the bucket versioning configuration | `bool` | `false` | no |
97103
| name | The name of the S3 bucket for the access logs. The bucket name can contain only lowercase letters, numbers, periods (.), and dashes (-). Must be globally unique. If changed, forces a new resource. | `string` | n/a | yes |
98104
| noncurrent\_version\_expiration\_days | Indicates after how many days we are deleting previous version of objects. Set to 0 to disable or at least 365 days longer than noncurrent\_version\_transition\_glacier\_days. i.e. 0 to disable, 1-999 otherwise | `number` | `0` | no |
99105
| noncurrent\_version\_transition\_glacier\_days | Indicates after how many days we are moving previous versions to Glacier. Should be 0 to disable or at least 30 days longer than noncurrent\_version\_transition\_ia\_days. i.e. 0 to disable, 1-999 otherwise | `number` | `0` | no |
@@ -108,7 +114,7 @@ No Modules.
108114
| tags | A map of tags to be applied to the Bucket. i.e {Environment='Development'} | `map(string)` | `{}` | no |
109115
| transition\_to\_glacier\_days | Indicates after how many days we are moving current versions to Glacier. Should be 0 to disable or at least 30 days longer than transition\_to\_ia\_days. i.e. 0 to disable, otherwise 1-999 | `number` | `0` | no |
110116
| transition\_to\_ia\_days | Indicates after how many days we are moving current objects to Standard-IA storage. i.e. 0 to disable, otherwise 1-999 | `number` | `0` | no |
111-
| versioning | Enable bucket versioning. i.e. true \| false | `bool` | `false` | no |
117+
| versioning | Enable bucket versioning. | `bool` | `false` | no |
112118
| website | Use bucket as a static website. i.e. true \| false | `bool` | `false` | no |
113119
| website\_error | Location of Error HTML file. i.e. error.html | `string` | `"error.html"` | no |
114120
| website\_index | Location of Index HTML file. i.e index.html | `string` | `"index.html"` | no |

main.tf

Lines changed: 77 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@
5151
*/
5252

5353
terraform {
54-
required_version = ">= 0.12"
54+
required_version = ">= 0.13"
5555

5656
required_providers {
57-
aws = ">= 2.7.0"
57+
aws = {
58+
source = "hashicorp/aws"
59+
version = "~> 3.0"
60+
}
5861
}
5962
}
6063

@@ -72,22 +75,7 @@ locals {
7275
SkipBucket = var.rax_mpu_cleanup_enabled ? null : "True"
7376
}
7477

75-
##############################################################
76-
# CORS rules local variables
77-
##############################################################
78-
79-
cors_rules = {
80-
enabled = [
81-
{
82-
allowed_headers = var.allowed_headers
83-
allowed_methods = var.allowed_methods
84-
allowed_origins = var.allowed_origins
85-
expose_headers = var.expose_headers
86-
max_age_seconds = var.max_age_seconds
87-
},
88-
]
89-
disabled = []
90-
}
78+
cors_rules = try(jsondecode(var.cors_rule), var.cors_rule)
9179

9280
##############################################################
9381
# Lifecycle Rules local variables
@@ -114,7 +102,7 @@ locals {
114102
]
115103
mpu_cleanup_enabled = [
116104
{
117-
abort_incomplete_multipart_upload_days = 7
105+
abort_incomplete_multipart_upload_days = var.abort_incomplete_multipart_upload_days
118106
enabled = true
119107
id = "rax-cleanup-incomplete-mpu-objects"
120108
expiration = [{}]
@@ -165,20 +153,6 @@ locals {
165153
disabled = []
166154
}
167155

168-
##############################################################
169-
# Bucket Logging local variables
170-
##############################################################
171-
172-
bucket_logging = {
173-
enabled = [
174-
{
175-
target_bucket = var.logging_bucket_name
176-
target_prefix = var.logging_bucket_prefix
177-
},
178-
]
179-
disabled = []
180-
}
181-
182156
##############################################################
183157
# Bucket object lock local variables
184158
##############################################################
@@ -198,28 +172,6 @@ locals {
198172
Disabled = []
199173
}
200174

201-
##############################################################
202-
# Server side encryption rule local variables
203-
##############################################################
204-
205-
server_side_encryption_rule = {
206-
enabled = [
207-
{
208-
rule = [
209-
{
210-
apply_server_side_encryption_by_default = [
211-
{
212-
kms_master_key_id = var.kms_key_id
213-
sse_algorithm = var.sse_algorithm
214-
},
215-
]
216-
},
217-
]
218-
},
219-
]
220-
disabled = []
221-
}
222-
223175
##############################################################
224176
# Bucket website local variables
225177
##############################################################
@@ -236,22 +188,10 @@ locals {
236188
}
237189

238190
resource "aws_s3_bucket" "s3_bucket" {
239-
acl = contains(local.acl_list, var.bucket_acl) ? var.bucket_acl : "ACL_ERROR"
240191
bucket = var.name
241192
force_destroy = var.force_destroy_bucket
242193
tags = merge(var.tags, local.default_tags)
243194

244-
dynamic "cors_rule" {
245-
for_each = local.cors_rules[length(var.allowed_origins) > 0 ? "enabled" : "disabled"]
246-
content {
247-
allowed_headers = lookup(cors_rule.value, "allowed_headers", null)
248-
allowed_methods = cors_rule.value.allowed_methods
249-
allowed_origins = cors_rule.value.allowed_origins
250-
expose_headers = lookup(cors_rule.value, "expose_headers", null)
251-
max_age_seconds = lookup(cors_rule.value, "max_age_seconds", null)
252-
}
253-
}
254-
255195
dynamic "lifecycle_rule" {
256196
for_each = concat(
257197
local.lifecycle_rules[(var.lifecycle_enabled ? "enabled" : "disabled")],
@@ -300,14 +240,6 @@ resource "aws_s3_bucket" "s3_bucket" {
300240
}
301241
}
302242

303-
dynamic "logging" {
304-
for_each = local.bucket_logging[var.bucket_logging ? "enabled" : "disabled"]
305-
content {
306-
target_bucket = logging.value.target_bucket
307-
target_prefix = lookup(logging.value, "target_prefix", null)
308-
}
309-
}
310-
311243
dynamic "object_lock_configuration" {
312244
for_each = local.object_lock_rule[var.object_lock_enabled ? "Enabled" : "Disabled"]
313245
content {
@@ -325,27 +257,6 @@ resource "aws_s3_bucket" "s3_bucket" {
325257
}
326258
}
327259

328-
dynamic "server_side_encryption_configuration" {
329-
for_each = local.server_side_encryption_rule[var.sse_algorithm == "none" ? "disabled" : "enabled"]
330-
content {
331-
dynamic "rule" {
332-
for_each = lookup(server_side_encryption_configuration.value, "rule", [])
333-
content {
334-
dynamic "apply_server_side_encryption_by_default" {
335-
for_each = lookup(rule.value, "apply_server_side_encryption_by_default", [])
336-
content {
337-
kms_master_key_id = lookup(apply_server_side_encryption_by_default.value, "kms_master_key_id", null)
338-
sse_algorithm = apply_server_side_encryption_by_default.value.sse_algorithm
339-
}
340-
}
341-
}
342-
}
343-
}
344-
}
345-
346-
versioning {
347-
enabled = var.versioning
348-
}
349260

350261
dynamic "website" {
351262
for_each = local.bucket_website_config[var.website ? "enabled" : "disabled"]
@@ -372,3 +283,73 @@ resource "aws_s3_bucket_public_access_block" "block_public_access_settings" {
372283
ignore_public_acls = var.block_public_access_ignore_acl
373284
restrict_public_buckets = var.block_public_access_restrict_bucket
374285
}
286+
287+
##############################################################
288+
# S3 Access Control List
289+
##############################################################
290+
resource "aws_s3_bucket_acl" "s3_acl" {
291+
bucket = aws_s3_bucket.s3_bucket.id
292+
acl = contains(local.acl_list, var.bucket_acl) ? var.bucket_acl : "ACL_ERROR"
293+
}
294+
295+
296+
##############################################################
297+
# S3 Versioning Configuration
298+
##############################################################
299+
resource "aws_s3_bucket_versioning" "s3_versioning" {
300+
bucket = aws_s3_bucket.s3_bucket.id
301+
versioning_configuration {
302+
status = var.versioning ? "Enabled" : "Disabled"
303+
mfa_delete = var.mfa_delete ? "Enabled" : "Disabled"
304+
}
305+
}
306+
307+
##############################################################
308+
# S3 Logging Configuration
309+
##############################################################
310+
resource "aws_s3_bucket_logging" "s3_logging" {
311+
count = var.bucket_logging ? 1 : 0
312+
313+
bucket = aws_s3_bucket.s3_bucket.id
314+
target_bucket = var.logging_bucket_name
315+
target_prefix = var.logging_bucket_prefix
316+
}
317+
318+
##############################################################
319+
# S3 Server Side Encryption (SSE) Configuration
320+
##############################################################
321+
resource "aws_s3_bucket_server_side_encryption_configuration" "s3_sse" {
322+
count = var.sse_algorithm == "none" ? 0 : 1
323+
324+
bucket = aws_s3_bucket.s3_bucket.id
325+
rule {
326+
apply_server_side_encryption_by_default {
327+
sse_algorithm = var.sse_algorithm
328+
kms_master_key_id = var.kms_key_id
329+
}
330+
bucket_key_enabled = var.bucket_key_enabled
331+
}
332+
}
333+
334+
##############################################################
335+
# S3 CORS Configuration
336+
##############################################################
337+
resource "aws_s3_bucket_cors_configuration" "this" {
338+
count = var.cors ? 1 : 0
339+
340+
bucket = aws_s3_bucket.s3_bucket.id
341+
expected_bucket_owner = var.expected_bucket_owner
342+
343+
dynamic "cors_rule" {
344+
for_each = local.cors_rules
345+
346+
content {
347+
id = try(cors_rule.value.id, null)
348+
allowed_methods = cors_rule.value.allowed_methods
349+
allowed_origins = cors_rule.value.allowed_origins
350+
allowed_headers = try(cors_rule.value.allowed_headers, null)
351+
expose_headers = try(cors_rule.value.expose_headers, null)
352+
max_age_seconds = try(cors_rule.value.max_age_seconds, null)
353+
}
354+
}
355+
}

tests/test2/main.tf

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ resource "random_string" "s3_rstring" {
2020
module "s3" {
2121
source = "../../module"
2222

23-
allowed_headers = ["*"]
24-
allowed_methods = ["PUT", "POST"]
25-
allowed_origins = ["*"]
2623
bucket_acl = "private"
2724
bucket_logging = false
2825
environment = "Development"
@@ -38,10 +35,25 @@ module "s3" {
3835
website = true
3936
website_error = "error.html"
4037
website_index = "index.html"
38+
cors = true
39+
cors_rule = [
40+
{
41+
allowed_methods = ["PUT", "POST"]
42+
allowed_origins = ["https://modules.tf", "https://terraform-aws-modules.modules.tf"]
43+
allowed_headers = ["*"]
44+
expose_headers = ["ETag"]
45+
max_age_seconds = 3000
46+
}, {
47+
allowed_methods = ["PUT"]
48+
allowed_origins = ["https://example.com"]
49+
allowed_headers = ["*"]
50+
expose_headers = ["ETag"]
51+
max_age_seconds = 3000
52+
}
53+
]
4154

4255
# Not defining these to ensure it can properly handle undefined variable lists or strings
4356
# expose_headers = ["Accept-Ranges", "Content-Range", "Content-Encoding", "Content-Length"]
44-
# max_age_seconds = 3000
4557

4658
tags = {
4759
RightSaid = "Fred"

tests/test3/main.tf

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,34 @@ module "s3" {
4343
LeftSaid = "George"
4444
}
4545
}
46+
47+
module "s3_logging_test" {
48+
source = "../../module"
49+
50+
bucket_acl = "private"
51+
bucket_logging = true
52+
logging_bucket_name = module.s3.bucket_id
53+
logging_bucket_prefix = "logs/"
54+
environment = "Development"
55+
lifecycle_enabled = true
56+
name = "${random_string.s3_rstring.result}-example-s3-log-bucket"
57+
noncurrent_version_expiration_days = "425"
58+
noncurrent_version_transition_glacier_days = "60"
59+
noncurrent_version_transition_ia_days = "30"
60+
object_expiration_days = "425"
61+
rax_mpu_cleanup_enabled = true
62+
transition_to_glacier_days = "60"
63+
transition_to_ia_days = "30"
64+
versioning = true
65+
kms_key_id = "aws/s3"
66+
sse_algorithm = "aws:kms"
67+
bucket_key_enabled = true
68+
69+
70+
tags = {
71+
RightSaid = "Fred"
72+
LeftSaid = "George"
73+
}
74+
75+
depends_on = [module.s3]
76+
}

0 commit comments

Comments
 (0)