Skip to content

Commit

Permalink
fix(stepfunctions-tasks): specify tags in BatchSubmitJob properties (#…
Browse files Browse the repository at this point in the history
…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*
  • Loading branch information
lpizzinidev authored Jul 25, 2023
1 parent bdfdc91 commit f24ece1
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 3 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@
"JobQueueArn"
]
},
"\",\"Parameters\":{\"foo.$\":\"$.bar\"},\"ContainerOverrides\":{\"Environment\":[{\"Name\":\"key\",\"Value\":\"value\"}],\"ResourceRequirements\":[{\"Type\":\"MEMORY\",\"Value\":\"256\"},{\"Type\":\"VCPU\",\"Value\":\"1\"}]},\"RetryStrategy\":{\"Attempts\":3},\"Timeout\":{\"AttemptDurationSeconds\":60}}}}}"
"\",\"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}}}}}"
]
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class RunBatchStack extends cdk.Stack {
}),
attempts: 3,
taskTimeout: sfn.Timeout.duration(cdk.Duration.seconds(60)),
tags: {
key: 'value',
},
});

const definition = new sfn.Pass(this, 'Start', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,89 @@ test('Task throws if attempt duration is less than 60 sec', () => {
/attempt duration must be greater than 60 seconds./,
);
});

test('supports passing tags', () => {
// WHEN
const task = new BatchSubmitJob(stack, 'Task', {
jobDefinitionArn: batchJobDefinition.jobDefinitionArn,
jobQueueArn: batchJobQueue.jobQueueArn,
jobName: sfn.JsonPath.stringAt('$.jobName'),
tags: {
test: 'this is a tag',
},
});

// THEN
expect(stack.resolve(task.toStateJson())).toEqual({
Type: 'Task',
Resource: {
'Fn::Join': [
'',
[
'arn:',
{
Ref: 'AWS::Partition',
},
':states:::batch:submitJob.sync',
],
],
},
End: true,
Parameters: {
'JobDefinition': { Ref: 'JobDefinition24FFE3ED' },
'JobName.$': '$.jobName',
'JobQueue': {
'Fn::GetAtt': [
'JobQueueEE3AD499',
'JobQueueArn',
],
},
'Tags': {
test: 'this is a tag',
},
},
});
});

test('throws if tags has invalid value', () => {
expect(() => {
const tags: { [key: string]: string } = {};
for (let i = 0; i < 100; i++) {
tags[i] = 'tag';
}
new BatchSubmitJob(stack, 'Task1', {
jobDefinitionArn: batchJobDefinition.jobDefinitionArn,
jobName: 'JobName',
jobQueueArn: batchJobQueue.jobQueueArn,
tags,
});
}).toThrow(
/Maximum tag number of entries is 50./,
);

expect(() => {
const keyTooLong = 'k'.repeat(150);
const tags: { [key: string]: string } = {};
tags[keyTooLong] = 'tag';
new BatchSubmitJob(stack, 'Task2', {
jobDefinitionArn: batchJobDefinition.jobDefinitionArn,
jobName: 'JobName',
jobQueueArn: batchJobQueue.jobQueueArn,
tags,
});
}).toThrow(
/Tag key size must be between 1 and 128/,
);

expect(() => {
const tags = { key: 'k'.repeat(300) };
new BatchSubmitJob(stack, 'Task3', {
jobDefinitionArn: batchJobDefinition.jobDefinitionArn,
jobName: 'JobName',
jobQueueArn: batchJobQueue.jobQueueArn,
tags,
});
}).toThrow(
/Tag value maximum size is 256/,
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ export interface BatchSubmitJobProps extends sfn.TaskStateBaseProps {
* @default 1
*/
readonly attempts?: number;

/**
* The tags applied to the job request.
*
* @default {} - no tags
*/
readonly tags?: { [key: string]: string };
}

/**
Expand Down Expand Up @@ -212,6 +219,8 @@ export class BatchSubmitJob extends sfn.TaskStateBase {
});
}

this.validateTags(props.tags);

this.taskPolicies = this.configurePolicyStatements();
}

Expand Down Expand Up @@ -255,7 +264,7 @@ export class BatchSubmitJob extends sfn.TaskStateBase {
this.props.attempts !== undefined
? { Attempts: this.props.attempts }
: undefined,

Tags: this.props.tags,
Timeout: timeout
? { AttemptDurationSeconds: timeout }
: undefined,
Expand Down Expand Up @@ -337,4 +346,20 @@ export class BatchSubmitJob extends sfn.TaskStateBase {
ResourceRequirements: resources.length ? resources : undefined,
};
}

private validateTags(tags?: { [key: string]: string }) {
if (tags === undefined) return;
const tagEntries = Object.entries(tags);
if (tagEntries.length > 50) {
throw new Error(`Maximum tag number of entries is 50. Received ${tagEntries.length}.`);
}
for (const [key, value] of tagEntries) {
if (key.length < 1 || key.length > 128) {
throw new Error(`Tag key size must be between 1 and 128, but got ${key.length}.`);
}
if (value.length > 256) {
throw new Error(`Tag value maximum size is 256, but got ${value.length}.`);
}
}
}
}

0 comments on commit f24ece1

Please sign in to comment.