Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set up an AWS API Gateway endpoint for a Lambda function, using the AWS API?

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?

like image 580
Codo Sapiens Avatar asked Aug 16 '15 13:08

Codo Sapiens


2 Answers

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.

like image 111
r7kamura Avatar answered Sep 17 '22 20:09

r7kamura


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);
    }
});
like image 21
Kevin Mansel Avatar answered Sep 17 '22 20:09

Kevin Mansel