Terraform 1.4.2: advanced patterns with `time_sleep` Resource in Module Dependencies
This might be a silly question, but I'm working with an scenario with the `time_sleep` resource in Terraform 1.4.2, which I am using to create a delay between the provisioning of two dependent resources in my module. My intention is to ensure that an S3 bucket is fully created and available before starting the next task, which is configuring an IAM policy. However, I am working with the following behavior during the apply phase: ``` behavior: behavior applying plan: 1 behavior occurred: * module.my_module.aws_iam_policy.example: resource 'aws_iam_policy' "example" has a question: InvalidParameter: The specified policy document is not well-formed JSON. ``` I've set up the `time_sleep` resource like this in my module: ```hcl resource "time_sleep" "wait_for_bucket" { depends_on = [aws_s3_bucket.my_bucket] create_duration = "10s" } ``` And I'm using it in the IAM policy resource like this: ```hcl resource "aws_iam_policy" "example" { depends_on = [time_sleep.wait_for_bucket] name = "example_policy" description = "An example policy" policy = jsonencode({ Version = "2012-10-17" Statement = [ { Action = ["s3:ListBucket"], Effect = "Allow", Resource = aws_s3_bucket.my_bucket.arn, }, ] }) } ``` I confirmed that the S3 bucket is being created successfully before the IAM policy runs. However, the behavior suggests that my IAM policy document might not be valid JSON. I’ve tried simplifying the policy document to just return an empty object, but the same behavior continues. It seems like the `time_sleep` resource might not be providing enough time for the S3 bucket to become fully available before the policy resource kicks in. I've checked the AWS Console, and the bucket exists, but I'm not sure if there are background processes that might still be finalizing the creation. Could there be a better approach to handle this dependency or a way to ensure that the S3 bucket is fully ready before proceeding with the IAM policy? Any insights or best practices would be greatly appreciated. For context: I'm using Hcl on Ubuntu. Is there a better approach? I'm coming from a different tech stack and learning Hcl.