Nick
Nick

Reputation: 352

How to retrieve x-api-key from headers in aws lambda function

I have a AWS Lambda function that gets called utilizing an api gateway. I set it up where API Key Required on my Method request. How do I get the value of x-api-key in my Lambda function? I tried printing out event and context in my Lambda function but event is empty, and context has basic info about the lambda function.

Do I need to map the variable somehow in Method/Integration for it to be available in my Lambda function?

Upvotes: 0

Views: 2274

Answers (2)

Sándor Bakos
Sándor Bakos

Reputation: 503

Yes you have to map the headers through the integration request tab.

API Gateway -> (your-gateway) -> resources -> (your-method) -> Integration Request -> Mapping templates -> There is a section about request body passthrough - here select When there are no templates defined (recommended) -> fill the Content-Type to application/json and press the check buttom -> you will see a dropdown called generate template - here you can select a predefined ones - and select Method request passthrough.

there is an official link as well - link

Upvotes: 1

Nick
Nick

Reputation: 352

Figured it out incase someone is looking for the answer:

In API Gateway > Resources > Method > Integration Request > Mapping Templates > Set content-type to application/json > Generate template > Method request passthrough

This will put body/header/context in your event parameter inside your lambda function

Upvotes: 1

Related Questions