- name: set up AWS connection info
  module_defaults:
    group/aws:
      aws_access_key: '{{ aws_access_key }}'
      aws_secret_key: '{{ aws_secret_key }}'
      security_token: '{{ security_token | default(omit) }}'
      region: '{{ aws_region }}'
  collections:
    - amazon.aws

  block:
    - name: Create an SNS topic
      sns_topic:
        name: "{{ sns_topic_name }}"
        display_name: "Test topic"
      register: sns_topic

    - name: Publish to the topic by name
      sns:
        topic: "{{ sns_topic_name }}"
        subject: Test message
        msg: Default test message
        http: Test message for HTTP
        https: Test message for HTTPS
        email: Test message for email
        email_json: Test message for email-json
        sms: Short test message for SMS
        sqs: Test message for SQS
        application: Test message for apps
        lambda: Test message for Lambda
      register: result

    - name: Check for expected result structure
      assert:
        that:
          - result is not changed
          - "'message_id' in result"

    - name: Publish to the topic by ARN
      sns:
        topic: "{{ sns_topic.sns_arn }}"
        subject: Second test message
        msg: Simple test message

  always:
    - name: Remove topic
      sns_topic:
        name: "{{ sns_topic_name }}"
        state: absent
      ignore_errors: yes
