title: AWS Lambda: Clarification on retrieving data from event object
body: <p>At the moment I think I have a foundational misunderstanding of how to extract/pass data from lambda functions &amp; I'm seeking clarification</p>
  
  <p>Example: Let's say I want to pass some data to a lambda function ie <code>{"hello":"world"}</code> and do so by passing data to that Lambda function by creating a REST endpoint in <a href="https://aws.amazon.com/api-gateway/" rel="nofollow noreferrer">AWS API Gateway</a></p>
  
  <p>At the moment I understand there are three ways to extract data:</p>
  
  <p>1) event.queryStringParameters (makes sense)</p>
  
  <p>ex. We can attach query parameters to the request URL: <a href="https://fakefakefake.execute-api.us-west-2.amazonaws.com/test/myapi?hello=world" rel="nofollow noreferrer">https://fakefakefake.execute-api.us-west-2.amazonaws.com/test/myapi?hello=world</a> and in the lambda function:</p>
  
  <pre><code>const data = event.queryStringParameters.hello; // 'world'
  </code></pre>
  
  <p>2) event.body (makes sense &amp; this is possible because of "Lambda Proxy Integration")</p>
  
  <p>ex. If we attach the data in the body of the POST/PUT/etc request using <a href="https://stackoverflow.com/questions/42474264/lambda-integration-vs-lambda-proxy-pros-and-cons">Lambda Proxy Integration</a> (ie forward all the data), we can access it via <code>event.body</code> &amp; within the lambda function (but making sure to JSON.parse the event.body since Lambda Proxy Integration will pass through stringified JSON &amp; not valid/"real" JSON):</p>
  
  <pre><code>const parsedBody = JSON.parse(event.body); // should wrap in try/catch
  const data = parsedBody.hello; // 'world'
  </code></pre>
  
  <p>3) Directly on the event object (Unclear)</p>
  
  <p>ex. This case is unclear at the moment-- pass data to the lambda function from a REST endpoint setup in API Gateway where it is then accessible directly from the event object?</p>
  
  <pre><code>const data = event.hello; // 'world'
  </code></pre>
  
  <p>What is an example of how to pass data "directly" on the event object in a Lambda function like in case #3? I <em>THINK</em> this case requires that I create a "mapping template" when setting up the API/Lambda but I'm still unclear.</p>
  
  <p>For a simple Node script, case 2 appears to have the "overhead" of parsing event body from stringified JSON so that's an understandable downside, but in addition to how to do it why or when would Case 3 be a more desirable approach?</p>
  
  <ul>
  <li><p><a href="https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html" rel="nofollow noreferrer">https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html</a></p></li>
  <li><p>Lambda proxy integration: <a href="https://stackoverflow.com/questions/41648467/getting-json-body-in-aws-lambda-via-api-gateway/52240132#52240132">Getting json body in aws Lambda via API gateway</a></p></li>
  <li><p>Lambda proxy integration (AWS example): <a href="https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html" rel="nofollow noreferrer">https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html</a> </p></li>
  <li><p>Lambda Proxy Integration vs Lambda Proxy (option #2 vs option #3 above): <a href="https://stackoverflow.com/questions/42474264/lambda-integration-vs-lambda-proxy-pros-and-cons">Lambda Integration vs. Lambda Proxy: Pros and Cons</a></p></li>
  <li><p><a href="https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html" rel="nofollow noreferrer">https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html</a></p></li>
  </ul>