Skip to content

Commit 3ffcb0f

Browse files
Allow bucket and dynamodb to be optional (#95)
* Add bucket and dynamodb enabled vars * Add bucket and dynamodb enabled logic * Auto Format * Update variables.tf * Auto Format * Add empty list to avoid coalescelist error * Allow tests to pass in enabled vars Co-authored-by: cloudpossebot <[email protected]>
1 parent 2c9cb41 commit 3ffcb0f

File tree

6 files changed

+196
-5
lines changed

6 files changed

+196
-5
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,10 @@ Available targets:
297297
| <a name="input_billing_mode"></a> [billing\_mode](#input\_billing\_mode) | DynamoDB billing mode | `string` | `"PROVISIONED"` | no |
298298
| <a name="input_block_public_acls"></a> [block\_public\_acls](#input\_block\_public\_acls) | Whether Amazon S3 should block public ACLs for this bucket | `bool` | `true` | no |
299299
| <a name="input_block_public_policy"></a> [block\_public\_policy](#input\_block\_public\_policy) | Whether Amazon S3 should block public bucket policies for this bucket | `bool` | `true` | no |
300+
| <a name="input_bucket_enabled"></a> [bucket\_enabled](#input\_bucket\_enabled) | Whether to create the s3 bucket. | `bool` | `true` | no |
300301
| <a name="input_context"></a> [context](#input\_context) | Single object for setting entire context at once.<br>See description of individual variables for details.<br>Leave string and numeric variables as `null` to use default value.<br>Individual variable settings (non-null) override settings in context object,<br>except for attributes, tags, and additional\_tag\_map, which are merged. | `any` | <pre>{<br> "additional_tag_map": {},<br> "attributes": [],<br> "delimiter": null,<br> "enabled": true,<br> "environment": null,<br> "id_length_limit": null,<br> "label_key_case": null,<br> "label_order": [],<br> "label_value_case": null,<br> "name": null,<br> "namespace": null,<br> "regex_replace_chars": null,<br> "stage": null,<br> "tags": {}<br>}</pre> | no |
301302
| <a name="input_delimiter"></a> [delimiter](#input\_delimiter) | Delimiter to be used between `namespace`, `environment`, `stage`, `name` and `attributes`.<br>Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no |
303+
| <a name="input_dynamodb_enabled"></a> [dynamodb\_enabled](#input\_dynamodb\_enabled) | Whether to create the dynamodb table. | `bool` | `true` | no |
302304
| <a name="input_enable_point_in_time_recovery"></a> [enable\_point\_in\_time\_recovery](#input\_enable\_point\_in\_time\_recovery) | Enable DynamoDB point-in-time recovery | `bool` | `true` | no |
303305
| <a name="input_enable_public_access_block"></a> [enable\_public\_access\_block](#input\_enable\_public\_access\_block) | Enable Bucket Public Access Block | `bool` | `true` | no |
304306
| <a name="input_enable_server_side_encryption"></a> [enable\_server\_side\_encryption](#input\_enable\_server\_side\_encryption) | Enable DynamoDB server-side encryption | `bool` | `true` | no |

docs/terraform.md

+2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@
5252
| <a name="input_billing_mode"></a> [billing\_mode](#input\_billing\_mode) | DynamoDB billing mode | `string` | `"PROVISIONED"` | no |
5353
| <a name="input_block_public_acls"></a> [block\_public\_acls](#input\_block\_public\_acls) | Whether Amazon S3 should block public ACLs for this bucket | `bool` | `true` | no |
5454
| <a name="input_block_public_policy"></a> [block\_public\_policy](#input\_block\_public\_policy) | Whether Amazon S3 should block public bucket policies for this bucket | `bool` | `true` | no |
55+
| <a name="input_bucket_enabled"></a> [bucket\_enabled](#input\_bucket\_enabled) | Whether to create the s3 bucket. | `bool` | `true` | no |
5556
| <a name="input_context"></a> [context](#input\_context) | Single object for setting entire context at once.<br>See description of individual variables for details.<br>Leave string and numeric variables as `null` to use default value.<br>Individual variable settings (non-null) override settings in context object,<br>except for attributes, tags, and additional\_tag\_map, which are merged. | `any` | <pre>{<br> "additional_tag_map": {},<br> "attributes": [],<br> "delimiter": null,<br> "enabled": true,<br> "environment": null,<br> "id_length_limit": null,<br> "label_key_case": null,<br> "label_order": [],<br> "label_value_case": null,<br> "name": null,<br> "namespace": null,<br> "regex_replace_chars": null,<br> "stage": null,<br> "tags": {}<br>}</pre> | no |
5657
| <a name="input_delimiter"></a> [delimiter](#input\_delimiter) | Delimiter to be used between `namespace`, `environment`, `stage`, `name` and `attributes`.<br>Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no |
58+
| <a name="input_dynamodb_enabled"></a> [dynamodb\_enabled](#input\_dynamodb\_enabled) | Whether to create the dynamodb table. | `bool` | `true` | no |
5759
| <a name="input_enable_point_in_time_recovery"></a> [enable\_point\_in\_time\_recovery](#input\_enable\_point\_in\_time\_recovery) | Enable DynamoDB point-in-time recovery | `bool` | `true` | no |
5860
| <a name="input_enable_public_access_block"></a> [enable\_public\_access\_block](#input\_enable\_public\_access\_block) | Enable Bucket Public Access Block | `bool` | `true` | no |
5961
| <a name="input_enable_server_side_encryption"></a> [enable\_server\_side\_encryption](#input\_enable\_server\_side\_encryption) | Enable DynamoDB server-side encryption | `bool` | `true` | no |

examples/complete/main.tf

+3
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@ module "tfstate_backend" {
77

88
force_destroy = true
99

10+
bucket_enabled = var.bucket_enabled
11+
dynamodb_enabled = var.dynamodb_enabled
12+
1013
context = module.this.context
1114
}

examples/complete/variables.tf

+167
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,170 @@
11
variable "region" {
22
type = string
33
}
4+
5+
variable "arn_format" {
6+
type = string
7+
default = "arn:aws"
8+
description = "ARN format to be used. May be changed to support deployment in GovCloud/China regions."
9+
}
10+
11+
variable "acl" {
12+
type = string
13+
description = "The canned ACL to apply to the S3 bucket"
14+
default = "private"
15+
}
16+
17+
variable "billing_mode" {
18+
default = "PROVISIONED"
19+
description = "DynamoDB billing mode"
20+
}
21+
22+
variable "read_capacity" {
23+
default = 5
24+
description = "DynamoDB read capacity units"
25+
}
26+
27+
variable "write_capacity" {
28+
default = 5
29+
description = "DynamoDB write capacity units"
30+
}
31+
32+
variable "force_destroy" {
33+
type = bool
34+
description = "A boolean that indicates the S3 bucket can be destroyed even if it contains objects. These objects are not recoverable"
35+
default = false
36+
}
37+
38+
variable "mfa_delete" {
39+
type = bool
40+
description = "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 )"
41+
default = false
42+
}
43+
44+
variable "enable_point_in_time_recovery" {
45+
type = bool
46+
description = "Enable DynamoDB point-in-time recovery"
47+
default = true
48+
}
49+
50+
variable "enable_server_side_encryption" {
51+
type = bool
52+
description = "Enable DynamoDB server-side encryption"
53+
default = true
54+
}
55+
56+
variable "enable_public_access_block" {
57+
type = bool
58+
description = "Enable Bucket Public Access Block"
59+
default = true
60+
}
61+
62+
variable "block_public_acls" {
63+
type = bool
64+
description = "Whether Amazon S3 should block public ACLs for this bucket"
65+
default = true
66+
}
67+
68+
variable "ignore_public_acls" {
69+
type = bool
70+
description = "Whether Amazon S3 should ignore public ACLs for this bucket"
71+
default = true
72+
}
73+
74+
variable "block_public_policy" {
75+
description = "Whether Amazon S3 should block public bucket policies for this bucket"
76+
default = true
77+
}
78+
79+
variable "restrict_public_buckets" {
80+
type = bool
81+
description = "Whether Amazon S3 should restrict public bucket policies for this bucket"
82+
default = true
83+
}
84+
85+
variable "prevent_unencrypted_uploads" {
86+
type = bool
87+
default = true
88+
description = "Prevent uploads of unencrypted objects to S3"
89+
}
90+
91+
variable "profile" {
92+
type = string
93+
default = ""
94+
description = "AWS profile name as set in the shared credentials file"
95+
}
96+
97+
variable "role_arn" {
98+
type = string
99+
default = ""
100+
description = "The role to be assumed"
101+
}
102+
103+
variable "terraform_backend_config_file_name" {
104+
type = string
105+
default = "terraform.tf"
106+
description = "Name of terraform backend config file"
107+
}
108+
109+
variable "terraform_backend_config_file_path" {
110+
type = string
111+
default = ""
112+
description = "Directory for the terraform backend config file, usually `.`. The default is to create no file."
113+
}
114+
115+
variable "terraform_backend_config_template_file" {
116+
type = string
117+
default = ""
118+
description = "The path to the template used to generate the config file"
119+
}
120+
121+
variable "terraform_version" {
122+
type = string
123+
default = "0.12.2"
124+
description = "The minimum required terraform version"
125+
}
126+
127+
variable "terraform_state_file" {
128+
type = string
129+
default = "terraform.tfstate"
130+
description = "The path to the state file inside the bucket"
131+
}
132+
133+
variable "s3_bucket_name" {
134+
type = string
135+
default = ""
136+
description = "S3 bucket name. If not provided, the name will be generated by the label module in the format namespace-stage-name"
137+
}
138+
139+
variable "s3_replication_enabled" {
140+
type = bool
141+
default = false
142+
description = "Set this to true and specify `s3_replica_bucket_arn` to enable replication"
143+
}
144+
145+
variable "s3_replica_bucket_arn" {
146+
type = string
147+
default = ""
148+
description = "The ARN of the S3 replica bucket (destination)"
149+
}
150+
151+
variable "logging" {
152+
type = object({
153+
bucket_name = string
154+
prefix = string
155+
})
156+
default = null
157+
description = "Bucket access logging configuration."
158+
}
159+
160+
variable "bucket_enabled" {
161+
type = bool
162+
default = true
163+
description = "Whether to create the s3 bucket."
164+
}
165+
166+
variable "dynamodb_enabled" {
167+
type = bool
168+
default = true
169+
description = "Whether to create the dynamodb table."
170+
}

main.tf

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
locals {
22
enabled = module.this.enabled
33

4+
bucket_enabled = local.enabled && var.bucket_enabled
5+
dynamodb_enabled = local.enabled && var.dynamodb_enabled
6+
47
prevent_unencrypted_uploads = local.enabled && var.prevent_unencrypted_uploads && var.enable_server_side_encryption
58

69
policy = local.prevent_unencrypted_uploads ? join(
@@ -105,7 +108,7 @@ data "aws_iam_policy_document" "prevent_unencrypted_uploads" {
105108
}
106109

107110
resource "aws_s3_bucket" "default" {
108-
count = local.enabled ? 1 : 0
111+
count = local.bucket_enabled ? 1 : 0
109112

110113
#bridgecrew:skip=BC_AWS_S3_13:Skipping `Enable S3 Bucket Logging` check until Bridgecrew will support dynamic blocks (https://github.com/bridgecrewio/checkov/issues/776).
111114
#bridgecrew:skip=CKV_AWS_52:Skipping `Ensure S3 bucket has MFA delete enabled` check due to issues operating with `mfa_delete` in terraform
@@ -157,7 +160,7 @@ resource "aws_s3_bucket" "default" {
157160
}
158161

159162
resource "aws_s3_bucket_public_access_block" "default" {
160-
count = local.enabled && var.enable_public_access_block ? 1 : 0
163+
count = local.bucket_enabled && var.enable_public_access_block ? 1 : 0
161164
bucket = join("", aws_s3_bucket.default.*.id)
162165
block_public_acls = var.block_public_acls
163166
ignore_public_acls = var.ignore_public_acls
@@ -170,10 +173,11 @@ module "dynamodb_table_label" {
170173
version = "0.22.0"
171174
attributes = compact(concat(var.attributes, ["lock"]))
172175
context = module.this.context
176+
enabled = local.dynamodb_enabled
173177
}
174178

175179
resource "aws_dynamodb_table" "with_server_side_encryption" {
176-
count = local.enabled && var.enable_server_side_encryption ? 1 : 0
180+
count = local.dynamodb_enabled && var.enable_server_side_encryption ? 1 : 0
177181
name = module.dynamodb_table_label.id
178182
billing_mode = var.billing_mode
179183
read_capacity = var.billing_mode == "PROVISIONED" ? var.read_capacity : null
@@ -207,7 +211,7 @@ resource "aws_dynamodb_table" "with_server_side_encryption" {
207211
}
208212

209213
resource "aws_dynamodb_table" "without_server_side_encryption" {
210-
count = local.enabled && ! var.enable_server_side_encryption ? 1 : 0
214+
count = local.dynamodb_enabled && ! var.enable_server_side_encryption ? 1 : 0
211215
name = module.dynamodb_table_label.id
212216
billing_mode = var.billing_mode
213217
read_capacity = var.billing_mode == "PROVISIONED" ? var.read_capacity : null
@@ -250,7 +254,8 @@ data "template_file" "terraform_backend_config" {
250254
dynamodb_table = element(
251255
coalescelist(
252256
aws_dynamodb_table.with_server_side_encryption.*.name,
253-
aws_dynamodb_table.without_server_side_encryption.*.name
257+
aws_dynamodb_table.without_server_side_encryption.*.name,
258+
[""]
254259
),
255260
0
256261
)

variables.tf

+12
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,15 @@ variable "logging" {
152152
default = null
153153
description = "Bucket access logging configuration."
154154
}
155+
156+
variable "bucket_enabled" {
157+
type = bool
158+
default = true
159+
description = "Whether to create the s3 bucket."
160+
}
161+
162+
variable "dynamodb_enabled" {
163+
type = bool
164+
default = true
165+
description = "Whether to create the dynamodb table."
166+
}

0 commit comments

Comments
 (0)