Skip to content

Commit 493d99d

Browse files
dmattiamaximmi
andauthored
Make it optional to override the origin s3 policy (#67)
When specifying var.origin_bucket, it can be nice to leave the existing bucket's policy as is. As an example, I manage an s3 bucket that multiple CloudFront dists use as their origin (without paths, they just use the same code). In this case, I do not want to restrict the bucket to only talk to a single CF dist, as this module does by default. Co-authored-by: Maxim Mironenko <[email protected]>
1 parent 99a794d commit 493d99d

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,9 @@ Available targets:
190190
| namespace | Namespace (e.g. `eg` or `cp`) | string | `` | no |
191191
| ordered_cache | An ordered list of cache behaviors resource for this distribution. List from top to bottom in order of precedence. The topmost cache behavior will have precedence 0. The fields can be described by the other variables in this file. For example, the field 'lambda_function_association' in this object has a description in var.lambda_function_association variable earlier in this file. The only difference is that fields on this object are in ordered caches, whereas the rest of the vars in this file apply only to the default cache. | object | `<list>` | no |
192192
| origin_bucket | Origin S3 bucket name | string | `` | no |
193-
| origin_force_destroy | Delete all objects from the bucket so that the bucket can be destroyed without error (e.g. `true` or `false`) | bool | `false` | no |
193+
| origin_force_destroy | Delete all objects from the bucket so that the bucket can be destroyed without error (e.g. `true` or `false`) | bool | `false` | no |
194194
| origin_path | An optional element that causes CloudFront to request your content from a directory in your Amazon S3 bucket or your custom origin. It must begin with a /. Do not add a / at the end of the path. | string | `` | no |
195+
| override_origin_bucket_policy | When using an existing origin bucket (through var.origin_bucket), setting this to 'false' will make it so the existing bucket policy will not be overriden | bool | `true` | no |
195196
| parent_zone_id | ID of the hosted zone to contain this record (or specify `parent_zone_name`) | string | `` | no |
196197
| parent_zone_name | Name of the hosted zone to contain this record (or specify `parent_zone_id`) | string | `` | no |
197198
| price_class | Price class for this distribution: `PriceClass_All`, `PriceClass_200`, `PriceClass_100` | string | `PriceClass_100` | no |

docs/terraform.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@
4646
| namespace | Namespace (e.g. `eg` or `cp`) | string | `` | no |
4747
| ordered_cache | An ordered list of cache behaviors resource for this distribution. List from top to bottom in order of precedence. The topmost cache behavior will have precedence 0. The fields can be described by the other variables in this file. For example, the field 'lambda_function_association' in this object has a description in var.lambda_function_association variable earlier in this file. The only difference is that fields on this object are in ordered caches, whereas the rest of the vars in this file apply only to the default cache. | object | `<list>` | no |
4848
| origin_bucket | Origin S3 bucket name | string | `` | no |
49-
| origin_force_destroy | Delete all objects from the bucket so that the bucket can be destroyed without error (e.g. `true` or `false`) | bool | `false` | no |
49+
| origin_force_destroy | Delete all objects from the bucket so that the bucket can be destroyed without error (e.g. `true` or `false`) | bool | `false` | no |
5050
| origin_path | An optional element that causes CloudFront to request your content from a directory in your Amazon S3 bucket or your custom origin. It must begin with a /. Do not add a / at the end of the path. | string | `` | no |
51+
| override_origin_bucket_policy | When using an existing origin bucket (through var.origin_bucket), setting this to 'false' will make it so the existing bucket policy will not be overriden | bool | `true` | no |
5152
| parent_zone_id | ID of the hosted zone to contain this record (or specify `parent_zone_name`) | string | `` | no |
5253
| parent_zone_name | Name of the hosted zone to contain this record (or specify `parent_zone_id`) | string | `` | no |
5354
| price_class | Price class for this distribution: `PriceClass_All`, `PriceClass_200`, `PriceClass_100` | string | `PriceClass_100` | no |

main.tf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ data "template_file" "default" {
9494
}
9595

9696
resource "aws_s3_bucket_policy" "default" {
97+
count = ! local.using_existing_origin || var.override_origin_bucket_policy ? 1 : 0
9798
bucket = local.bucket
9899
policy = data.template_file.default.rendered
99100
}
@@ -102,7 +103,7 @@ data "aws_region" "current" {
102103
}
103104

104105
resource "aws_s3_bucket" "origin" {
105-
count = signum(length(var.origin_bucket)) == 1 ? 0 : 1
106+
count = local.using_existing_origin ? 0 : 1
106107
bucket = module.origin_label.id
107108
acl = "private"
108109
tags = module.origin_label.tags
@@ -173,6 +174,8 @@ data "aws_s3_bucket" "selected" {
173174
}
174175

175176
locals {
177+
using_existing_origin = signum(length(var.origin_bucket)) == 1
178+
176179
bucket = join("",
177180
compact(
178181
concat([var.origin_bucket], concat([""], aws_s3_bucket.origin.*.id))

variables.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ variable "additional_bucket_policy" {
8181
description = "Additional policies for the bucket. If included in the policies, the variables `$${bucket_name}`, `$${origin_path}` and `$${cloudfront_origin_access_identity_iam_arn}` will be substituted. It is also possible to override the default policy statements by providing statements with `S3GetObjectForCloudFront` and `S3ListBucketForCloudFront` sid."
8282
}
8383

84+
variable "override_origin_bucket_policy" {
85+
type = bool
86+
default = true
87+
description = "When using an existing origin bucket (through var.origin_bucket), setting this to 'false' will make it so the existing bucket policy will not be overriden"
88+
}
89+
8490
variable "origin_bucket" {
8591
type = string
8692
default = ""
@@ -97,7 +103,7 @@ variable "origin_path" {
97103
variable "origin_force_destroy" {
98104
type = bool
99105
default = false
100-
description = "Delete all objects from the bucket so that the bucket can be destroyed without error (e.g. `true` or `false`)"
106+
description = "Delete all objects from the bucket so that the bucket can be destroyed without error (e.g. `true` or `false`)"
101107
}
102108

103109
variable "bucket_domain_format" {

0 commit comments

Comments
 (0)