Skip to content

Commit de67aae

Browse files
authoredDec 10, 2021
fix(cli): hotswapping StateMachines with a name fails (#17892)
Before, when the `stateMachineName` property was used, the value of `stateMachineName` was passed directly to the SDK where an ARN was required. Now, when the `stateMachineName` property is used, we construct the ARN from its value, and pass that ARN to the SDK. Closes #17716 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 1df478b commit de67aae

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed
 

‎packages/aws-cdk/lib/api/hotswap/stepfunctions-state-machines.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ISDK } from '../aws-auth';
2-
import { ChangeHotswapImpact, ChangeHotswapResult, HotswapOperation, HotswappableChangeCandidate, establishResourcePhysicalName } from './common';
2+
import { ChangeHotswapImpact, ChangeHotswapResult, HotswapOperation, HotswappableChangeCandidate } from './common';
33
import { EvaluateCloudFormationTemplate } from './evaluate-cloudformation-template';
44

55
export async function isHotswappableStateMachineChange(
@@ -11,15 +11,20 @@ export async function isHotswappableStateMachineChange(
1111
return stateMachineDefinitionChange;
1212
}
1313

14-
const machineNameInCfnTemplate = change.newValue?.Properties?.StateMachineName;
15-
const machineName = await establishResourcePhysicalName(logicalId, machineNameInCfnTemplate, evaluateCfnTemplate);
16-
if (!machineName) {
14+
const stateMachineNameInCfnTemplate = change.newValue?.Properties?.StateMachineName;
15+
const stateMachineArn = stateMachineNameInCfnTemplate
16+
? await evaluateCfnTemplate.evaluateCfnExpression({
17+
'Fn::Sub': 'arn:${AWS::Partition}:states:${AWS::Region}:${AWS::AccountId}:stateMachine:' + stateMachineNameInCfnTemplate,
18+
})
19+
: await evaluateCfnTemplate.findPhysicalNameFor(logicalId);
20+
21+
if (!stateMachineArn) {
1722
return ChangeHotswapImpact.REQUIRES_FULL_DEPLOYMENT;
1823
}
1924

2025
return new StateMachineHotswapOperation({
2126
definition: stateMachineDefinitionChange,
22-
stateMachineName: machineName,
27+
stateMachineArn: stateMachineArn,
2328
});
2429
}
2530

@@ -43,7 +48,7 @@ async function isStateMachineDefinitionOnlyChange(
4348
}
4449

4550
interface StateMachineResource {
46-
readonly stateMachineName: string;
51+
readonly stateMachineArn: string;
4752
readonly definition: string;
4853
}
4954

@@ -56,8 +61,7 @@ class StateMachineHotswapOperation implements HotswapOperation {
5661
public async apply(sdk: ISDK): Promise<any> {
5762
// not passing the optional properties leaves them unchanged
5863
return sdk.stepFunctions().updateStateMachine({
59-
// even though the name of the property is stateMachineArn, passing the name of the state machine is allowed here
60-
stateMachineArn: this.stepFunctionResource.stateMachineName,
64+
stateMachineArn: this.stepFunctionResource.stateMachineArn,
6165
definition: this.stepFunctionResource.definition,
6266
}).promise();
6367
}

‎packages/aws-cdk/test/api/hotswap/state-machine-hotswap-deployments.test.ts

+11-10
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ test('calls the updateStateMachine() API when it receives only a definitionStrin
6363
expect(deployStackResult).not.toBeUndefined();
6464
expect(mockUpdateMachineDefinition).toHaveBeenCalledWith({
6565
definition: '{ Prop: "new-value" }',
66-
stateMachineArn: 'my-machine',
66+
stateMachineArn: 'arn:aws:states:here:123456789012:stateMachine:my-machine',
6767
});
6868
});
6969

@@ -138,7 +138,7 @@ test('calls the updateStateMachine() API when it receives only a definitionStrin
138138
},
139139
},
140140
}, null, 2),
141-
stateMachineArn: 'my-machine',
141+
stateMachineArn: 'arn:aws:states:here:123456789012:stateMachine:my-machine',
142142
});
143143
});
144144

@@ -168,14 +168,14 @@ test('calls the updateStateMachine() API when it receives a change to the defini
168168
});
169169

170170
// WHEN
171-
setup.pushStackResourceSummaries(setup.stackSummaryOf('Machine', 'AWS::StepFunctions::StateMachine', 'mock-machine-resource-id'));
171+
setup.pushStackResourceSummaries(setup.stackSummaryOf('Machine', 'AWS::StepFunctions::StateMachine', 'arn:aws:states:here:123456789012:stateMachine:my-machine'));
172172
const deployStackResult = await hotswapMockSdkProvider.tryHotswapDeployment(cdkStackArtifact);
173173

174174
// THEN
175175
expect(deployStackResult).not.toBeUndefined();
176176
expect(mockUpdateMachineDefinition).toHaveBeenCalledWith({
177177
definition: '{ "Prop" : "new-value" }',
178-
stateMachineArn: 'mock-machine-resource-id', // the sdk will convert the ID to the arn in a production environment
178+
stateMachineArn: 'arn:aws:states:here:123456789012:stateMachine:my-machine',
179179
});
180180
});
181181

@@ -256,7 +256,7 @@ test('can correctly hotswap old style synth changes', async () => {
256256
setup.setCurrentCfnStackTemplate({
257257
Parameters: { AssetParam1: { Type: 'String' } },
258258
Resources: {
259-
SM: {
259+
Machine: {
260260
Type: 'AWS::StepFunctions::StateMachine',
261261
Properties: {
262262
DefinitionString: { Ref: 'AssetParam1' },
@@ -269,7 +269,7 @@ test('can correctly hotswap old style synth changes', async () => {
269269
template: {
270270
Parameters: { AssetParam2: { Type: String } },
271271
Resources: {
272-
SM: {
272+
Machine: {
273273
Type: 'AWS::StepFunctions::StateMachine',
274274
Properties: {
275275
DefinitionString: { Ref: 'AssetParam2' },
@@ -281,13 +281,14 @@ test('can correctly hotswap old style synth changes', async () => {
281281
});
282282

283283
// WHEN
284+
setup.pushStackResourceSummaries(setup.stackSummaryOf('Machine', 'AWS::StepFunctions::StateMachine', 'arn:aws:states:here:123456789012:stateMachine:my-machine'));
284285
const deployStackResult = await hotswapMockSdkProvider.tryHotswapDeployment(cdkStackArtifact, { AssetParam2: 'asset-param-2' });
285286

286287
// THEN
287288
expect(deployStackResult).not.toBeUndefined();
288289
expect(mockUpdateMachineDefinition).toHaveBeenCalledWith({
289290
definition: 'asset-param-2',
290-
stateMachineArn: 'machine-name',
291+
stateMachineArn: 'arn:aws:states:here:123456789012:stateMachine:machine-name',
291292
});
292293
});
293294

@@ -348,7 +349,7 @@ test('calls the updateStateMachine() API when it receives a change to the defini
348349

349350
// WHEN
350351
setup.pushStackResourceSummaries(
351-
setup.stackSummaryOf('Machine', 'AWS::StepFunctions::StateMachine', 'mock-machine-resource-id'),
352+
setup.stackSummaryOf('Machine', 'AWS::StepFunctions::StateMachine', 'arn:aws:states:here:123456789012:stateMachine:my-machine'),
352353
setup.stackSummaryOf('Func', 'AWS::Lambda::Function', 'my-func'),
353354
);
354355
const deployStackResult = await hotswapMockSdkProvider.tryHotswapDeployment(cdkStackArtifact);
@@ -357,7 +358,7 @@ test('calls the updateStateMachine() API when it receives a change to the defini
357358
expect(deployStackResult).not.toBeUndefined();
358359
expect(mockUpdateMachineDefinition).toHaveBeenCalledWith({
359360
definition: '"Resource": arn:aws:lambda:here:123456789012:function:my-func',
360-
stateMachineArn: 'my-machine',
361+
stateMachineArn: 'arn:aws:states:here:123456789012:stateMachine:my-machine',
361362
});
362363
});
363364

@@ -446,7 +447,7 @@ test("will not perform a hotswap deployment if it doesn't know how to handle a s
446447
},
447448
});
448449
setup.pushStackResourceSummaries(
449-
setup.stackSummaryOf('Machine', 'AWS::StepFunctions::StateMachine', 'my-machine'),
450+
setup.stackSummaryOf('Machine', 'AWS::StepFunctions::StateMachine', 'arn:aws:states:here:123456789012:stateMachine:my-machine'),
450451
setup.stackSummaryOf('Bucket', 'AWS::S3::Bucket', 'my-bucket'),
451452
);
452453
const cdkStackArtifact = setup.cdkStackArtifactOf({

0 commit comments

Comments
 (0)
Please sign in to comment.