Coder Social home page Coder Social logo

dianaionita / serverless-api-gateway-caching Goto Github PK

View Code? Open in Web Editor NEW
136.0 5.0 35.0 304 KB

A plugin for the Serverless framework which helps with configuring caching for API Gateway endpoints.

License: ISC License

JavaScript 100.00%
api-gateway serverless-framework serverless-plugin caching

serverless-api-gateway-caching's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

serverless-api-gateway-caching's Issues

License

HI,

I was wondering if you would be willing to put a license on this repo, maybe something like MIT? I wonder if having no license even allows projects to use this?

Internal Server Error when setting cache key parameter request.header.Authorization

I have the following in serverless.yml:

GET method that uses Lambda Proxy Integration:

        caching:
            enabled: true
            cacheKeyParameters:
              - name: request.header.Authorization
GET /api-path
Headers:
  Authorization: Bearer <token>

However, this always yields no results due to Internal Server Error when there should be results.
By removing the caching for the method, it works fine.

I am using a custom Token-based lambda authorizer. The token source is "Authorization". Not sure if that makes a difference?

Here's the error I see from X-Ray trace:

Error 403: Execution failed due to configuration error: The request signature we calculated does not match the signature you provided.

Any ideas?

Problem in deploying ttlInSeconds and perKeyInvalidation

Following are my global properties

custom:
configFile: ${file(../config.${self:provider.stage}.yml)}
apiGatewayCaching:
enabled: true
clusterSize: '0.5' # defaults to '0.5'
dataEncrypted: true
ttlInSeconds: 900 # defaults to the maximum allowed: 3600
perKeyInvalidation:
requireAuthorization: false

and my function has these cache properties.

caching:
enabled: true
cacheKeyParameters:
- name: request.path.proxy

The properties ttlInSeconds and perKeyInvalidation are not reflected on API Gateway console when i deploy this. All other properties can be verified.
Can someone please suggest ?

Cannot read property 'cachingEnabled' of undefined

Summary

A two step deployment throws an exception Cannot read property 'cachingEnabled' of undefined. This is likely due to the this.settings not being initialised during the deploy step.

Steps to reproduce

  1. serverless package
  2. serverless deploy --package ./.serverless

Stack trace

Type Error ---------------------------------------------

Cannot read property 'cachingEnabled' of undefined

 For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

Stack Trace --------------------------------------------

TypeError: Cannot read property 'cachingEnabled' of undefined
at updateStageCacheSettings (/Project Root/node_modules/serverless-api-gateway-caching/src/stageCache.js:133:16)
at ApiGatewayCachingPlugin.updateStage (/Project Root/node_modules/serverless-api-gateway-caching/src/apiGatewayCachingPlugin.js:47:12)
at BbPromise.reduce (/Project Root/node_modules/serverless/lib/classes/PluginManager.js:391:55)
From previous event:
at PluginManager.invoke (/Project Root/node_modules/serverless/lib/classes/PluginManager.js:391:22)
at PluginManager.run (/Project Root/node_modules/serverless/lib/classes/PluginManager.js:422:17)
at variables.populateService.then.then (/Project Root/node_modules/serverless/lib/Serverless.js:157:33)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
at process.topLevelDomainCallback (domain.js:121:23)
From previous event:
at Serverless.run (/Project Root/node_modules/serverless/lib/Serverless.js:144:8)
at serverless.init.then (/Project Root/node_modules/serverless/bin/serverless:44:28)

Get Support --------------------------------------------
Docs: docs.serverless.com
Bugs: github.com/serverless/serverless/issues
Issues: forum.serverless.com

Your Environment Information -----------------------------
OS: darwin
Node Version: 10.14.1
Serverless Version: 1.34.0

multivaluequerystring fails to build

Hi,

Do you have an example of how to use request.multivaluequerystring ->

I tried to deploy it and I just get this error below

Invalid mapping expression specified: Validation Result: warnings : [], errors : [Invalid mapping expression specified: method.request.multivaluequerystring.versionId] (Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException; Request ID: efe76a48-f2c6-4da1-b089-d45d13330232; Proxy: null)
          caching:
            enabled: true
            cacheKeyParameters:
              - name: request.multivaluequerystring.versionId
              - name: request.multivaluequerystring._id
              - name: request.multivaluequerystring.pluginVersion

any info on how to use multivaluequerystring would be helpful, I have three keys i need to cache against, versionid&id& pluginVersionId

Cheers

[Bug]: Empty paths cause issues with caching settings. Properly handle empty paths.

Summary:

When the path resolves to "", the stage level cache settings are auto propagated to this endpoint.

Config:

custom:
  apiGatewayCaching:
    enabled: true
    clusterSize: '0.5'
    ttlInSeconds: 600
    perKeyInvalidation:
      requireAuthorization: false

functions:
  blah:
    events:
      - http:
          path: ""
          method: get
      - http:
          path: "/{id}"
          method: get
          caching:
            enabled: true
            cacheKeyParameters:
              - name: request.path.id

On Deploy

  • Serverless handles the empty paths as meaning root "/".
  • Serverless succeeds in creating the endpoints on the apigateway stage.
  • serverless-api-gateway-caching applies the settings and responds with a success.
  • [outcome] cache is enabled on stage.
  • [problem] the endpoint cache did not get disabled.

i.e.
Even though this endpoint "/" does not have caching enabled, since the plugin silently fails with undefined for the operation, the stage level cache settings are propagated to this specific endpoint.

Version Info

npm run sls info

  Your Environment Information ---------------------------
     Operating System:          darwin
     Node Version:              12.14.1
     Framework Version:         1.71.3
     Plugin Version:            3.8.4
     SDK Version:               2.3.2
     Components Version:        2.34.9

package-lock.json

{
    "serverless-api-gateway-caching": {
      "version": "1.7.3",
      "resolved": "https://registry.npmjs.org/serverless-api-gateway-caching/-/serverless-api-gateway-caching-1.7.3.tgz",
      "integrity": "sha512-SqcuqAlAsOqbkHgwcyTh835TmkJfh/r9nbcjaC8gXXDZ6il3aZxtFgeX3fUGMaOl9hcC9SSJAkSO1ESL+oTGvQ==",
      "dev": true,
      "requires": {
        "lodash.get": "^4.4.2",
        "lodash.isempty": "^4.4.0"
      }
    },
}

ability to encrypt cache

In the stage cache settings, there is an option for Encrypt cache data. The ability to set this to true would be nice.

Cache miss and no cache when using custom domain

Hi, I followed the instructions, Im using a custom domain. When calling the function directly I always see a cache miss and when using the custom domain I dont even see the cache header.

Are there any instructions with detailed steps?

Thanks
Joao

Two-step deployment - No Rest API found. Caching settings will not be updated.

Summary

The plugin throws a [serverless-api-gateway-caching] No Rest API found. Caching settings will not be updated. in a two-step serverless deployment process.

  1. severless package
  2. severless deploy --package .serverless

Solution hints

The problem may lie in the check for the weather a Rest API Gateway resource is defined (src here).
This check might need to perform a re-evaluation of this.thereIsARestApi = restApiExists(this.serverless);.

Does not work with "serverless-aws-alias" plugin

I've realized that the plugin does not work for aliases. Every time I deploy to any alias it performs all actions on the stage instead of alias name.

Plugins:

plugins:
  - serverless-python-requirements
  - serverless-aws-alias
  - serverless-api-gateway-caching
  - serverless-api-gateway-throttling

Fun:

functions:
  energy:
    handler: src/handler.energy
    package: {}
    events:
      - http:
          path: energy
          method: get
          cors: true
          private: true
          caching:
            enabled: true
            clusterSize: '0.5'
            ttlInSeconds: 60
            perKeyInvalidation:
              requireAuthorization: true
              handleUnauthorizedRequests: Ignore
            cacheKeyParameters:
              - name: request.header.x-api-key

Am I doing something wrong or is there any workaround? Thanks!

[serverless-api-gateway-caching] No Rest API found. Caching settings will not be updated - when used in conjunction with serverless-nested-stack.

The serverless-nested-stack breaks down the stacks in few nested stacks to avoid the cloudformation limitation of 200 resources per stack.
And when I use serverless-nested-stack breaks and serverless-api-gateway-caching I get:
Serverless: [serverless-api-gateway-caching] No Rest API found. Caching settings will not be updated.

It seems to me that the code:
https://github.com/DianaIonita/serverless-api-gateway-caching/blob/develop/src/restApiId.js#L15
is not checking in all nested stacks if resource 'ApiGatewayRestApi' exists!
Let me know if you need me to provide any additional information!

cache global with ttl = 0 nor working

when setup ttlInSeconds: 0 and deploy code, deployed cache ttl is 3600 , because const DEFAULT_TTL = 3600;

it is not possible to implement ttl = 0 and this can be very useful to enable global cache without storage but if cache on specific endpoints

No Rest API found. Caching settings will not be updated. when using shared API GW

There seems to be an issue with the plugin when attempting to use it with the shared API Gateway.

It looks like when using a shared API GW, defined through

provider:
  apiGateway:
    restApiId:
      'Fn::ImportValue': CfOutputRefFromAnotherStack
    restApiRootResourceId:
      'Fn::ImportValue': CfOutputRefFromAnotherStack

the plugin throws the No Rest API found. Caching settings will not be updated. error.

This seems to be caused by the following code:

let resource = this.serverless.service.provider.compiledCloudFormationTemplate.Resources['ApiGatewayRestApi'];

While the plugin seems to be referencing
get(serverless, 'service.provider.apiGateway.restApiId');
to get a custom API GW id, it does not seem to pass the validation (as SLS does not add an external API GW to the service.provider.compiledCloudFormationTemplate.Resources). Manually overiding the restApiExists function to return true successfully deploys the functions and correctly enables caching.

In addition (and this is probably out of scope of this issue and the plugin), but it would be nice to be able to apply caching settings to the individual functions without having to define the caching settings for the gateway (for uses cases where you have functions in many different serverless.yml files that you want to add caching to and an API GW defined in another service/stack).

Http shorthand events is not supported

Summary

Declaring a HTTP event using the shorthand notation produces an error.

Example

lambda:
  handler: src/lambda.handler
  events:
    - http: get /users

Debug output

Type Error ---------------------------------------------

  Cannot read property 'toUpperCase' of undefined

     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

Stack Trace --------------------------------------------

TypeError: Cannot read property 'toUpperCase' of undefined
    at createPatchForEndpoint (/<project root>/node_modules/serverless-api-gateway-caching/src/stageCache.js:88:14)
    at updateStageCacheSettings (/<project root>/node_modules/serverless-api-gateway-caching/src/stageCache.js:128:25)
From previous event:
    at PluginManager.invoke (/<project root>/node_modules/serverless/lib/classes/PluginManager.js:372:22)
    at PluginManager.run (/<project root>/node_modules/serverless/lib/classes/PluginManager.js:403:17)
    at variables.populateService.then (/<project root>/node_modules/serverless/lib/Serverless.js:102:33)
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
    at process.topLevelDomainCallback (domain.js:121:23)
From previous event:
    at Serverless.run (/<project root>/node_modules/serverless/lib/Serverless.js:89:74)
    at serverless.init.then (/<project root>/node_modules/serverless/bin/serverless:42:50)

URL Query String Parameters Required

schermafbeelding 2018-10-09 om 16 54 48

Nice plugin! finally i do not have to make a workaround in my resources anymore.
But i still have a small problem with Query strings.

enable caching works without problems but it also makes them required.
Is it also possible to make them not required, but caching turned on?

Proxy integration type header should not be specified.

When using your plugin with an api that is set to use lambda Proxy integration true. The API call will fail only sometimes depending on what headers you are using and if there is authorization on it.
I have confirmed with AWS support the integration request cannot add query parameters or headers when the type is lambda proxy. And it usually ignores these except when you have an authorizer.
Function definition
sampleProtectedRoute:
handler: handler.sampleProtectedRoute
events:
- http:
path: sampleprotectedroute
method: get
cors: true
private: true
request:
parameters:
headers:
Authorization: true
caching:
enabled: true
cacheKeyParameters:
- name: request.header.Authorization
authorizer:
name: authorize
resultTtlInSeconds: 60
identitySource: method.request.header.Authorization, context.identity.sourceIp
type: request

If I go into API gateway uncheck proxy integration and then remove the header it works.
I also verified this with Jared at serverless.
He made a quick fix for me that works
"serverless-api-gateway-caching": "git://github.com/shortjared/serverless-api-gateway-caching.git#develop",

But he is not check the function is Proxy integration.
Did he reach out to you?
And if you use the console to make an api the minute you check Proxy integration check box the ability to list url path, query strings, http headers and mapping template goes away.

This only breaks when authorization is on.

The results are
{
"message": ""
}
Execution log for request 9f4d355f-2c6f-11e9-8d0a-9dcbaa5e4066
Sat Feb 09 13:35:53 UTC 2019 : Starting execution for request: 9f4d355f-2c6f-11e9-8d0a-9dcbaa5e4066
Sat Feb 09 13:35:53 UTC 2019 : HTTP Method: GET, Resource Path: /sampleprotectedroute
Sat Feb 09 13:35:53 UTC 2019 : Method request path: {}
Sat Feb 09 13:35:53 UTC 2019 : Method request query string: {}
Sat Feb 09 13:35:53 UTC 2019 : Method request headers: {Authorization=**************************************************************************************************************************************************************************************************************************************************************************************************e2nE3E}
Sat Feb 09 13:35:53 UTC 2019 : Method request body before transformations:
Sat Feb 09 13:35:53 UTC 2019 : Request validation succeeded for content type application/json
Sat Feb 09 13:35:53 UTC 2019 : Endpoint request URI: https://lambda.us-east-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-east-1:924149985142:function:iam-auth-api-dev-sampleProtectedRoute/invocations
Sat Feb 09 13:35:53 UTC 2019 : Endpoint request headers: {Authorization=**************************************************************************************************************************************************************************************************************************************************************************************************e2nE3E,**************************************************************************************************************************************************************************************************************************************************************************************************************************************262cc1, x-amzn-lambda-integration-tag=9f4d355f-2c6f-11e9-8d0a-9dcbaa5e4066, X-Amz-Date=20190209T133553Z, x-amzn-apigateway-api-id=7f16zarwv7, X-Amz-Source-Arn=arn:aws:execute-api:us-east-1:924149985142:7f16zarwv7/test-invoke-stage/GET/sampleprotectedroute, Accept=application/json, User-Agent=AmazonAPIGateway_7f16zarwv7, X-Amz-Security-Token=FQoGZXIvYXdzEB0aD [TRUNCATED]
Sat Feb 09 13:35:53 UTC 2019 : Endpoint request body after transformations: {"resource":"/sampleprotectedroute","path":"/sampleprotectedroute","httpMethod":"GET","headers":{"Authorization":" eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHBpcmVzSW4iOiIxIGRheSIsImlzc3VlciI6ImxlZ2FjeSByZXNlYXJjaCIsImF1ZGllbmNlIjoic3Vic2NyaWJlcnMiLCJzdWJqZWN0Ijp7ImN1c3RvbWVyTnVtYmVyIjoiU0FDMDAxMTc0MzU2OSIsInVzZXJJcCI6IjE3Mi42OC41NC4xMjIifSwiaWF0IjoxNTQ4NTQyODg1fQ.Uromv03zOk03wY7ilS3fA5rFulxRGRNGKAfvle2nE3E"},"multiValueHeaders":{"Authorization":[" eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHBpcmVzSW4iOiIxIGRheSIsImlzc3VlciI6ImxlZ2FjeSByZXNlYXJjaCIsImF1ZGllbmNlIjoic3Vic2NyaWJlcnMiLCJzdWJqZWN0Ijp7ImN1c3RvbWVyTnVtYmVyIjoiU0FDMDAxMTc0MzU2OSIsInVzZXJJcCI6IjE3Mi42OC41NC4xMjIifSwiaWF0IjoxNTQ4NTQyODg1fQ.Uromv03zOk03wY7ilS3fA5rFulxRGRNGKAfvle2nE3E"]},"queryStringParameters":null,"multiValueQueryStringParameters":null,"pathParameters":null,"stageVariables":null,"requestContext":{"path":"/sampleprotectedroute","accountId":"924149985142","resourceId":"678gti","stage":"test-inv [TRUNCATED]
Sat Feb 09 13:35:53 UTC 2019 : Sending request to https://lambda.us-east-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-east-1:924149985142:function:iam-auth-api-dev-sampleProtectedRoute/invocations
Sat Feb 09 13:35:53 UTC 2019 : Received response. Integration latency: 2 ms
Sat Feb 09 13:35:53 UTC 2019 : Endpoint response body before transformations:

<title>400 Bad Request</title>

400 Bad Request

Sat Feb 09 13:35:53 UTC 2019 : Endpoint response headers: {Server=awselb/2.0, Date=Sat, 09 Feb 2019 13:35:53 GMT, Content-Type=text/html, Content-Length=138, Connection=close}
Sat Feb 09 13:35:53 UTC 2019 : Lambda invocation failed with status: 400. Lambda request id: N/A
Sat Feb 09 13:35:53 UTC 2019 : Execution failed:
Sat Feb 09 13:35:53 UTC 2019 : Method completed with status: 400

Multiple HTTP events and custom name

Hi @DianaIonita,

I've just created a pull request which adds support for multiple HTTP events and allows the user to have a custom name for the lambda functions: #18. Maybe take a look and let me know what you think :)

Luci

Miss from cloudfront

Should this be showing a hit from cloudfront if configured correctly?

This is what I have and doesn't seem to be caching:

custom:
  splitStacks:
    perFunction: true
    perType: true
  warmup:
    enabled: true
    events:
      - schedule: rate(15 minutes)
    prewarm: true
  apiGatewayXray: false
  baseUrl: v1
  prune:
    automatic: true
    number: 2
  apiGatewayCaching:
    enabled: true

plugins:
  - serverless-plugin-split-stacks
  - serverless-plugin-warmup
  - serverless-prune-plugin
  - serverless-api-gateway-caching

For functions:

getStats:
  handler: src/functions/reports.getStats
  events:
    - http:
        path: ${self:custom.baseUrl}/reports/stats
        method: get
        private: true
        cors: true
        caching:
          enabled: true

Thank you!

Question about caching

When caching is enabled (using query string key parameter), and they visit a URL without the query string cache parameter, the request is still cached. Is there a way to not cache the response unless query string parameter is present?

Desired output

cacheKeyParameters:
  - name: request.querystring.cache

This should not cache or return a cached response
api.domain.com/v2/users

This should cache and return a cached response
api.domain.com/v2/users?cache=1

The method XXX couldn't be found in the compiled CloudFormation template

When I create a lambda with an underscore in its path:

get-linked-products-by-model-id:
  handler: models/handler.getLinkedProductsByModelId
  events:
    - http:
        path: /v1/models/{model_id}/linked
        method: get
        private: true

The generated CloudFormation method is ApiGatewayMethodV1ModelsModelidVarLinkedGet but this plugin try to find ApiGatewayMethodV1ModelsModel_idVarLinkedGet.

Caching lambda proxy requests

I have a lambda function which proxy's several requests under a /v1/pages/{proxy+} path.

Is it possible to set individual cache keys for each page underneath it?

Path examples.

/v1/pages/home
/v1/pages/categories
/v1/pages/categories/:categoryAlias

    handler: index.main
    events:
      - http:
          path: '/v1/pages/{proxy+}'
          method: any
          cors: true
          caching:
            enabled: true

cache settings not applying to endpoints in v1.2.x

Tested:

  • v1.2.2
  • v1.2.1
  • v1.2.0
  • v1.1.6 <-- Works

serverless.yml

service: template

custom:
  cors:
    origin: '*'
    headers:
      - Authorization
      - Content-Type
      - X-Amz-Date
      - X-Amz-Security-Token
      - X-Amz-User-Agent
      - X-Api-Key
    allowCredentials: false
  contentEncoding:
    contentCompression: 1400
  apiGatewayCaching:
    enabled: true
    clusterSize: '0.5'
    ttlInSeconds: 300
    # TODO encrypt cache
    perKeyInvalidation:
      requireAuthorization: false
      handleUnauthorizedRequests: Ignore

provider:
  name: aws
  runtime: nodejs8.10
  versionFunctions: false
  region: "ca-central-1"

package:
  individually: false
  excludeDevDependencies: true
  exclude:
    - ./**
  include:
    - package.json
    - node_modules/**
    - '!node_modules/serverless/**'
    - '!node_modules/serverless-*/**'
    - src/**

functions:
  health:
    description: Used for health checks
    memorySize: 128
    timeout: 30
    handler: src/handlers/health.handler
    events:
      - http:
          path: health
          method: GET
          cors: ${self:custom.cors}
          caching:
            enabled: true
            ttlInSeconds: 3600

plugins:
  - serverless-api-gateway-caching

Generate RequestParameters with invalid value, empty object

Assuming you have this lambda:

get-tree:
  handler: trees/handler.getTree
  events:
    - http:
        path: /v1/trees
        method: get
        private: true
        caching:
          enabled: true
          cacheKeyParameters:
            - name: request.header.x-target-country
            - name: request.queryString.store_id
        request:
          parameters:
            queryStrings:
              store_id: false
            headers:
              x-target-country: true
              channel: true

When serverless (with this plugin activate), the serverless-state.json contains this CloudFormation template:

"ApiGatewayMethodV1TreesGet": {
  "Type": "AWS::ApiGateway::Method",
  "Properties": {
    "HttpMethod": "GET",
    "RequestParameters": {
      "method.request.header.x-target-country": true,
      "method.request.header.channel": true,
      "method.request.querystring.store_id": {}
    },
    "ResourceId": {
      "Ref": "ApiGatewayResourceV1Trees"
    },
    "RestApiId": {
      "Ref": "ApiGatewayRestApi"
    },
    "ApiKeyRequired": true,
    "AuthorizationType": "NONE",
    "Integration": {
      "IntegrationHttpMethod": "POST",
      "Type": "AWS_PROXY",
      "Uri": {
        // No interesting stuff here
      },
      "CacheKeyParameters": [
        "method.request.header.x-target-country",
        "method.request.querystring.store_id"
      ],
      "RequestParameters": {
        "integration.request.header.x-target-country": "method.request.header.x-target-country",
        "integration.request.querystring.store_id": "method.request.querystring.store_id"
      },
      "CacheNamespace": "ApiGatewayMethodV1TreesGetCacheNS"
    },
    "MethodResponses": []
  }
}

As you can see on the RequestParameters property, method.request.querystring.store_id key has the value {} but should be a boolean. Between 1.2.2 and 1.1.3, this is the only diff change (always true in 1.1.3).

Suppose, you just need to switch the default value at this line from {} to false but I don't understand why existingValue is null or undefined for my query string.

I wanted to contribute directly to your project but you have 7 tests who fail because you assume that you are waiting a javascript object.

Is there a way to cache the whole query string?

I know that there is a way to cache the path using this configuration:

`cacheKeyParameters:
- name: request.path.proxy

Is there a way to do the same with the query string?
instead of naming the fields, because i have dynamic filters, i need it to be dynamic.

Caching Settings Not Overriding Parent Settings

Hello, am trying to configure API Gateway caching using two endpoints, / which is cached, and /admin/, which is uncached.

I have tried two different ways:

    events:
      - http:
          path: /
          method: get
          cors: true # <-- CORS!
          caching:
            enabled: true
            cacheKeyParameters:
              - name: request.querystring.country
              - name: request.querystring.siteCode
              - name: request.querystring.slug
              - name: request.path.id
      - http:
          path: /admin/
          method: get
          cors: true # <-- CORS!
          authorizer: aws_iam

or

    events:
      - http:
          path: /
          method: get
          cors: true # <-- CORS!
          caching:
            enabled: true
            cacheKeyParameters:
              - name: request.querystring.country
              - name: request.querystring.siteCode
              - name: request.querystring.slug
              - name: request.path.id
      - http:
          path: /admin/
          method: get
          cors: true # <-- CORS!
          authorizer: aws_iam
          caching:
            enabled: false

Either way, the resulting deployment does not override the / method
Screen Shot 2020-10-02 at 10 25 35 AM

Is this the desired effect, and is there any way to get around this, other than converted them to sibling endpoints (aka /admin and /not-admin?

Caching was not enabled following provided example

I installed this via npm without issue.

Next, I followed the serverless.yml example from https://www.npmjs.com/package/serverless-api-gateway-caching, and added these to mine.

  plugins:
  - serverless-domain-manager
  - serverless-api-gateway-caching

  site:
    handler: bin/details
    timeout: 30
    events:
      - http:
          path: v1/details/{detail_id}
          method: GET
          cors: true
          authorizer:
            arn: ${ssm:/serverless/global/cognito_arn}
          private: true
          cacheKeyParameters:
            - name: request.path.detail_id
              required: true
            - name: request.header.Accept-Language
              required: false 

I ran the serverless deploy command.
saml2aws exec 'npm run -- serverless deploy -v'
Again no issues.

However when I went to the API Gateway in the AWS console, the caching option for this request was not enabled.

It would be great if this worked as we have not had success trying other options.

Configuration schema extension does not pass strict validation mode

Hello,

along with the upcoming v3 release of the Serverless Framework, internal schema validation has been improved and now enforces strict validation mode. Unfortunately, after preliminary tests, it seems like the schema extension implemented in this plugin does not adhere to strict validation rules and needs to be adjusted in order to support v3 release of the Framework. The best way to verify that is to use the plugin along with the latest v3 prerelease (or official release soon).

I'm happy to answer any additional questions.

Method couldn't be found in the compiled CloudFormation template

Hello,

Im getting this following error when I try to deploy the serverless code. The app has been in production for quite sometime and now its showing this error and I need to modify the cache settings

The method ApiGatewayMethodInstant-articleSlugVarGet couldn't be found in the compiled CloudFormation template. Caching settings will not be updated for this endpoint.

  instantarticlebyslug:
    handler: services/instantarticles/postbyslug.show
    environment:
      ENDPOINT_URL: <domain>
    events:
      - http:
          path: post/{slug}/instant-articles
          method: get
          caching:
            enabled: true
            cacheKeyParameters:
              - name: request.path.slug

I think its the - in instant-articles causing the issue

Serverless: WARNING: Inappropriate call of provider.request()

Summary

Enabling debug logs SLS_DEBUG=* reveals serverless warnings.

Serverless: WARNING: Inappropriate call of provider.request()

This is likely due to calling aws.request() with the stage and region arguments, which is no longer supported.

Upgrade from 1.7.5 to 1.8.0 breaking API Gateway endpoint

Hi

The changes here #107 are setting these request parameters and breaking the API endpoint that has them applied. The Cloudformation template now has:

"RequestParameters":
  {
    "integration.request.header.Origin": "method.request.header.Origin",
    "integration.request.header.origin": "method.request.header.origin",
  },

When before it was:

"RequestParameters": {}

The serverless.yml entry looks like:

  health:
    handler: src/api/v1/health.handler
    events:
      - http:
          path: v1/health
          method: get
          cors: true
          caching:
            enabled: true
            cacheKeyParameters:
              - name: request.header.Origin
              - name: request.header.origin

Is there something that I can do to fix this?

Enable/disable option not working for shared API GW

When using shared API GW, the global configuration apiGatewayCaching.enable and endpoint configuration caching.enable doesn't work as expected. As long as the cache is enabled for shared API GW, it will be enabled for all individual endpoints in it.

Wondering is those two options designed only for dedicated API GWs? Thanks

Support for shared api gateway

We are sharing out api gateway in multiple sub stack (not using the plugin but splitting the stacks manually).
Now if we use the plugin in more than one stack the base configuration will be updated from each stack (like cache enable or cluster size).
I've created a PR to support this use case it would be nice to have this feature integrated #55

No Rest API found. Caching settings will not be updated.

Hi
I get the above message when trying to use the plugin. Here is my serverless.yml

provider:
  name: aws
  runtime: nodejs8.10
  stage: ${opt:stage, 'test'}
  region: eu-west-1

functions:
  app:
    handler: src/index.handler
    events:
      - http:
          path: 'products/{id}'
          method: 'GET'
          resp: http
          private: true
          cors: true
          caching:
            enabled: true
            ttlInSeconds: 300
            cacheKeyParameters:
              - name: request.path.id

package:
  individually: true
  exclude:
    - tests/**

plugins:
  - serverless-webpack
  - serverless-api-gateway-caching
  # - serverless-domain-manager

custom:
  webpack:
    webpackConfig: 'webpack.prod.js'
    includeModules: true
    packager: 'npm'

  apiGatewayCaching:
    enabled: true

Configuration warning unrecognized property 'caching'

With the property caching inside like this one:

 companies:
    handler: src/index.companiesHandler
    events:
      - http:
          path: '{version}/companies'
          method: ANY
          cors:
            origin: "*"
            headers: ${self:custom.allowed-headers}
            allowCredentials: true
          private: false
          caching:
            enabled: true #false
            ttlInSeconds: 120 #3600
            cacheKeyParameters:
              - name: request.querystring.regionId
              - name: request.querystring.skip
              - name: request.querystring.take
              - name: request.querystring.filterName
              - name: request.querystring.filterValue

The Service configuration validation throws Configuration warning unrecognized property 'caching'.
According to service configuration validation page:
Configuration related to external plugin does not have an associated JSON Schema. In such cases, please report the issue with the plugin author and provide them the details on how to extend validation schema in order to permanently correct the issue.

Wrong status of header X-Cache: Miss from cloudfront

i implemented this for a simple api in
serverless + aws lambda with caching enabled

i can confirm the responses are cached because there are no lambda execution logs for subsequent hits. But in the response, the header X-Cache says "Miss from cloudfront"

config file:

service: survey-responce-endpoint

plugins:
  - serverless-api-gateway-caching
  - serverless-domain-manager

custom:
  customDomain:
    domainName: survey.staging.endpoint.martechsurveys.in
    basePath: 'survey'
    stage: ${self:provider.stage}
    createRoute53Record: true
  apiGatewayCaching:
    enabled: true

provider:
  name: aws
  profile: martech
  runtime: nodejs10.x
  region: ap-south-1
  memorySize: 512
  stage: ${opt:stage, 'development'}
  role: arn:aws:iam::xxxxx
  logRetentionInDays: 7
  environment:
    NODE_ENV: ${opt:stage, 'development'}

functions:

  get_survey:
    handler: handler.getSurvey
    timeout: 5
    memorySize: 128
    reservedConcurrency: 50
    events:
      - http:
          path: get_survey
          method: get
          cors: true
          caching:
            enabled: true
            ttlInSeconds: 300 # 5 mins
            cacheKeyParameters:
              - name: request.querystring.surveyId

handler:

module.exports.getSurvey = async event => {
  return {
        statusCode: 200,
        headers: {
          "Content-Type": "application/json",
          "Cache-Control": "public, max-age=300, s-maxage=300",
          'Access-Control-Allow-Origin': '*',
          'Access-Control-Allow-Credentials': true,
        },
        body: JSON.stringify({"hello":"world"}),
      };
};

don't know if this has anything to do with this plugin or a quirk of cloudfront.

Question on caching with querystring

Hi, I have a couple of questions on how caching works with querystrings, and perhaps the README.md can be updated to clarify this:

Suppose we're working with the following endpoint /test and it can take 2 optional querystring parameters: param1 and param2

  1. When I specify request.path.querystring.param1 will it cache as just /test?
  2. If I use multivaluequerystring, will it cache all these 3 possibilities: /test?param1=test, /test?param2=test, /test?param1=test&param2=test?
  3. Is there a way to specify a catch all for querystring params, instead of explicitly specifying the params in multivaluequerystring? So I would want to cache on the url + querystringparams whatever the querystring parameters may be

Thanks

Not using AWS profile

Hi,
thanks for the plugin. I found one problem with it. If I run it with...
sls deploy --stage dev --aws-profile profile
.. I got "The security token included in the request is invalid.".

If I export the aws credentials and run it with...
sls deploy --stage dev
... then it works like expected.

So it seems that it can't use credentials from profile.

client certificate as cacheKeyParameters

Hi, I'm writing APIs that use MTLS (client certificates) as authentication, I would like to cache results based on that property (i.e. cache per client), I tried:

caching:
  enabled: true
  ttlInSeconds: 60
  cacheKeyParameters:
    - name: request.requestContext.identity.clientCert.serialNumber

but fails with Invalid mapping expression

For reference, this is the event my lambda receives:

{
  "event": {
    "resource": "/v1/...",
    "path": "/v1/...",
    "httpMethod": "GET",
    "headers": {
      "accept": "*/*",
      "accept-encoding": "gzip, deflate, br",
      "cache-control": "no-cache",
      "User-Agent": "PostmanRuntime/7.28.2"
    },
    "multiValueHeaders": {
      "accept": [
        "*/*"
      ],
      "accept-encoding": [
        "gzip, deflate, br"
      ],
      "cache-control": [
        "no-cache"
      ],
      "User-Agent": [
        "PostmanRuntime/7.28.2"
      ]
    },
    "queryStringParameters": null,
    "multiValueQueryStringParameters": null,
    "pathParameters": null,
    "stageVariables": null,
    "requestContext": {
      "resourceId": "yzpjp5",
      "resourcePath": "/v1/...",
      "httpMethod": "GET",
      "extendedRequestId": "PCkYaG3ziYcFouQ=",
      "requestTime": "15/Mar/2022:19:22:16 +0000",
      "path": "/v1/...",
      "accountId": "105843011380",
      "protocol": "HTTP/1.1",
      "stage": "dev",
      "domainPrefix": "core",
      "requestTimeEpoch": 1647372136811,
      "requestId": "1cfff372-3db0-4ad0-a023-46c570c8adeb",
      "identity": {
        "cognitoIdentityPoolId": null,
        "clientCert": {
          "clientCertPem": "-----BEGIN CERTIFICATE-----...-----END CERTIFICATE-----",
          "serialNumber": "620155093573104977087960001264040226218798030419",
          "issuerDN": "CN=...",
          "validity": {
            "notAfter": "Mar  3 20:27:42 2023 GMT",
            "notBefore": "Mar  3 20:27:43 2022 GMT"
          },
          "subjectDN": "CN=acme,O=acme"
        },
        "cognitoIdentityId": null,
        "principalOrgId": null,
        "cognitoAuthenticationType": null,
        "userArn": null,
        "userAgent": "PostmanRuntime/7.28.2",
        "accountId": null,
        "caller": null,
        "accessKey": null,
        "cognitoAuthenticationProvider": null,
        "user": null
      },
      "apiId": "lz44gudovl"
    },
    "body": null,
    "isBase64Encoded": false
  }
}

Is this possible? Thanks

Configuration Error in [email protected]

Plugin is throwing errors when trying to deploy.

 Serverless Error ---------------------------------------

 Configuration error at 'functions.connect.events[0]': unrecognized property 'caching'

 Your Environment Information ---------------------------
     Operating System:          darwin
     Node Version:                 12.16.1
     Framework Version:       2.19.0
     Plugin Version:               4.4.2
     SDK Version:                  2.3.2
     Components Version:   3.4.7
connect:
    handler: connect/gateway/index.handler
    description: Test API Connection
    events:
      - http: 'GET /test/connect'
        caching:
          enabled: true
          ttlInSeconds: 3600

Problem on deploy

Hello,

I tried add the serverless-api-gateway-caching to our serverless.yml to enable the caching on a path param, but the deploy returns this error on pipeline:

serverless deploy --stage=prd
 
  Serverless Error ---------------------------------------
 
  Serverless plugin "serverless-api-gateway-caching" initialization errored: Unexpected token (
 
  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com
 
  Your Environment Information ---------------------------
     OS:                     linux
     Node Version:           6.17.1
     Serverless Version:     1.42.3

Here is our serverless.yml:

service:
  name: serverless-endpoint

custom:
  apiGatewayCaching:
    enabled: true
    ttlInSeconds: 3600

plugins:
  - serverless-webpack
  - serverless-api-gateway-caching

package:
  exclude:
    - tests/**
    - doc/**

provider:
  name: aws
  region: us-east-1
  runtime: nodejs10.x
  environment:
    INDEX_LOG: logs_endpoint

functions:
  get-endpoint:
    handler: lambda/endpoint-public.get
    timeout: 30
    memorySize: 256
    events:
      - http:
          method: get
          path: /endpoint/{url}
          caching:
            enabled: true
            cacheKeyParameters:
              - name: request.path.url

Did I forget anything?

Cheers

Deployment Error when Caching Request Body on POST Method

I'm trying to enable caching on a POST method endpoint based on the request body. When I try and deploy I get the following error (replacing sensitive info with XXXX):

  Serverless Error --------------------------------------- 
  
  An error occurred: XXXX - Invalid mapping expression specified: Validation Result: warnings : [], errors : [Invalid mapping expression specified: method.request.body] (Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException; Request ID: a86f8247-9c10-4433-964c-386d334bfd64). 

Here are my configuration settings for the endpoint (replacing sensitive info with XXXX):

  events:
    - http:
        integration: lambda-proxy
        path: apps/XXXX/XXXX
        method: post
        cors: true
        authorizer:
          name: XXXX
          arn: >-
            XXXX
        caching:
          enabled: true
          cacheKeyParameters:
            - name: request.body

Is there something I'm missing?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.