6

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?

1

5 Answers 5

4

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

Sign up to request clarification or add additional context in comments.

Comments

2

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;

Comments

1

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.

1 Comment

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();
0

1 Comment

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.
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;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.