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 unable to use arn from aws_iam_role immediately after creation, does not handle retries. #21355

Closed
BrandonALXEllisSS opened this issue Oct 18, 2021 · 3 comments
Labels
bug Addresses a defect in current functionality. service/iam Issues and PRs that pertain to the iam service. service/sqs Issues and PRs that pertain to the sqs service. stale Old or inactive issues managed by automation, if no further action taken these will get closed.

Comments

@BrandonALXEllisSS
Copy link

BrandonALXEllisSS commented Oct 18, 2021

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 CLI and Terraform AWS Provider Version

Terraform v1.0.3
on linux_amd64
+ provider registry.terraform.io/hashicorp/aws v3.63.0
+ provider registry.terraform.io/hashicorp/time v0.7.2

Affected Resource(s)

  • aws_sqs_queue_policy resource

Terraform Configuration Files

This fails:

provider "aws" {
  region = "us-east-1"
}

#IAM Role for ECS Tasks
resource "aws_iam_role" "ecs_task_role" {
  name               = "ecs-iam-role"
  assume_role_policy = <<EOF
{
    "Version": "2012-10-17",
    "Statement": [
        {
        "Action": "sts:AssumeRole",
        "Principal": {
            "Service": [
                "ecs-tasks.amazonaws.com",
                "ecs.amazonaws.com"
            ]
        },
        "Effect": "Allow",
        "Sid": ""
        }
    ]
}   
EOF
}

resource "aws_sqs_queue" "queue" {}

#Permissions for SQS Queue
resource "aws_sqs_queue_policy" "queue_policy" {
  queue_url = aws_sqs_queue.queue.id
  policy    = <<EOF
{
    "Version": "2012-10-17",
    "Id": "default_policy_ID",
    "Statement": [
        {
        "Sid": "owner_statement",
        "Effect": "Allow",
        "Principal": {
            "AWS": "${aws_iam_role.ecs_task_role.arn}"
        },
        "Action": "SQS:*",
        "Resource": "${aws_sqs_queue.queue.arn}"
        }
    ]
}
EOF
}

This succeeds:

provider "aws" {
  region = "us-east-1"
}

#IAM Role for ECS Tasks
resource "aws_iam_role" "ecs_task_role" {
  name               = "ecs-iam-role"
  assume_role_policy = <<EOF
{
    "Version": "2012-10-17",
    "Statement": [
        {
        "Action": "sts:AssumeRole",
        "Principal": {
            "Service": [
                "ecs-tasks.amazonaws.com",
                "ecs.amazonaws.com"
            ]
        },
        "Effect": "Allow",
        "Sid": ""
        }
    ]
}   
EOF
}

resource "time_sleep" "wait_60_seconds" {
  depends_on = [aws_iam_role.ecs_task_role]

  create_duration = "60s"
}

resource "aws_sqs_queue" "queue" {}

#Permissions for SQS Queue
resource "aws_sqs_queue_policy" "queue_policy" {
  depends_on = [
    time_sleep.wait_60_seconds
  ]
  queue_url = aws_sqs_queue.queue.id
  policy    = <<EOF
{
    "Version": "2012-10-17",
    "Id": "default_policy_ID",
    "Statement": [
        {
        "Sid": "owner_statement",
        "Effect": "Allow",
        "Principal": {
            "AWS": "${aws_iam_role.ecs_task_role.arn}"
        },
        "Action": "SQS:*",
        "Resource": "${aws_sqs_queue.queue.arn}"
        }
    ]
}
EOF
}

Debug Output

https://gist.github.com/BrandonALXEllisSS/7273c3bdaf4767278de52184a1d49f3a

Expected Behavior

Queue policy is applied successfully with no issues.

Actual Behavior

Errors with output:

 Error: error setting SQS Queue Policy (https://sqs.us-east-1.amazonaws.com/XXXXXXXXXXXX/terraform-20211018205857785300000002): InvalidAttributeValue: Invalid value for the parameter Policy.

Steps to Reproduce

  1. terraform apply -auto-approve

Important Factoids

Using us-east-1

References

@github-actions github-actions bot added needs-triage Waiting for first response or review from a maintainer. service/iam Issues and PRs that pertain to the iam service. service/sqs Issues and PRs that pertain to the sqs service. labels Oct 18, 2021
@ewbankkit ewbankkit added bug Addresses a defect in current functionality. and removed needs-triage Waiting for first response or review from a maintainer. labels Oct 19, 2021
@gdavison
Copy link
Contributor

Investigation notes: The AWS API sometimes returns an ID instead of a correct ARN when initially creating the IAM role. Role creation should wait until arn returns an ARN. The acceptance tests for aws_iam_role should also check that a valid ARN is returned.

Also appears to be related to #21716

Copy link

Marking this issue as stale due to inactivity. This helps our maintainers find and focus on the active issues. If this issue receives no comments in the next 30 days it will automatically be closed. Maintainers can also remove the stale label.

If this issue was automatically closed and you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thank you!

@github-actions github-actions bot added the stale Old or inactive issues managed by automation, if no further action taken these will get closed. label Nov 20, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 21, 2023
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 Jan 21, 2024
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/iam Issues and PRs that pertain to the iam service. service/sqs Issues and PRs that pertain to the sqs service. stale Old or inactive issues managed by automation, if no further action taken these will get closed.
Projects
None yet
Development

No branches or pull requests

3 participants