Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for bucket policy count when value is not computed #12

Merged
merged 1 commit into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ These features of S3 bucket configurations are supported:
- server-side encryption
- object locking
- Cross-Region Replication (CRR)
- ELB log delivery bucket policy

## Terraform versions

Expand Down Expand Up @@ -83,6 +84,7 @@ module "s3_bucket" {
| acceleration\_status | (Optional) Sets the accelerate configuration of an existing bucket. Can be Enabled or Suspended. | string | `"null"` | no |
| acl | (Optional) The canned ACL to apply. Defaults to 'private'. | string | `"private"` | no |
| attach\_elb\_log\_delivery\_policy | Controls if S3 bucket should have ELB log delivery policy attached | bool | `"false"` | no |
| attach\_policy | Controls if S3 bucket should have bucket policy attached (set to `true` to use value of `policy` as bucket policy) | bool | `"false"` | no |
| bucket | (Optional, Forces new resource) The name of the bucket. If omitted, Terraform will assign a random, unique name. | string | `"null"` | no |
| bucket\_prefix | (Optional, Forces new resource) Creates a unique bucket name beginning with the specified prefix. Conflicts with bucket. | string | `"null"` | no |
| cors\_rule | Map containing a rule of Cross-Origin Resource Sharing. | any | `{}` | no |
Expand Down
37 changes: 29 additions & 8 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,29 @@ resource "aws_kms_key" "objects" {
deletion_window_in_days = 7
}

module "log_bucket" {
source = "../../"

bucket = "logs-${random_pet.this.id}"
acl = "log-delivery-write"
force_destroy = true
attach_elb_log_delivery_policy = true
resource "aws_iam_role" "this" {
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}

data "aws_iam_policy_document" "bucket_policy" {
statement {
principals {
type = "AWS"
identifiers = ["*"]
identifiers = [aws_iam_role.this.arn]
}

actions = [
Expand All @@ -36,12 +45,24 @@ data "aws_iam_policy_document" "bucket_policy" {
]
}
}

module "log_bucket" {
source = "../../"

bucket = "logs-${random_pet.this.id}"
acl = "log-delivery-write"
force_destroy = true
attach_elb_log_delivery_policy = true
}

module "s3_bucket" {
source = "../../"

bucket = local.bucket_name
acl = "private"
force_destroy = true

attach_policy = true
policy = data.aws_iam_policy_document.bucket_policy.json

tags = {
Expand Down
2 changes: 1 addition & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ resource "aws_s3_bucket" "this" {
}

resource "aws_s3_bucket_policy" "this" {
count = var.create_bucket && (var.attach_elb_log_delivery_policy || var.policy != null) ? 1 : 0
count = var.create_bucket && (var.attach_elb_log_delivery_policy || var.attach_policy) ? 1 : 0

bucket = aws_s3_bucket.this[0].id
policy = var.attach_elb_log_delivery_policy ? data.aws_iam_policy_document.elb_log_delivery[0].json : var.policy
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ variable "attach_elb_log_delivery_policy" {
default = false
}

variable "attach_policy" {
description = "Controls if S3 bucket should have bucket policy attached (set to `true` to use value of `policy` as bucket policy)"
type = bool
default = false
}

variable "bucket" {
description = "(Optional, Forces new resource) The name of the bucket. If omitted, Terraform will assign a random, unique name."
type = string
Expand Down