Skip to content

Commit f24ece1

Browse files
authored
fix(stepfunctions-tasks): specify tags in BatchSubmitJob properties (#26349)
This change adds the possibility to specify the [`tags`](https://docs.aws.amazon.com/batch/latest/APIReference/API_SubmitJob.html#:~:text=Required%3A%20No-,tags,-The%20tags%20that) property in the `BatchSubmitJob` construct. Closes #26336. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent bdfdc91 commit f24ece1

File tree

5 files changed

+120
-3
lines changed

5 files changed

+120
-3
lines changed

packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/batch/integ.submit-job.js

+4-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/batch/integ.submit-job.js.snapshot/aws-stepfunctions-integ.template.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@
758758
"JobQueueArn"
759759
]
760760
},
761-
"\",\"Parameters\":{\"foo.$\":\"$.bar\"},\"ContainerOverrides\":{\"Environment\":[{\"Name\":\"key\",\"Value\":\"value\"}],\"ResourceRequirements\":[{\"Type\":\"MEMORY\",\"Value\":\"256\"},{\"Type\":\"VCPU\",\"Value\":\"1\"}]},\"RetryStrategy\":{\"Attempts\":3},\"Timeout\":{\"AttemptDurationSeconds\":60}}}}}"
761+
"\",\"Parameters\":{\"foo.$\":\"$.bar\"},\"ContainerOverrides\":{\"Environment\":[{\"Name\":\"key\",\"Value\":\"value\"}],\"ResourceRequirements\":[{\"Type\":\"MEMORY\",\"Value\":\"256\"},{\"Type\":\"VCPU\",\"Value\":\"1\"}]},\"RetryStrategy\":{\"Attempts\":3},\"Tags\":{\"key\":\"value\"},\"Timeout\":{\"AttemptDurationSeconds\":60}}}}}"
762762
]
763763
]
764764
}

packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/batch/integ.submit-job.ts

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ class RunBatchStack extends cdk.Stack {
5656
}),
5757
attempts: 3,
5858
taskTimeout: sfn.Timeout.duration(cdk.Duration.seconds(60)),
59+
tags: {
60+
key: 'value',
61+
},
5962
});
6063

6164
const definition = new sfn.Pass(this, 'Start', {

packages/@aws-cdk/aws-batch-alpha/test/aws-stepfunctions-tasks/submit-job.test.ts

+86
Original file line numberDiff line numberDiff line change
@@ -361,3 +361,89 @@ test('Task throws if attempt duration is less than 60 sec', () => {
361361
/attempt duration must be greater than 60 seconds./,
362362
);
363363
});
364+
365+
test('supports passing tags', () => {
366+
// WHEN
367+
const task = new BatchSubmitJob(stack, 'Task', {
368+
jobDefinitionArn: batchJobDefinition.jobDefinitionArn,
369+
jobQueueArn: batchJobQueue.jobQueueArn,
370+
jobName: sfn.JsonPath.stringAt('$.jobName'),
371+
tags: {
372+
test: 'this is a tag',
373+
},
374+
});
375+
376+
// THEN
377+
expect(stack.resolve(task.toStateJson())).toEqual({
378+
Type: 'Task',
379+
Resource: {
380+
'Fn::Join': [
381+
'',
382+
[
383+
'arn:',
384+
{
385+
Ref: 'AWS::Partition',
386+
},
387+
':states:::batch:submitJob.sync',
388+
],
389+
],
390+
},
391+
End: true,
392+
Parameters: {
393+
'JobDefinition': { Ref: 'JobDefinition24FFE3ED' },
394+
'JobName.$': '$.jobName',
395+
'JobQueue': {
396+
'Fn::GetAtt': [
397+
'JobQueueEE3AD499',
398+
'JobQueueArn',
399+
],
400+
},
401+
'Tags': {
402+
test: 'this is a tag',
403+
},
404+
},
405+
});
406+
});
407+
408+
test('throws if tags has invalid value', () => {
409+
expect(() => {
410+
const tags: { [key: string]: string } = {};
411+
for (let i = 0; i < 100; i++) {
412+
tags[i] = 'tag';
413+
}
414+
new BatchSubmitJob(stack, 'Task1', {
415+
jobDefinitionArn: batchJobDefinition.jobDefinitionArn,
416+
jobName: 'JobName',
417+
jobQueueArn: batchJobQueue.jobQueueArn,
418+
tags,
419+
});
420+
}).toThrow(
421+
/Maximum tag number of entries is 50./,
422+
);
423+
424+
expect(() => {
425+
const keyTooLong = 'k'.repeat(150);
426+
const tags: { [key: string]: string } = {};
427+
tags[keyTooLong] = 'tag';
428+
new BatchSubmitJob(stack, 'Task2', {
429+
jobDefinitionArn: batchJobDefinition.jobDefinitionArn,
430+
jobName: 'JobName',
431+
jobQueueArn: batchJobQueue.jobQueueArn,
432+
tags,
433+
});
434+
}).toThrow(
435+
/Tag key size must be between 1 and 128/,
436+
);
437+
438+
expect(() => {
439+
const tags = { key: 'k'.repeat(300) };
440+
new BatchSubmitJob(stack, 'Task3', {
441+
jobDefinitionArn: batchJobDefinition.jobDefinitionArn,
442+
jobName: 'JobName',
443+
jobQueueArn: batchJobQueue.jobQueueArn,
444+
tags,
445+
});
446+
}).toThrow(
447+
/Tag value maximum size is 256/,
448+
);
449+
});

packages/aws-cdk-lib/aws-stepfunctions-tasks/lib/batch/submit-job.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,13 @@ export interface BatchSubmitJobProps extends sfn.TaskStateBaseProps {
147147
* @default 1
148148
*/
149149
readonly attempts?: number;
150+
151+
/**
152+
* The tags applied to the job request.
153+
*
154+
* @default {} - no tags
155+
*/
156+
readonly tags?: { [key: string]: string };
150157
}
151158

152159
/**
@@ -212,6 +219,8 @@ export class BatchSubmitJob extends sfn.TaskStateBase {
212219
});
213220
}
214221

222+
this.validateTags(props.tags);
223+
215224
this.taskPolicies = this.configurePolicyStatements();
216225
}
217226

@@ -255,7 +264,7 @@ export class BatchSubmitJob extends sfn.TaskStateBase {
255264
this.props.attempts !== undefined
256265
? { Attempts: this.props.attempts }
257266
: undefined,
258-
267+
Tags: this.props.tags,
259268
Timeout: timeout
260269
? { AttemptDurationSeconds: timeout }
261270
: undefined,
@@ -337,4 +346,20 @@ export class BatchSubmitJob extends sfn.TaskStateBase {
337346
ResourceRequirements: resources.length ? resources : undefined,
338347
};
339348
}
349+
350+
private validateTags(tags?: { [key: string]: string }) {
351+
if (tags === undefined) return;
352+
const tagEntries = Object.entries(tags);
353+
if (tagEntries.length > 50) {
354+
throw new Error(`Maximum tag number of entries is 50. Received ${tagEntries.length}.`);
355+
}
356+
for (const [key, value] of tagEntries) {
357+
if (key.length < 1 || key.length > 128) {
358+
throw new Error(`Tag key size must be between 1 and 128, but got ${key.length}.`);
359+
}
360+
if (value.length > 256) {
361+
throw new Error(`Tag value maximum size is 256, but got ${value.length}.`);
362+
}
363+
}
364+
}
340365
}

0 commit comments

Comments
 (0)