aws-lambda

Description

The aws-lambda Plugin is used for integrating APISIX with AWS Lambda as a dynamic upstream to proxy all requests for a particular URI to the AWS Cloud.

When enabled, the Plugin terminates the ongoing request to the configured URI and initiates a new request to the AWS Lambda Gateway URI on behalf of the client with configured authorization details, request headers, body and parameters (all three passed from the original request). It returns back the response with headers, status code and the body to the client that initiated the request with APISIX.

This Plugin supports authorization via AWS API key and AWS IAM secrets.

Attributes

NameTypeRequiredDefaultValid valuesDescription
function_uristringTrueAWS API Gateway endpoint which triggers the lambda serverless function.
authorizationobjectFalseAuthorization credentials to access the cloud function.
authorization.apikeystringFalseGenerated API Key to authorize requests to the AWS Gateway endpoint.
authorization.iamobjectFalseUsed for AWS IAM role based authorization performed via AWS v4 request signing. See IAM authorization schema.
timeoutintegerFalse3000[100,…]Proxy request timeout in milliseconds.
ssl_verifybooleanFalsetruetrue/falseWhen set to true performs SSL verification.
keepalivebooleanFalsetruetrue/falseWhen set to true keeps the connection alive for reuse.
keepalive_poolintegerFalse5[1,…]Maximum number of requests that can be sent on this connection before closing it.
keepalive_timeoutintegerFalse60000[1000,…]Time is ms for connection to remain idle without closing.

IAM Authorization Schema

NameTypeRequiredDefaultDescription
accesskeystringTrueGenerated access key ID from AWS IAM console.
secret_keystringTrueGenerated access key secret from AWS IAM console.
aws_regionstringFalse“us-east-1”AWS region where the request is being sent.
servicestringFalse“execute-api”The service that is receiving the request. For HTTP trigger, it is “execute-api”.

Enabling the Plugin

The example below shows how you can configure the Plugin on a specific Route:

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "plugins": {
  4. "aws-lambda": {
  5. "function_uri": "https://x9w6z07gb9.execute-api.us-east-1.amazonaws.com/default/test-apisix",
  6. "authorization": {
  7. "apikey": "<Generated API Key from aws console>",
  8. },
  9. "ssl_verify":false
  10. }
  11. },
  12. "uri": "/aws"
  13. }'

Now, any requests (HTTP/1.1, HTTPS, HTTP2) to the endpoint /aws will invoke the configured AWS Functions URI and the response will be proxied back to the client.

In the example below, AWS Lambda takes in name from the query and returns a message “Hello $name”:

  1. curl -i -XGET localhost:9080/aws\?name=APISIX
  1. HTTP/1.1 200 OK
  2. Content-Type: application/json
  3. Connection: keep-alive
  4. Date: Sat, 27 Nov 2021 13:08:27 GMT
  5. x-amz-apigw-id: JdwXuEVxIAMFtKw=
  6. x-amzn-RequestId: 471289ab-d3b7-4819-9e1a-cb59cac611e0
  7. Content-Length: 16
  8. X-Amzn-Trace-Id: Root=1-61a22dca-600c552d1c05fec747fd6db0;Sampled=0
  9. Server: APISIX/2.10.2
  10. "Hello, APISIX!"

Another example of a request where the client communicates with APISIX via HTTP/2 is shown below (make sure you have configured enable_http2: true for a in your default configuration file (config-default.yaml). You can do this by uncommenting the port 9081 from the field apisix.node_listen):

  1. curl -i -XGET --http2 --http2-prior-knowledge localhost:9081/aws\?name=APISIX
  1. HTTP/2 200
  2. content-type: application/json
  3. content-length: 16
  4. x-amz-apigw-id: JdwulHHrIAMFoFg=
  5. date: Sat, 27 Nov 2021 13:10:53 GMT
  6. x-amzn-trace-id: Root=1-61a22e5d-342eb64077dc9877644860dd;Sampled=0
  7. x-amzn-requestid: a2c2b799-ecc6-44ec-b586-38c0e3b11fe4
  8. server: APISIX/2.10.2
  9. "Hello, APISIX!"

Similarly the function can be triggered via AWS API Gateway by using AWS IAM permissions for authorization. The Plugin includes authentication signatures in HTTP calls via AWS v4 request signing. The example below shows this method:

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "plugins": {
  4. "aws-lambda": {
  5. "function_uri": "https://ajycz5e0v9.execute-api.us-east-1.amazonaws.com/default/test-apisix",
  6. "authorization": {
  7. "iam": {
  8. "accesskey": "<access key>",
  9. "secretkey": "<access key secret>"
  10. }
  11. },
  12. "ssl_verify": false
  13. }
  14. },
  15. "uri": "/aws"
  16. }'
aws-lambda - 图1note

This approach assumes that you have already an IAM user with programmatic access enabled with the required permissions (AmazonAPIGatewayInvokeFullAccess) to access the endpoint.

Configuring path forwarding

The aws-lambda Plugins also supports URL path forwarding while proxying requests to the AWS upstream. Extensions to the base request path gets appended to the function_uri specified in the Plugin configuration.

aws-lambda - 图2IMPORTANT

The uri configured on a Route must end with * for this feature to work properly. APISIX Routes are matched strictly and the * implies that any subpath to this URI would be matched to the same Route.

The example below configures this feature:

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "plugins": {
  4. "aws-lambda": {
  5. "function_uri": "https://x9w6z07gb9.execute-api.us-east-1.amazonaws.com",
  6. "authorization": {
  7. "apikey": "<Generate API key>"
  8. },
  9. "ssl_verify":false
  10. }
  11. },
  12. "uri": "/aws/*"
  13. }'

Now, any requests to the path aws/default/test-apisix will invoke the AWS Lambda Function and the added path is forwarded:

  1. curl -i -XGET http://127.0.0.1:9080/aws/default/test-apisix\?name\=APISIX
  1. HTTP/1.1 200 OK
  2. Content-Type: application/json
  3. Connection: keep-alive
  4. Date: Wed, 01 Dec 2021 14:23:27 GMT
  5. X-Amzn-Trace-Id: Root=1-61a7855f-0addc03e0cf54ddc683de505;Sampled=0
  6. x-amzn-RequestId: f5f4e197-9cdd-49f9-9b41-48f0d269885b
  7. Content-Length: 16
  8. x-amz-apigw-id: JrHG8GC4IAMFaGA=
  9. Server: APISIX/2.11.0
  10. "Hello, APISIX!"

Disable Plugin

To disable the aws-lambda Plugin, you can delete the corresponding JSON configuration from the Plugin configuration. APISIX will automatically reload and you do not have to restart for this to take effect.

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "uri": "/aws",
  4. "plugins": {},
  5. "upstream": {
  6. "type": "roundrobin",
  7. "nodes": {
  8. "127.0.0.1:1980": 1
  9. }
  10. }
  11. }'