最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

typescript - How to specify cache keys on AWS API Gateway using TS CDK? - Stack Overflow

programmeradmin1浏览0评论

I am managing my AWS infra with AWS CDK for TypeScript, specifically [email protected]. My goal is to enable API Gateway caching for an endpoint like GET subpath/{customerId}/path using customerId as the cache key. Effectively, on AWS console, it should look like this after deployment:

But this is what I am getting currently:

This my API gateway deployOptions:

deployOptions: {
  stageName: props.stageName,
  metricsEnabled: true,
  cachingEnabled: true,
  cacheClusterEnabled: true,
  cacheClusterSize: '0.5',
  cacheTtl: Duration.seconds(3600),
  cacheDataEncrypted: true,
},

Note: This is a REST API from API Gateway V1
And my method configuration:

methods.addMethod('GET', new LambdaIntegration(lambda), {
      authorizer: requestAuthorizer,
      requestValidator: requestParamValidator,
      requestParameters: {
        'method.request.path.customerId': true,
      },
      methodResponses: [
        {
          statusCode: '200',
        },
        {
          statusCode: '404',
        },
        {
          statusCode: '401',
        },
      ],
    });

I tried adding the cacheKeyParameters parameter like this:

methods.addMethod('GET', new LambdaIntegration(lambda), {
      authorizer: requestAuthorizer,
      requestValidator: requestParamValidator,
      cacheKeyParameters: ['method.request.path.customerId'],
      requestParameters: {
        'method.request.path.customerId': true,
      },
      methodResponses: [
        {
          statusCode: '200',
        },
        {
          statusCode: '404',
        },
        {
          statusCode: '401',
        },
      ],
    });

or inside methodOptions in my deployOptions that I showed, but I get this compile error:
Object literal may only specify known properties, and cacheKeyParameters does not exist in type MethodOptions
Even if I ignore this with @ts-ignore, it does not work and the result is still:

I also tried:

myMethod.addPropertyOverride('CacheKeyParameters', [
  'method.request.path.customerId',
]);

or

myMethod.addPropertyOverride('cacheKeyParameters', [
  'method.request.path.customerId',
]);

But then the deployment failed with:
[#: extraneous key [CacheKeyParameters] is not permitted]
or
[#: extraneous key [cacheKeyParameters] is not permitted]

That makes sense since the CDK documentation for MethodOptions does not have cacheKeyParameters or any cache-related parameter. But then, how would I do that?

Not even ChatGPT o1 could help me with this one XD

I am managing my AWS infra with AWS CDK for TypeScript, specifically [email protected]. My goal is to enable API Gateway caching for an endpoint like GET subpath/{customerId}/path using customerId as the cache key. Effectively, on AWS console, it should look like this after deployment:

But this is what I am getting currently:

This my API gateway deployOptions:

deployOptions: {
  stageName: props.stageName,
  metricsEnabled: true,
  cachingEnabled: true,
  cacheClusterEnabled: true,
  cacheClusterSize: '0.5',
  cacheTtl: Duration.seconds(3600),
  cacheDataEncrypted: true,
},

Note: This is a REST API from API Gateway V1
And my method configuration:

methods.addMethod('GET', new LambdaIntegration(lambda), {
      authorizer: requestAuthorizer,
      requestValidator: requestParamValidator,
      requestParameters: {
        'method.request.path.customerId': true,
      },
      methodResponses: [
        {
          statusCode: '200',
        },
        {
          statusCode: '404',
        },
        {
          statusCode: '401',
        },
      ],
    });

I tried adding the cacheKeyParameters parameter like this:

methods.addMethod('GET', new LambdaIntegration(lambda), {
      authorizer: requestAuthorizer,
      requestValidator: requestParamValidator,
      cacheKeyParameters: ['method.request.path.customerId'],
      requestParameters: {
        'method.request.path.customerId': true,
      },
      methodResponses: [
        {
          statusCode: '200',
        },
        {
          statusCode: '404',
        },
        {
          statusCode: '401',
        },
      ],
    });

or inside methodOptions in my deployOptions that I showed, but I get this compile error:
Object literal may only specify known properties, and cacheKeyParameters does not exist in type MethodOptions
Even if I ignore this with @ts-ignore, it does not work and the result is still:

I also tried:

myMethod.addPropertyOverride('CacheKeyParameters', [
  'method.request.path.customerId',
]);

or

myMethod.addPropertyOverride('cacheKeyParameters', [
  'method.request.path.customerId',
]);

But then the deployment failed with:
[#: extraneous key [CacheKeyParameters] is not permitted]
or
[#: extraneous key [cacheKeyParameters] is not permitted]

That makes sense since the CDK documentation for MethodOptions does not have cacheKeyParameters or any cache-related parameter. But then, how would I do that?

Not even ChatGPT o1 could help me with this one XD

Share Improve this question edited Mar 6 at 15:44 Vinícius Santos de Pontes asked Mar 6 at 15:06 Vinícius Santos de PontesVinícius Santos de Pontes 1143 silver badges9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Cache key parameters exist on API Gateway's integration object and not method object shown here and here

发布评论

评论列表(0)

  1. 暂无评论