Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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*
- Loading branch information
Showing
8 changed files
with
481 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
packages/aws-cdk/lib/api/hotswap/appsync-mapping-templates.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import * as AWS from 'aws-sdk'; | ||
import { ISDK } from '../aws-auth'; | ||
import { EvaluateCloudFormationTemplate } from '../evaluate-cloudformation-template'; | ||
import { ChangeHotswapImpact, ChangeHotswapResult, HotswapOperation, HotswappableChangeCandidate, lowerCaseFirstCharacter, transformObjectKeys } from './common'; | ||
|
||
export async function isHotswappableAppSyncChange( | ||
logicalId: string, change: HotswappableChangeCandidate, evaluateCfnTemplate: EvaluateCloudFormationTemplate, | ||
): Promise<ChangeHotswapResult> { | ||
const isResolver = change.newValue.Type === 'AWS::AppSync::Resolver'; | ||
const isFunction = change.newValue.Type === 'AWS::AppSync::FunctionConfiguration'; | ||
|
||
if (!isResolver && !isFunction) { | ||
return ChangeHotswapImpact.REQUIRES_FULL_DEPLOYMENT; | ||
} | ||
|
||
for (const updatedPropName in change.propertyUpdates) { | ||
if (updatedPropName !== 'RequestMappingTemplate' && updatedPropName !== 'ResponseMappingTemplate') { | ||
return ChangeHotswapImpact.REQUIRES_FULL_DEPLOYMENT; | ||
} | ||
} | ||
|
||
const resourceProperties = change.newValue.Properties; | ||
if (isResolver && resourceProperties?.Kind === 'PIPELINE') { | ||
// Pipeline resolvers can't be hotswapped as they reference | ||
// the FunctionId of the underlying functions, which can't be resolved. | ||
return ChangeHotswapImpact.REQUIRES_FULL_DEPLOYMENT; | ||
} | ||
|
||
const resourcePhysicalName = await evaluateCfnTemplate.establishResourcePhysicalName(logicalId, isFunction ? resourceProperties?.Name : undefined); | ||
if (!resourcePhysicalName) { | ||
return ChangeHotswapImpact.REQUIRES_FULL_DEPLOYMENT; | ||
} | ||
|
||
const evaluatedResourceProperties = await evaluateCfnTemplate.evaluateCfnExpression(resourceProperties); | ||
const sdkCompatibleResourceProperties = transformObjectKeys(evaluatedResourceProperties, lowerCaseFirstCharacter); | ||
|
||
if (isResolver) { | ||
// Resolver physical name is the ARN in the format: | ||
// arn:aws:appsync:us-east-1:111111111111:apis/<apiId>/types/<type>/resolvers/<field>. | ||
// We'll use `<type>.<field>` as the resolver name. | ||
const arnParts = resourcePhysicalName.split('/'); | ||
const resolverName = `${arnParts[3]}.${arnParts[5]}`; | ||
return new ResolverHotswapOperation(resolverName, sdkCompatibleResourceProperties); | ||
} else { | ||
return new FunctionHotswapOperation(resourcePhysicalName, sdkCompatibleResourceProperties); | ||
} | ||
} | ||
|
||
class ResolverHotswapOperation implements HotswapOperation { | ||
public readonly service = 'appsync' | ||
public readonly resourceNames: string[]; | ||
|
||
constructor(resolverName: string, private readonly updateResolverRequest: AWS.AppSync.UpdateResolverRequest) { | ||
this.resourceNames = [`AppSync resolver '${resolverName}'`]; | ||
} | ||
|
||
public async apply(sdk: ISDK): Promise<any> { | ||
return sdk.appsync().updateResolver(this.updateResolverRequest).promise(); | ||
} | ||
} | ||
|
||
class FunctionHotswapOperation implements HotswapOperation { | ||
public readonly service = 'appsync' | ||
public readonly resourceNames: string[]; | ||
|
||
constructor( | ||
private readonly functionName: string, | ||
private readonly updateFunctionRequest: Omit<AWS.AppSync.UpdateFunctionRequest, 'functionId'>, | ||
) { | ||
this.resourceNames = [`AppSync function '${functionName}'`]; | ||
} | ||
|
||
public async apply(sdk: ISDK): Promise<any> { | ||
const { functions } = await sdk.appsync().listFunctions({ apiId: this.updateFunctionRequest.apiId }).promise(); | ||
const { functionId } = functions?.find(fn => fn.name === this.functionName) ?? {}; | ||
const request = { | ||
...this.updateFunctionRequest, | ||
functionId: functionId!, | ||
}; | ||
return sdk.appsync().updateFunction(request).promise(); | ||
} | ||
} |
Oops, something went wrong.