diff --git a/examples/terraform-aws-s3-example/main.tf b/examples/terraform-aws-s3-example/main.tf index d76f3a1bf..a68f56e63 100644 --- a/examples/terraform-aws-s3-example/main.tf +++ b/examples/terraform-aws-s3-example/main.tf @@ -18,6 +18,60 @@ terraform { # See test/terraform_aws_s3_example_test.go for how to write automated tests for this code. # --------------------------------------------------------------------------------------------------------------------- +# Deploy and configure test S3 bucket with versioning and access log +resource "aws_s3_bucket" "test_bucket" { + bucket = "${local.aws_account_id}-${var.tag_bucket_name}" + + tags = { + Name = var.tag_bucket_name + Environment = var.tag_bucket_environment + } +} + +resource "aws_s3_bucket_logging" "test_bucket" { + bucket = aws_s3_bucket.test_bucket.id + target_bucket = aws_s3_bucket.test_bucket_logs.id + target_prefix = "TFStateLogs/" +} + +resource "aws_s3_bucket_versioning" "test_bucket" { + bucket = aws_s3_bucket.test_bucket.id + versioning_configuration { + status = "Enabled" + } +} + +resource "aws_s3_bucket_acl" "test_bucket" { + bucket = aws_s3_bucket.test_bucket.id + acl = "private" +} + + +# Deploy S3 bucket to collect access logs for test bucket +resource "aws_s3_bucket" "test_bucket_logs" { + bucket = "${local.aws_account_id}-${var.tag_bucket_name}-logs" + + tags = { + Name = "${local.aws_account_id}-${var.tag_bucket_name}-logs" + Environment = var.tag_bucket_environment + } + + force_destroy = true +} + +resource "aws_s3_bucket_acl" "test_bucket_logs" { + bucket = aws_s3_bucket.test_bucket_logs.id + acl = "log-delivery-write" +} + +# Configure bucket access policies + +resource "aws_s3_bucket_policy" "bucket_access_policy" { + count = var.with_policy ? 1 : 0 + bucket = aws_s3_bucket.test_bucket.id + policy = data.aws_iam_policy_document.s3_bucket_policy.json +} + data "aws_iam_policy_document" "s3_bucket_policy" { statement { effect = "Allow" @@ -56,43 +110,6 @@ data "aws_iam_policy_document" "s3_bucket_policy" { } } -resource "aws_s3_bucket" "test_bucket_logs" { - bucket = "${local.aws_account_id}-${var.tag_bucket_name}-logs" - acl = "log-delivery-write" - - tags = { - Name = "${local.aws_account_id}-${var.tag_bucket_name}-logs" - Environment = var.tag_bucket_environment - } - - force_destroy = true -} - -resource "aws_s3_bucket" "test_bucket" { - bucket = "${local.aws_account_id}-${var.tag_bucket_name}" - acl = "private" - - versioning { - enabled = true - } - - logging { - target_bucket = aws_s3_bucket.test_bucket_logs.id - target_prefix = "TFStateLogs/" - } - - tags = { - Name = var.tag_bucket_name - Environment = var.tag_bucket_environment - } -} - -resource "aws_s3_bucket_policy" "bucket_access_policy" { - count = var.with_policy ? 1 : 0 - bucket = aws_s3_bucket.test_bucket.id - policy = data.aws_iam_policy_document.s3_bucket_policy.json -} - # --------------------------------------------------------------------------------------------------------------------- # LOCALS # Used to represent any data that requires complex expressions/interpolations diff --git a/examples/terraform-aws-s3-example/outputs.tf b/examples/terraform-aws-s3-example/outputs.tf index 21001977a..8a9719a4f 100644 --- a/examples/terraform-aws-s3-example/outputs.tf +++ b/examples/terraform-aws-s3-example/outputs.tf @@ -7,9 +7,9 @@ output "bucket_arn" { } output "logging_target_bucket" { - value = tolist(aws_s3_bucket.test_bucket.logging)[0].target_bucket + value = aws_s3_bucket_logging.test_bucket.target_bucket } output "logging_target_prefix" { - value = tolist(aws_s3_bucket.test_bucket.logging)[0].target_prefix -} \ No newline at end of file + value = aws_s3_bucket_logging.test_bucket.target_prefix +} diff --git a/modules/terraform/apply_test.go b/modules/terraform/apply_test.go index 2cabcfe8a..ce7f2f0ac 100644 --- a/modules/terraform/apply_test.go +++ b/modules/terraform/apply_test.go @@ -155,7 +155,8 @@ func TestIdempotentWithChanges(t *testing.T) { } func TestParallelism(t *testing.T) { - t.Parallel() + // This test depends on precise timing of the concurrent parallel calls in terraform, so we need to run this test + // serially by itself so that other concurrent test runs won't influence the timing. testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-parallelism", t.Name()) require.NoError(t, err)