Skip to content

Commit dae54ec

Browse files
authored
feat(stepfunctions-tasks): add support for CodeBuild StartBuild API (#9757)
**Implementation** Update package `@aws-cdk/aws-stepfunctions-tasks` to include support for CodeBuild **StartBuild** API as per documentation here: https://docs.aws.amazon.com/step-functions/latest/dg/connect-codebuild.html Includes support for the following Amazon SageMaker API calls: * `StartBuild` Closes #8043 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 371e8da commit dae54ec

File tree

7 files changed

+637
-0
lines changed

7 files changed

+637
-0
lines changed

packages/@aws-cdk/aws-stepfunctions-tasks/README.md

+41
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ This module is part of the [AWS Cloud Development Kit](https://github.com/aws/aw
2828
- [Evaluate Expression](#evaluate-expression)
2929
- [Batch](#batch)
3030
- [SubmitJob](#submitjob)
31+
- [CodeBuild](#codebuild)
32+
- [StartBuild](#startbuild)
3133
- [DynamoDB](#dynamodb)
3234
- [GetItem](#getitem)
3335
- [PutItem](#putitem)
@@ -235,6 +237,45 @@ const task = new tasks.BatchSubmitJob(this, 'Submit Job', {
235237
});
236238
```
237239

240+
## CodeBuild
241+
242+
Step Functions supports [CodeBuild](https://docs.aws.amazon.com/step-functions/latest/dg/connect-codebuild.html) through the service integration pattern.
243+
244+
### StartBuild
245+
246+
[StartBuild](https://docs.aws.amazon.com/codebuild/latest/APIReference/API_StartBuild.html) starts a CodeBuild Project by Project Name.
247+
248+
```ts
249+
import * as codebuild from '@aws-cdk/aws-codebuild';
250+
import * as tasks from '@aws-cdk/aws-stepfunctions-tasks';
251+
import * as sfn from '@aws-cdk/aws-stepfunctions';
252+
253+
const codebuildProject = new codebuild.Project(stack, 'Project', {
254+
projectName: 'MyTestProject',
255+
buildSpec: codebuild.BuildSpec.fromObject({
256+
version: '0.2',
257+
phases: {
258+
build: {
259+
commands: [
260+
'echo "Hello, CodeBuild!"',
261+
],
262+
},
263+
},
264+
}),
265+
});
266+
267+
const task = new tasks.CodeBuildStartBuild(stack, 'Task', {
268+
project: codebuildProject,
269+
integrationPattern: sfn.IntegrationPattern.RUN_JOB,
270+
environmentVariablesOverride: {
271+
ZONE: {
272+
type: codebuild.BuildEnvironmentVariableType.PLAINTEXT,
273+
value: sfn.JsonPath.stringAt('$.envVariables.zone'),
274+
},
275+
},
276+
});
277+
```
278+
238279
## DynamoDB
239280

240281
You can call DynamoDB APIs from a `Task` state.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import * as codebuild from '@aws-cdk/aws-codebuild';
2+
import * as iam from '@aws-cdk/aws-iam';
3+
import * as sfn from '@aws-cdk/aws-stepfunctions';
4+
import * as cdk from '@aws-cdk/core';
5+
import { integrationResourceArn, validatePatternSupported } from '../private/task-utils';
6+
7+
/**
8+
* Properties for CodeBuildStartBuild
9+
*/
10+
export interface CodeBuildStartBuildProps extends sfn.TaskStateBaseProps {
11+
/**
12+
* CodeBuild project to start
13+
*/
14+
readonly project: codebuild.IProject;
15+
/**
16+
* A set of environment variables to be used for this build only.
17+
*
18+
* @default - the latest environment variables already defined in the build project.
19+
*/
20+
readonly environmentVariablesOverride?: { [name: string]: codebuild.BuildEnvironmentVariable };
21+
}
22+
23+
/**
24+
* Start a CodeBuild Build as a task
25+
*
26+
* @see https://docs.aws.amazon.com/step-functions/latest/dg/connect-codebuild.html
27+
*/
28+
export class CodeBuildStartBuild extends sfn.TaskStateBase {
29+
private static readonly SUPPORTED_INTEGRATION_PATTERNS: sfn.IntegrationPattern[] = [
30+
sfn.IntegrationPattern.REQUEST_RESPONSE,
31+
sfn.IntegrationPattern.RUN_JOB,
32+
];
33+
34+
protected readonly taskMetrics?: sfn.TaskMetricsConfig;
35+
protected readonly taskPolicies?: iam.PolicyStatement[];
36+
37+
private readonly integrationPattern: sfn.IntegrationPattern;
38+
39+
constructor(scope: cdk.Construct, id: string, private readonly props: CodeBuildStartBuildProps) {
40+
super(scope, id, props);
41+
this.integrationPattern = props.integrationPattern ?? sfn.IntegrationPattern.REQUEST_RESPONSE;
42+
43+
validatePatternSupported(this.integrationPattern, CodeBuildStartBuild.SUPPORTED_INTEGRATION_PATTERNS);
44+
45+
this.taskMetrics = {
46+
metricPrefixSingular: 'CodeBuildProject',
47+
metricPrefixPlural: 'CodeBuildProjects',
48+
metricDimensions: {
49+
ProjectArn: this.props.project.projectArn,
50+
},
51+
};
52+
53+
this.taskPolicies = this.configurePolicyStatements();
54+
}
55+
56+
private configurePolicyStatements(): iam.PolicyStatement[] {
57+
let policyStatements = [
58+
new iam.PolicyStatement({
59+
resources: [this.props.project.projectArn],
60+
actions: [
61+
'codebuild:StartBuild',
62+
'codebuild:StopBuild',
63+
],
64+
}),
65+
];
66+
67+
if (this.integrationPattern === sfn.IntegrationPattern.RUN_JOB) {
68+
policyStatements.push(
69+
new iam.PolicyStatement({
70+
actions: ['events:PutTargets', 'events:PutRule', 'events:DescribeRule'],
71+
resources: [
72+
cdk.Stack.of(this).formatArn({
73+
service: 'events',
74+
resource: 'rule/StepFunctionsGetEventForCodeBuildStartBuildRule',
75+
}),
76+
],
77+
}),
78+
);
79+
}
80+
81+
return policyStatements;
82+
}
83+
84+
/**
85+
* Provides the CodeBuild StartBuild service integration task configuration
86+
*/
87+
/**
88+
* @internal
89+
*/
90+
protected _renderTask(): any {
91+
return {
92+
Resource: integrationResourceArn('codebuild', 'startBuild', this.integrationPattern),
93+
Parameters: sfn.FieldUtils.renderObject({
94+
ProjectName: this.props.project.projectName,
95+
EnvironmentVariablesOverride: this.props.environmentVariablesOverride
96+
? this.serializeEnvVariables(this.props.environmentVariablesOverride)
97+
: undefined,
98+
}),
99+
};
100+
}
101+
102+
private serializeEnvVariables(environmentVariables: { [name: string]: codebuild.BuildEnvironmentVariable }) {
103+
return Object.keys(environmentVariables).map(name => ({
104+
Name: name,
105+
Type: environmentVariables[name].type || codebuild.BuildEnvironmentVariableType.PLAINTEXT,
106+
Value: environmentVariables[name].value,
107+
}));
108+
}
109+
}

packages/@aws-cdk/aws-stepfunctions-tasks/lib/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ export * from './dynamodb/put-item';
3434
export * from './dynamodb/update-item';
3535
export * from './dynamodb/delete-item';
3636
export * from './dynamodb/shared-types';
37+
export * from './codebuild/start-build';

packages/@aws-cdk/aws-stepfunctions-tasks/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"@aws-cdk/assets": "0.0.0",
7272
"@aws-cdk/aws-batch": "0.0.0",
7373
"@aws-cdk/aws-cloudwatch": "0.0.0",
74+
"@aws-cdk/aws-codebuild": "0.0.0",
7475
"@aws-cdk/aws-dynamodb": "0.0.0",
7576
"@aws-cdk/aws-ec2": "0.0.0",
7677
"@aws-cdk/aws-ecr": "0.0.0",
@@ -92,6 +93,7 @@
9293
"@aws-cdk/assets": "0.0.0",
9394
"@aws-cdk/aws-batch": "0.0.0",
9495
"@aws-cdk/aws-cloudwatch": "0.0.0",
96+
"@aws-cdk/aws-codebuild": "0.0.0",
9597
"@aws-cdk/aws-dynamodb": "0.0.0",
9698
"@aws-cdk/aws-ec2": "0.0.0",
9799
"@aws-cdk/aws-ecr": "0.0.0",

0 commit comments

Comments
 (0)