Invoke an AWS Lambda function from Kong. It
can be used in combination with other request plugins to secure, manage or extend
the function.
Note: The functionality of this plugin as bundled
with versions of Kong prior to 0.14.0 and Kong Gateway prior to 0.34
differs from what is documented herein. Refer to the
CHANGELOG
for details.
Configuration Reference
Example plugin configuration
Enable on a service
Enable on a route
Enable on a consumer
Enable globally
The following examples provide some typical configurations for enabling
the aws-lambda
plugin on a
service.
Admin API
Kubernetes
Declarative (YAML)
Konnect Cloud
Kong Manager
Make the following request:
curl -X POST http://localhost:8001/services/SERVICE_NAME|SERVICE_ID/plugins \
--data "name=aws-lambda" \
--data-urlencode "config.aws_key=AWS_KEY" \
--data-urlencode "config.aws_secret=AWS_SECRET" \
--data "config.aws_region=AWS_REGION" \
--data "config.function_name=LAMBDA_FUNCTION_NAME"
Replace SERVICE_NAME|SERVICE_ID
with the id
or name
of the service that this plugin configuration will target.
First, create a KongPlugin
resource:
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
name: aws-lambda-example
config:
aws_key: AWS_KEY
aws_secret: AWS_SECRET
aws_region: AWS_REGION
function_name: LAMBDA_FUNCTION_NAME
plugin: aws-lambda
Next, apply the KongPlugin resource to a
service by annotating the
service as follows:
apiVersion: v1
kind: Service
metadata:
name: SERVICE_NAME|SERVICE_ID
labels:
app: SERVICE_NAME|SERVICE_ID
annotations:
konghq.com/plugins: aws-lambda-example
spec:
ports:
- port: 80
targetPort: 80
protocol: TCP
name: SERVICE_NAME|SERVICE_ID
selector:
app: SERVICE_NAME|SERVICE_ID
Replace SERVICE_NAME|SERVICE_ID
with the id
or name
of the service that this plugin configuration will target.
Note: The KongPlugin resource only needs to be defined once
and can be applied to any service, consumer, or route in the namespace. If you
want the plugin to be available cluster-wide, create the resource as a
KongClusterPlugin
instead of KongPlugin
.
Add this section to your declarative configuration file:
plugins:
- name: aws-lambda
service: SERVICE_NAME|SERVICE_ID
config:
aws_key: AWS_KEY
aws_secret: AWS_SECRET
aws_region: AWS_REGION
function_name: LAMBDA_FUNCTION_NAME
Replace SERVICE_NAME|SERVICE_ID
with the id
or name
of the service that this plugin configuration will target.
You can configure this plugin through the Konnect UI.
From the
Service Hub, select a service version, then set up the plugin:
- In the Plugins section, click Add Plugin.
- Find and select the AWS Lambda plugin.
-
Configure the plugin’s parameters.
You can test out the plugin with the following sample configuration:
- Config.Aws Key:
AWS_KEY
- Config.Aws Secret:
AWS_SECRET
- Config.Aws Region:
AWS_REGION
- Config.Function Name:
LAMBDA_FUNCTION_NAME
- Click Create.
You can configure this plugin through the Kong Manager UI.
- In Kong Manager, select the workspace.
- From the Services section, click View for the
service row.
- From the plugin section, click Add Plugin.
- Find and select the AWS Lambda plugin.
- If the option is available, select Scoped.
- Add the service name and ID to the Service field if it
is not already pre-filled.
-
Configure the plugin’s parameters.
You can test out the plugin with the following sample configuration:
- Config.Aws Key:
AWS_KEY
- Config.Aws Secret:
AWS_SECRET
- Config.Aws Region:
AWS_REGION
- Config.Function Name:
LAMBDA_FUNCTION_NAME
- Click Create.
The following examples provide some typical configurations for enabling
the aws-lambda
plugin on a
route.
Admin API
Kubernetes
Declarative (YAML)
Konnect Cloud
Kong Manager
Make the following request:
$ curl -X POST http://localhost:8001/routes/ROUTE_NAME|ROUTE_ID/plugins \
--data "name=aws-lambda" \
--data-urlencode "config.aws_key=AWS_KEY" \
--data-urlencode "config.aws_secret=AWS_SECRET" \
--data "config.aws_region=AWS_REGION" \
--data "config.function_name=LAMBDA_FUNCTION_NAME"
Replace ROUTE_NAME|ROUTE_ID
with the id
or name
of the route that this plugin configuration will target.
First, create a KongPlugin
resource:
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
name: aws-lambda-example
config:
aws_key: AWS_KEY
aws_secret: AWS_SECRET
aws_region: AWS_REGION
function_name: LAMBDA_FUNCTION_NAME
plugin: aws-lambda
Then, apply it to an ingress (route or routes)
by annotating the ingress as follows:
apiVersion: networking/v1beta1
kind: Ingress
metadata:
name: ROUTE_NAME|ROUTE_ID
annotations:
kubernetes.io/ingress.class: kong
konghq.com/plugins: aws-lambda-example
spec:
rules:
- host: examplehostname.com
http:
paths:
- path: /bar
backend:
serviceName: echo
servicePort: 80
Replace ROUTE_NAME|ROUTE_ID
with the id
or name
of the route that this plugin configuration will target.
Note: The KongPlugin resource only needs to be defined once
and can be applied to any service, consumer, or route in the namespace. If you
want the plugin to be available cluster-wide, create the resource as a
KongClusterPlugin
instead of KongPlugin
.
Add this section to your declarative configuration file:
plugins:
- name: aws-lambda
route: ROUTE_NAME
config:
aws_key: AWS_KEY
aws_secret: AWS_SECRET
aws_region: AWS_REGION
function_name: LAMBDA_FUNCTION_NAME
Replace ROUTE_NAME|ROUTE_ID
with the id
or name
of the route that this plugin configuration
will target.
You can configure this plugin through the Konnect UI.
From the
Service Hub, select a service version, then set up the plugin:
- Select a route.
- In the Plugins section, click Add Plugin.
- Find and select the AWS Lambda plugin.
-
Configure the plugin’s parameters.
You can test out the plugin with the following sample configuration:
- Config.Aws Key:
AWS_KEY
- Config.Aws Secret:
AWS_SECRET
- Config.Aws Region:
AWS_REGION
- Config.Function Name:
LAMBDA_FUNCTION_NAME
- Click Create.
You can configure this plugin through the Kong Manager UI.
- In Kong Manager, select the workspace.
- Open Routes from the menu, then click View for the
route row.
- From the plugin section, click Add Plugin.
- Find and select the AWS Lambda plugin.
- If the option is available, select Scoped.
- Add the route ID if it is not already prefilled.
-
Configure the plugin’s parameters.
You can test out the plugin with the following sample configuration:
- Config.Aws Key:
AWS_KEY
- Config.Aws Secret:
AWS_SECRET
- Config.Aws Region:
AWS_REGION
- Config.Function Name:
LAMBDA_FUNCTION_NAME
- Click Create.
The following examples provide some typical configurations for enabling
the aws-lambda
plugin on a
consumer.
Admin API
Kubernetes
Declarative (YAML)
Kong Manager
Make the following request:
$ curl -X POST http://localhost:8001/consumers/CONSUMER_NAME|CONSUMER_ID/plugins \
--data "name=aws-lambda" \
--data-urlencode "config.aws_key=AWS_KEY" \
--data-urlencode "config.aws_secret=AWS_SECRET" \
--data "config.aws_region=AWS_REGION" \
--data "config.function_name=LAMBDA_FUNCTION_NAME"
Replace CONSUMER_NAME|CONSUMER_ID
with the id
or name
of the consumer that this plugin configuration will target.
You can combine consumer.id
, service.id
, or route.id
in the same request, to further narrow the scope of the plugin.
First, create a KongPlugin
resource:
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
name: aws-lambda-example
config:
aws_key: AWS_KEY
aws_secret: AWS_SECRET
aws_region: AWS_REGION
function_name: LAMBDA_FUNCTION_NAME
plugin: aws-lambda
Then, apply it to a consumer by
annotating the KongConsumer resource as follows:
apiVersion: configuration.konghq.com/v1
kind: KongConsumer
metadata:
name: CONSUMER_NAME|CONSUMER_ID
annotations:
konghq.com/plugins: aws-lambda-example
kubernetes.io/ingress.class: kong
Replace CONSUMER_NAME|CONSUMER_ID
with the id
or name
of the consumer that this plugin configuration will target.
Note: The KongPlugin resource only needs to be defined once
and can be applied to any service, consumer, or route in the namespace. If you
want the plugin to be available cluster-wide, create the resource as a
KongClusterPlugin
instead of KongPlugin
.
Add this section to your declarative configuration file:
plugins:
- name: aws-lambda
consumer: CONSUMER_NAME|CONSUMER_ID
config:
aws_key: AWS_KEY
aws_secret: AWS_SECRET
aws_region: AWS_REGION
function_name: LAMBDA_FUNCTION_NAME
Replace CONSUMER_NAME|CONSUMER_ID
with the id
or name
of the consumer that this plugin configuration will target.
You can configure this plugin through the Kong Manager UI.
- In Kong Manager, select the workspace.
- From the Consumers section, click View for the consumer row.
- Select the Plugins tab, then click Add Plugin.
- Find and select the AWS Lambda plugin.
- If the option is available, select Scoped.
- Add the consumer ID if it is not already prefilled.
-
Configure the plugin’s parameters.
You can test out the plugin with the following sample configuration:
- Config.Aws Key:
AWS_KEY
- Config.Aws Secret:
AWS_SECRET
- Config.Aws Region:
AWS_REGION
- Config.Function Name:
LAMBDA_FUNCTION_NAME
- Click Create.
A plugin which is not associated to any service, route, or consumer is
considered global, and will be run on every request. Read the
Plugin Reference and the Plugin Precedence
sections for more information.
The following examples provide some typical configurations for enabling
the aws-lambda
plugin globally.
Admin API
Kubernetes
Declarative (YAML)
Kong Manager
Make the following request:
$ curl -X POST http://localhost:8001/plugins/ \
--data "name=aws-lambda" \
--data-urlencode "config.aws_key=AWS_KEY" \
--data-urlencode "config.aws_secret=AWS_SECRET" \
--data "config.aws_region=AWS_REGION" \
--data "config.function_name=LAMBDA_FUNCTION_NAME"
Create a KongClusterPlugin
resource and label it as global:
apiVersion: configuration.konghq.com/v1
kind: KongClusterPlugin
metadata:
name: <global-aws-lambda>
annotations:
kubernetes.io/ingress.class: kong
labels:
global: \"true\"
config:
aws_key: AWS_KEY
aws_secret: AWS_SECRET
aws_region: AWS_REGION
function_name: LAMBDA_FUNCTION_NAME
plugin: aws-lambda
Add a plugins
entry in the declarative
configuration file:
plugins:
- name: aws-lambda
config:
aws_key: AWS_KEY
aws_secret: AWS_SECRET
aws_region: AWS_REGION
function_name: LAMBDA_FUNCTION_NAME
You can configure this plugin through the Kong Manager UI.
- In Kong Manager, select the workspace.
- Open Plugins from the menu, then click New Plugin.
- Find and select the AWS Lambda plugin.
- If the option is available, set the plugin scope to Global.
-
Configure the plugin’s parameters.
You can test out the plugin with the following sample configuration:
- Config.Aws Key:
AWS_KEY
- Config.Aws Secret:
AWS_SECRET
- Config.Aws Region:
AWS_REGION
- Config.Function Name:
LAMBDA_FUNCTION_NAME
- Click Create.
Parameters
Here's a list of all the parameters which can be used in this plugin's configuration:
Form Parameter |
Description |
name
required
Type: string |
The name of the plugin, in this case aws-lambda . |
service.name or service.id
Type: string |
The name or ID of the service the plugin targets.
Set one of these parameters if adding the plugin to a service through the top-level /plugins endpoint.
Not required if using /services/SERVICE_NAME|SERVICE_ID/plugins . |
route.name or route.id
Type: string |
The name or ID of the route the plugin targets.
Set one of these parameters if adding the plugin to a route through the top-level /plugins endpoint.
Not required if using /routes/ROUTE_NAME|ROUTE_ID/plugins . |
consumer.name or consumer.id
Type: string |
The name or ID of the consumer the plugin targets.
Set one of these parameters if adding the plugin to a consumer through the top-level /plugins endpoint.
Not required if using /consumers/CONSUMER_NAME|CONSUMER_ID/plugins . |
enabled
Type: boolean
Default value: true |
Whether this plugin will be applied. |
api_id
Type: string |
The ID of the API the plugin targets.
Note: The API Entity is deprecated in favor of Services since CE 0.13.0 and EE 0.32. |
config.aws_key
required
|
The AWS key credential to be used when invoking the function
|
config.aws_secret
required
|
The AWS secret credential to be used when invoking the function
|
config.aws_region
required
|
The AWS region where the Lambda function is located. Regions supported are: us-east-1 , us-east-2 , ap-northeast-1 , ap-northeast-2 , ap-southeast-1 , ap-southeast-2 , eu-central-1 , eu-west-1
|
config.function_name
required
|
The AWS Lambda function name to invoke
|
config.qualifier
optional
|
The Qualifier to use when invoking the function.
|
config.invocation_type
optional
Default value: RequestResponse
|
The InvocationType to use when invoking the function. Available types are RequestResponse , Event , DryRun
|
config.log_type
optional
Default value: Tail
|
The LogType to use when invoking the function. By default None and Tail are supported
|
config.timeout
optional
Default value: 60000
|
An optional timeout in milliseconds when invoking the function
|
config.keepalive
optional
Default value: 60000
|
An optional value in milliseconds that defines how long an idle connection will live before being closed
|
config.unhandled_status
optional
Default value: 200, 202 or 204
|
The response status code to use (instead of the default 200 , 202 , or 204 ) in the case of an Unhandled Function Error
|
config.forward_request_body
optional
Default value: false
|
An optional value that defines whether the request body is to be sent in the request_body field of the JSON-encoded request. If the body arguments can be parsed, they will be sent in the separate request_body_args field of the request. The body arguments can be parsed for application/json , application/x-www-form-urlencoded , and multipart/form-data content types.
|
config.forward_request_headers
optional
Default value: false
|
An optional value that defines whether the original HTTP request headers are to be sent as a map in the request_headers field of the JSON-encoded request.
|
config.forward_request_method
optional
Default value: false
|
An optional value that defines whether the original HTTP request method verb is to be sent in the request_method field of the JSON-encoded request.
|
config.forward_request_uri
optional
Default value: false
|
An optional value that defines whether the original HTTP request URI is to be sent in the request_uri field of the JSON-encoded request. Request URI arguments (if any) will be sent in the separate request_uri_args field of the JSON body.
|
Reminder: curl by default sends payloads with an
application/x-www-form-urlencoded
MIME type, which will naturally be URL-
decoded by Kong. To ensure special characters that are likely to appear in your
AWS key or secret (like +
) are correctly decoded, you must URL-encode them,
hence use --data-urlencode
if you are using curl. Alternatives to this
approach would be to send your payload with a different MIME type (like
application/json
), or to use a different HTTP client.
Sending parameters
Any form parameter sent along with the request, will be also sent as an
argument to the AWS Lambda function.
Known Issues
Use a fake upstream service
When using the AWS Lambda plugin, the response will be returned by the plugin
itself without proxying the request to any upstream service. This means that
a Service’s host
, port
, path
properties will be ignored, but must still
be specified for the entity to be validated by Kong. The host
property in
particular must either be an IP address, or a hostname that gets resolved by
your nameserver.
When the plugin is added to an API entity (which is deprecated as of 0.13.0),
it is the upsream_url
property which must be specified and resolvable as well
(but ignored).
Response plugins
There is a known limitation in the system that prevents some response plugins
from being executed. We are planning to remove this limitation in the future.
Step By Step Guide
The Steps
- Access to AWS Console as user allowed to operate with lambda functions and create user and roles.
- Create an Execution role in AWS
- Create an user which will be invoke the function via Kong, test it.
- Create an API in Kong, add the aws-lambda plugin linked to our aws function and execute it.
-
First, let’s create an execution role called LambdaExecutor
for our lambda function.
In IAM Console create a new Role choosing the AWS Lambda service, there will be no policies as our function in this example will simply execute itself giving us back an hardcoded JSON as response without accessing other AWS resources.
-
Now let’s create a user named KongInvoker, used by our Kong API gateway to invoke the function.
In IAM Console create a new user, must be provided to it programmatic access via Access and Secret keys; then will attach existing policies directly particularly the AWSLambdaRole predefined. Once the user creation is confirmed, store Access Key and Secret Key in a safe place.
-
Now we need to create the lambda function itself, will do so in N.Virginia Region (code us-east-1).
In Lambda Management, create a new function Mylambda, there will be no blueprint as we are going to paste the code below; for the execution role let’s choose an existing role specifically LambdaExecutor created previously
Use the inline code below to have a simple JSON response in return, note this is code for Python 3.6 interpreter.
import json
def lambda_handler(event, context):
jsonbody='''{"response": "yes"}'''
return json.loads(jsonbody)
Test the lambda function from the AWS console and make sure the execution succeeds.
-
Finally we setup the api in Kong and link it to the function just created.
The api that we are going to create doesn’t really need a real upstream_url
since we are not going to have an HTTP call to upstream but rather a response generated by our function.
curl -i -X POST http://{kong_hostname}:8001/apis \
--data 'name=lambda1' \
--data 'upstream_url=http://localhost:8000' \
--data 'uris=/lambda1'
Add the plugin:
curl -i -X POST http://{kong_hostname}:8001/apis/lambda1/plugins \
--data 'name=aws-lambda' \
--data-urlencode 'config.aws_key={KongInvoker user key}' \
--data-urlencode 'config.aws_secret={KongInvoker user secret}' \
--data 'config.aws_region=us-east-1' \
--data 'config.function_name=MyLambda'
Call the Api and verify the correct invocation, execution and response:
curl http://{kong_hostname}:8000/lambda1
Additional headers:
x-amzn-Remapped-Content-Length, X-Amzn-Trace-Id, x-amzn-RequestId
JSON response:
Have fun leveraging the power of AWS Lambda in Kong!