Commit 86a50f52 authored by Anton Babenko's avatar Anton Babenko Committed by GitHub

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

parent caf45170
...@@ -17,6 +17,7 @@ These features of S3 bucket configurations are supported: ...@@ -17,6 +17,7 @@ These features of S3 bucket configurations are supported:
- server-side encryption - server-side encryption
- object locking - object locking
- Cross-Region Replication (CRR) - Cross-Region Replication (CRR)
- ELB log delivery bucket policy
## Terraform versions ## Terraform versions
...@@ -83,6 +84,7 @@ module "s3_bucket" { ...@@ -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 | | 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 | | 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\_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 | (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 | | 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 | | cors\_rule | Map containing a rule of Cross-Origin Resource Sharing. | any | `{}` | no |
......
...@@ -11,20 +11,29 @@ resource "aws_kms_key" "objects" { ...@@ -11,20 +11,29 @@ resource "aws_kms_key" "objects" {
deletion_window_in_days = 7 deletion_window_in_days = 7
} }
module "log_bucket" { resource "aws_iam_role" "this" {
source = "../../" assume_role_policy = <<EOF
{
bucket = "logs-${random_pet.this.id}" "Version": "2012-10-17",
acl = "log-delivery-write" "Statement": [
force_destroy = true {
attach_elb_log_delivery_policy = true "Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
} }
data "aws_iam_policy_document" "bucket_policy" { data "aws_iam_policy_document" "bucket_policy" {
statement { statement {
principals { principals {
type = "AWS" type = "AWS"
identifiers = ["*"] identifiers = [aws_iam_role.this.arn]
} }
actions = [ actions = [
...@@ -36,12 +45,24 @@ data "aws_iam_policy_document" "bucket_policy" { ...@@ -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" { module "s3_bucket" {
source = "../../" source = "../../"
bucket = local.bucket_name bucket = local.bucket_name
acl = "private" acl = "private"
force_destroy = true force_destroy = true
attach_policy = true
policy = data.aws_iam_policy_document.bucket_policy.json policy = data.aws_iam_policy_document.bucket_policy.json
tags = { tags = {
......
...@@ -218,7 +218,7 @@ resource "aws_s3_bucket" "this" { ...@@ -218,7 +218,7 @@ resource "aws_s3_bucket" "this" {
} }
resource "aws_s3_bucket_policy" "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 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 policy = var.attach_elb_log_delivery_policy ? data.aws_iam_policy_document.elb_log_delivery[0].json : var.policy
......
...@@ -10,6 +10,12 @@ variable "attach_elb_log_delivery_policy" { ...@@ -10,6 +10,12 @@ variable "attach_elb_log_delivery_policy" {
default = false 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" { variable "bucket" {
description = "(Optional, Forces new resource) The name of the bucket. If omitted, Terraform will assign a random, unique name." description = "(Optional, Forces new resource) The name of the bucket. If omitted, Terraform will assign a random, unique name."
type = string type = string
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment