From 0344623210766210a749bf88605c9e3abcae9251 Mon Sep 17 00:00:00 2001 From: kaizen3031593 Date: Thu, 17 Feb 2022 17:00:43 -0500 Subject: [PATCH] fix(stepfunctions): imported State Machine sill has region and account from its Stack, instead of its ARN --- .../aws-stepfunctions/lib/state-machine.ts | 4 ++- .../test/state-machine.test.ts | 31 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-stepfunctions/lib/state-machine.ts b/packages/@aws-cdk/aws-stepfunctions/lib/state-machine.ts index 4143e4bba6601..7fe4423b8a310 100644 --- a/packages/@aws-cdk/aws-stepfunctions/lib/state-machine.ts +++ b/packages/@aws-cdk/aws-stepfunctions/lib/state-machine.ts @@ -142,7 +142,9 @@ abstract class StateMachineBase extends Resource implements IStateMachine { public readonly stateMachineArn = stateMachineArn; public readonly grantPrincipal = new iam.UnknownPrincipal({ resource: this }); } - return new Import(scope, id); + return new Import(scope, id, { + environmentFromArn: stateMachineArn, + }); } public abstract readonly stateMachineArn: string; diff --git a/packages/@aws-cdk/aws-stepfunctions/test/state-machine.test.ts b/packages/@aws-cdk/aws-stepfunctions/test/state-machine.test.ts index f60c0b756e66a..f99cc479c9975 100644 --- a/packages/@aws-cdk/aws-stepfunctions/test/state-machine.test.ts +++ b/packages/@aws-cdk/aws-stepfunctions/test/state-machine.test.ts @@ -277,4 +277,35 @@ describe('State Machine', () => { ], }); }); + + describe('StateMachine.fromStateMachineArn()', () => { + let stack: cdk.Stack; + + beforeEach(() => { + const app = new cdk.App(); + stack = new cdk.Stack(app, 'Base', { + env: { account: '111111111111', region: 'stack-region' }, + }); + }); + + describe('for a state machine in a different account and region', () => { + let mach: stepfunctions.IStateMachine; + + beforeEach(() => { + mach = stepfunctions.StateMachine.fromStateMachineArn( + stack, + 'iMach', + 'arn:aws:states:machine-region:222222222222:stateMachine:machine-name', + ); + }); + + test("the state machine's region is taken from the ARN", () => { + expect(mach.env.region).toBe('machine-region'); + }); + + test("the state machine's account is taken from the ARN", () => { + expect(mach.env.account).toBe('222222222222'); + }); + }); + }); });