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

aws_sqs_queue_policy - fails on first apply, succeeds on second apply with no changes #13980

Closed
TomTucka opened this issue Jun 29, 2020 · 16 comments · Fixed by #19639
Closed
Labels
bug Addresses a defect in current functionality. service/sqs Issues and PRs that pertain to the sqs service.
Milestone

Comments

@TomTucka
Copy link
Contributor

TomTucka commented Jun 29, 2020

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Terraform Version

0.12.28

Affected Resource(s)

  • aws_sqs_queue_policy

Terraform Configuration Files

resource "aws_sqs_queue_policy" "deadletter_queue" {
  queue_url = aws_sqs_queue.deadletter_queue.id
  policy    = data.aws_iam_policy_document.deadletter_queue.json
}

data "aws_iam_policy_document" "deadletter_queue" {
  statement {
    effect    = "Allow"
    resources = [aws_sqs_queue.deadletter_queue.arn]
    actions = [
      "sqs:ChangeMessageVisibility",
      "sqs:DeleteMessage",
      "sqs:GetQueueAttributes",
      "sqs:GetQueueUrl",
      "sqs:ListQueueTags",
      "sqs:ReceiveMessage",
      "sqs:SendMessage",
    ]
    principals {
      type        = "AWS"
      identifiers = [var.allowed_arn]
    }
  }

Debug Output

Error: Error updating SQS attributes: InvalidAttributeValue: Invalid value for the parameter Policy.
	status code: 400, request id: 30b6f60c-a403-5af4-8fdb-a178a4dda18f

  on modules/sqs/main.tf line 54, in resource "aws_sqs_queue_policy" "deadletter_queue":
  54: resource "aws_sqs_queue_policy" "deadletter_queue" {

Expected Behavior

Terraform should create the resource on the first apply

Actual Behavior

Terraform fails with the error above, on a re-run with no changes the apply succeeds.

Steps to Reproduce

  1. terraform apply
  2. terraform fails
  3. terraform apply
  4. terraform succeeds

Important Factoids

Happens on both CI and local machine

@ghost ghost added service/iam Issues and PRs that pertain to the iam service. service/sqs Issues and PRs that pertain to the sqs service. labels Jun 29, 2020
@github-actions github-actions bot added the needs-triage Waiting for first response or review from a maintainer. label Jun 29, 2020
@breathingdust breathingdust added bug Addresses a defect in current functionality. and removed needs-triage Waiting for first response or review from a maintainer. labels Jul 7, 2020
@laurenty
Copy link

laurenty commented Nov 10, 2020

I am running into the same issue. I even set explicit dependencies between the aws_iam_policy_document and the resource that the policy refers to and then between the aws_sqs_queue_policy and aws_iam_policy_document. Still got the error.
Terraform 0.13.5
AWS Provider hashicorp/aws v2.70.0

@laurenty
Copy link

It looks like this is a timing issue. After reading the docs on SQS SetQueueAttributes, I did notice that it mentions a potential 60s propagation delay so I added a 60s time_sleep resource between the aws_sqs_queue and aws_sqs_queue_policy resources and the apply was successful on the first attempt.
https://registry.terraform.io/providers/hashicorp/time/latest/docs/resources/sleep
https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_SetQueueAttributes.html

@natanlao
Copy link

I can reproduce this:

(.venv) ~/u/c/terraform develop> terraform -version
Terraform v0.14.0
+ provider registry.terraform.io/hashicorp/archive v2.0.0
+ provider registry.terraform.io/hashicorp/aws v3.20.0
+ provider registry.terraform.io/hashicorp/local v2.0.0

Your version of Terraform is out of date! The latest version
is 0.14.2. You can update by downloading from https://www.terraform.io/downloads.html

@rexxavier
Copy link

rexxavier commented Dec 11, 2020 via email

@laurenty
Copy link

@rexxavier could you elaborate? thank you

@amartopoulos
Copy link

I also ran into this exact problem just now. After the second "successful" run (no changes, according to Terraform), I checked out the queue in the console. It has the requested policy, so this is more of an annoyance than anything else. Still, would be nice if that 60 second sleep could be integrated into the process.

@natanlao
Copy link

Forgot to add here that @laurenty's suggested fix worked for me.

@SalasSanchez
Copy link

Is it resolved in TF 14, @rexxavier? What do you mean?

@BhushanGitHub
Copy link

This seems to be still an issue. I am facing the same problem even with a 0.14.4

@laurenty
Copy link

This seems to be still an issue. I am facing the same problem even with a 0.14.4

If this were to be fixed, it would be in the AWS provider. Not in TF itself.

@SalasSanchez
Copy link

This seems to be still an issue. I am facing the same problem even with a 0.14.4

If this were to be fixed, it would be in the AWS provider. Not in TF itself.

I think the point here is that there is a TF workaround for this timing issue, and the suggestion was made above that TF14 was going to have some such workaround built in, instead of requiring users to add a sleep time.

It appears that this is not the case, so I'll stick to my time_sleep.

@floresfactor
Copy link

same problem.
Terraform 0.13.2

@cesarpball
Copy link

Same problem Terraform 0.13.5

@ewbankkit ewbankkit removed the service/iam Issues and PRs that pertain to the iam service. label Jun 7, 2021
@ewbankkit
Copy link
Contributor

In #19639 I have modified the aws_sqs_queue resource so that on Create and Update we wait for up to 1 minute to ensure that the queue attributes completely propagate.
This should fix the issue.

@github-actions github-actions bot added this to the v3.45.0 milestone Jun 7, 2021
@github-actions
Copy link

This functionality has been released in v3.45.0 of the Terraform AWS Provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

chrisclayson added a commit to govuk-one-login/authentication-api that referenced this issue Jun 29, 2021
- Remove SQS module as this introduced a cyclic dependency
- Add explicit `depends_on` to resources
- Move IAM role creation to outside endpoint module, creating them inside then needing to refer to them during SQS creation added to cyclic dependency.
- Remove API Gateway root module, adds to dependencies between modules.
- Move API Gateway deployment and stage outside of endpoint module, these only have to be applied once.
- Add a sleep into the SQS deployment to allow propagation of queue config before applying policy (as discussed here: hashicorp/terraform-provider-aws#13980)
@github-actions
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Addresses a defect in current functionality. service/sqs Issues and PRs that pertain to the sqs service.
Projects
None yet
Development

Successfully merging a pull request may close this issue.