AWS Protips: Passing data from client to server in HTTP Headers with API Gateway and Lambda using Gordon


You're building a API on AWS with Gordon, and you need to get some HTTP header data from the client to your Lambda.  This isn't too difficult, but does require some surgery in the API Gateway settings file in Gordon.

Let's say that you're trying to get a token of some kind from the client to your Lambda code in an HTTP header.  Pretty common task, especially if you're passing some kind of authorization data to the server.

Method Parameters

HTTP headers are passed into your Lambda through method parameters from API Gateway.  Let's say your client is passing you a header called 'API-Token', and you need to grab that to see if the client has permission to use your API.  Let's look at the settings.yml file.

/validate_user:
  methods: GET
  parameters:
    method.request.header.API-token: method.request.header.API-token
  integration:
    lambda: validate_user
    responses:
        - code: "401"
          pattern: "^\\[Unauthorized\\].*"
          template:
              application/json: |
                  #set($inputRoot = $input.path('$.errorMessage'))
                  $inputRoot
        - code: "200"
          pattern: ""
  responses:
    - code: "401"
    - code: "200"
  request_templates: |
        {
            "api-token": "$input.params('API-token')"
        }



This sets up the header parameter to be pulled from the client request (in the method parameters), and sets the value to the new key 'api-token'.  This will put the header parameter ('api-token') square into your event data in your Lambda, so you can retrieve it easily.


def handler(event, context):

    # get the api token
    token = event['api-token']
    valid = check_token(token)
    if not valid:
        raise Exception("[Unauthorized]")

    ...do interesting stuff here

    user = {}
    user["message"] = "welcome to my api"
    return user
 


Easy peasy, huh?  Well, not so fast.  This way introduces a few slight changes in the way you might check for the token's existence.  For example, when API Gateway is set up as such, the key api-token will always be in event data.  This means that you can't simply check for the existence of api-token in event to see if it has been set -- you have to check the value.  In this case, we'll just check for the value to be set to "" or None.  Let's modify our above handler a bit:


def handler(event, context):
    # get the api token
    token = event['api-token']

    # check for a token
    if (token=="") or not token:
        raise Exception("[Missing Parameters]")

    # check the token with your check method
    valid = check_token(token)
    if not valid:
        raise Exception("[Unauthorized]")

    ...do interesting stuff here
    
    # welcome message
    user = {}
    user["message"] = "welcome to my api"
    return user
  

The other rub is that you have to map the request data body as well, in order to get it to your Lambda.  We'll discuss that in another post.

Again, like other short pro-tips on my blog, this article is intended to be there for the engineer who is in the middle of building a system with AWS, but needs some quick answers on how to do something critical with the Gordon tool.  I'm working on a Gordon tutorial, which will feature later this month, but, generally, with these short articles, I jump right into the dense part of the topic, assuming some prior experience with Gordon and AWS.

Good luck with your project.

Peace
-r


references

Gordon - https://github.com/jorgebastida/gordon
Apache Velocity Engine - http://velocity.apache.org/engine/devel/vtl-reference.html
API Gateway Mapping Template Reference http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html