Create a SQS and subscribe to SNS | create a SNS in AWS and subscribe a queue (SQS)

Stack to Create a SQS and subscribe to SNS | create a SNS in AWS and subscribe a queue (SQS)

Cloudformation: Template for create a SNS in AWS and subscribe a queue (SQS)

      

      Description: Create an SNS
      Parameters:
        Stage:
          Description: Environment Name
          Type: String
        CrossAccountExternalAccountId:
          Type: String
          Description: Account Id
        StandardTopicName:
          Type: String
          Description: Queue Name

      Resources:
        SnsTopicRef:
          Type: AWS::SNS::Topic
          Properties:
            TopicName: !Join [ "-", [!Ref StandardTopicName] ]
            DisplayName: !Join [ "-", [!Ref StandardTopicName] ]
            Tags:
              - 
                Key: "environment"
                Value: !Ref Stage
        SnsTopicRefPolicy:
          Type: AWS::SNS::TopicPolicy
          Properties:
            PolicyDocument:
              Version: "2012-10-17"
              Statement:
                - Sid: "allow-coss-account-lambda"
                  Effect: Allow
                  Principal:
                    AWS:
                      - !Ref CrossAccountExternalAccountId
                  Resource: !Ref SnsTopicRef
                  Action: 
                      - "sns:Publish"   
            Topics:
              - Ref: SnsTopicRef

      Outputs:
        SnsTopicRef:
          Value: !Ref SnsTopicRef
          Description: Create an SNS

      
  

Cloudformation: Create a SQS and subscribe to SNS

      
      AWSTemplateFormatVersion: '2010-09-09'
      Description: Template to Create a SQS and subscribe to SNS
      Parameters:
        Stage:
          Description: Environment Name
          Type: String
        StandardQueueName:
          Type: String
          Description: Queue Name
        StandardTopicName:
          Type: String
          Description: Queue Name

      Resources:
        QueueRefDLQ:
          Type: AWS::SQS::Queue
          Properties:
            QueueName: !Join [ "-", [!Ref StandardQueueName, "dlq"] ]
            Tags:
              - 
                Key: "environment"
                Value: !Ref Stage

        QueueRef:
          Type: AWS::SQS::Queue
          Properties:
            QueueName: !Ref StandardQueueName
            ReceiveMessageWaitTimeSeconds: 10
            VisibilityTimeout: 900
            KmsMasterKeyId: alias/aws/sqs
            RedrivePolicy:
              deadLetterTargetArn: !GetAtt
                - QueueRefDLQ
                - Arn
              maxReceiveCount: 3
            Tags:
              - 
                Key: "environment"
                Value: !Ref Stage

        QueueRefPolicy:
          Type: AWS::SQS::QueuePolicy
          Properties:
            PolicyDocument:
              Version: "2012-10-17"
              Statement:
                - Sid: "allow-role-lambdas"
                  Effect: Allow
                  Principal:
                    AWS:
                      - !Sub ${AWS::AccountId}
                  Resource: !GetAtt
                    - QueueRef
                    - Arn
                  Action: 
                      - "SQS:SendMessage"
                      - "SQS:DeleteMessage"
                - Sid: "allow-sns-send-message"
                  Effect: Allow
                  Principal: '*'
                  Action: 
                      - "SQS:SendMessage"
                      - "SQS:DeleteMessage"
                  Resource: !GetAtt
                    - QueueRef
                    - Arn
                  Condition:
                    ArnEquals:
                      'aws:SourceArn':
                        - !Sub 'arn:aws:sns:${AWS::Region}:${AWS::AccountId}:${StandardTopicName}'

                - Sid: "allow-lambda-receive-delete-message"
                  Effect: Allow
                  Principal: '*'
                  Action: 
                      - "SQS:ReceiveMessage"
                      - "SQS:DeleteMessage"
                  Resource: !GetAtt
                    - QueueRef
                    - Arn
                  Condition:
                    ArnEquals:
                      'aws:SourceArn':
                        - !Sub 'arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:lambda-that-messages-process'
            Queues:
              - Ref: QueueRef
              
        QueueSubscription:
          Type: AWS::SNS::Subscription
          DependsOn: QueueRef
          Properties:
            Protocol: sqs
            Endpoint: !GetAtt
              - QueueRef
              - Arn
            TopicArn: !Sub 'arn:aws:sns:${AWS::Region}:${AWS::AccountId}:${StandardTopicName}'
            #FilterPolicy: {"type_filter":["any","other"]}
            FilterPolicy:
                type_filter:
                  - any
                  - other

      
  
      
          example for send an message:

          const SNSParameters: AWS.SNS.PublishInput = {
              TopicArn: `arn:aws:sns:AWS_REGION:AWS_ACCOUNT_ID:NAME_SNS`,
              Message: JSON.stringify({...}),
              MessageAttributes: {
                  "type_filter": {
                      StringValue: "any",
                      DataType: "String"
                  }
              },
          }
          const result = await new AWS.SNS({ region: AWS_REGION }).publish(SNSParameters).promise();
      
  

Comentarios

Entradas más populares de este blog

Ejemplo Log4j 2 en JAVA | Log4j 2 en Springboot | Configuración Log4j 2 | Log4j 2 in SpringBoot| Example Log4j 2 in SpringBoot | Configuring Log4j 2

Python: Inyección de dependencias

GOlang con Docker | GOlang with Docker | GO con Docker | GO with Docker