AWS CloudFormation テンプレートを使用して Amazon SQS キューにメッセージを送信するトピックを作成する - Amazon Simple Notification Service

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

AWS CloudFormation テンプレートを使用して Amazon SQS キューにメッセージを送信するトピックを作成する

AWS CloudFormation では、テンプレートファイルを使用して、AWS リソースの集合を単一のユニットとして作成/制御することができます。このセクションでは、キューに発行するトピックのデプロイを容易にするサンプルテンプレートを使用します。このテンプレートは、2 つのキューの作成、キューにサブスクリプションを持つトピックの作成、トピックがキューにメッセージを送信できるようにするポリシーのキューへの追加、これらのリソースへのアクセスを制御する IAM ユーザーとグループの作成を行って、セットアップ手順を代行します。

AWS CloudFormation テンプレートを使用した AWS リソースのデプロイの詳細については、『AWS CloudFormation ユーザーガイド』の「開始方法」を参照してください。

AWS CloudFormation テンプレートを使用して AWS アカウント 内のトピックとキューをセットアップする

このサンプルテンプレートでは、1 つは IAM グループのメンバーがトピックに発行するため、もう 1 つはキューからのメッセージを読み取るために、それぞれ適切なアクセス権限を付与された 2 つの Amazon SQS キューにメッセージを送信できる Amazon SNS トピックを作成します。このテンプレートは、各グループに追加される IAM ユーザーも作成します。

テンプレートの内容をファイルにコピーします。[AWS CloudFormation テンプレート] ページからテンプレートをダウンロードすることもできます。テンプレートページで [Browse sample templates by AWS service ] 、[Amazon Simple Queue Service] の順に選択します。

MySNSTopic は 2 つのサブスクライブされたエンドポイント (MyQueue1 と MyQueue2 という 2 つの Amazon SQS キュー) に発行するようにセットアップされます。MyPublishTopicGroup は、メンバーが API アクションの [Publish] または [sns-publish] コマンドを使用して MySNSTopic に発行する許可を持つ IAM グループです。テンプレートは、MyPublishUser と MyQueueUser という IAM ユーザーを作成し、このユーザーにログインプロファイルとアクセスキーを付与します。このテンプレートを使用してスタックを作成するユーザーは、入力パラメータとしてログインプロファイル用のパスワードを指定します。テンプレートは、2 人の IAM ユーザー用に MyPublishUserKey と MyQueueUserKey というアクセスキーを作成します。AddUserToMyPublishTopicGroup は MyPublishTopicGroup に MyPublishUser を追加して、グループに割り当てられたアクセス権限がユーザーに付与されるようにします。

MyRDMessageQueueGroup はメンバーが ReceiveMessage および DeleteMessage API アクションを使用して、2 つの Amazon SQS キューからメッセージを読み取り/削除するためのアクセス権限を持つIAM グループです。AddUserToMyQueueGroup は MyQueueUser を MyRDMessageQueueGroup に追加して、グループに割り当てられたアクセス権限がユーザーに付与されるようにします。MyQueuePolicy は、MySNSTopic が 2 つのキューに通知を発行する許可を割り当てます。

以下のリストは AWS CloudFormation テンプレートの内容を示しています。

{ "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"] } ] ] } } } }