aws / aws/aws-cdk

ecs: unable to change networkMode back to the default

Open
#33,410 7 comments 0 reactions 0 assignees View on GitHub
@aws-cdk/aws-ecs bug effort/medium p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

An EcsService defined like this:

```ts

const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDef', {
family: 'task-def',
taskRole: taskRole,
networkMode: ecs.NetworkMode.AWS_VPC,
});

const realtimeWsService = new ecs.Ec2Service(this, 'Service', {
serviceName: 'Service',
cluster: props.cluster,
taskDefinition,
capacityProviderStrategies: [gpucapacityProviderStrategy],
serviceConnectConfiguration: {
logDriver: new ecs.AwsLogDriver({
logGroup,
streamPrefix: 'serviceconnect',
}),
namespace: 'local',
// realtimews.local:5001
services: [{ portMappingName: 'rtws', dnsName: 'rtws' }],
},
});

this.lb = new elbv2.ApplicationLoadBalancer(this, 'LB', {
loadBalancerName: 'Ecs-RealtimeWs-ALB',
vpc: props.vpc,
internetFacing: true,
preserveHostHeader: true,
});

const listener = this.lb.addListener('PublicListener', {
port: 80,
open: true,
protocol: elbv2.ApplicationProtocol.HTTP,
});

// Attach ALB to ECS Service
listener.addTargets('ECS', {
targetGroupName: 'EcsWebsocketAlbTG',
port: 80,
protocol: elbv2.ApplicationProtocol.HTTP,
targets: [
realtimeWsService.loadBalancerTarget({
containerName: containerDef.containerName,
containerPort: containerDef.containerPort,
}),
],
});
```

This deploys and works perfectly fine. However, now, I want to change the approach and I want to remove the public ALB altogether and go back to the default `networkMode`.

```ts

const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDef', {
family: 'task-def',
taskRole: taskRole,
// networkMode: ecs.NetworkMode.AWS_VPC,
});

const realtimeWsService = new ecs.Ec2Service(this, 'Service', {
serviceName: 'Service',
cluster: props.cluster,
taskDefinition,
capacityProviderStrategies: [gpucapacityProviderStrategy],
serviceConnectConfiguration: {
logDriver: new ecs.AwsLogDriver({
logGroup,
streamPrefix: 'serviceconnect',
}),
namespace: 'local',
// realtimews.local:5001
services: [{ portMappingName: 'rtws', dnsName: 'rtws' }],
},
});

```

This bails out with:

> Service-Stack | 2/6 | 10:42:38 PM | UPDATE_FAILED | AWS::ECS::Service | Resource handler returned message: "Invalid request provided: UpdateService error: The target group arn:aws:elasticloadbalancing:us-west-2:679849022850:targetgroup/EcsWebsocketAlbTG/efbc21827edf62e8 does not exist. (Service: AmazonECS; Status Code: 400; Error Code: InvalidParameterException;

If you check the synchronised template, you'll see it is still referencing the TargetGroup, even though it's nowhere in the code.

### Regression Issue

- [ ] Select this option if this issue appears to be a regression.

### Last Known Working CDK Version

_No response_

### Expected Behavior

above

### Current Behavior

above

### Reproduction Steps

above

### Possible Solution

_No response_

### Additional Information/Context

```json
"devDependencies": {
"@biomejs/biome": "1.9.4",
"@types/babel__traverse": "^7.18.2",
"@types/js-yaml": "^4.0.5",
"@types/node": "^22.10.2",
"@typescript-eslint/eslint-plugin": "^8.18.0",
"@typescript-eslint/parser": "^8.18.0",
"aws-cdk": "^2.80.0",
"lefthook": "^1.10.1",
"typescript": "^5.6.3"
},
"dependencies": {
"aws-cdk": "^2.80.0",
"aws-cdk-lib": "^2.80.0",
"cdk-nag": "^2.28.179",
"cloudwatch-retention-setter": "^0.0.15",
"constructs": "^10.0.0",
"js-yaml": "^4.1.0",
"source-map-support": "^0.5.21"
}
```

### CDK CLI Version

2.177.0 (build b396961)

### Framework Version

_No response_

### Node.js Version

v22.12.0

### OS

MacOS

### Language

TypeScript

### Language Version

```
├── @biomejs/biome@1.9.4
├── @types/babel__traverse@7.20.6
├── @types/js-yaml@4.0.9
├── @types/node@22.10.10
├── @typescript-eslint/eslint-plugin@8.21.0
├── @typescript-eslint/parser@8.21.0
├── aws-cdk-lib@2.177.0
├── aws-cdk@2.177.0
├── cdk-nag@2.35.1
├── cloudwatch-retention-setter@0.0.15
├── constructs@10.4.2
├── js-yaml@4.1.0
├── lefthook@1.10.10
├── source-map-support@0.5.21
└── typescript@5.7.3
```

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Reproduce the transition from an Ec2TaskDefinition using AWS_VPC and an ALB target to the default network mode, then inspect the synthesized template and ECS service update. Trace the Ec2Service and loadBalancerTarget handling; done means the removed target group is no longer referenced and the stack update succeeds.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, infrastructure
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.