针对SQS队列的CloudWatch事件无法正常工作 [英] CloudWatch Event that targets SQS Queue fails to work

查看:103
本文介绍了针对SQS队列的CloudWatch事件无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

According to this article it's possible to set SQS as target for scheduled CloudWatch event:

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

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

I've created a simple Cloud Formation template that aims to trigger CloudWatch event each minute so the new message should appear in SQS, but something is missing as there are no messages in 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": {

}

}

那里可能有什么问题?我应该添加队列侦听器以使消息出现吗?

What can be wrong there? Should I add a queue listener to make messages appear?

问题2:

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

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

尽管 AWS :: SQS :: Queue 根本没有这样的属性(仅存在名称):

Though AWS::SQS::Queue has no such property at all (only Name is present):

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

在SQ时应将 CloudWatch Event Rule Target 属性设置为Id

What should be put to CloudWatch Event Rule Target Id property when SQS is used as a target?

非常感谢。

推荐答案

模板中缺少的部分是 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": {        
    }
}

这篇关于针对SQS队列的CloudWatch事件无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆