我无法创建CloudWatch规则来使用Terraform创建SQS消息

时间:2019-06-20 17:33:57

标签: amazon-web-services terraform amazon-sqs amazon-cloudwatch

我正在尝试创建一个AWS CloudWatch规则,该规则每分钟执行一次,以在SQS中创建消息。当我使用下面的Terraform脚本时,一切看起来都可以找到,但是SQS my_queue没有收到任何消息。

使用Amazon控制台更新规则my_trigger时,消息将在SQS中创建。

provider "aws" {
  region = "eu-central-1"
}

resource "aws_sqs_queue" "this_sqs_queue" {
  name                        = "my_queue"
  fifo_queue                  = "false"
  content_based_deduplication = "false"
  visibility_timeout_seconds  = 30
  message_retention_seconds   = 345600
  max_message_size            = 262144
  receive_wait_time_seconds   = 0
  delay_seconds               = 0
}

resource "aws_cloudwatch_event_rule" "cloudwatch_event_rule" {
  name                = "my_trigger"
  schedule_expression = "rate(1 minute)"
}

resource "aws_cloudwatch_event_target" "cloudwatch_event_target" {
  rule       = "${aws_cloudwatch_event_rule.cloudwatch_event_rule.name}"
  arn        = "${aws_sqs_queue.this_sqs_queue.arn}"
}

我们非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您需要在SQS队列上创建策略。当您使用控制台创建/更新时,AWS会自动为您执行此操作。但是,在使用Terraform时,您需要明确地创建它。

resource "aws_sqs_queue_policy" "this_sqs_queue_policy" {
  queue_url = "${aws_sqs_queue.this_sqs_queue.id}"
  policy    = <<POLICY
{
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "events.amazonaws.com"
      },
      "Action": "sqs:SendMessage",
      "Resource": "${aws_sqs_queue.this_sqs_queue.arn}",
      "Condition": {
        "ArnEquals": {
          "aws:SourceArn": "${aws_cloudwatch_event_rule.cloudwatch_event_rule.arn}"
        }
      }
    }
  ]
}
POLICY
}