Skip to content

Commit 4876fe4

Browse files
Merge branch 'main' into OpenSearch_1_3
2 parents 3d51356 + ff3c01a commit 4876fe4

4 files changed

Lines changed: 105 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ export enum TagType {
480480
export interface ICfnResourceOptions {
481481
/**
482482
* A condition to associate with this resource. This means that only if the condition evaluates to 'true' when the stack
483-
* is deployed, the resource will be included. This is provided to allow CDK projects to produce legacy templates, but noramlly
483+
* is deployed, the resource will be included. This is provided to allow CDK projects to produce legacy templates, but normally
484484
* there is no need to use it in CDK projects.
485485
*/
486486
condition?: CfnCondition;

packages/@aws-cdk/pipelines/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,34 @@ class MyJenkinsStep extends pipelines.Step implements pipelines.ICodePipelineAct
835835
}
836836
```
837837

838+
### Using an existing AWS Codepipeline
839+
840+
If you wish to use an existing `CodePipeline.Pipeline` while using the modern API's
841+
methods and classes, you can pass in the existing `CodePipeline.Pipeline` to be built upon
842+
instead of having the `pipelines.CodePipeline` construct create a new `CodePipeline.Pipeline`.
843+
This also gives you more direct control over the underlying `CodePipeline.Pipeline` construct
844+
if the way the modern API creates it doesn't allow for desired configurations.
845+
846+
Here's an example of passing in an existing pipeline:
847+
848+
```ts
849+
declare const codePipeline: codepipeline.Pipeline;
850+
851+
const pipeline = new pipelines.CodePipeline(this, 'Pipeline', {
852+
synth: new pipelines.ShellStep('Synth', {
853+
input: pipelines.CodePipelineSource.connection('my-org/my-app', 'main', {
854+
connectionArn: 'arn:aws:codestar-connections:us-east-1:222222222222:connection/7d2469ff-514a-4e4f-9003-5ca4a43cdc41', // Created using the AWS console * });',
855+
}),
856+
commands: ['npm ci','npm run build','npx cdk synth'],
857+
}),
858+
codePipeline: codePipeline,
859+
});
860+
```
861+
862+
Note that if you provide an existing pipeline, you cannot provide values for
863+
`pipelineName`, `crossAccountKeys`, `reuseCrossRegionSupportStacks`, or `role`
864+
because those values are passed in directly to the underlying `codepipeline.Pipeline`.
865+
838866
## Using Docker in the pipeline
839867

840868
Docker can be used in 3 different places in the pipeline:

packages/@aws-cdk/pipelines/lib/codepipeline/codepipeline.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,13 @@ export interface CodePipelineProps {
207207
* @default - true (Use the same support stack for all pipelines in App)
208208
*/
209209
readonly reuseCrossRegionSupportStacks?: boolean;
210+
211+
/**
212+
* The IAM role to be assumed by this Pipeline
213+
*
214+
* @default - A new role is created
215+
*/
216+
readonly role?: iam.IRole;
210217
}
211218

212219
/**
@@ -362,6 +369,9 @@ export class CodePipeline extends PipelineBase {
362369
if (this.props.reuseCrossRegionSupportStacks !== undefined) {
363370
throw new Error('Cannot set \'reuseCrossRegionSupportStacks\' if an existing CodePipeline is given using \'codePipeline\'');
364371
}
372+
if (this.props.role !== undefined) {
373+
throw new Error('Cannot set \'role\' if an existing CodePipeline is given using \'codePipeline\'');
374+
}
365375

366376
this._pipeline = this.props.codePipeline;
367377
} else {
@@ -372,6 +382,7 @@ export class CodePipeline extends PipelineBase {
372382
// This is necessary to make self-mutation work (deployments are guaranteed
373383
// to happen only after the builds of the latest pipeline definition).
374384
restartExecutionOnUpdate: true,
385+
role: this.props.role,
375386
});
376387
}
377388

packages/@aws-cdk/pipelines/test/codepipeline/codepipeline.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { Template, Annotations, Match } from '@aws-cdk/assertions';
22
import * as ccommit from '@aws-cdk/aws-codecommit';
33
import { Pipeline } from '@aws-cdk/aws-codepipeline';
4+
import * as iam from '@aws-cdk/aws-iam';
45
import * as sqs from '@aws-cdk/aws-sqs';
56
import * as cdk from '@aws-cdk/core';
7+
import { Stack } from '@aws-cdk/core';
68
import { Construct } from 'constructs';
79
import * as cdkp from '../../lib';
10+
import { CodePipeline } from '../../lib';
811
import { PIPELINE_ENV, TestApp, ModernTestGitHubNpmPipeline, FileAssetApp } from '../testhelpers';
912

1013
let app: TestApp;
@@ -88,6 +91,15 @@ describe('Providing codePipeline parameter and prop(s) of codePipeline parameter
8891
reuseCrossRegionSupportStacks: true,
8992
}).create()).toThrowError('Cannot set \'reuseCrossRegionSupportStacks\' if an existing CodePipeline is given using \'codePipeline\'');
9093
});
94+
test('Providing codePipeline parameter and role parameter should throw error', () => {
95+
const stack = new Stack(app, 'Stack');
96+
97+
expect(() => new CodePipelinePropsCheckTest(stack, 'CodePipeline', {
98+
role: new iam.Role(stack, 'Role', {
99+
assumedBy: new iam.ServicePrincipal('codebuild.amazonaws.com'),
100+
}),
101+
}).create()).toThrowError('Cannot set \'role\' if an existing CodePipeline is given using \'codePipeline\'');
102+
});
91103
});
92104

93105
test('Policy sizes do not exceed the maximum size', () => {
@@ -180,6 +192,51 @@ test('CodeBuild action role has the right AssumeRolePolicyDocument', () => {
180192
});
181193
});
182194

195+
test('CodePipeline supports use of existing role', () => {
196+
const pipelineStack = new cdk.Stack(app, 'PipelineStack', { env: PIPELINE_ENV });
197+
const repo = new ccommit.Repository(pipelineStack, 'Repo', {
198+
repositoryName: 'MyRepo',
199+
});
200+
const cdkInput = cdkp.CodePipelineSource.codeCommit(
201+
repo,
202+
'main',
203+
);
204+
205+
new CodePipeline(pipelineStack, 'Pipeline', {
206+
synth: new cdkp.ShellStep('Synth', {
207+
input: cdkInput,
208+
installCommands: ['npm ci'],
209+
commands: [
210+
'npm run build',
211+
'npx cdk synth',
212+
],
213+
}),
214+
role: new iam.Role(pipelineStack, 'CustomRole', {
215+
assumedBy: new iam.ServicePrincipal('codepipeline.amazonaws.com'),
216+
roleName: 'MyCustomPipelineRole',
217+
}),
218+
});
219+
220+
const template = Template.fromStack(pipelineStack);
221+
template.hasResourceProperties('AWS::IAM::Role', {
222+
AssumeRolePolicyDocument: {
223+
Statement: [
224+
{
225+
Action: 'sts:AssumeRole',
226+
Effect: 'Allow',
227+
Principal: {
228+
Service: 'codepipeline.amazonaws.com',
229+
},
230+
},
231+
],
232+
},
233+
RoleName: 'MyCustomPipelineRole',
234+
});
235+
template.hasResourceProperties('AWS::CodePipeline::Pipeline', {
236+
RoleArn: { 'Fn::GetAtt': ['CustomRole6D8E6809', 'Arn'] },
237+
});
238+
});
239+
183240
interface ReuseCodePipelineStackProps extends cdk.StackProps {
184241
reuseCrossRegionSupportStacks?: boolean;
185242
}
@@ -241,6 +298,7 @@ interface CodePipelineStackProps extends cdk.StackProps {
241298
pipelineName?: string;
242299
crossAccountKeys?: boolean;
243300
reuseCrossRegionSupportStacks?: boolean;
301+
role?: iam.IRole;
244302
}
245303

246304
class CodePipelinePropsCheckTest extends cdk.Stack {
@@ -271,5 +329,12 @@ class CodePipelinePropsCheckTest extends cdk.Stack {
271329
synth: new cdkp.ShellStep('Synth', { commands: ['ls'] }),
272330
}).buildPipeline();
273331
}
332+
if (this.cProps.role !== undefined) {
333+
new cdkp.CodePipeline(this, 'CodePipeline4', {
334+
role: this.cProps.role,
335+
codePipeline: new Pipeline(this, 'Pipline4'),
336+
synth: new cdkp.ShellStep('Synth', { commands: ['ls'] }),
337+
}).buildPipeline();
338+
}
274339
}
275340
}

0 commit comments

Comments
 (0)