Skip to content

Commit 0baa573

Browse files
authored
fix(ecs): fromServiceArnWithCluster not accepting value from SSM Parameter string (#30902)
### Issue # (if applicable) Closes #30798. ### Reason for this change `fromServiceArnWithCluster()` function can't handle token value correctly. ### Description of changes Replace ``` const resourceName = arn.resourceName; ``` With ``` Arn.extractResourceName() ``` because the function above has the ability to handle token values. ### Description of how you validated changes - Updated the unit test - Manually verified the ECS cluster resource was used in the code pipeline when using the SSM parameter. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 8651bbe commit 0baa573

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

packages/aws-cdk-lib/aws-ecs/lib/base/base-service.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import {
1919
ArnFormat,
2020
FeatureFlags,
2121
Token,
22+
Arn,
23+
Fn,
2224
} from '../../../core';
2325
import * as cxapi from '../../../cx-api';
2426
import { RegionInfo } from '../../../region-info';
@@ -516,16 +518,20 @@ export abstract class BaseService extends Resource
516518
public static fromServiceArnWithCluster(scope: Construct, id: string, serviceArn: string): IBaseService {
517519
const stack = Stack.of(scope);
518520
const arn = stack.splitArn(serviceArn, ArnFormat.SLASH_RESOURCE_NAME);
519-
const resourceName = arn.resourceName;
520-
if (!resourceName) {
521-
throw new Error(`Missing resource Name from service ARN: ${serviceArn}`);
522-
}
523-
const resourceNameParts = resourceName.split('/');
524-
if (resourceNameParts.length !== 2) {
525-
throw new Error(`resource name ${resourceName} from service ARN: ${serviceArn} is not using the ARN cluster format`);
521+
const resourceName = Arn.extractResourceName(serviceArn, 'service');
522+
let clusterName: string;
523+
let serviceName: string;
524+
if (Token.isUnresolved(resourceName)) {
525+
clusterName = Fn.select(0, Fn.split('/', resourceName));
526+
serviceName = Fn.select(1, Fn.split('/', resourceName));
527+
} else {
528+
const resourceNameParts = resourceName.split('/');
529+
if (resourceNameParts.length !== 2) {
530+
throw new Error(`resource name ${resourceName} from service ARN: ${serviceArn} is not using the ARN cluster format`);
531+
}
532+
clusterName = resourceNameParts[0];
533+
serviceName = resourceNameParts[1];
526534
}
527-
const clusterName = resourceNameParts[0];
528-
const serviceName = resourceNameParts[1];
529535

530536
const clusterArn = Stack.of(scope).formatArn({
531537
partition: arn.partition,

packages/aws-cdk-lib/aws-ecs/test/base-service.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('When import an ECS Service', () => {
3838
test('throws an expection if no resourceName provided on fromServiceArnWithCluster', () => {
3939
expect(() => {
4040
ecs.BaseService.fromServiceArnWithCluster(stack, 'Service', 'arn:aws:ecs:service-region:service-account:service');
41-
}).toThrowError(/Missing resource Name from service ARN/);
41+
}).toThrowError(/Expected resource name in ARN, didn't find one: 'arn:aws:ecs:service-region:service-account:service'/);
4242
});
4343

4444
test('throws an expection if not using cluster arn format on fromServiceArnWithCluster', () => {
@@ -47,6 +47,11 @@ describe('When import an ECS Service', () => {
4747
}).toThrowError(/is not using the ARN cluster format/);
4848
});
4949

50+
test('skip validation for tokenized values', () => {
51+
expect(() => ecs.BaseService.fromServiceArnWithCluster(stack, 'Service',
52+
cdk.Lazy.string({ produce: () => 'arn:aws:ecs:service-region:service-account:service' }))).not.toThrow();
53+
});
54+
5055
test('should add a dependency on task role', () => {
5156
// GIVEN
5257
const vpc = new ec2.Vpc(stack, 'Vpc');

0 commit comments

Comments
 (0)