Amazon Lambda – Part 2 – Basic Call As A Rest Service

Armed with the ability to return a satellite update from the Satellite Amazon Dynamo DB in the Amazon Lambda console, I set out to see if exposing it as a web service was equally as easy.  The idea of not having to maintain the scaffolding of a service is really starting to appeal to me.  I was ecstatic when I saw the ‘Add API Endpoint’ in the ‘Endpoint’ tab of the Lambda function.  I promptly clicked through the provided wizard and I saw the endpoint.  There was a note about configuring the endpoint through the IAM console.  So like everyone, I only read the directions when I have too 🙂  I clicked on the link and got the ‘{“message”:”Missing Authentication Token”}’ error.  I thought ‘Awesome…couple clicks in the IAM screen, and I will be there’…or so I thought.

I almost always use Postman to research calls and figure out the specifics before starting on an application.  So, after creating the IAM user, I had some issues figuring out the headers needed to make the call.  When I saw the AWS option for authorization in postman, I filled in those values with the IAM user and tried again.  The result was the ‘Credential should be scoped to correct service: ‘execute-api” error.  I found a few posts on the IOT API, but nothing I could easily attribute to this case.

So, I went to the API Gateway and created one from the sample pet store API.  Worked right out of the box.  So, I tried again.  This time, I created an open API for my satellite method and to see if it worked.  I put in what I think should be correct for the URL and I got the ‘{“message”:”Missing Authentication Token”}’ error 😦

So, after more digging some more and following these instructions (see reference #3), I was able to recreate the hello world on my own API (link removed now):

https://2n4oxal31h.execute-api.us-west-2.amazonaws.com/test/data

Part2-ApiGateway2I noticed a couple of other things:

  • Following these instructions (see reference #3), I also found out about the node context object.  Among other things, it is used to return data (i.e. context.done(null, {“Hello” : “World”});) to the calling method.  Previously, I was writing to the log.  I was also able to use the callback method to return data, but it was in an error wrapper.
  • Instead of creating a method which is what I thought was the normal process, you have to create a resource first and then add the method to it.
  • In creating an API method in the gateway, you have to tie it to something.  I thought initially I could test the method itself.  However, you have to tie it to an HTTP Proxy, a Lambda Method, a Mock (it may be possible to return something simple to test here) or an Amazon Proxy Service.

For the next step, I added the satellite call.  It seems like AWS API calls are physically abstracted from the Lambda functions and a separate link is needed.  I found this out when I added the “SatelliteUpdateId” query parameter to the GET method and it wasn’t showing up on the log or in the result of the Lambda call.  So, I found an example of how to map the parameters (see reference #4) and once this was in place, I was able to test the call using the Amazon API console test function.

I deployed the method and I was able to see the satellite update (link removed now):

https://2n4oxal31h.execute-api.us-west-2.amazonaws.com/test/satellite?SatelliteUpdateId=5828

Part2-ApiGateway3

The API definition looks like this in the Gateway:

Part2-ApiGateway1

A summary of the steps are:

  • Create the API
  • Add a resource to that API
  • Add a HTTP method to that resource
    • Tie the GET method to your Lambda method
    • Add ‘SatelliteUpdateId’ to the query string window

SetQueryString

  • Once created, open of the Integration Request
    • Add this Mapping so the Lambda Method can see the query string

MappingQueryToLambda

I have removed the API since they were all open, but for the next blog entry, I will attempt to add a secured API having these functions as well as additional methods as I continue to explore what Amazon Lambda can do.

Stay tuned!

References

1) http://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started.html
2) http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-step-by-step.html
3) https://aws.amazon.com/blogs/aws/amazon-api-gateway-build-and-run-scalable-application-
4) https://forums.aws.amazon.com/thread.jspa?messageID=643660

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s