以 SQS 队列为目标的 CloudWatch 事件无法正常工作

Svj*_*Man 6 amazon-sqs amazon-web-services aws-cloudformation amazon-cloudwatch

根据这篇文章,可以将SQS设置为预定CloudWatch事件的目标:

https://aws.amazon.com/ru/about-aws/whats-new/2016/03/cloudwatch-events-now-supports-amazon-sqs-queue-targets/

我创建了一个简单的Cloud Formation模板,旨在每分钟触发一次 CloudWatch事件,因此新消息应出现在SQS 中,但由于SQS中没有消息,因此缺少某些内容。

编码:

{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "stack 1",
"Parameters": {

},
"Resources": {
    "MyQueue": {
        "Type": "AWS::SQS::Queue",
        "Properties": {
            "QueueName": "MyQueue"
        }
    },
    "MyRole": {
        "Type": "AWS::IAM::Role",
        "Properties": {
            "RoleName": "MyRole",
            "AssumeRolePolicyDocument": {
                "Version": "2012-10-17",
                "Statement": [{
                    "Effect": "Allow",
                    "Principal": {
                        "Service": ["events.amazonaws.com", "lambda.amazonaws.com"]
                    },
                    "Action": "sts:AssumeRole"
                }]
            },
            "Path": "/",
            "Policies": [{
                "PolicyName": "CloudWatchPolicy",
                "PolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [{
                        "Effect": "Allow",
                        "Action": "*",
                        "Resource": "*"
                    }]
                }
            }]
        }
    },
    "MyRule": {
        "Type": "AWS::Events::Rule",
        "Properties": {
            "Description": "A rule to schedule data update",
            "Name": "MyRule",
            "ScheduleExpression": "rate(1 minute)",
            "State": "ENABLED",
            "RoleArn": {
                "Fn::GetAtt": ["MyRole",
                "Arn"]
            },
            "Targets": [{
                "Arn": {
                    "Fn::GetAtt": ["MyQueue",
                    "Arn"]
                },
                "Id": "MyRule"
            }]
        }
    }
},
"Outputs": {

}
Run Code Online (Sandbox Code Playgroud)

}

那里有什么问题?我应该添加一个队列侦听器来显示消息吗?

问题2:

关于CloudWatch 事件规则目标的文档声明Id是必填字段:

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-events-rule-target.html

虽然AWS::SQS::Queue根本没有这样的属性(只有 Name 存在):

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-sqs-queues.html#aws-properties-sqs-queues-prop

将SQS 用作目标时,应向 CloudWatch Event Rule Target Id 属性添加什么内容?

提前谢谢了。

Svj*_*Man 10

我的模板中缺少的部分是AWS::SQS::QueuePolicy

工作模板:

    {
     "AWSTemplateFormatVersion": "2010-09-09",
     "Description": "stack 1",
     "Parameters": {},
     "Resources": {
        "MyPolicy": {
            "Type": "AWS::IAM::Policy",
            "Properties": {
                "PolicyDocument": {
                    "Statement": [{
                        "Action": "sqs:*",
                        "Effect": "Allow",
                        "Resource": {
                            "Fn::GetAtt": ["MyQueue",
                            "Arn"]
                        }
                    }],
                    "Version": "2012-10-17"
                },
                "PolicyName": "MyPolicyName",
                "Roles": [{
                    "Ref": "MyRole"
                }]
            }
        },
        "MyRole": {
            "Type": "AWS::IAM::Role",
            "Properties": {
                "AssumeRolePolicyDocument": {
                    "Statement": [{
                        "Action": "sts:AssumeRole",
                        "Effect": "Allow",
                        "Principal": {
                            "Service": ["events.amazonaws.com",
                            "sqs.amazonaws.com"]
                        }
                    }],
                    "Version": "2012-10-17"
                }
            }
        },
        "MyQueue": {
            "Type": "AWS::SQS::Queue",
            "Properties": {
                "QueueName": "MyQueue2"
            }
        },
        "MyRule": {
            "Type": "AWS::Events::Rule",
            "Properties": {
                "Description": "A rule to schedule data update",
                "Name": "MyRule",
                "ScheduleExpression": "rate(1 minute)",
                "State": "ENABLED",
                "RoleArn": {
                    "Fn::GetAtt": ["MyRole",
                    "Arn"]
                },
                "Targets": [{
                    "Arn": {
                        "Fn::GetAtt": ["MyQueue",
                        "Arn"]
                    },
                    "Id": "MyRule1",
                    "Input": "{\"a\":\"b\"}"
                }]
            }
        },
        "MyQueuePolicy": {
            "DependsOn": ["MyQueue", "MyRule"],
            "Type": "AWS::SQS::QueuePolicy",
            "Properties": {
                "PolicyDocument": {
                    "Version": "2012-10-17",
                    "Id": "MyQueuePolicy",
                    "Statement": [{                     
                        "Effect": "Allow",
                        "Principal": {
                            "Service": ["events.amazonaws.com",
                            "sqs.amazonaws.com"]
                        },
                        "Action": "sqs:SendMessage",
                        "Resource": {
                            "Fn::GetAtt": ["MyQueue",
                            "Arn"]
                        }
                    }]
                },
                "Queues": [{
                    "Ref": "MyQueue"
                }]
            }
        }
    },
    "Outputs": {        
    }
}
Run Code Online (Sandbox Code Playgroud)