0

I am new to AWS. and I want to integrate API Gateway with my lambda function. The thing which I want to achieve is that: for every HTTP request made i need to just print a statement that the specific request is hit!. For e.g. suppose a GET request hits my api endpoint, I need to display just "GET request made" in the browser. And if say a post request is made.. I want to display the message "POST request made" and if possible additionally the posted data. I searched a lot but couldn't find anything. However I found a code snippet but it's written in node.js. Below I am attaching the snippet.

exports.handler = async (event) => {
    console.log(event);
    if (event.httpMethod === 'PUT') {
        let response = putShows(event)
        return done(response);
    } else if (event.httpMethod === 'GET') {
        let response = getShows(event);
        return done(response);
    }
};`

The whole code is available here (on GitHub)

WayToDoor
  • 1,180
  • 9
  • 24

1 Answers1

0

Lambda: Create a lambda with python as the run time. We will set the the api gateway to send the http request type to the lambda. Lambda will receive this information in the event parameter. Here is the python code:

def lambda_handler(event, context):
    return event

In api gateway, create a resource with the integration type: lambda. Type in the name of the lambda in the lambda function field. Once created select Integration request on the Method diagram that appears. Here in the mapping templates section, you can input the information that goes to the lambda (this will come to the lambda in the event parameter): enter image description here

When you run test on this api gateway you receive back the mapping template section.

Mock Resources: If all you are doing is testing to see if you get a response back from api gateway you can use mock resources so that you don't need lambda configured. With mock resources you can configure the response to match the type of request you made.

Create a Method (get / post) and set the integration type to Mock. Go to the integration response off the method and expand the mapping templates section. You can add "message": "This is a get request".enter image description here

Testing API Gateway: To see what kind of responses you're getting back use the test functionality described in Step 5 of the aws docs I linked.

You have python linked so here is python code that can be used to see the response back:

import requests
url = 'https://your_api_url'
r = requests.get(url)
data = r.json()
data['message'] #this will have your get message in it.

If you want to do this through javascript, set enable CORS for your resources by clicking Actions and choosing 'Enable CORS'. Then you will need to use the following javascript for your page or browser's console:

const response = await fetch('your api gateway url')
console.log(response.body.message)
Moh. Anwer
  • 41
  • 4
  • Alright so basically what you are trying to say is that this can be achieved only through API Gateway, nothing to write in Lambda? –  Dec 10 '19 at 05:16
  • You can achieve what you are requesting solely with api gateway. If you really want, you can use lambda. I've adjusted my answer to include how to solve the problem with lambda involved. – Moh. Anwer Dec 10 '19 at 15:18