Skip to content

Commit 9858002

Browse files
authored
feat(cli): hotswap for appsync vtl mapping template changes (#18881)
I do a lot of updating VTL mapping templates for my AppSync API throughout the day, and I'd love to have those changes hot swapped through the SDK for faster feedback loops. This PR handles changes to `RequestMappingTemplate` and `ResponseMappingTemplate` for both resolvers (`AWS::AppSync::Resolver`) and functions (`AWS::AppSync::FunctionConfiguration`). ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 90d7f02 commit 9858002

8 files changed

+481
-2
lines changed

packages/aws-cdk/README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ Hotswapping is currently supported for the following changes
368368
- Container asset changes of AWS ECS Services.
369369
- Website asset changes of AWS S3 Bucket Deployments.
370370
- Source and Environment changes of AWS CodeBuild Projects.
371+
- VTL mapping template changes for AppSync Resolvers and Functions
371372

372373
**⚠ Note #1**: This command deliberately introduces drift in CloudFormation stacks in order to speed up deployments.
373374
For this reason, only use it for development purposes.
@@ -549,8 +550,8 @@ Some of the interesting keys that can be used in the JSON configuration files:
549550
```
550551

551552
If specified, the command in the `build` key will be executed immediately before synthesis.
552-
This can be used to build Lambda Functions, CDK Application code, or other assets.
553-
`build` cannot be specified on the command line or in the User configuration,
553+
This can be used to build Lambda Functions, CDK Application code, or other assets.
554+
`build` cannot be specified on the command line or in the User configuration,
554555
and must be specified in the Project configuration. The command specified
555556
in `build` will be executed by the "watch" process before deployment.
556557

packages/aws-cdk/lib/api/aws-auth/sdk.ts

+5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export interface ISDK {
6363
stepFunctions(): AWS.StepFunctions;
6464
codeBuild(): AWS.CodeBuild
6565
cloudWatchLogs(): AWS.CloudWatchLogs;
66+
appsync(): AWS.AppSync;
6667
}
6768

6869
/**
@@ -190,6 +191,10 @@ export class SDK implements ISDK {
190191
return this.wrapServiceErrorHandling(new AWS.CloudWatchLogs(this.config));
191192
}
192193

194+
public appsync(): AWS.AppSync {
195+
return this.wrapServiceErrorHandling(new AWS.AppSync(this.config));
196+
}
197+
193198
public async currentAccount(): Promise<Account> {
194199
// Get/refresh if necessary before we can access `accessKeyId`
195200
await this.forceCredentialRetrieval();

packages/aws-cdk/lib/api/evaluate-cloudformation-template.ts

+6
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ const RESOURCE_TYPE_ATTRIBUTES_FORMATS: { [type: string]: { [attribute: string]:
342342
// the name attribute of the EventBus is the same as the Ref
343343
Name: parts => parts.resourceName,
344344
},
345+
'AWS::AppSync::GraphQLApi': { ApiId: appsyncGraphQlApiApiIdFmt },
345346
};
346347

347348
function iamArnFmt(parts: ArnParts): string {
@@ -364,6 +365,11 @@ function stdSlashResourceArnFmt(parts: ArnParts): string {
364365
return `arn:${parts.partition}:${parts.service}:${parts.region}:${parts.account}:${parts.resourceType}/${parts.resourceName}`;
365366
}
366367

368+
function appsyncGraphQlApiApiIdFmt(parts: ArnParts): string {
369+
// arn:aws:appsync:us-east-1:111111111111:apis/<apiId>
370+
return parts.resourceName.split('/')[1];
371+
}
372+
367373
interface Intrinsic {
368374
readonly name: string;
369375
readonly args: any;

packages/aws-cdk/lib/api/hotswap-deployments.ts

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { print } from '../logging';
55
import { ISDK, Mode, SdkProvider } from './aws-auth';
66
import { DeployStackResult } from './deploy-stack';
77
import { EvaluateCloudFormationTemplate, LazyListStackResources } from './evaluate-cloudformation-template';
8+
import { isHotswappableAppSyncChange } from './hotswap/appsync-mapping-templates';
89
import { isHotswappableCodeBuildProjectChange } from './hotswap/code-build-projects';
910
import { ICON, ChangeHotswapImpact, ChangeHotswapResult, HotswapOperation, HotswappableChangeCandidate } from './hotswap/common';
1011
import { isHotswappableEcsServiceChange } from './hotswap/ecs-services';
@@ -79,6 +80,7 @@ async function findAllHotswappableChanges(
7980
isHotswappableEcsServiceChange(logicalId, resourceHotswapEvaluation, evaluateCfnTemplate),
8081
isHotswappableS3BucketDeploymentChange(logicalId, resourceHotswapEvaluation, evaluateCfnTemplate),
8182
isHotswappableCodeBuildProjectChange(logicalId, resourceHotswapEvaluation, evaluateCfnTemplate),
83+
isHotswappableAppSyncChange(logicalId, resourceHotswapEvaluation, evaluateCfnTemplate),
8284
]);
8385
}
8486
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import * as AWS from 'aws-sdk';
2+
import { ISDK } from '../aws-auth';
3+
import { EvaluateCloudFormationTemplate } from '../evaluate-cloudformation-template';
4+
import { ChangeHotswapImpact, ChangeHotswapResult, HotswapOperation, HotswappableChangeCandidate, lowerCaseFirstCharacter, transformObjectKeys } from './common';
5+
6+
export async function isHotswappableAppSyncChange(
7+
logicalId: string, change: HotswappableChangeCandidate, evaluateCfnTemplate: EvaluateCloudFormationTemplate,
8+
): Promise<ChangeHotswapResult> {
9+
const isResolver = change.newValue.Type === 'AWS::AppSync::Resolver';
10+
const isFunction = change.newValue.Type === 'AWS::AppSync::FunctionConfiguration';
11+
12+
if (!isResolver && !isFunction) {
13+
return ChangeHotswapImpact.REQUIRES_FULL_DEPLOYMENT;
14+
}
15+
16+
for (const updatedPropName in change.propertyUpdates) {
17+
if (updatedPropName !== 'RequestMappingTemplate' && updatedPropName !== 'ResponseMappingTemplate') {
18+
return ChangeHotswapImpact.REQUIRES_FULL_DEPLOYMENT;
19+
}
20+
}
21+
22+
const resourceProperties = change.newValue.Properties;
23+
if (isResolver && resourceProperties?.Kind === 'PIPELINE') {
24+
// Pipeline resolvers can't be hotswapped as they reference
25+
// the FunctionId of the underlying functions, which can't be resolved.
26+
return ChangeHotswapImpact.REQUIRES_FULL_DEPLOYMENT;
27+
}
28+
29+
const resourcePhysicalName = await evaluateCfnTemplate.establishResourcePhysicalName(logicalId, isFunction ? resourceProperties?.Name : undefined);
30+
if (!resourcePhysicalName) {
31+
return ChangeHotswapImpact.REQUIRES_FULL_DEPLOYMENT;
32+
}
33+
34+
const evaluatedResourceProperties = await evaluateCfnTemplate.evaluateCfnExpression(resourceProperties);
35+
const sdkCompatibleResourceProperties = transformObjectKeys(evaluatedResourceProperties, lowerCaseFirstCharacter);
36+
37+
if (isResolver) {
38+
// Resolver physical name is the ARN in the format:
39+
// arn:aws:appsync:us-east-1:111111111111:apis/<apiId>/types/<type>/resolvers/<field>.
40+
// We'll use `<type>.<field>` as the resolver name.
41+
const arnParts = resourcePhysicalName.split('/');
42+
const resolverName = `${arnParts[3]}.${arnParts[5]}`;
43+
return new ResolverHotswapOperation(resolverName, sdkCompatibleResourceProperties);
44+
} else {
45+
return new FunctionHotswapOperation(resourcePhysicalName, sdkCompatibleResourceProperties);
46+
}
47+
}
48+
49+
class ResolverHotswapOperation implements HotswapOperation {
50+
public readonly service = 'appsync'
51+
public readonly resourceNames: string[];
52+
53+
constructor(resolverName: string, private readonly updateResolverRequest: AWS.AppSync.UpdateResolverRequest) {
54+
this.resourceNames = [`AppSync resolver '${resolverName}'`];
55+
}
56+
57+
public async apply(sdk: ISDK): Promise<any> {
58+
return sdk.appsync().updateResolver(this.updateResolverRequest).promise();
59+
}
60+
}
61+
62+
class FunctionHotswapOperation implements HotswapOperation {
63+
public readonly service = 'appsync'
64+
public readonly resourceNames: string[];
65+
66+
constructor(
67+
private readonly functionName: string,
68+
private readonly updateFunctionRequest: Omit<AWS.AppSync.UpdateFunctionRequest, 'functionId'>,
69+
) {
70+
this.resourceNames = [`AppSync function '${functionName}'`];
71+
}
72+
73+
public async apply(sdk: ISDK): Promise<any> {
74+
const { functions } = await sdk.appsync().listFunctions({ apiId: this.updateFunctionRequest.apiId }).promise();
75+
const { functionId } = functions?.find(fn => fn.name === this.functionName) ?? {};
76+
const request = {
77+
...this.updateFunctionRequest,
78+
functionId: functionId!,
79+
};
80+
return sdk.appsync().updateFunction(request).promise();
81+
}
82+
}

0 commit comments

Comments
 (0)