Reputation: 192
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?
Upvotes: 6
Views: 5889
Reputation: 1313
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;
Upvotes: 0
Reputation: 797
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;
Upvotes: 2
Reputation: 2189
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.
Upvotes: 1
Reputation: 14049
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
Upvotes: 4
Reputation: 2355
apiGatway API referance
or use the aws CLI
to get the name of the key
Amazon API Gateway REST API Reference
or
AWS CLI command referanceUpvotes: 0