Skip to content

Commit 39755fd

Browse files
authored
chore: update name of sdk-v2-to-v3-adapter and improve documentation (#29847)
### Issue # (if applicable) Closes #29843 ### Reason for this change The AWS SDK V2 is being deprecated. The `sdk-v2-to-v3-adapter` was created as a way to migrate `AwsCustomResource` to use AWS SDK V3 without introducing breaking changes. The documentation for the adapter does not provide enough detail about what it is used for and what functionality it provides. Additionally, the naming of the adapter should convey that it is used to provide an abstraction over all SDK versions, not just SDK V2 to SDK V3. ### Description of changes The `sdk-v2-to-v3-adapter` was changed to `aws-custom-resource-sdk-adapter`. Any usage of `sdk-v2-to-v3-adapter` was updated to now use `aws-custom-resource-sdk-adapter`. The `README` for `aws-custom-resource-sdk-adapter` was improved to provide an overview of the tooling that exists as part of the adapter. ### Description of how you validated changes No new unit tests or integ tests were needed as part of this PR. The current build is succeeding which means that the naming update has not introduced a breaking change. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 5347369 commit 39755fd

File tree

30 files changed

+110
-19
lines changed

30 files changed

+110
-19
lines changed

aws-cdk.code-workspace

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
"rootPath": "packages/@aws-cdk/integ-tests-alpha"
2828
},
2929
{
30-
"name": "sdk-v2-to-v3-adapter",
31-
"rootPath": "packages/@aws-cdk/sdk-v2-to-v3-adapter"
30+
"name": "aws-custom-resource-sdk-adapter",
31+
"rootPath": "packages/@aws-cdk/aws-custom-resource-sdk-adapter"
3232
}
3333
]
3434
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# AWS Custom Resource SDK Adapter
2+
3+
The AWS custom resource SDK adapter is an internal collection of tools built to maintain compatibility with the contracts defined for `AwsCustomResource` and the current AWS SDK version. `AwsCustomResource` can be used where a single API call can fill a gap in CloudFormation coverage by allowing a user to specify a single API call specific to create, update, and delete stack events. Users specify a `service`, an `action`, and `parameters` as part of the `AwsSdkCall` interface which are used to dynamically invoke the API. Since `AwsCustomResource` was created while SDKv2 was active, it implicitly inherited the SDKv2 contract. As a result, migrating to newer SDK versions will result in breaking changes in the `AwsCustomResource` construct. In its current state, the AWS custom resource SDK adapter contains tooling that allows `AwsCustomResource` to make API calls using SDKv3 and return the associated response. These tools are related to:
4+
5+
* Input type coercion
6+
* Response coercion
7+
* Naming normalizaion
8+
9+
## Input Type Coercion
10+
11+
In general, input types expected by SDKv3 are more restrictive than what was expected by SDKv2. One such example is the regression to number types with respect to AWS SDKv2. More specifically, in SDKv2, number types that are accidentally passed as strings will be silently converted to the right type. SDKv3, however, will not do the conversion which causes the server call to fail because of mismatched types. For example,
12+
13+
**SDKv2**
14+
15+
```ts
16+
const codedeploy = new AWS.CodeDeploy({ region: 'eu-west-1' });
17+
18+
const input: AWS.CodeDeploy.CreateDeploymentConfigInput = {
19+
deploymentConfigName: 'testtest',
20+
computePlatform: 'Lambda',
21+
trafficRoutingConfig: {
22+
type: "TimeBasedLinear",
23+
timeBasedLinear: {
24+
linearInterval: "1" as any, // The type says 'number' but we're forcing strings here
25+
linearPercentage:"5" as any,
26+
},
27+
},
28+
};
29+
30+
// Following call happily succeeds
31+
console.log(await codedeploy.createDeploymentConfig(input).promise());
32+
```
33+
34+
**SDKv3**
35+
36+
```ts
37+
const codedeploy = new CodeDeploy();
38+
39+
const input: CreateDeploymentConfigCommandInput = {
40+
deploymentConfigName: 'testtest',
41+
computePlatform: 'Lambda',
42+
trafficRoutingConfig: {
43+
type: "TimeBasedLinear",
44+
timeBasedLinear: {
45+
linearInterval: "1" as any, // The type says 'number' but we're forcing strings here
46+
linearPercentage:"5" as any,
47+
},
48+
},
49+
};
50+
51+
await codedeploy.createDeploymentConfig(input);
52+
53+
// The above call fails with the following message:
54+
'SerializationException: STRING_VALUE can not be converted to an Integer'
55+
```
56+
57+
Another example is the regression to blob types with respect to SDKv2. More specifically, in SDKv2, input fields marked as blob types used to permissively accept strings, buffers, and uint8arrays. In SDKv3, these same fields now only accept uint8arrays.
58+
59+
In response, the [`Coercer`](./lib/coerce-api-parameters.ts) class was created to coerce input parameters to the type expected by SDKv3. At a high-level, this class coerces parameter types using a state-machine generated using smithy models. The state-machine is gzipped to save bytes and the gzipped representation can be seen [here](./lib/parameter-types.ts).
60+
61+
## Response Coercion
62+
63+
In some cases, API call responses for SDKv3 differ from API call responses for SDKv2. One example is streaming vs. buffered responses. More specifically, SDKv3 prefers not to buffer potentially large responses. For node.js, you must consume the stream or garbage collect the client or its request handler to keep the connection open to new traffic by freeing sockets. For example,
64+
65+
**SDKv2**
66+
67+
```ts
68+
// this buffers (consumes) the stream already
69+
const get = await s3.getObject({ ... }).promise();
70+
```
71+
72+
**SDKv3**
73+
74+
```ts
75+
// consume the stream to free the socket
76+
const get = await s3.getObject({ ... }); // object .Body has unconsumed stream
77+
const str = await get.Body.transformToString(); // consumes the stream
78+
```
79+
80+
Users are able to retrieve values returned as a result of API calls they’ve defined while using the `AwsCustomResource` construct. To be retrievable, the response values must be strings. With SDKv3, the stream must be fully consumed to a string before the custom resource exits. Thus, response coercion was built as another tool in the AWS custom resource SDK adapter. Return type coercion exists in the [api-call](./lib/api-call.ts) file and the logic is implemented in the `coerceSdkv3Response` function.
81+
82+
## Naming Normalization
83+
84+
Originally, the `AwsCustomResource` construct advertised that the `service` argument defined in the `AwsSdkCall` interface could be in any one of the following formats:
85+
86+
* The SDK v2 constructor name: APIGateway
87+
* The SDK v2 constructor name in all lower case: apigateway (primary format)
88+
89+
Similarly, the `action` argument defined in the `AwsSdkCall` interface was advertised as being permitted in any of the following formats:
90+
91+
* The API call name: GetRestApi
92+
* The API call name with a lower case starting letter: getRestApi (primary format)
93+
94+
Migrating `AwsCustomResource` to SDKv3 meant that the permitted `service` and `action` values for SDKv2 needed to be accepted as well as new formats with respect to SDKv3. As a result, naming normalization was established as a way to convert the `service` and `action` arguments into the format expected for SDKv3 API calls. The normalization functions for `service` and `action` can be seen in the [sdk-info](./lib/sdk-info.ts) file and are named `normalizeServiceName` and `normalizeActionName`, respectively. To enable normalization of the `service` argument, the client package names map file from the `aws-sdk-js-codemod` repository is used and can be found [here](./lib/sdk-v2-to-v3.json). More specifically, this file is used to map SDKv2 `service` names to the equivalent SDKv3 `service` name. To enable normalization of the `action` argument a [sdk-v3-metadata](./lib/sdk-v3-metadata.json) file was extracted which contains a list of APIs that end in the word `Command` which allows us to disambiguate around these when generating the SDKv3 `action` name. Note that the [sdk-v3-metadata](./lib/sdk-v3-metadata.json) file also contains a mapping of `service` names into an IAM name allowing us to correctly identify the IAM prefix for each `service`.

packages/@aws-cdk/sdk-v2-to-v3-adapter/package.json renamed to packages/@aws-cdk/aws-custom-resource-sdk-adapter/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "@aws-cdk/sdk-v2-to-v3-adapter",
3-
"description": "Adapter to convert AWS SDK v2 to AWS CDK v3 calls",
2+
"name": "@aws-cdk/aws-custom-resource-sdk-adapter",
3+
"description": "Adapter to convert AWS SDK v2 to AWS CDK v3 calls for AwsCustomResource",
44
"private": true,
55
"version": "0.0.0",
66
"main": "lib/index.js",
@@ -36,7 +36,7 @@
3636
"repository": {
3737
"url": "https://github.com/aws/aws-cdk.git",
3838
"type": "git",
39-
"directory": "packages/@aws-cdk/sdk-v2-to-v3-adapter"
39+
"directory": "packages/@aws-cdk/aws-custom-resource-sdk-adapter"
4040
},
4141
"keywords": [
4242
"aws",

packages/@aws-cdk/sdk-v2-to-v3-adapter/tsconfig.json renamed to packages/@aws-cdk/aws-custom-resource-sdk-adapter/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"noFallthroughCasesInSwitch": true,
1515
"resolveJsonModule": true,
1616
"composite": true,
17-
"incremental": true
17+
"incremental": true,
1818
},
1919
"include": ["**/*.ts", "**/*.d.ts"]
2020
}

packages/@aws-cdk/custom-resource-handlers/lib/aws-events-targets/aws-api-handler/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable no-console */
22
// eslint-disable-next-line import/no-extraneous-dependencies
3-
import { ApiCall } from '@aws-cdk/sdk-v2-to-v3-adapter';
3+
import { ApiCall } from '@aws-cdk/aws-custom-resource-sdk-adapter';
44

55
interface AwsApiInput {
66
readonly service: string;

packages/@aws-cdk/custom-resource-handlers/lib/custom-resources/aws-custom-resource-handler/aws-sdk-v2-handler.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import * as AWSLambda from 'aws-lambda';
1111
import { AwsSdkCall } from './construct-types';
1212
import { decodeCall, decodeSpecialValues, filterKeys, respond, startsWithOneOf } from './shared';
1313
// eslint-disable-next-line import/no-extraneous-dependencies
14-
import { flatten } from '@aws-cdk/sdk-v2-to-v3-adapter';
14+
import { flatten } from '@aws-cdk/aws-custom-resource-sdk-adapter';
1515

1616
let latestSdkInstalled = false;
1717

packages/@aws-cdk/custom-resource-handlers/lib/custom-resources/aws-custom-resource-handler/aws-sdk-v3-handler.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* eslint-disable no-console */
33
import { execSync } from 'child_process';
44
// eslint-disable-next-line import/no-extraneous-dependencies
5-
import { ApiCall } from '@aws-cdk/sdk-v2-to-v3-adapter';
5+
import { ApiCall } from '@aws-cdk/aws-custom-resource-sdk-adapter';
66
// import the AWSLambda package explicitly,
77
// which is globally available in the Lambda runtime,
88
// as otherwise linking this repository with link-all.sh

packages/@aws-cdk/custom-resource-handlers/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"devDependencies": {
2828
"@aws-cdk/cdk-build-tools": "0.0.0",
2929
"@aws-cdk/pkglint": "0.0.0",
30-
"@aws-cdk/sdk-v2-to-v3-adapter": "0.0.0",
30+
"@aws-cdk/aws-custom-resource-sdk-adapter": "0.0.0",
3131
"@aws-sdk/client-ecs": "3.451.0",
3232
"@aws-sdk/client-ssm": "3.453.0",
3333
"@aws-sdk/client-kinesis": "3.451.0",

packages/@aws-cdk/integ-tests-alpha/lib/assertions/providers/lambda-handler/sdk.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable no-console */
22
import { CustomResourceHandler } from './base';
33
import { AwsApiCallRequest, AwsApiCallResult } from './types';
4-
import { ApiCall, flatten } from '@aws-cdk/sdk-v2-to-v3-adapter';
4+
import { ApiCall, flatten } from '@aws-cdk/aws-custom-resource-sdk-adapter';
55
import { decodeParameters, deepParseJson } from './utils';
66

77
export class AwsApiCallHandler extends CustomResourceHandler<AwsApiCallRequest, AwsApiCallResult | { [key: string]: unknown }> {

packages/@aws-cdk/integ-tests-alpha/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"@aws-cdk/cdk-build-tools": "0.0.0",
7070
"@aws-cdk/integ-runner": "0.0.0",
7171
"@aws-cdk/pkglint": "0.0.0",
72-
"@aws-cdk/sdk-v2-to-v3-adapter": "0.0.0",
72+
"@aws-cdk/aws-custom-resource-sdk-adapter": "0.0.0",
7373
"@aws-sdk/client-ec2": "3.421.0",
7474
"@aws-sdk/client-s3": "3.421.0",
7575
"@aws-sdk/client-sfn": "3.421.0",

packages/@aws-cdk/sdk-v2-to-v3-adapter/README.md

-3
This file was deleted.

packages/aws-cdk-lib/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139
"@aws-cdk/cdk-build-tools": "0.0.0",
140140
"@aws-cdk/custom-resource-handlers": "0.0.0",
141141
"@aws-cdk/pkglint": "0.0.0",
142-
"@aws-cdk/sdk-v2-to-v3-adapter": "0.0.0",
142+
"@aws-cdk/aws-custom-resource-sdk-adapter": "0.0.0",
143143
"@aws-cdk/spec2cdk": "0.0.0",
144144
"@aws-sdk/client-acm": "3.421.0",
145145
"@aws-sdk/client-account": "3.421.0",

scripts/update-sdkv3-parameters-model.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,15 @@ async function main(argv: string[]) {
8787
}
8888

8989
const root = path.resolve(__dirname, '..');
90-
await renderStateMachineToTypeScript(sortedStateMachine, path.join(root, 'packages/@aws-cdk/sdk-v2-to-v3-adapter/lib/parameter-types.ts'));
90+
await renderStateMachineToTypeScript(sortedStateMachine, path.join(root, 'packages/@aws-cdk/aws-custom-resource-sdk-adapter/lib/parameter-types.ts'));
9191

9292
await writeAllServiceToModelFile(allServices, [
9393
path.join(root, 'packages/aws-cdk-lib/custom-resources/lib/helpers-internal/sdk-v3-metadata.json'),
94-
path.join(root, 'packages/@aws-cdk/sdk-v2-to-v3-adapter/lib/sdk-v3-metadata.json'),
94+
path.join(root, 'packages/@aws-cdk/aws-custom-resource-sdk-adapter/lib/sdk-v3-metadata.json'),
9595
]);
9696
await writeV2ToV3Mapping([
9797
path.join(root, 'packages/aws-cdk-lib/custom-resources/lib/helpers-internal/sdk-v2-to-v3.json'),
98-
path.join(root, 'packages/@aws-cdk/sdk-v2-to-v3-adapter/lib/sdk-v2-to-v3.json'),
98+
path.join(root, 'packages/@aws-cdk/aws-custom-resource-sdk-adapter/lib/sdk-v2-to-v3.json'),
9999
]);
100100
}
101101

0 commit comments

Comments
 (0)