Skip to content

Commit ca9e268

Browse files
Correct enabled behavior (#147)
* correct enabled behavior * Auto Format * fix docs * Auto Format * use indexes * wrap outputs in try * do not generate data if we arent using it * wrap data in try * wrap check * wrap more checks * wrap more checks * change s3 bucket check * change s3 policy bucket check * update docs * Auto Format * fix * remove leftover Co-authored-by: cloudpossebot <[email protected]>
1 parent e882361 commit ca9e268

File tree

5 files changed

+31
-17
lines changed

5 files changed

+31
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ Available targets:
277277
| <a name="input_default_root_object"></a> [default\_root\_object](#input\_default\_root\_object) | Object that CloudFront return when requests the root URL | `string` | `"index.html"` | no |
278278
| <a name="input_default_ttl"></a> [default\_ttl](#input\_default\_ttl) | Default amount of time (in seconds) that an object is in a CloudFront cache | `number` | `60` | no |
279279
| <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 |
280+
| <a name="input_distribution_enabled"></a> [distribution\_enabled](#input\_distribution\_enabled) | Set to `true` if you want CloudFront to begin processing requests as soon as the distribution is created, or to false if you do not want CloudFront to begin processing requests after the distribution is created. | `bool` | `true` | no |
280281
| <a name="input_dns_alias_enabled"></a> [dns\_alias\_enabled](#input\_dns\_alias\_enabled) | Create a DNS alias for the CDN. Requires `parent_zone_id` or `parent_zone_name` | `bool` | `false` | no |
281282
| <a name="input_enabled"></a> [enabled](#input\_enabled) | Set to false to prevent the module from creating any resources | `bool` | `null` | no |
282283
| <a name="input_encryption_enabled"></a> [encryption\_enabled](#input\_encryption\_enabled) | When set to 'true' the resource will have aes256 encryption enabled by default | `bool` | `true` | no |

docs/terraform.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
| <a name="input_default_root_object"></a> [default\_root\_object](#input\_default\_root\_object) | Object that CloudFront return when requests the root URL | `string` | `"index.html"` | no |
6464
| <a name="input_default_ttl"></a> [default\_ttl](#input\_default\_ttl) | Default amount of time (in seconds) that an object is in a CloudFront cache | `number` | `60` | no |
6565
| <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 |
66+
| <a name="input_distribution_enabled"></a> [distribution\_enabled](#input\_distribution\_enabled) | Set to `true` if you want CloudFront to begin processing requests as soon as the distribution is created, or to false if you do not want CloudFront to begin processing requests after the distribution is created. | `bool` | `true` | no |
6667
| <a name="input_dns_alias_enabled"></a> [dns\_alias\_enabled](#input\_dns\_alias\_enabled) | Create a DNS alias for the CDN. Requires `parent_zone_id` or `parent_zone_name` | `bool` | `false` | no |
6768
| <a name="input_enabled"></a> [enabled](#input\_enabled) | Set to false to prevent the module from creating any resources | `bool` | `null` | no |
6869
| <a name="input_encryption_enabled"></a> [encryption\_enabled](#input\_encryption\_enabled) | When set to 'true' the resource will have aes256 encryption enabled by default | `bool` | `true` | no |

main.tf

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ module "origin_label" {
2323
}
2424

2525
resource "aws_cloudfront_origin_access_identity" "default" {
26-
count = local.using_existing_cloudfront_origin ? 0 : 1
26+
count = (! module.this.enabled || local.using_existing_cloudfront_origin) ? 0 : 1
2727

2828
comment = module.this.id
2929
}
3030

3131
data "aws_iam_policy_document" "origin" {
32+
count = module.this.enabled ? 1 : 0
33+
3234
override_json = var.additional_bucket_policy
3335

3436
statement {
@@ -57,6 +59,8 @@ data "aws_iam_policy_document" "origin" {
5759
}
5860

5961
data "aws_iam_policy_document" "origin_website" {
62+
count = module.this.enabled ? 1 : 0
63+
6064
override_json = var.additional_bucket_policy
6165

6266
statement {
@@ -73,7 +77,7 @@ data "aws_iam_policy_document" "origin_website" {
7377
}
7478

7579
resource "aws_s3_bucket_policy" "default" {
76-
count = ! local.using_existing_origin || var.override_origin_bucket_policy ? 1 : 0
80+
count = (module.this.enabled && (! local.using_existing_origin || var.override_origin_bucket_policy)) ? 1 : 0
7781
bucket = join("", aws_s3_bucket.origin.*.bucket)
7882
policy = local.iam_policy_document
7983
}
@@ -82,7 +86,7 @@ resource "aws_s3_bucket" "origin" {
8286
#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).
8387
#bridgecrew:skip=BC_AWS_S3_14:Skipping `Ensure all data stored in the S3 bucket is securely encrypted at rest` check until bridgecrew will support dynamic blocks (https://github.com/bridgecrewio/checkov/issues/776).
8488
#bridgecrew:skip=CKV_AWS_52:Skipping `Ensure S3 bucket has MFA delete enabled` due to issue in terraform (https://github.com/hashicorp/terraform-provider-aws/issues/629).
85-
count = local.using_existing_origin ? 0 : 1
89+
count = (! module.this.enabled || local.using_existing_origin) ? 0 : 1
8690
bucket = module.origin_label.id
8791
acl = "private"
8892
tags = module.origin_label.tags
@@ -135,7 +139,7 @@ resource "aws_s3_bucket" "origin" {
135139
}
136140

137141
resource "aws_s3_bucket_public_access_block" "origin" {
138-
count = ! local.using_existing_origin && var.block_origin_public_access_enabled ? 1 : 0
142+
count = (module.this.enabled && ! local.using_existing_origin && var.block_origin_public_access_enabled) ? 1 : 0
139143
bucket = local.bucket
140144
block_public_acls = true
141145
block_public_policy = true
@@ -150,7 +154,7 @@ resource "aws_s3_bucket_public_access_block" "origin" {
150154
module "logs" {
151155
source = "cloudposse/s3-log-storage/aws"
152156
version = "0.20.0"
153-
enabled = var.logging_enabled
157+
enabled = (module.this.enabled && var.logging_enabled)
154158
attributes = compact(concat(module.this.attributes, var.extra_logs_attributes))
155159
lifecycle_prefix = var.log_prefix
156160
standard_transition_days = var.log_standard_transition_days
@@ -163,7 +167,7 @@ module "logs" {
163167
}
164168

165169
data "aws_s3_bucket" "selected" {
166-
count = local.using_existing_origin ? 1 : 0
170+
count = (module.this.enabled && local.using_existing_origin) ? 1 : 0
167171
bucket = var.origin_bucket
168172
}
169173

@@ -174,7 +178,7 @@ locals {
174178

175179
origin_path = coalesce(var.origin_path, "/")
176180
cloudfront_origin_access_identity_iam_arn = local.using_existing_cloudfront_origin ? var.cloudfront_origin_access_identity_iam_arn : join("", aws_cloudfront_origin_access_identity.default.*.iam_arn)
177-
iam_policy_document = var.website_enabled ? data.aws_iam_policy_document.origin_website.json : data.aws_iam_policy_document.origin.json
181+
iam_policy_document = var.website_enabled ? try(data.aws_iam_policy_document.origin_website[0].json, "") : try(data.aws_iam_policy_document.origin[0].json, "")
178182

179183
bucket = join("",
180184
compact(
@@ -188,8 +192,10 @@ locals {
188192
}
189193

190194
resource "aws_cloudfront_distribution" "default" {
195+
count = module.this.enabled ? 1 : 0
196+
191197
#bridgecrew:skip=BC_AWS_LOGGING_20:Skipping `CloudFront Access Logging` check until bridgecrew will support dynamic blocks (https://github.com/bridgecrewio/checkov/issues/776).
192-
enabled = module.this.enabled
198+
enabled = var.distribution_enabled
193199
is_ipv6_enabled = var.ipv6_enabled
194200
comment = var.comment
195201
default_root_object = var.default_root_object
@@ -362,12 +368,12 @@ resource "aws_cloudfront_distribution" "default" {
362368
module "dns" {
363369
source = "cloudposse/route53-alias/aws"
364370
version = "0.12.0"
365-
enabled = module.this.enabled && var.dns_alias_enabled ? true : false
371+
enabled = (module.this.enabled && var.dns_alias_enabled) ? true : false
366372
aliases = var.aliases
367373
parent_zone_id = var.parent_zone_id
368374
parent_zone_name = var.parent_zone_name
369-
target_dns_name = aws_cloudfront_distribution.default.domain_name
370-
target_zone_id = aws_cloudfront_distribution.default.hosted_zone_id
375+
target_dns_name = try(aws_cloudfront_distribution.default[0].domain_name, "")
376+
target_zone_id = try(aws_cloudfront_distribution.default[0].hosted_zone_id, "")
371377
ipv6_enabled = var.ipv6_enabled
372378

373379
context = module.this.context

outputs.tf

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
output "cf_id" {
2-
value = aws_cloudfront_distribution.default.id
2+
value = try(aws_cloudfront_distribution.default[0].id, "")
33
description = "ID of AWS CloudFront distribution"
44
}
55

66
output "cf_arn" {
7-
value = aws_cloudfront_distribution.default.arn
7+
value = try(aws_cloudfront_distribution.default[0].arn, "")
88
description = "ARN of AWS CloudFront distribution"
99
}
1010

1111
output "cf_status" {
12-
value = aws_cloudfront_distribution.default.status
12+
value = try(aws_cloudfront_distribution.default[0].status, "")
1313
description = "Current status of the distribution"
1414
}
1515

1616
output "cf_domain_name" {
17-
value = aws_cloudfront_distribution.default.domain_name
17+
value = try(aws_cloudfront_distribution.default[0].domain_name, "")
1818
description = "Domain name corresponding to the distribution"
1919
}
2020

2121
output "cf_etag" {
22-
value = aws_cloudfront_distribution.default.etag
22+
value = try(aws_cloudfront_distribution.default[0].etag, "")
2323
description = "Current version of the distribution's information"
2424
}
2525

2626
output "cf_hosted_zone_id" {
27-
value = aws_cloudfront_distribution.default.hosted_zone_id
27+
value = try(aws_cloudfront_distribution.default[0].hosted_zone_id, "")
2828
description = "CloudFront Route 53 zone ID"
2929
}
3030

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,3 +424,9 @@ variable "access_log_bucket_name" {
424424
default = ""
425425
description = "Name of the S3 bucket where s3 access log will be sent to"
426426
}
427+
428+
variable "distribution_enabled" {
429+
type = bool
430+
default = true
431+
description = "Set to `true` if you want CloudFront to begin processing requests as soon as the distribution is created, or to false if you do not want CloudFront to begin processing requests after the distribution is created."
432+
}

0 commit comments

Comments
 (0)