jac
jac

Reputation: 51

Creating API gateway api keys in Lambda

I'm trying to create API Gateway api keys in lambda using the sdk, but I can't seem to make it work Here's my code

exports.handler = (event, context, callback) => {
        var apigateway = new AWS.APIGateway({apiVersion: '2015-07-09'});
        var params = {
      description: 'desc',
      enabled: true,
      generateDistinctId: true,
      name: 'apiKey1',
      value: 'qwerty'
    };
    apigateway.createApiKey(params, function(err, data) {
      if (err) console.log(err, err.stack); // an error occurred
      else     console.log(data);           // successful response
    });
}

The function just times out after 1 minute, without logging anything at all to CloudWatch. It feels like the callback is never actually called. Here are the policies attached to the function's role: enter image description here Anyone know what I'm missing?

Upvotes: 1

Views: 1188

Answers (3)

洪偉豪
洪偉豪

Reputation: 1

You have to set the internet connectivity with lambda since the createAPIKey function sends the internet request to the lambda function. If you are using VPC for your lambda, that means you can only connect within VPC. You will need a NAT service to connect to the internet. If you don't use VPC for your lambda, lambda is connecting to the internet by default. In your case, you seem to be setting the lambda with VPC, so you can't receive the request sent by the API gateway (request to create API keys).

Solution:

  1. If your lambda has VPC groups, create a NAT to connect with your lambda. Link
  2. Detach your lambda with original VPC groups and it will automatically connect to the internet. (This is a dangerous approach, but if you are in a testing environment, you can use this method to check the timeout problem.)

Upvotes: 0

Ashan
Ashan

Reputation: 19728

You need to call the callback method, upon createKey success and error to avoid timeout.

exports.handler = (event, context, callback) => {
  var apigateway = new AWS.APIGateway({apiVersion: '2015-07-09'});
  var params = {
      description: 'desc',
      enabled: true,
      generateDistinctId: true,
      name: 'apiKey1',
      value: 'qwerty'
    };
    apigateway.createApiKey(params, function(err, data) {
      if (err) { 
          console.log(err, err.stack); // an error occurredelse
          callback(err);
      } else {
          console.log(data); // successful response
          callback(null, data);
      }
    });
}

Upvotes: 2

Kannaiyan
Kannaiyan

Reputation: 13025

Parameters are not matching as documented,

var params = {
  customerId: 'STRING_VALUE',
  description: 'STRING_VALUE',
  enabled: true || false,
  generateDistinctId: true || false,
  name: 'STRING_VALUE',
  stageKeys: [
    {
      restApiId: 'STRING_VALUE',
      stageName: 'STRING_VALUE'
    },
    /* more items */
  ],
  value: 'STRING_VALUE'
};
apigateway.createApiKey(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});

stageKeys is missing.

Hope it helps.

Upvotes: 0

Related Questions