Skip to content

Commit 2a81f0f

Browse files
authored
feat(apigateway): minCompressionSize on SpecRestApi (#24067)
This PR adds minCompressionSize to SpecRestApi, which allows compression on api's payload. Within SpecRestApi ``` minComppresssionSize: Size``` is the additional prop added to SpecRestApiProps to support minCompressionSize which takes class Size, the value of how much the payload needs to be compressed. This PR also adds minCompressionSize to RestApi which supports Size class e.g ``` minComppresssionSize: Size``` and deprecates minimumCompressionSize which has number as a type. For example, ```ts const api = new apigw.RestApi(stack, 'RestApi', { minCompressionSize: Size.bytes(1024), }); ``` compression for the payload would be 1024 bytes which is equivalent to 1 kibibyte. Closes #22926. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 52aa03c commit 2a81f0f

18 files changed

+429
-202
lines changed

packages/@aws-cdk/aws-apigateway/lib/restapi.ts

+32-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
22
import { IVpcEndpoint } from '@aws-cdk/aws-ec2';
33
import * as iam from '@aws-cdk/aws-iam';
4-
import { ArnFormat, CfnOutput, IResource as IResourceBase, Resource, Stack, Token, FeatureFlags, RemovalPolicy } from '@aws-cdk/core';
4+
import { ArnFormat, CfnOutput, IResource as IResourceBase, Resource, Stack, Token, FeatureFlags, RemovalPolicy, Size } from '@aws-cdk/core';
55
import { APIGATEWAY_DISABLE_CLOUDWATCH_ROLE } from '@aws-cdk/cx-api';
66
import { Construct } from 'constructs';
77
import { ApiDefinition } from './api-definition';
@@ -225,9 +225,22 @@ export interface RestApiProps extends RestApiOptions {
225225
* payload size.
226226
*
227227
* @default - Compression is disabled.
228+
* @deprecated - superseded by `minCompressionSize`
228229
*/
229230
readonly minimumCompressionSize?: number;
230231

232+
/**
233+
* A Size(in bytes, kibibytes, mebibytes etc) that is used to enable compression (with non-negative
234+
* between 0 and 10485760 (10M) bytes, inclusive) or disable compression
235+
* (when undefined) on an API. When compression is enabled, compression or
236+
* decompression is not applied on the payload if the payload size is
237+
* smaller than this value. Setting it to zero allows compression for any
238+
* payload size.
239+
*
240+
* @default - Compression is disabled.
241+
*/
242+
readonly minCompressionSize?: Size;
243+
231244
/**
232245
* The ID of the API Gateway RestApi resource that you want to clone.
233246
*
@@ -261,6 +274,18 @@ export interface SpecRestApiProps extends RestApiBaseProps {
261274
* @see https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-import-api.html
262275
*/
263276
readonly apiDefinition: ApiDefinition;
277+
278+
/**
279+
* A Size(in bytes, kibibytes, mebibytes etc) that is used to enable compression (with non-negative
280+
* between 0 and 10485760 (10M) bytes, inclusive) or disable compression
281+
* (when undefined) on an API. When compression is enabled, compression or
282+
* decompression is not applied on the payload if the payload size is
283+
* smaller than this value. Setting it to zero allows compression for any
284+
* payload size.
285+
*
286+
* @default - Compression is disabled.
287+
*/
288+
readonly minCompressionSize?: Size;
264289
}
265290

266291
/**
@@ -648,6 +673,7 @@ export class SpecRestApi extends RestApiBase {
648673
name: this.restApiName,
649674
policy: props.policy,
650675
failOnWarnings: props.failOnWarnings,
676+
minimumCompressionSize: props.minCompressionSize?.toBytes(),
651677
body: apiDefConfig.inlineDefinition ?? undefined,
652678
bodyS3Location: apiDefConfig.inlineDefinition ? undefined : apiDefConfig.s3Location,
653679
endpointConfiguration: this._configureEndpoints(props),
@@ -758,12 +784,16 @@ export class RestApi extends RestApiBase {
758784
constructor(scope: Construct, id: string, props: RestApiProps = { }) {
759785
super(scope, id, props);
760786

787+
if (props.minCompressionSize !== undefined && props.minimumCompressionSize !== undefined) {
788+
throw new Error('both properties minCompressionSize and minimumCompressionSize cannot be set at once.');
789+
}
790+
761791
const resource = new CfnRestApi(this, 'Resource', {
762792
name: this.physicalName,
763793
description: props.description,
764794
policy: props.policy,
765795
failOnWarnings: props.failOnWarnings,
766-
minimumCompressionSize: props.minimumCompressionSize,
796+
minimumCompressionSize: props.minCompressionSize?.toBytes() ?? props.minimumCompressionSize,
767797
binaryMediaTypes: props.binaryMediaTypes,
768798
endpointConfiguration: this._configureEndpoints(props),
769799
apiKeySourceType: props.apiKeySourceType,

packages/@aws-cdk/aws-apigateway/test/integ.restapi.js.snapshot/apigatewayrestapiDefaultTestDeployAssert6A9696A7.assets.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "21.0.0",
2+
"version": "30.0.0",
33
"files": {
44
"21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": {
55
"source": {
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"version":"21.0.0"}
1+
{"version":"30.0.0"}

packages/@aws-cdk/aws-apigateway/test/integ.restapi.js.snapshot/integ.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "21.0.0",
2+
"version": "30.0.0",
33
"testCases": {
44
"apigateway-restapi/DefaultTest": {
55
"stacks": [

packages/@aws-cdk/aws-apigateway/test/integ.restapi.js.snapshot/manifest.json

+14-23
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
{
2-
"version": "21.0.0",
2+
"version": "30.0.0",
33
"artifacts": {
4-
"Tree": {
5-
"type": "cdk:tree",
6-
"properties": {
7-
"file": "tree.json"
8-
}
9-
},
104
"test-apigateway-restapi.assets": {
115
"type": "cdk:asset-manifest",
126
"properties": {
@@ -23,7 +17,7 @@
2317
"validateOnSynth": false,
2418
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}",
2519
"cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}",
26-
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/527feb6f65d1f41404abb6acfb6504a918e1d7489665b74acb9fa0ca23f34049.json",
20+
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/94f416193221135915e06201345d08e3db001c8bbf22b073f16ec02d3b8fba4b.json",
2721
"requiresBootstrapStackVersion": 6,
2822
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
2923
"additionalDependencies": [
@@ -60,7 +54,7 @@
6054
"/test-apigateway-restapi/my-api/Deployment/Resource": [
6155
{
6256
"type": "aws:cdk:logicalId",
63-
"data": "myapiDeployment92F2CB4996088bac04ec19d9db88f146f3d88bca"
57+
"data": "myapiDeployment92F2CB496a6dee9579dae89ab3a0b094b4d193eb"
6458
}
6559
],
6660
"/test-apigateway-restapi/my-api/DeploymentStage.beta/Resource": [
@@ -204,7 +198,7 @@
204198
"/test-apigateway-restapi/TestDeployment/Resource": [
205199
{
206200
"type": "aws:cdk:logicalId",
207-
"data": "TestDeploymentD77B56866f3141cef1e8fea8ab3b3b8bc90df36d"
201+
"data": "TestDeploymentD77B56860a9c34c1bc306bfbc7d318827eacd1e1"
208202
}
209203
],
210204
"/test-apigateway-restapi/TestStage/Resource": [
@@ -231,28 +225,19 @@
231225
"data": "CheckBootstrapVersion"
232226
}
233227
],
234-
"myapiDeployment92F2CB4963d40685c54c6f8da21d80a83f16d3d5": [
228+
"myapiDeployment92F2CB4996088bac04ec19d9db88f146f3d88bca": [
235229
{
236230
"type": "aws:cdk:logicalId",
237-
"data": "myapiDeployment92F2CB4963d40685c54c6f8da21d80a83f16d3d5",
231+
"data": "myapiDeployment92F2CB4996088bac04ec19d9db88f146f3d88bca",
238232
"trace": [
239233
"!!DESTRUCTIVE_CHANGES: WILL_DESTROY"
240234
]
241235
}
242236
],
243-
"myapiv1appliances507FEFF4": [
237+
"TestDeploymentD77B56866f3141cef1e8fea8ab3b3b8bc90df36d": [
244238
{
245239
"type": "aws:cdk:logicalId",
246-
"data": "myapiv1appliances507FEFF4",
247-
"trace": [
248-
"!!DESTRUCTIVE_CHANGES: WILL_DESTROY"
249-
]
250-
}
251-
],
252-
"myapiv1appliancesGET8FE872EC": [
253-
{
254-
"type": "aws:cdk:logicalId",
255-
"data": "myapiv1appliancesGET8FE872EC",
240+
"data": "TestDeploymentD77B56866f3141cef1e8fea8ab3b3b8bc90df36d",
256241
"trace": [
257242
"!!DESTRUCTIVE_CHANGES: WILL_DESTROY"
258243
]
@@ -307,6 +292,12 @@
307292
]
308293
},
309294
"displayName": "apigateway-restapi/DefaultTest/DeployAssert"
295+
},
296+
"Tree": {
297+
"type": "cdk:tree",
298+
"properties": {
299+
"file": "tree.json"
300+
}
310301
}
311302
}
312303
}

packages/@aws-cdk/aws-apigateway/test/integ.restapi.js.snapshot/test-apigateway-restapi.assets.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
2-
"version": "21.0.0",
2+
"version": "30.0.0",
33
"files": {
4-
"527feb6f65d1f41404abb6acfb6504a918e1d7489665b74acb9fa0ca23f34049": {
4+
"94f416193221135915e06201345d08e3db001c8bbf22b073f16ec02d3b8fba4b": {
55
"source": {
66
"path": "test-apigateway-restapi.template.json",
77
"packaging": "file"
88
},
99
"destinations": {
1010
"current_account-current_region": {
1111
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12-
"objectKey": "527feb6f65d1f41404abb6acfb6504a918e1d7489665b74acb9fa0ca23f34049.json",
12+
"objectKey": "94f416193221135915e06201345d08e3db001c8bbf22b073f16ec02d3b8fba4b.json",
1313
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
1414
}
1515
}

packages/@aws-cdk/aws-apigateway/test/integ.restapi.js.snapshot/test-apigateway-restapi.template.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"myapi4C7BF186": {
44
"Type": "AWS::ApiGateway::RestApi",
55
"Properties": {
6+
"MinimumCompressionSize": 1024,
67
"Name": "my-api"
78
}
89
},
@@ -55,7 +56,7 @@
5556
"UpdateReplacePolicy": "Retain",
5657
"DeletionPolicy": "Retain"
5758
},
58-
"myapiDeployment92F2CB4996088bac04ec19d9db88f146f3d88bca": {
59+
"myapiDeployment92F2CB496a6dee9579dae89ab3a0b094b4d193eb": {
5960
"Type": "AWS::ApiGateway::Deployment",
6061
"Properties": {
6162
"RestApiId": {
@@ -87,7 +88,7 @@
8788
"CacheClusterEnabled": true,
8889
"CacheClusterSize": "0.5",
8990
"DeploymentId": {
90-
"Ref": "myapiDeployment92F2CB4996088bac04ec19d9db88f146f3d88bca"
91+
"Ref": "myapiDeployment92F2CB496a6dee9579dae89ab3a0b094b4d193eb"
9192
},
9293
"Description": "beta stage",
9394
"MethodSettings": [
@@ -672,7 +673,7 @@
672673
"MyHandlerServiceRoleFFA06653"
673674
]
674675
},
675-
"TestDeploymentD77B56866f3141cef1e8fea8ab3b3b8bc90df36d": {
676+
"TestDeploymentD77B56860a9c34c1bc306bfbc7d318827eacd1e1": {
676677
"Type": "AWS::ApiGateway::Deployment",
677678
"Properties": {
678679
"RestApiId": {
@@ -695,7 +696,7 @@
695696
"Ref": "myapi4C7BF186"
696697
},
697698
"DeploymentId": {
698-
"Ref": "TestDeploymentD77B56866f3141cef1e8fea8ab3b3b8bc90df36d"
699+
"Ref": "TestDeploymentD77B56860a9c34c1bc306bfbc7d318827eacd1e1"
699700
},
700701
"StageName": "prod"
701702
},

packages/@aws-cdk/aws-apigateway/test/integ.restapi.js.snapshot/tree.json

+62-11
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@
44
"id": "App",
55
"path": "",
66
"children": {
7-
"Tree": {
8-
"id": "Tree",
9-
"path": "Tree",
10-
"constructInfo": {
11-
"fqn": "constructs.Construct",
12-
"version": "10.1.129"
13-
}
14-
},
157
"test-apigateway-restapi": {
168
"id": "test-apigateway-restapi",
179
"path": "test-apigateway-restapi",
@@ -26,6 +18,7 @@
2618
"attributes": {
2719
"aws:cdk:cloudformation:type": "AWS::ApiGateway::RestApi",
2820
"aws:cdk:cloudformation:props": {
21+
"minimumCompressionSize": 1024,
2922
"name": "my-api"
3023
}
3124
},
@@ -38,6 +31,14 @@
3831
"id": "CloudWatchRole",
3932
"path": "test-apigateway-restapi/my-api/CloudWatchRole",
4033
"children": {
34+
"ImportCloudWatchRole": {
35+
"id": "ImportCloudWatchRole",
36+
"path": "test-apigateway-restapi/my-api/CloudWatchRole/ImportCloudWatchRole",
37+
"constructInfo": {
38+
"fqn": "@aws-cdk/core.Resource",
39+
"version": "0.0.0"
40+
}
41+
},
4142
"Resource": {
4243
"id": "Resource",
4344
"path": "test-apigateway-restapi/my-api/CloudWatchRole/Resource",
@@ -145,7 +146,7 @@
145146
"cacheClusterEnabled": true,
146147
"cacheClusterSize": "0.5",
147148
"deploymentId": {
148-
"Ref": "myapiDeployment92F2CB4996088bac04ec19d9db88f146f3d88bca"
149+
"Ref": "myapiDeployment92F2CB496a6dee9579dae89ab3a0b094b4d193eb"
149150
},
150151
"description": "beta stage",
151152
"methodSettings": [
@@ -992,6 +993,14 @@
992993
"id": "ServiceRole",
993994
"path": "test-apigateway-restapi/MyHandler/ServiceRole",
994995
"children": {
996+
"ImportServiceRole": {
997+
"id": "ImportServiceRole",
998+
"path": "test-apigateway-restapi/MyHandler/ServiceRole/ImportServiceRole",
999+
"constructInfo": {
1000+
"fqn": "@aws-cdk/core.Resource",
1001+
"version": "0.0.0"
1002+
}
1003+
},
9951004
"Resource": {
9961005
"id": "Resource",
9971006
"path": "test-apigateway-restapi/MyHandler/ServiceRole/Resource",
@@ -1107,7 +1116,7 @@
11071116
"Ref": "myapi4C7BF186"
11081117
},
11091118
"deploymentId": {
1110-
"Ref": "TestDeploymentD77B56866f3141cef1e8fea8ab3b3b8bc90df36d"
1119+
"Ref": "TestDeploymentD77B56860a9c34c1bc306bfbc7d318827eacd1e1"
11111120
},
11121121
"stageName": "prod"
11131122
}
@@ -1156,6 +1165,22 @@
11561165
"fqn": "@aws-cdk/aws-apigateway.Stage",
11571166
"version": "0.0.0"
11581167
}
1168+
},
1169+
"BootstrapVersion": {
1170+
"id": "BootstrapVersion",
1171+
"path": "test-apigateway-restapi/BootstrapVersion",
1172+
"constructInfo": {
1173+
"fqn": "@aws-cdk/core.CfnParameter",
1174+
"version": "0.0.0"
1175+
}
1176+
},
1177+
"CheckBootstrapVersion": {
1178+
"id": "CheckBootstrapVersion",
1179+
"path": "test-apigateway-restapi/CheckBootstrapVersion",
1180+
"constructInfo": {
1181+
"fqn": "@aws-cdk/core.CfnRule",
1182+
"version": "0.0.0"
1183+
}
11591184
}
11601185
},
11611186
"constructInfo": {
@@ -1176,12 +1201,30 @@
11761201
"path": "apigateway-restapi/DefaultTest/Default",
11771202
"constructInfo": {
11781203
"fqn": "constructs.Construct",
1179-
"version": "10.1.129"
1204+
"version": "10.1.249"
11801205
}
11811206
},
11821207
"DeployAssert": {
11831208
"id": "DeployAssert",
11841209
"path": "apigateway-restapi/DefaultTest/DeployAssert",
1210+
"children": {
1211+
"BootstrapVersion": {
1212+
"id": "BootstrapVersion",
1213+
"path": "apigateway-restapi/DefaultTest/DeployAssert/BootstrapVersion",
1214+
"constructInfo": {
1215+
"fqn": "@aws-cdk/core.CfnParameter",
1216+
"version": "0.0.0"
1217+
}
1218+
},
1219+
"CheckBootstrapVersion": {
1220+
"id": "CheckBootstrapVersion",
1221+
"path": "apigateway-restapi/DefaultTest/DeployAssert/CheckBootstrapVersion",
1222+
"constructInfo": {
1223+
"fqn": "@aws-cdk/core.CfnRule",
1224+
"version": "0.0.0"
1225+
}
1226+
}
1227+
},
11851228
"constructInfo": {
11861229
"fqn": "@aws-cdk/core.Stack",
11871230
"version": "0.0.0"
@@ -1198,6 +1241,14 @@
11981241
"fqn": "@aws-cdk/integ-tests.IntegTest",
11991242
"version": "0.0.0"
12001243
}
1244+
},
1245+
"Tree": {
1246+
"id": "Tree",
1247+
"path": "Tree",
1248+
"constructInfo": {
1249+
"fqn": "constructs.Construct",
1250+
"version": "10.1.249"
1251+
}
12011252
}
12021253
},
12031254
"constructInfo": {

0 commit comments

Comments
 (0)