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(core): serviceTimeout for CustomResource does not work with token #33541

Merged
merged 5 commits into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions packages/aws-cdk-lib/core/lib/custom-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ export interface CustomResourceProps {
*
* Maps to [ServiceTimeout](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cloudformation-customresource.html#cfn-cloudformation-customresource-servicetimeout) property for the `AWS::CloudFormation::CustomResource` resource
*
* A token can be specified for this property, but it must be specified with `Duration.seconds()`.
*
* @example
* const stack = new Stack();
* const durToken = new CfnParameter(stack, 'MyParameter', {
* type: 'Number',
* default: 60,
* });
* new CustomResource(stack, 'MyCustomResource', {
* serviceToken: 'MyServiceToken',
* serviceTimeout: Duration.seconds(durToken.valueAsNumber),
* });
*
* @default Duration.seconds(3600)
*/
readonly serviceTimeout?: Duration;
Expand Down Expand Up @@ -154,8 +167,7 @@ export class CustomResource extends Resource {
const pascalCaseProperties = props.pascalCaseProperties ?? false;
const properties = pascalCaseProperties ? uppercaseProperties(props.properties || {}) : (props.properties || {});

if (props.serviceTimeout !== undefined && !Token.isUnresolved(props.serviceTimeout)
) {
if (props.serviceTimeout !== undefined && !props.serviceTimeout.isUnresolved()) {
const serviceTimeoutSeconds = props.serviceTimeout.toSeconds();

if (serviceTimeoutSeconds < 1 || serviceTimeoutSeconds > 3600) {
Expand Down
57 changes: 56 additions & 1 deletion packages/aws-cdk-lib/core/test/custom-resource.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { toCloudFormation } from './util';
import { Annotations } from '../../assertions';
import { CustomResource, Duration, RemovalPolicy, Stack } from '../lib';
import { CfnParameter, CustomResource, Duration, RemovalPolicy, Stack } from '../lib';

describe('custom resource', () => {
test('simple case provider identified by service token', () => {
Expand Down Expand Up @@ -201,6 +201,61 @@ describe('custom resource', () => {
});
});

test('set serviceTimeout with token as seconds', () => {
// GIVEN
const stack = new Stack();
const durToken = new CfnParameter(stack, 'MyParameter', {
type: 'Number',
default: 60,
});

// WHEN
new CustomResource(stack, 'MyCustomResource', {
serviceToken: 'MyServiceToken',
serviceTimeout: Duration.seconds(durToken.valueAsNumber),
});

// THEN
expect(toCloudFormation(stack)).toEqual({
Parameters: {
MyParameter: {
Default: 60,
Type: 'Number',
},
},
Resources: {
MyCustomResource: {
Type: 'AWS::CloudFormation::CustomResource',
Properties: {
ServiceToken: 'MyServiceToken',
ServiceTimeout: {
Ref: 'MyParameter',
},
},
UpdateReplacePolicy: 'Delete',
DeletionPolicy: 'Delete',
},
},
});
});

test('throws error when serviceTimeout is set with token as units other than seconds', () => {
// GIVEN
const stack = new Stack();
const durToken = new CfnParameter(stack, 'MyParameter', {
type: 'Number',
default: 60,
});

// WHEN
expect(() => {
new CustomResource(stack, 'MyCustomResource', {
serviceToken: 'MyServiceToken',
serviceTimeout: Duration.minutes(durToken.valueAsNumber),
});
}).toThrow('Duration must be specified as \'Duration.seconds()\' here since its value comes from a token and cannot be converted (got Duration.minutes)');
});

test.each([0, 4000])('throw an error when serviceTimeout is set to %d seconds.', (invalidSeconds: number) => {
// GIVEN
const stack = new Stack();
Expand Down