8
votes

I am exposing a AWS Lambda function to public HTTP requests by setting up an AWS API Gateway endpoint pointing to it.

There are two parts to this:

  • Create and upload my AWS Lambda function
  • Set up the API Gateway to point a HTTP endpoint to my Lambda function

I want to do both parts using API calls instead of the web interface. I can do that for the first part using the AWS SDK and AWS CLI.

However, for the second part, I'm stuck. I haven't found a mention of the API Gateway when looking through the AWS SDK for node.js, or the AWS CLI

Is there a way to set up an API Gateway endpoint for a Lambda function, programatically using the AWS API?

4
CLI / SDK support for API Gateway is not ready yet. I personally expect more details about these features to be published at re:Invent 2015.adamkonrad
Also, CloudFormation is something that may very well help you in your future efforts.adamkonrad

4 Answers

8
votes

Yes, it's possible via AWS's API to set up your Amazon API Gateway endpoints for your AWS Lambda functions.

While the AWS SDK for JavaScript in Node.js and AWS CLI haven't supported Amazon API Gateway yet, you can set up them using Amazon API Gateway REST API without official SDK. In this case, you will probably use these APIs:

  1. restapi:create
  2. resource:create
  3. method:put
  4. integration:put
  5. integrationresponse:put
  6. methodresponse:put

You might want to use 3rd party libraries to integrate Amazon API Gateway with AWS Lambda such as jaws-stack/JAWS or r7kamura/fluct.

3
votes

Yes...it is absolutely possibly. Below is some node.js code that uses the AWS-SDK for node.js. I'm doing a POST here for the method integration. Now there are a few things you'll need. Hope this helps...good luck!

The ResourceId of the Method you're using for the Gateway API

The Gateway API Rest Id

The ARN of the Invoke Role that is able to invoke your Lambda Function

The ARN of the Lambda function you want to integrate.

var AWS = require('aws-sdk');

api = new AWS.APIGateway(); 

var params = {
    httpMethod: 'POST',
    resourceId: [YOUR RESOURCE ID],
    restApiId: [YOUR REST API ID],
    type: 'AWS',
    uri: [YOUR LAMBDA FUNCTION ARN],
    integrationHttpMethod: 'POST',
    credentials : [YOUR INVOKE ROLE ARN]
};

api.putIntegration(params, function (err, data) {
    if (err) {
        console.log('AWS Error', err);
    } else {
        console.log('Put Integration Method Created', data);
    }
});
1
votes

Yes, it's doable.

JAWS project has a nice and simple js implementation, and it works pretty well https://github.com/jaws-stack/jaws-api-gateway-client

Also the latest AWS CLI (version 1.99) has supported API Gateway fully. http://docs.aws.amazon.com/cli/latest/reference/apigateway/index.html#cli-aws-apigateway

0
votes

You can point your API endpoint to a Lambda Function. While creating new HTTP method in API Gateway, select "Integration type" as "Lambda Function", then select the Lambda function you need to invoke.

Main challenge would be mapping the API input parameters to the Lambda model object, you can either define the mapping manually or provide a model schema(uses json-schema).

Example mapping:

{
    "firstname" : "$input.params('firstname')",
    "lastname"  : "$input.params('lastname')"
}

Refer this blog post for step-by-step tutorial written for Java 8.