Invoke an AWS Lambda function from Kong. The
AWS Lambda plugin can be used in combination with other request plugins to secure, manage, or
extend the function.
Configuration Reference
This plugin is compatible with DB-less mode.
In DB-less mode, you configure Kong Gateway
declaratively.
Therefore, the Admin API is mostly read-only. The only tasks it can perform are all
related to handling the declarative config, including:
- Setting a target's health status in the load balancer
- Validating configurations against schemas
- Uploading the declarative configuration using the
/config
endpoint
Enable the plugin on a service
Admin API
Kubernetes
Declarative (YAML)
Konnect Cloud
Kong Manager
For example, configure this plugin on a service by
making the following request:
curl -X POST http://{HOST}:8001/services/{SERVICE}/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>" \
--data "config.invocation_type=RequestResponse" \
--data "config.log_type=Tail" \
--data "config.timeout=60000" \
--data "config.keepalive=60000" \
--data "config.proxy_url=http://my-proxy-server:3128"
SERVICE
is 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>
invocation_type: RequestResponse
log_type: Tail
timeout: 60000
keepalive: 60000
proxy_url: http://my-proxy-server:3128
plugin: aws-lambda
Next, apply the KongPlugin resource to a
Service by annotating the
Service as follows:
apiVersion: v1
kind: Service
metadata:
name: {SERVICE}
labels:
app: {SERVICE}
annotations:
konghq.com/plugins: <aws-lambda-example>
spec:
ports:
- port: 80
targetPort: 80
protocol: TCP
name: {SERVICE}
selector:
app: {SERVICE}
{SERVICE}
is 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
.
For example, configure this plugin on a service by
adding this section to your declarative configuration file:
plugins:
- name: aws-lambda
service: {SERVICE}
config:
aws_key: <AWS_KEY>
aws_secret: <AWS_SECRET>
aws_region: <AWS_REGION>
function_name: <LAMBDA_FUNCTION_NAME>
invocation_type: RequestResponse
log_type: Tail
timeout: 60000
keepalive: 60000
proxy_url: http://my-proxy-server:3128
SERVICE
is the id
or name
of the service that this plugin
configuration will target.
Configure this plugin on a service:
- In Konnect Cloud, select the service on the ServiceHub page.
- Scroll down to Versions and select the version.
- Scroll down to Plugins and click New Plugin.
- Find and select the AWS Lambda plugin.
-
Enter the following parameters, updating the default
or sample values as needed:
- Config.Function Name:
<LAMBDA_FUNCTION_NAME>
- Config.Invocation Type:
RequestResponse
- Config.Log Type:
Tail
- Config.Timeout:
60000
- Config.Keepalive:
60000
- Click Create.
Configure this plugin on a service:
- In Kong Manager, select the workspace.
- From the Dashboard, scroll down to Services and click View for the
service row.
- Scroll down to plugins and click Add Plugin.
-
Find and select the AWS Lambda plugin.
Note: If the plugin is greyed out, then it is not available
for your product tier. See
Kong Gateway tiers.
- If the option is available, select Scoped.
- Add the service name and ID to the Service field if it
is not already prefilled.
-
Enter the following parameters, updating the default or sample values as needed:
- Config.Function Name:
<LAMBDA_FUNCTION_NAME>
- Config.Invocation Type:
RequestResponse
- Config.Log Type:
Tail
- Config.Timeout:
60000
- Config.Keepalive:
60000
- Click Create.
Enable the plugin on a route
Admin API
Kubernetes
Declarative (YAML)
Konnect Cloud
Kong Manager
For example, configure this plugin on a route with:
$ curl -X POST http://{HOST}:8001/routes/{ROUTE}/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>" \
--data "config.invocation_type=RequestResponse" \
--data "config.log_type=Tail" \
--data "config.timeout=60000" \
--data "config.keepalive=60000" \
--data "config.proxy_url=http://my-proxy-server:3128"
ROUTE
is 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>
invocation_type: RequestResponse
log_type: Tail
timeout: 60000
keepalive: 60000
proxy_url: http://my-proxy-server:3128
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}
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
ROUTE
is 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
.
For example, configure this plugin on a route by
adding this section to your declarative configuration file:
plugins:
- name: aws-lambda
route: <route>
config:
aws_key: <AWS_KEY>
aws_secret: <AWS_SECRET>
aws_region: <AWS_REGION>
function_name: <LAMBDA_FUNCTION_NAME>
invocation_type: RequestResponse
log_type: Tail
timeout: 60000
keepalive: 60000
proxy_url: http://my-proxy-server:3128
ROUTE
is the id
or name
of the route that this plugin configuration
will target.
Configure this plugin on a route:
- In Konnect Cloud, select the service from the ServiceHub page.
- Scroll down to Versions and select the version.
- Select the route.
- Scroll down to Plugins and click Add Plugin.
- Find and select the AWS Lambda plugin.
-
Enter the following parameters, updating the default
or sample values as needed:
- Config.Function Name:
<LAMBDA_FUNCTION_NAME>
- Config.Invocation Type:
RequestResponse
- Config.Log Type:
Tail
- Config.Timeout:
60000
- Config.Keepalive:
60000
- Click Create.
Configure this plugin on a route:
- In Kong Manager, select the workspace.
- From the Dashboard, select Routes in the left navigation.
- Click View for the route row.
- Scroll down to plugins and click Add Plugin.
-
Find and select the AWS Lambda plugin.
Note: If the plugin is greyed out, then it is not available
for your product tier. See
Kong Gateway tiers.
- If the option is available, select Scoped.
- Add the Route ID if it is not already prefilled.
-
Enter the following parameters, updating the default
or sample values as needed:
- Config.Function Name:
<LAMBDA_FUNCTION_NAME>
- Config.Invocation Type:
RequestResponse
- Config.Log Type:
Tail
- Config.Timeout:
60000
- Config.Keepalive:
60000
- Click Create.
Enabling the plugin on a consumer
Admin API
Kubernetes
Declarative (YAML)
Kong Manager
For example, configure this plugin on a consumer with:
$ curl -X POST http://{HOST}:8001/consumers/{CONSUMER}/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>" \
--data "config.invocation_type=RequestResponse" \
--data "config.log_type=Tail" \
--data "config.timeout=60000" \
--data "config.keepalive=60000" \
--data "config.proxy_url=http://my-proxy-server:3128"
CONSUMER
is the id
or username
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>
invocation_type: RequestResponse
log_type: Tail
timeout: 60000
keepalive: 60000
proxy_url: http://my-proxy-server:3128
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}
annotations:
konghq.com/plugins: <aws-lambda-example>
kubernetes.io/ingress.class: kong
CONSUMER
is the id
or username
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
.
For example, configure this plugin on a consumer by
adding this section to your declarative configuration file:
plugins:
- name: aws-lambda
consumer: {CONSUMER}
config:
aws_key: <AWS_KEY>
aws_secret: <AWS_SECRET>
aws_region: <AWS_REGION>
function_name: <LAMBDA_FUNCTION_NAME>
invocation_type: RequestResponse
log_type: Tail
timeout: 60000
keepalive: 60000
proxy_url: http://my-proxy-server:3128
CONSUMER
is the id
or username
of the consumer that this plugin
configuration will target.
Configure this plugin on a consumer:
- In Kong Manager, select the workspace.
- From the Dashboard, scroll down to Consumers and click View for the consumer row.
- Select the Plugins tab.
- Click Add Plugin.
-
Find and select the AWS Lambda plugin.
Note: If the plugin is greyed out, then it is not available
for your product tier. See
Kong Gateway tiers.
- If the option is available, select Global.
-
Enter the following parameters, updating the default or sample values as needed:
- Config.Function Name:
<LAMBDA_FUNCTION_NAME>
- Config.Invocation Type:
RequestResponse
- Config.Log Type:
Tail
- Config.Timeout:
60000
- Config.Keepalive:
60000
- Click Create.
Enable the plugin globally
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.
Admin API
Kubernetes
Declarative (YAML)
Kong Manager
For example, configure this plugin globally with:
$ curl -X POST http://{HOST}: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>" \
--data "config.invocation_type=RequestResponse" \
--data "config.log_type=Tail" \
--data "config.timeout=60000" \
--data "config.keepalive=60000" \
--data "config.proxy_url=http://my-proxy-server:3128"
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>
invocation_type: RequestResponse
log_type: Tail
timeout: 60000
keepalive: 60000
proxy_url: http://my-proxy-server:3128
plugin: aws-lambda
For example, configure this plugin using the 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>
invocation_type: RequestResponse
log_type: Tail
timeout: 60000
keepalive: 60000
proxy_url: http://my-proxy-server:3128
Configure this plugin globally:
- In Kong Manager, select the workspace.
- From the Dashboard, select Plugins in the left navigation.
- Click New Plugin.
-
Find and select the AWS Lambda plugin.
Note: If the plugin is greyed out, then it is not available
for your product tier. See
Kong Gateway tiers.
- If the option is available, set the plugin scope to Global.
-
Enter the following parameters, updating the
default/sample values as needed:
- Config.Function Name:
<LAMBDA_FUNCTION_NAME>
- Config.Invocation Type:
RequestResponse
- Config.Log Type:
Tail
- Config.Timeout:
60000
- Config.Keepalive:
60000
- 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.id
Type: string |
The ID of the Service the plugin targets. |
route.id
Type: string |
The ID of the Route the plugin targets. |
consumer.id
Type: string |
The ID of the Consumer the plugin targets. |
enabled
required
Type: boolean
Default value: true |
Whether this plugin will be applied. |
config.aws_key
semi-optional
Type: string
|
The AWS key credential to be used when invoking the function. The aws_key value is required
if aws_secret is defined. If aws_key and aws_secret are not set, the plugin uses an
IAM role inherited from the instance running Kong to authenticate. Can be symmetrically encrypted
if using Kong Gateway and data encryption
is configured.
If keyring database encryption is enabled, this value will be encrypted.
|
config.aws_secret
semi-optional
Type: string
|
The AWS secret credential to be used when invoking the function. The aws_secret value is required
if aws_key is defined. If aws_key and aws_secret are not set, the plugin uses an
IAM role inherited from the instance running Kong to authenticate. Can be symmetrically encrypted
if using Kong Gateway and data encryption
is configured.
If keyring database encryption is enabled, this value will be encrypted.
|
config.aws_region
optional
Type: string
|
The AWS region where the Lambda function is located. The plugin does not
attempt to validate the supplied region name. If an invalid region name
is provided, the plugin responds with an HTTP 500 Internal Server Error
at run-time and logs a DNS resolution failure. The plugin will automatically
detect AWS region on runtime via AWS_REGION or AWS_DEFAULT_REGION environment
variables when neither region nor host is specified in plugin configuration.
Using environment variables enables regionally distributed Kong cluster nodes
to connect to the closest AWS region. If region , host and environment
variables have not been specified, the plugin responds with an HTTP
500 Internal Server Error at run-time.
|
config.host
optional
Type: string
|
The host where the Lambda function is located. This value can point to a
local Lambda server, allowing for easier debugging.
|
config.function_name
required
Type: string
|
The AWS Lambda function name to invoke.
|
config.qualifier
optional
Type: string
|
The Qualifier to use when invoking the function.
|
config.invocation_type
required
Type: string
Default value: RequestResponse
|
The InvocationType to use when invoking the function. Available types are RequestResponse , Event , DryRun .
|
config.log_type
required
Type: string
Default value: Tail
|
The LogType to use when invoking the function. By default, None and Tail are supported.
|
config.timeout
required
Type: number
Default value: 60000
|
An optional timeout in milliseconds when invoking the function.
|
config.port
optional
Type: integer
Default value: 443
|
The TCP port that the plugin uses to connect to the server.
|
config.keepalive
required
Type: number
Default value: 60000
|
An optional value in milliseconds that defines how long an idle connection lives before being closed.
|
config.unhandled_status
optional
Type: integer
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
Type: boolean
Default value: false
|
An optional value that defines whether the request body is sent in the request_body field of the JSON-encoded request.
If the body arguments can be parsed, they are 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
Type: boolean
Default value: false
|
An optional value that defines whether the original HTTP request headers are
sent as a map in the request_headers field of the JSON-encoded request.
|
config.forward_request_method
optional
Type: boolean
Default value: false
|
An optional value that defines whether the original HTTP request method verb is
sent in the request_method field of the JSON-encoded request.
|
config.forward_request_uri
optional
Type: boolean
Default value: false
|
An optional value that defines whether the original HTTP request URI is sent in
the request_uri field of the JSON-encoded request. Request URI arguments (if any) are sent in
the separate request_uri_args field of the JSON body.
|
config.is_proxy_integration
optional
Type: boolean
Default value: false
|
An optional value that defines whether the response format to receive from the Lambda to
this format.
|
config.awsgateway_compatible
optional
Type: boolean
Default value: false
|
An optional value that defines whether the plugin should wrap requests into the Amazon API gateway.
|
config.proxy_url
semi-optional
Type: string
|
An optional value that defines whether the plugin should connect through
the given proxy server URL. Include the request scheme in the URL, which
must be http . For example: http://my-proxy-server:3128 .
Kong Gateway uses HTTP tunneling via the CONNECT HTTP
method so that no details of the AWS Lambda request are leaked to the proxy server.
|
config.proxy_scheme
semi-optional
Type: string
|
As of Kong Gateway 2.8.0.0, this parameter is deprecated and will be
removed in 3.x.x.
If running Kong Gateway 2.7.x or earlier, the
proxy_scheme value is required if proxy_url is defined. In 2.8.x or
later versions, proxy_scheme is not required.
An optional value that defines which HTTP scheme to use for connecting through the proxy server. The
supported schemes are http and https .
|
config.skip_large_bodies
optional
Type: boolean
Default value: true
|
An optional value that defines whether Kong should send large
bodies that are buffered to disk. Note that enabling this option will have an impact
on system memory depending on the number of requests simultaneously in flight at any given point in time
and on the maximum size of each request. Also this option blocks all requests being handled by the
nginx workers. That could be tens of thousands of other transactions that are not being processed. For small I/O
operations, such a delay would generally not be problematic. In cases where the body size is in the order of MB,
such a delay would cause notable interruptions in request processing. Given all of the potential
downsides resulting from enabling this option, consider increasing the
client_body_buffer_size
value instead.
|
config.base64_encode_body
optional
Type: boolean
Default value: true
|
An optional value that Base64-encodes the request body.
|
Reminder: By default, cURL 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 with --data-urlencode
.
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 is also sent as an
argument to the AWS Lambda function.
Notes
If you do not provide an aws_key
and aws_secret
, the plugin uses an IAM role inherited
from the instance running Kong.
First, the plugin tries ECS metadata to get the role. If no ECS metadata is available,
the plugin falls back on EC2 metadata.
AWS Region as Environment Variable
If the plugin configuration aws_region
is unset, the plugin attempts to obtain the
AWS region through environment variables AWS_REGION
and AWS_DEFAULT_REGION
,
with the former taking higher precedence. For example, if both AWS_REGION
and
AWS_DEFAULT_REGION
are set, the AWS_REGION
value is used; otherwise, if only
AWS_DEFAULT_REGION
is set, its value is used. If neither configuration aws_region
nor environment variables are set, a run-time error “no region or host specified”
will be thrown.
Step-By-Step Guide
Prerequisite: You must have access to the AWS Console as a user who is
allowed to operate with lambda functions, and create users and roles.
-
First, create an execution role called LambdaExecutor
for your
lambda function.
In the IAM Console, create a new Role choosing the AWS Lambda service. There
will be no policies because the function in this example will simply execute
itself, returning a hardcoded JSON response without accessing other
AWS resources.
-
Create a user named KongInvoker
, used by the Kong API gateway
to invoke the function.
In the IAM Console, create a new user. Programmatic access must be provided to the user via Access and Secret keys. Then, attach existing policies directly, particularly the predefined AWSLambdaRole
. After the user creation is confirmed, store the Access Key and Secret Key in a safe place.
-
Next, create the lambda function itself in the N. Virginia Region
(code us-east-1
).
In Lambda Management, create a new function MyLambda
. There will be no blueprint because you are going to paste the code below (which is an example code snippet). For the execution role, choose the LambdaExecutor
created previously.
Note: The following code snippet is only an example. The Kong AWS Lambda plugin supports all runtimes provided by AWS. See the list of runtimes in the AWS Lambda > Functions > Create function dialog.
import json
def lambda_handler(event, context):
"""
If is_proxy_integration is set to true :
jsonbody='''{"statusCode": 200, "body": {"response": "yes"}}'''
"""
jsonbody='''{"response": "yes"}'''
return json.loads(jsonbody)
Test the lambda function from the AWS console and make sure the execution succeeds.
-
Set up a route in Kong and link it to the MyLambda
function you just created.
With a database
Without a database
Create the route:
curl -i -X POST http://<kong_hostname>:8001/routes \
--data 'name=lambda1' \
--data 'paths[1]=/lambda1'
Add the plugin:
curl -i -X POST http://<kong_hostname>:8001/routes/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'
Add a route and plugin to the declarative config file:
routes:
- name: lambda1
paths: [ "/lambda1" ]
plugins:
- route: lambda1
name: aws-lambda
config:
aws_key: {KongInvoker user key}
aws_secret: {KongInvoker user secret}
aws_region: us-east-1
function_name: MyLambda
Test your Lambda with Kong
After everything is created, make the http request 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!
Changelog
See the Kong GitHub repository for the
full plugin changelog.
Kong Gateway 2.8.x (plugin version 3.6.3)
- The
proxy_scheme
configuration parameter is deprecated and planned to be
removed in 3.x.x.
Kong Gateway 2.7.x (plugin version 3.6.0)
- Starting with Kong Gateway 2.7.0.0, if keyring encryption is enabled,
the
config.aws_key
and config.aws_secret
parameter values will be encrypted.