4

I create the api in "Api-gateway" and set "API Key Required" to true in Method execution settings , But in lambda function i only get the "apiKeyId" from the request header. Is there any way to get the apiKeyName too?

Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
Boom
  • 162
  • 1
  • 13
  • https://stackoverflow.com/questions/39061041/using-an-api-key-in-amazon-api-gateway -- this might be helpful – error404 Apr 08 '19 at 12:27

5 Answers5

2

In short, the ApiKey name is not available within the executing lambda. You can only use the SDK to query all keys and then filter manually with code.

On a side note, you can also do this in a custom authorizer and map the name to the invocation context. This way you only have to code it once and all lambdas get the parameter as a context variable. Another bonus of this implementation is, that the result of custom authorizer is cached.

nodejs implementation of custom authorizer with apikey name mapper

jens walter
  • 13,269
  • 2
  • 56
  • 54
1

Building on the answer above I found I could get this to work with the following code:

const APIGateway = require("aws-sdk").APIGateway;
    
const apiKeyId = event.requestContext.identity.apiKeyId;
const apiKeyDetails = await new APIGateway().getApiKey({apiKey:apiKeyId}).promise();
const apiKeyName = apiKeyDetails.name;
Simon
  • 767
  • 3
  • 12
  • 22
0
amittn
  • 2,249
  • 1
  • 12
  • 19
  • 1
    Is there any way to make apiKeyName available in request header in lamda function?. e.g in event.requestContext.identity i got the value information. `"identity": { "cognitoIdentityPoolId": null, "cognitoIdentityId": null, "apiKey": "iqUQOowmC7", "cognitoAuthenticationType": null, "userArn": null, "apiKeyId": "r3k", "userAgent": "PostmanRuntime/7.6.0", }` i need apiKeyName too. – Boom Apr 08 '19 at 13:12
0

A little late but here is a way to get the details for the API key, as others have said its necessary to fetch them.

import { APIGateway } from 'aws-sdk'
// Add the below to your handler
const apiKey = event.requestContext.identity.apiKey 
const apiKeyDetails = await new APIGateway().getApiKey({ apiKey }).promise()

Now its possible to fetch the api key name, tags, description etc.

omeanwell
  • 1,847
  • 1
  • 10
  • 16
  • I had to tweak this slightly to get it to work as getApiKey expects an apiKeyId: const APIGateway = require("aws-sdk").APIGateway; const apiKeyId = event.requestContext.identity.apiKeyId`; const apiKeyDetails = await new APIGateway().getApiKey({apiKey:apiKeyId}).promise(); – Simon Apr 13 '22 at 16:55
  • Likely due to you having an updated version of pulumi since this was posted – omeanwell Apr 14 '22 at 14:04
0

with AWSSDK.APIGateway for .Net:

By name (to check if already exists)

var apiGatewayClient = new AmazonAPIGatewayClient();
var getMethodRequest = new GetApiKeysRequest() { NameQuery = "SomeApiKeyName" };
var getMethodResponse = await apiGatewayClient.GetApiKeysAsync(getMethodRequest);
return getMethodResponse.Items != null && getMethodResponse.Items.Any() && getMethodResponse.Items[0].Name == "SomeName";

What you are referring as "apiKeyId" is indeed the "api key" which comes in the "x-api-key" header, because an apy key has another (internal) id. Said this, you can also find the api name by Usage Plan Id with method GetUsagePlanKeysAsync:

var p = new GetUsagePlanKeysRequest() { UsagePlanId = usagePlanId };
var result = await apiGatewayClient.GetUsagePlanKeysAsync(p);
return result.Items.Where(c => c.Value == apiKey).Select(s => s.Name);

If you don't have a Usage Plan Id you can obtain the complete list with method GetUsagePlansAsync:

var result = await apiGatewayClient.GetUsagePlansAsync(new GetUsagePlansRequest()));
return result.Items;

Or if you know the api key Id and Usage Plan Id, you can find the api key name with method GetUsagePlanKeyAsync:

var p = new GetUsagePlanKeyRequest() { UsagePlanId = usagePlanId, KeyId = apiKeyId };
var result = await apiGatewayClient.GetUsagePlanKeyAsync(p));
return result.Name;
LeonardoX
  • 1,113
  • 14
  • 31