Document updated on Jun 9, 2022
Integration with AWS Lambda functions
The Lambda integration allows you to invoke Amazon Lambda functions on a KrakenD endpoint call. The content returned by the lambda function can be treated and manipulated as any other backend.
The payload that is sent to the Lambda function comes from the request and depends on the method used by the endpoint
:
- Method
GET
: The payload contains all the request parameters. - Non-
GET
methods: The payload is defined by the content of the body in the request.
You don’t need to set an Amazon API Gateway in the middle as KrakenD does this job for you.
Lambda configuration
host
and url_pattern
are needed as per the backend definition, but KrakenD will never use them. Feel free to add any value in there, but they must be present.The inclusion requires you to add the code in the extra_config
of your backend
section, using the backend/lambda
namespace.
The supported parameters are:
function_name
: Name of the lambda function as saved in the AWS service.function_param_name
: The endpoint{placeholder}
that sets the function name. You have to choose betweenfunction_name
andfunction_param_name
but not both.region
: The AWS identifier region (e.g.:us-east-1
,eu-west-2
, etc. )max_retries
: Maximum times you want to execute the function until you have a successful response.endpoint
: An optional parameter to customize the Lambda endpoint to call. Useful when Localstack is used for testing.
{route}
, define it in the config as Route
.Fields of AWS Lambda functions
endpoint
string- An optional parameter to customize the Lambda endpoint to call. Useful when Localstack is used for testing instead of direct AWS usage.
function_name
string- Name of the lambda function as saved in the AWS service. You have to choose between function_name and function_param_name but not both.
function_param_name
string- The endpoint {placeholder} that sets the function name. You have to choose between function_name and function_param_name but not both.
max_retries
integer- Maximum times you want to execute the function until you have a successful response. The value -1 defers the max retry setting to the service specific configuration.Defaults to
0
region
string- The AWS identifier region (e.g.: us-east-1, eu-west-2, etc.)
Authentication and connectivity
The KrakenD machine needs to have connectivity with your AWS account and have the credentials to do so. There are several ways you can achieve this:
- By copying your AWS credentials in the default file,
~/.aws/credentials
(and maybe an additional~/.aws/config
and the env varAWS_PROFILE
if you have several profiles) - By passing the environment variables with at least
AWS_ACCESS_KEY_ID
andAWS_SECRET_ACCESS_KEY
(and maybeAWS_REGION
) when starting KrakenD. - By having an IAM user with a policy and execution role that let you invoke the function from the machine
When setting the credentials, ensure the Lambda is callable within the KrakenD box with the selected method.
If your machine has the AWS CLI installed, you can test your Lambda:
Invoking a Lambda function
$aws lambda invoke --region us-east-1 --function-name myLambdaFunction --output json result.json
Authentication examples
Mounting an existing .aws
directory with the credentials in it (notice that the home of the Docker user is krakend
):
Term
$docker run --rm -it -p "8080:8080" \
-e "AWS_PROFILE=default" \
-v "/home/user/.aws:/home/krakend/.aws:ro" \
-v "$PWD:/etc/krakend" devopsfaith/krakend:v2.1
Passing the credentials directly:
Term
$docker run --rm -it -p "8080:8080" \
-e "AWS_ACCESS_KEY_ID=XXX" \
-e "AWS_SECRET_ACCESS_KEY=XXX" \
-e "AWS_REGION=eu-west-1" \
-v "$PWD:/etc/krakend" devopsfaith/krakend:v2.1
Example: Associate a lambda to a backend
When you associate a KrakenD endpoint to a unique lambda function, use this configuration:
{
"endpoint": "/call-a-lambda",
"backend": [
{
"host": ["ignore"],
"url_pattern": "/ignore",
"extra_config": {
"backend/lambda": {
"function_name": "myLambdaFunction",
"region": "us-east-1",
"max_retries": 1
}
}
}
]
}
Here is an example of Lambda code that could run a Hello World (NodeJS):
exports.handler = async (event) => {
// TODO implement
const response = {
statusCode: 200,
body: {"message": "Hello World"},
};
return response;
};
Example: Take the lambda from the URL
When the name of the Lambda depends on a parameter passed in the endpoint, use this configuration instead:
{
"endpoint": "/call-a-lambda/{lambda}",
"backend": [
{
"host": ["ignore"],
"url_pattern": "/ignore",
"extra_config": {
"backend/lambda": {
"function_param_name": "Lambda",
"region": "us-west1",
"max_retries": 1
}
}
}
]
}
In this example, the function_param_name
tells us which is the placeholder in the endpoint setting the Lambda. In this case, {lambda}
.
Following the code, a call GET /call-a-lambda/my-lambda
would produce a call to the My-lambda
function in AWS.