title: AWS API gateway body mapping template for dynamodb map
body: <p>I am trying to figure out how to insert dynamic data into a dynamodb table via an API Gateway in AWS.  Currently I have a dynamodb table and an API endpoint setup that accepts a POST like so.</p>
  
  <p>POST https://{unique-id}.execute-api.us.east-1.amazonaws.com/notification/events</p>
  
  <pre><code>{
    "reference_number": 99,
    "purchase_date": "1/1/2017"
  }
  </code></pre>
  
  <p>I've setup a body mapping template in the API gateway to massage the data into the dynamodb.  </p>
  
  <pre><code>{ 
      "TableName": "Events",
      "Item": {
          "reference_number": {
              "N": "$input.path('$.reference_number')"
          },
          "purchase_date": {
              "S": "$input.path('$.purchase_date')"
          }
      }
  }
  </code></pre>
  
  <p>The above works and saves to the table.  </p>
  
  <p>Suppose I add the event hash to my json (which can change based on events).</p>
  
  <pre><code>{
    "reference_number": 99,
    "purchase_date": "1/1/2017",
    "event": {
       "name": "purchase",
       "items": [1,3,6],
       "info": {
         "currencyID": "USD",
         "countryID": "US"
       }
    }
  }
  </code></pre>
  
  <p>How do I save the event attribute to a Map in dynamodb using the API Gateway Body mapping template syntax?</p>
  
  <pre><code>{ 
      "TableName": "Events",
      "Item": {
          "reference_number": {
              "N": "$input.path('$.reference_number')"
          },
          "purchase_date": {
              "S": "$input.path('$.purchase_date')"
          },
          "event":{
              "M": "$input.path('$.event')"
          }
      }
  }
  </code></pre>
  
  <p>The above template gives me the following error.  "Expected map or null"</p>