Utilizzo di un modello di AWS CloudFormation per creare un argomento che invia messaggi alle code Amazon SQS - Amazon Simple Notification Service

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Utilizzo di un modello di AWS CloudFormation per creare un argomento che invia messaggi alle code Amazon SQS

AWS CloudFormation consente di utilizzare un file di modello per creare e configurare una raccolta di risorse AWS come una singola unità. Questa sezione include un modello di esempio in grado di semplificare la distribuzione di argomenti che effettuano pubblicazioni nelle code. I modelli eseguono automaticamente la procedura di configurazione creando due code e un argomento con sottoscrizioni alle code, aggiungendo una policy alle code affinché l'argomento possa inviare messaggi alle code e creando utenti e gruppi IAM per controllare l'accesso a tali risorse.

Per ulteriori informazioni sulla distribuzione delle risorse AWS mediante un modello AWS CloudFormation, consulta Operazioni di base nella AWS CloudFormation Guida per l'utente.

Utilizzo di un modello di AWS CloudFormation per configurare argomenti e code in un Account AWS

Il modello di esempio crea un argomento Amazon SNS in grado di inviare messaggi a due code Amazon SQS con autorizzazioni appropriate per consentire ai membri di un gruppo IAM di pubblicare nell'argomento e a un altro gruppo di leggere messaggi dalle code. Il modello crea inoltre utenti IAM che vengono aggiunti a ogni gruppo.

Copiare il contenuto del modello in un file. È inoltre possibile scaricare il modello dalla AWS pagina Modelli di CloudFormation. Nella pagina dei modelli scegliere Sfoglia i modelli di esempio AWS servizio e quindi scegliere Amazon Simple Queue Service.

MySNSTopic è configurato per eseguire pubblicazioni in due endpoint con sottoscrizione, ovvero due code Amazon SQS (MyQueue1 e MyQueue2). MyPublishTopicGroup è un gruppo IAM i cui membri sono autorizzati a pubblicare in MySNSTopic utilizzando l'operazione API Publish o il comando sns-publish. Il modello crea gli utenti IAM MyPublishUser e MyQueueUser e fornisce loro profili e chiavi di accesso. L'utente che crea uno stack con questo modello specifica le password per i profili di accesso come parametri di input. Il modello crea chiavi di accesso per i due utenti IAM con MyPublishUserKey e MyQueueUserKey. AddUserToMyPublishTopicGroup aggiunge MyPublishUser a MyPublishTopicGroup di modo che l'utente disponga delle autorizzazioni assegnate al gruppo.

MyRDMessageQueueGroup è un gruppo IAM i cui membri sono autorizzati a leggere ed eliminare messaggi dalle due code Amazon SQS utilizzando le azioni API ReceiveMessage e DeleteMessage. AddUserToMyQueueGroup aggiunge MyQueueUser a MyRDMessageQueueGroup di modo che l'utente disponga delle autorizzazioni assegnate al gruppo. MyQueuePolicy autorizza MySNSTopic a pubblicare le relative notifiche nelle due code.

L'elenco seguente mostra il contenuto del modello 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"] } ] ] } } } }