title: How to send json message body in aws SNS using console
body: <p>I am doing a hands on where I want to add an SNS trigger to a lambda function which then sends a message to a slack channel. There is a blueprint for this lambda in python and also a template test event which looks like the following</p>
  <pre><code>{
    &quot;Records&quot;: [
      {
        &quot;EventVersion&quot;: &quot;1.0&quot;,
        &quot;EventSubscriptionArn&quot;: &quot;arn:aws:sns:EXAMPLE&quot;,
        &quot;EventSource&quot;: &quot;aws:sns&quot;,
        &quot;Sns&quot;: {
          &quot;SignatureVersion&quot;: &quot;1&quot;,
          &quot;Timestamp&quot;: &quot;1970-01-01T00:00:00.000Z&quot;,
          &quot;Signature&quot;: &quot;EXAMPLE&quot;,
          &quot;SigningCertUrl&quot;: &quot;EXAMPLE&quot;,
          &quot;MessageId&quot;: &quot;12345&quot;,
          &quot;Message&quot;: {
            &quot;AlarmName&quot;: &quot;SlackAlarm&quot;,
            &quot;NewStateValue&quot;: &quot;OK&quot;,
            &quot;NewStateReason&quot;: &quot;Threshold Crossed: 1 datapoint (0.0) was not greater than or equal to the threshold (1.0).&quot;
          },
          &quot;MessageAttributes&quot;: {
            &quot;Test&quot;: {
              &quot;Type&quot;: &quot;String&quot;,
              &quot;Value&quot;: &quot;TestString&quot;
            },
            &quot;TestBinary&quot;: {
              &quot;Type&quot;: &quot;Binary&quot;,
              &quot;Value&quot;: &quot;TestBinary&quot;
            }
          },
          &quot;Type&quot;: &quot;Notification&quot;,
          &quot;UnsubscribeUrl&quot;: &quot;EXAMPLE&quot;,
          &quot;TopicArn&quot;: &quot;arn:aws:sns:EXAMPLE&quot;,
          &quot;Subject&quot;: &quot;TestInvoke&quot;
        }
      }
    ]
  </code></pre>
  <p>The code in lambda handler from the blueprint is as follows</p>
  <pre><code>import boto3
  import json
  import logging
  import os
  
  from base64 import b64decode
  from urllib.request import Request, urlopen
  from urllib.error import URLError, HTTPError
  
  HOOK_URL = os.environ['kmsEncryptedHookUrl']
  SLACK_CHANNEL = os.environ['slackChannel']
  
  logger = logging.getLogger()
  logger.setLevel(logging.INFO)
  
  def lambda_handler(event, context):
      logger.info(&quot;Event: &quot; + str(event))
      message = event['Records'][0]['Sns']['Message']
      logger.info(&quot;Message: &quot; + str(message))
  
      alarm_name = message['AlarmName']
      new_state = message['NewStateValue']
      reason = message['NewStateReason']
  
      slack_message = {
          'channel': SLACK_CHANNEL,
          'text': &quot;%s state is now %s: %s&quot; % (alarm_name, new_state, reason)
      }
  
      req = Request(HOOK_URL, json.dumps(slack_message).encode('utf-8'))
      try:
          response = urlopen(req)
          response.read()
          logger.info(&quot;Message posted to %s&quot;, slack_message['channel'])
      except HTTPError as e:
          logger.error(&quot;Request failed: %d %s&quot;, e.code, e.reason)
      except URLError as e:
          logger.error(&quot;Server connection failed: %s&quot;, e.reason)
  </code></pre>
  <p>When I run the test event, the lambda runs successfully.</p>
  <p>I wanted to publish a message in SNS topic from the console to see if the lambda is triggered correctly. But when I try to publish the JSON object as a message body, I am getting the error</p>
  <pre><code>[ERROR] TypeError: string indices must be integersTraceback (most recent call last):  File &quot;/var/task/lambda_function.py&quot;, line 21, in lambda_handler    alarm_name = message['AlarmName']
  </code></pre>
  <p>I tried giving plain json</p>
  <pre><code>{
    &quot;AlarmName&quot;: &quot;PublishedAlarm&quot;,
    &quot;NewStateValue&quot;: &quot;OK&quot;,
    &quot;NewStateReason&quot;: &quot;This alarm is published&quot;
  }
  </code></pre>
  <p>I tried giving a stringified JSON</p>
  <pre><code>&quot;{\&quot;AlarmName\&quot;: \&quot;PublishedAlarm\&quot;,\&quot;NewStateValue\&quot;: \&quot;OK\&quot;,\&quot;NewStateReason\&quot;: \&quot;This alarm is published\&quot;}&quot;
  </code></pre>
  <p>I tried choosing <code>Custom payload for each delivery</code> message structure and then gave the following message body</p>
  <pre><code>{
    &quot;default&quot;: &quot;Sample fallback message&quot;,
    &quot;email&quot;: &quot;Sample message for email endpoints&quot;,
    &quot;sqs&quot;: &quot;Sample message for Amazon SQS endpoints&quot;,
    &quot;lambda&quot;: &quot;{\&quot;AlarmName\&quot;: \&quot;PublishedAlarm\&quot;,\&quot;NewStateValue\&quot;: \&quot;OK\&quot;,\&quot;NewStateReason\&quot;: \&quot;This alarm is published\&quot;}&quot;,
    &quot;http&quot;: &quot;Sample message for HTTP endpoints&quot;,
    &quot;https&quot;: &quot;Sample message for HTTPS endpoints&quot;,
    &quot;sms&quot;: &quot;Sample message for SMS endpoints&quot;,
    &quot;firehose&quot;: &quot;Sample message for Amazon Kinesis Data Firehose endpoints&quot;,
    &quot;APNS&quot;: &quot;{\&quot;aps\&quot;:{\&quot;alert\&quot;: \&quot;Sample message for iOS endpoints\&quot;} }&quot;,
    &quot;APNS_SANDBOX&quot;: &quot;{\&quot;aps\&quot;:{\&quot;alert\&quot;:\&quot;Sample message for iOS development endpoints\&quot;}}&quot;,
    &quot;APNS_VOIP&quot;: &quot;{\&quot;aps\&quot;:{\&quot;alert\&quot;:\&quot;Sample message for Apple VoIP endpoints\&quot;}}&quot;,
    &quot;APNS_VOIP_SANDBOX&quot;: &quot;{\&quot;aps\&quot;:{\&quot;alert\&quot;: \&quot;Sample message for Apple VoIP development endpoints\&quot;} }&quot;,
    &quot;MACOS&quot;: &quot;{\&quot;aps\&quot;:{\&quot;alert\&quot;:\&quot;Sample message for MacOS endpoints\&quot;}}&quot;,
    &quot;MACOS_SANDBOX&quot;: &quot;{\&quot;aps\&quot;:{\&quot;alert\&quot;: \&quot;Sample message for MacOS development endpoints\&quot;} }&quot;,
    &quot;GCM&quot;: &quot;{ \&quot;data\&quot;: { \&quot;message\&quot;: \&quot;Sample message for Android endpoints\&quot; } }&quot;,
    &quot;ADM&quot;: &quot;{ \&quot;data\&quot;: { \&quot;message\&quot;: \&quot;Sample message for FireOS endpoints\&quot; } }&quot;,
    &quot;BAIDU&quot;: &quot;{\&quot;title\&quot;:\&quot;Sample message title\&quot;,\&quot;description\&quot;:\&quot;Sample message for Baidu endpoints\&quot;}&quot;,
    &quot;MPNS&quot;: &quot;&lt;?xml version=\&quot;1.0\&quot; encoding=\&quot;utf-8\&quot;?&gt;&lt;wp:Notification xmlns:wp=\&quot;WPNotification\&quot;&gt;&lt;wp:Tile&gt;&lt;wp:Count&gt;ENTER COUNT&lt;/wp:Count&gt;&lt;wp:Title&gt;Sample message for Windows Phone 7+ endpoints&lt;/wp:Title&gt;&lt;/wp:Tile&gt;&lt;/wp:Notification&gt;&quot;,
    &quot;WNS&quot;: &quot;&lt;badge version=\&quot;1\&quot; value=\&quot;42\&quot;/&gt;&quot;
  }
  </code></pre>
  <p>Nothing worked. I've also subscribed an email address to the topic and I'm getting emails without any issues.</p>
  <p>How can I simulate the test event given in lambda event templates from the SNS?</p>