Skip to content

Commit adfe4fa

Browse files
authored
fix(codepipeline): x-env ECS deployment lacking support stack-dependency (#24053)
In the case of a cross-account AND cross-region ECS deployment, a dependency between the support stacks that is necessary is missing. This cannot be fixed globally. Because the resources reference each other bidirectionally (user -> bucket, bucket -> user), the only way to fix this is to do it locally: in the CodePipeline module, where we can have the knowledge that we use generated names and that everything will work out if we deploy the role before the bucket. All CodePipeline Actions must have this fix eventually, but since people may have crazy stack setups in which addition of this dependency may introduce a cyclic dependency (breaking the synth), we're rolling this fix out with limited blast radius. Follow-up in #24050, and suggest a good clean-up in #24051. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 1a46808 commit adfe4fa

10 files changed

+64
-20
lines changed

packages/@aws-cdk/aws-codepipeline-actions/lib/codedeploy/ecs-deploy-action.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as iam from '@aws-cdk/aws-iam';
44
import { Lazy } from '@aws-cdk/core';
55
import { Construct } from 'constructs';
66
import { Action } from '../action';
7+
import { forceSupportStackDependency } from '../private/stack-dependency';
78

89
/**
910
* Configuration for replacing a placeholder string in the ECS task
@@ -177,6 +178,7 @@ export class CodeDeployEcsDeployAction extends Action {
177178

178179
// the Action's Role needs to read from the Bucket to get artifacts
179180
options.bucket.grantRead(options.role);
181+
forceSupportStackDependency(options.bucket, options.role);
180182

181183
const taskDefinitionTemplateArtifact = determineTaskDefinitionArtifact(this.actionProps);
182184
const appSpecTemplateArtifact = determineAppSpecArtifact(this.actionProps);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as iam from '@aws-cdk/aws-iam';
2+
import * as s3 from '@aws-cdk/aws-s3';
3+
import { Resource, Stack } from '@aws-cdk/core';
4+
5+
/**
6+
* Create a dependency between the stack of the replication bucket and the stack of the action role
7+
*
8+
* If the deployment action happens in across-account/cross-region fashion, we
9+
* create two support stacks (stack R for the cross-account role, and stack B for the
10+
* cross-region replication bucket), but these stacks are not related to each
11+
* other by default.
12+
*
13+
* To make it more interesting, if these are roles with autogenerated names, the
14+
* stacks have bidirectional policies: the bucket and key (B) refer to the role
15+
* (R), and the role (R) refers to the bucket and key (B). This is an
16+
* unfortunate way of setting up the policies, and it should really be
17+
* completely replaced with a tag-based mechanism.
18+
*
19+
* Until then, we've determined that deployment accidentally works fine if we deploy
20+
* the account stack R first, followed by the region stack B. So explicitly establish
21+
* this dependency in CodePipeline Actions.
22+
*/
23+
export function forceSupportStackDependency(bucket: s3.IBucket, role: iam.IRole) {
24+
if (Resource.isOwnedResource(bucket) && Resource.isOwnedResource(role)) {
25+
Stack.of(bucket).addDependency(Stack.of(role), `replication bucket {${bucket.node.path}} to action role {${role}}`);
26+
}
27+
}

packages/@aws-cdk/aws-codepipeline-actions/test/codedeploy/ecs-deploy-action.test.ts

+32
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,38 @@ describe('CodeDeploy ECS Deploy Action', () => {
197197

198198
});
199199
});
200+
201+
test('cross-account cross-region deployment has correct dependency between support stacks', () => {
202+
// GIVEN
203+
const stackEnv: cdk.Environment = { account: '111111111111', region: 'us-east-1' };
204+
const deployEnv: cdk.Environment = { account: '222222222222', region: 'us-east-2' };
205+
206+
const app = new cdk.App();
207+
const stack = new cdk.Stack(app, 'Pipe', { env: stackEnv });
208+
const deploymentGroup = codedeploy.EcsDeploymentGroup.fromEcsDeploymentGroupAttributes(stack, 'Group', {
209+
application: codedeploy.EcsApplication.fromEcsApplicationArn(stack, 'Application',
210+
`arn:aws:codedeploy:${deployEnv.region}:${deployEnv.account}:application:MyApplication`),
211+
deploymentGroupName: 'MyGroup',
212+
});
213+
214+
// WHEN
215+
addCodeDeployECSCodePipeline(stack, {
216+
actionName: 'DeployECS',
217+
deploymentGroup,
218+
taskDefinitionTemplateInput: new codepipeline.Artifact('Artifact'),
219+
appSpecTemplateInput: new codepipeline.Artifact('Artifact2'),
220+
});
221+
222+
// THEN - dependency from region stack to account stack
223+
// (region stack has bucket, account stack has role)
224+
const asm = app.synth();
225+
226+
const stacks = Object.fromEntries(asm.stacks.map(s => [s.stackName, s]));
227+
expect(Object.keys(stacks)).toContain('Pipe-support-us-east-2');
228+
expect(Object.keys(stacks)).toContain('Pipe-support-222222222222');
229+
230+
expect(stacks['Pipe-support-us-east-2'].dependencies).toContain(stacks['Pipe-support-222222222222']);
231+
});
200232
});
201233

202234
function addEcsDeploymentGroup(stack: cdk.Stack): codedeploy.IEcsDeploymentGroup {

packages/@aws-cdk/aws-ecs-patterns/test/ec2/integ.healthchecks-multiple-application-load-balanced-ecs-service.ts

-6
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,6 @@ applicationMultipleTargetGroupsFargateService.targetGroups[1].configureHealthChe
6767
healthyHttpCodes: '200',
6868
});
6969

70-
applicationMultipleTargetGroupsFargateService.loadBalancers[0]._enableCrossEnvironment;
71-
applicationMultipleTargetGroupsFargateService.loadBalancers[1]._enableCrossEnvironment;
72-
73-
applicationMultipleTargetGroupsFargateService.listeners[0].listenerArn;
74-
applicationMultipleTargetGroupsFargateService.listeners[1].listenerArn;
75-
7670
new IntegTest(app, 'Integ', { testCases: [stack] });
7771

7872
app.synth();

packages/@aws-cdk/aws-ecs-patterns/test/ec2/integ.healthchecks-multiple-network-load-balanced-ecs-service.ts

-3
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ networkMultipleTargetGroupsFargateService.targetGroups[0].configureHealthCheck({
5050

5151
networkMultipleTargetGroupsFargateService.targetGroups[1].configureHealthCheck({});
5252

53-
networkMultipleTargetGroupsFargateService.loadBalancers[0]._enableCrossEnvironment;
54-
networkMultipleTargetGroupsFargateService.loadBalancers[1]._enableCrossEnvironment;
55-
5653
new IntegTest(app, 'Integ', { testCases: [stack] });
5754

5855
app.synth();

packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.healthchecks-multiple-application-load-balanced-fargate-service.ts

-6
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,6 @@ applicationMultipleTargetGroupsFargateService.targetGroups[1].configureHealthChe
6666
healthyHttpCodes: '200',
6767
});
6868

69-
applicationMultipleTargetGroupsFargateService.loadBalancers[0]._enableCrossEnvironment;
70-
applicationMultipleTargetGroupsFargateService.loadBalancers[1]._enableCrossEnvironment;
71-
72-
applicationMultipleTargetGroupsFargateService.listeners[0].listenerArn;
73-
applicationMultipleTargetGroupsFargateService.listeners[1].listenerArn;
74-
7569
new IntegTest(app, 'Integ', { testCases: [stack] });
7670

7771
app.synth();

packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.healthchecks-multiple-network-load-balanced-fargate-service.ts

-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ networkMultipleTargetGroupsFargateService.targetGroups[0].configureHealthCheck({
5050

5151
networkMultipleTargetGroupsFargateService.targetGroups[1].configureHealthCheck({});
5252

53-
networkMultipleTargetGroupsFargateService.loadBalancers[0]._enableCrossEnvironment;
54-
networkMultipleTargetGroupsFargateService.loadBalancers[1]._enableCrossEnvironment;
5553

5654
new IntegTest(app, 'Integ', { testCases: [stack] });
5755

packages/@aws-cdk/core/lib/cfn-resource.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ export class CfnResource extends CfnRefElement {
297297
return;
298298
}
299299

300-
addDependency(this, target);
300+
addDependency(this, target, `{${this.node.path}}.addDependency({${target.node.path}})`);
301301
}
302302

303303
/**

packages/@aws-cdk/core/lib/stack.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ export class Stack extends Construct implements ITaggable {
605605
* app, and also supports nested stacks.
606606
*/
607607
public addDependency(target: Stack, reason?: string) {
608-
addDependency(this, target, reason);
608+
addDependency(this, target, reason ?? `{${this.node.path}}.addDependency({${target.node.path}})`);
609609
}
610610

611611
/**
@@ -1718,3 +1718,4 @@ import { Fact, RegionInfo } from '@aws-cdk/region-info';
17181718
import { deployTimeLookup } from './private/region-lookup';
17191719
import { makeUniqueResourceName } from './private/unique-resource-name';import { PRIVATE_CONTEXT_DEFAULT_STACK_SYNTHESIZER } from './private/private-context';
17201720

1721+

packages/@aws-cdk/core/test/stack.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -2231,7 +2231,6 @@ describe('regionalFact', () => {
22312231
},
22322232
});
22332233
});
2234-
22352234
});
22362235

22372236
class StackWithPostProcessor extends Stack {

0 commit comments

Comments
 (0)