Reputation: 51
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: Anyone know what I'm missing?
Upvotes: 1
Views: 1188
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:
Upvotes: 0
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
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