aws-lambda

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.

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”.

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

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. HTTP/2 200
  2. content-length: 16
  3. x-amz-apigw-id: JdwulHHrIAMFoFg=
  4. date: Sat, 27 Nov 2021 13:10:53 GMT
  5. x-amzn-trace-id: Root=1-61a22e5d-342eb64077dc9877644860dd;Sampled=0
  6. x-amzn-requestid: a2c2b799-ecc6-44ec-b586-38c0e3b11fe4
  7. server: APISIX/2.10.2
  8. "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. "secretkey": "<access key secret>"
  9. }
  10. },
  11. "ssl_verify": false
  12. }
  13. "uri": "/aws"
  14. }'
note

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 -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!"

To disable the 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.