Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(codepipeline): x-env ECS deployment lacking support stack-dependency #24053

Merged
merged 2 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as iam from '@aws-cdk/aws-iam';
import { Lazy } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { Action } from '../action';
import { forceSupportStackDependency } from '../private/stack-dependency';

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

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

const taskDefinitionTemplateArtifact = determineTaskDefinitionArtifact(this.actionProps);
const appSpecTemplateArtifact = determineAppSpecArtifact(this.actionProps);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as iam from '@aws-cdk/aws-iam';
import * as s3 from '@aws-cdk/aws-s3';
import { Resource, Stack } from '@aws-cdk/core';

/**
* Create a dependency between the stack of the replication bucket and the stack of the action role
*
* If the deployment action happens in across-account/cross-region fashion, we
* create two support stacks (stack R for the cross-account role, and stack B for the
* cross-region replication bucket), but these stacks are not related to each
* other by default.
*
* To make it more interesting, if these are roles with autogenerated names, the
* stacks have bidirectional policies: the bucket and key (B) refer to the role
* (R), and the role (R) refers to the bucket and key (B). This is an
* unfortunate way of setting up the policies, and it should really be
* completely replaced with a tag-based mechanism.
*
* Until then, we've determined that deployment accidentally works fine if we deploy
* the account stack R first, followed by the region stack B. So explicitly establish
* this dependency in CodePipeline Actions.
*/
export function forceSupportStackDependency(bucket: s3.IBucket, role: iam.IRole) {
if (Resource.isOwnedResource(bucket) && Resource.isOwnedResource(role)) {
Stack.of(bucket).addDependency(Stack.of(role), `replication bucket {${bucket.node.path}} to action role {${role}}`);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,38 @@ describe('CodeDeploy ECS Deploy Action', () => {

});
});

test('cross-account cross-region deployment has correct dependency between support stacks', () => {
// GIVEN
const stackEnv: cdk.Environment = { account: '111111111111', region: 'us-east-1' };
const deployEnv: cdk.Environment = { account: '222222222222', region: 'us-east-2' };

const app = new cdk.App();
const stack = new cdk.Stack(app, 'Pipe', { env: stackEnv });
const deploymentGroup = codedeploy.EcsDeploymentGroup.fromEcsDeploymentGroupAttributes(stack, 'Group', {
application: codedeploy.EcsApplication.fromEcsApplicationArn(stack, 'Application',
`arn:aws:codedeploy:${deployEnv.region}:${deployEnv.account}:application:MyApplication`),
deploymentGroupName: 'MyGroup',
});

// WHEN
addCodeDeployECSCodePipeline(stack, {
actionName: 'DeployECS',
deploymentGroup,
taskDefinitionTemplateInput: new codepipeline.Artifact('Artifact'),
appSpecTemplateInput: new codepipeline.Artifact('Artifact2'),
});

// THEN - dependency from region stack to account stack
// (region stack has bucket, account stack has role)
const asm = app.synth();

const stacks = Object.fromEntries(asm.stacks.map(s => [s.stackName, s]));
expect(Object.keys(stacks)).toContain('Pipe-support-us-east-2');
expect(Object.keys(stacks)).toContain('Pipe-support-222222222222');

expect(stacks['Pipe-support-us-east-2'].dependencies).toContain(stacks['Pipe-support-222222222222']);
});
});

function addEcsDeploymentGroup(stack: cdk.Stack): codedeploy.IEcsDeploymentGroup {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@ applicationMultipleTargetGroupsFargateService.targetGroups[1].configureHealthChe
healthyHttpCodes: '200',
});

applicationMultipleTargetGroupsFargateService.loadBalancers[0]._enableCrossEnvironment;
applicationMultipleTargetGroupsFargateService.loadBalancers[1]._enableCrossEnvironment;

applicationMultipleTargetGroupsFargateService.listeners[0].listenerArn;
applicationMultipleTargetGroupsFargateService.listeners[1].listenerArn;

Comment on lines -70 to -75
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't these breaking changes?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm, I thought these were snapshot changes but they're not

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

app.synth();
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ networkMultipleTargetGroupsFargateService.targetGroups[0].configureHealthCheck({

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

networkMultipleTargetGroupsFargateService.loadBalancers[0]._enableCrossEnvironment;
networkMultipleTargetGroupsFargateService.loadBalancers[1]._enableCrossEnvironment;

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

app.synth();
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@ applicationMultipleTargetGroupsFargateService.targetGroups[1].configureHealthChe
healthyHttpCodes: '200',
});

applicationMultipleTargetGroupsFargateService.loadBalancers[0]._enableCrossEnvironment;
applicationMultipleTargetGroupsFargateService.loadBalancers[1]._enableCrossEnvironment;

applicationMultipleTargetGroupsFargateService.listeners[0].listenerArn;
applicationMultipleTargetGroupsFargateService.listeners[1].listenerArn;

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

app.synth();
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ networkMultipleTargetGroupsFargateService.targetGroups[0].configureHealthCheck({

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

networkMultipleTargetGroupsFargateService.loadBalancers[0]._enableCrossEnvironment;
networkMultipleTargetGroupsFargateService.loadBalancers[1]._enableCrossEnvironment;

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

Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/core/lib/cfn-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ export class CfnResource extends CfnRefElement {
return;
}

addDependency(this, target);
addDependency(this, target, `{${this.node.path}}.addDependency({${target.node.path}})`);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/@aws-cdk/core/lib/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ export class Stack extends Construct implements ITaggable {
* app, and also supports nested stacks.
*/
public addDependency(target: Stack, reason?: string) {
addDependency(this, target, reason);
addDependency(this, target, reason ?? `{${this.node.path}}.addDependency({${target.node.path}})`);
}

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


1 change: 0 additions & 1 deletion packages/@aws-cdk/core/test/stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2231,7 +2231,6 @@ describe('regionalFact', () => {
},
});
});

});

class StackWithPostProcessor extends Stack {
Expand Down