(ecs): Base Service tokenized Service Arn doesn't contain region or account.
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 83
Description
### What is the problem?
The tokenized ARN](https://github.com/aws/aws-cdk/blob/ef8ab7245630f54b5a633925e27b34c31b712abc/packages/%40aws-cdk/aws-ecs/lib/base/base-service.ts#L429-L433) is using the clusterName and physicalName to generate the service ARN token.
1. The ARN should be using the serviceName vs the physicalName because the serviceName aligns with what the actual ARN would be.
2. The tokenized ARN should be fully qualified as a multiple parts so the ARN can be leveraged in `Ec2Service.fromEc2ServiceAttributes` or `FargateService.fromFargateServiceAttributes` to determine the region and account when deploying across regions.
### Reproduction Steps
This generates the token for the serviceArn.
```
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'MyVpc', {});
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'Ec2TaskDef');
taskDefinition.addContainer('web', {
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
memoryLimitMiB: 512,
});
const service = new ecs.Ec2Service(stack, 'Ec2Service', {
cluster,
taskDefinition,
});
```
`service.serviceArn` generates a token, if this is a different account or region the in the cause of using a code pipeline which is deploying across multiple accounts or regions leveraging `Ec2Service.fromEc2ServiceAttributes` with the service ARN would be used in the pipeline to get the service, as you directly pass the service across boundaries.
This means to get the serviceArn after the service definition you need to do something like below to get the serviceArn to not be a full token. Also this is related to issue #16634.
```
const serviceArn = this.formatArn({
service: 'ecs',
resource: 'service',
resourceName: `${cluster.clusterName}/${service.serviceName},
});
```
### What did you expect to happen?
I would expect the serviceArn look like this `arn:${Token[AWS.Partition.5]}:ecs:${Token[AWS.Region.6]}:${Token[AWS.AccountId.2]}:service/${Token[TOKEN.425]}/${Token[TOKEN.481]}`
Which will allow the region, account as well as the clusterName and serviceName to be populated correctly when deploying the token in.
### What actually happened?
The current serviceArn looks likes `${Token[TOKEN.489]}` which means using the `serviceArn` for `Ec2Service.fromEc2ServiceAttributes` doesn't set the region or account correctly.
### CDK CLI Version
N/A
### Framework Version
_No response_
### Node.js Version
v16.13.1
### OS
Mac
### Language
Typescript
### Language Version
_No response_
### Other information
This issue is seen in CDK tests so not related to specific CDK version.
Contributor guide
Research direction
Read packages/@aws-cdk/aws-ecs/lib/base/base-service.ts around lines 429-433, then inspect the ECS tests where this behavior is observed. Verify that serviceArn uses the service name and exposes partition, region, account, cluster, and service components so the fromEc2ServiceAttributes and fromFargateServiceAttributes entry points can recover deployment context.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, typescript
- Domain
- cloud
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100