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?
-
stackoverflow.com/questions/39061041/… -- this might be helpfulerror404– error4042019-04-08 12:27:30 +00:00Commented Apr 8, 2019 at 12:27
5 Answers
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
Comments
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
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 think you can use this
apiGatway API referanceor use theaws CLIto get the name of the key Amazon API Gateway REST API Reference or AWS CLI command referance
1 Comment
"identity": { "cognitoIdentityPoolId": null, "cognitoIdentityId": null, "apiKey": "iqUQOowmC7", "cognitoAuthenticationType": null, "userArn": null, "apiKeyId": "r3k", "userAgent": "PostmanRuntime/7.6.0", } i need apiKeyName too.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;