Otomatiskan pesan Amazon SNS ke Amazon SQS dengan AWS CloudFormation - Amazon Simple Notification Service

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Otomatiskan pesan Amazon SNS ke Amazon SQS dengan AWS CloudFormation

AWS CloudFormation memungkinkan Anda untuk menggunakan file template untuk membuat dan mengkonfigurasi kumpulan AWS sumber daya bersama-sama sebagai satu unit. Bagian ini memiliki contoh templat yang membuatnya mudah untuk menyebarkan topik yang mempublikasikan ke antrean. Templat mengurus langkah-langkah pengaturan untuk Anda dengan membuat dua antrean, membuat topik dengan langganan antrean, menambahkan kebijakan ke antrean sehingga topik dapat mengirim pesan ke antrean, dan membuat pengguna dan grup IAM untuk mengontrol akses ke sumber daya tersebut.

Untuk informasi selengkapnya tentang penerapan AWS sumber daya menggunakan AWS CloudFormation templat, lihat Memulai di Panduan AWS CloudFormation Pengguna.

Menggunakan AWS CloudFormation template untuk mengatur topik dan antrian dalam Akun AWS

Templat contoh membuat topik Amazon SNS yang dapat mengirim pesan ke dua antrean Amazon SQS dengan izin yang sesuai bagi anggota satu grup IAM untuk memublikasikan ke topik dan yang lain untuk membaca pesan dari antrean. Templat juga menciptakan pengguna IAM yang ditambahkan ke setiap kelompok.

Anda menyalin isi templat ke dalam file. Anda juga dapat mengunduh template dari halaman AWS CloudFormation Template. Pada halaman template, pilih Browse sample templates by AWS service lalu pilih Amazon Simple Queue Service.

Saya SNSTopic diatur untuk mempublikasikan ke dua titik akhir berlangganan, yang merupakan dua antrian Amazon SQS (1 dan MyQueue 2). MyQueue MyPublishTopicGroup adalah grup IAM yang anggotanya memiliki izin untuk mempublikasikan ke My SNSTopic menggunakan tindakan Publish API atau perintah sns-publish. Template menciptakan pengguna IAM MyPublishUser dan MyQueueUser dan memberi mereka profil login dan kunci akses. Pengguna yang membuat tumpukan dengan templat ini menentukan password untuk profil login sebagai parameter input. Template membuat kunci akses untuk dua pengguna IAM dengan MyPublishUserKey dan MyQueueUserKey. AddUserToMyPublishTopicGroup MyPublishUser menambah MyPublishTopicGroup sehingga pengguna akan memiliki izin yang ditetapkan ke grup.

Saya RDMessage QueueGroup adalah grup IAM yang anggotanya memiliki izin untuk membaca dan menghapus pesan dari dua antrian Amazon SQS menggunakan ReceiveMessagetindakan dan API. DeleteMessage AddUserToMyQueueGroup menambahkan MyQueueUser ke My RDMessage QueueGroup sehingga pengguna akan memiliki izin yang ditetapkan ke grup. MyQueuePolicy memberikan izin SNSTopic kepada My untuk mempublikasikan pemberitahuannya ke dua antrian.

Daftar berikut menunjukkan isi AWS CloudFormation template.

{ "AWSTemplateFormatVersion" : "2010-09-09", "Description" : "AWS CloudFormation Sample Template SNSToSQS: This Template creates an SNS topic that can send messages to two SQS queues with appropriate permissions for one IAM user to publish to the topic and another to read messages from the queues. MySNSTopic is set up to publish to two subscribed endpoints, which are two SQS queues (MyQueue1 and MyQueue2). MyPublishUser is an IAM user that can publish to MySNSTopic using the Publish API. MyTopicPolicy assigns that permission to MyPublishUser. MyQueueUser is an IAM user that can read messages from the two SQS queues. MyQueuePolicy assigns those permissions to MyQueueUser. It also assigns permission for MySNSTopic to publish its notifications to the two queues. The template creates access keys for the two IAM users with MyPublishUserKey and MyQueueUserKey. ***Warning*** you will be billed for the AWS resources used if you create a stack from this template.", "Parameters": { "MyPublishUserPassword": { "NoEcho": "true", "Type": "String", "Description": "Password for the IAM user MyPublishUser", "MinLength": "1", "MaxLength": "41", "AllowedPattern": "[a-zA-Z0-9]*", "ConstraintDescription": "must contain only alphanumeric characters." }, "MyQueueUserPassword": { "NoEcho": "true", "Type": "String", "Description": "Password for the IAM user MyQueueUser", "MinLength": "1", "MaxLength": "41", "AllowedPattern": "[a-zA-Z0-9]*", "ConstraintDescription": "must contain only alphanumeric characters." } }, "Resources": { "MySNSTopic": { "Type": "AWS::SNS::Topic", "Properties": { "Subscription": [{ "Endpoint": { "Fn::GetAtt": ["MyQueue1", "Arn"] }, "Protocol": "sqs" }, { "Endpoint": { "Fn::GetAtt": ["MyQueue2", "Arn"] }, "Protocol": "sqs" } ] } }, "MyQueue1": { "Type": "AWS::SQS::Queue" }, "MyQueue2": { "Type": "AWS::SQS::Queue" }, "MyPublishUser": { "Type": "AWS::IAM::User", "Properties": { "LoginProfile": { "Password": { "Ref": "MyPublishUserPassword" } } } }, "MyPublishUserKey": { "Type": "AWS::IAM::AccessKey", "Properties": { "UserName": { "Ref": "MyPublishUser" } } }, "MyPublishTopicGroup": { "Type": "AWS::IAM::Group", "Properties": { "Policies": [{ "PolicyName": "MyTopicGroupPolicy", "PolicyDocument": { "Statement": [{ "Effect": "Allow", "Action": [ "sns:Publish" ], "Resource": { "Ref": "MySNSTopic" } }] } }] } }, "AddUserToMyPublishTopicGroup": { "Type": "AWS::IAM::UserToGroupAddition", "Properties": { "GroupName": { "Ref": "MyPublishTopicGroup" }, "Users": [{ "Ref": "MyPublishUser" }] } }, "MyQueueUser": { "Type": "AWS::IAM::User", "Properties": { "LoginProfile": { "Password": { "Ref": "MyQueueUserPassword" } } } }, "MyQueueUserKey": { "Type": "AWS::IAM::AccessKey", "Properties": { "UserName": { "Ref": "MyQueueUser" } } }, "MyRDMessageQueueGroup": { "Type": "AWS::IAM::Group", "Properties": { "Policies": [{ "PolicyName": "MyQueueGroupPolicy", "PolicyDocument": { "Statement": [{ "Effect": "Allow", "Action": [ "sqs:DeleteMessage", "sqs:ReceiveMessage" ], "Resource": [{ "Fn::GetAtt": ["MyQueue1", "Arn"] }, { "Fn::GetAtt": ["MyQueue2", "Arn"] } ] }] } }] } }, "AddUserToMyQueueGroup": { "Type": "AWS::IAM::UserToGroupAddition", "Properties": { "GroupName": { "Ref": "MyRDMessageQueueGroup" }, "Users": [{ "Ref": "MyQueueUser" }] } }, "MyQueuePolicy": { "Type": "AWS::SQS::QueuePolicy", "Properties": { "PolicyDocument": { "Statement": [{ "Effect": "Allow", "Principal": { "Service": "sns.amazonaws.com" }, "Action": ["sqs:SendMessage"], "Resource": "*", "Condition": { "ArnEquals": { "aws:SourceArn": { "Ref": "MySNSTopic" } } } }] }, "Queues": [{ "Ref": "MyQueue1" }, { "Ref": "MyQueue2" }] } } }, "Outputs": { "MySNSTopicTopicARN": { "Value": { "Ref": "MySNSTopic" } }, "MyQueue1Info": { "Value": { "Fn::Join": [ " ", [ "ARN:", { "Fn::GetAtt": ["MyQueue1", "Arn"] }, "URL:", { "Ref": "MyQueue1" } ] ] } }, "MyQueue2Info": { "Value": { "Fn::Join": [ " ", [ "ARN:", { "Fn::GetAtt": ["MyQueue2", "Arn"] }, "URL:", { "Ref": "MyQueue2" } ] ] } }, "MyPublishUserInfo": { "Value": { "Fn::Join": [ " ", [ "ARN:", { "Fn::GetAtt": ["MyPublishUser", "Arn"] }, "Access Key:", { "Ref": "MyPublishUserKey" }, "Secret Key:", { "Fn::GetAtt": ["MyPublishUserKey", "SecretAccessKey"] } ] ] } }, "MyQueueUserInfo": { "Value": { "Fn::Join": [ " ", [ "ARN:", { "Fn::GetAtt": ["MyQueueUser", "Arn"] }, "Access Key:", { "Ref": "MyQueueUserKey" }, "Secret Key:", { "Fn::GetAtt": ["MyQueueUserKey", "SecretAccessKey"] } ] ] } } } }