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

aws_ecs: Fargate service not created because NetworkConfiguration is not null with External deployment controller #26335

Closed
msysh opened this issue Jul 12, 2023 · 4 comments · Fixed by #26338
Labels
@aws-cdk/aws-ecs Related to Amazon Elastic Container bug This issue is a bug. effort/medium Medium work item – several days of effort p2

Comments

@msysh
Copy link
Contributor

msysh commented Jul 12, 2023

Describe the bug

Creating a Fargate Service using an external deployment controller fails with following message:

Invalid request provided: CreateService error: NetworkConfiguration must be null.

Expected Behavior

When using an External deployment controller, it is expected that the Fargate Service will be deployed without network configuration.

Current Behavior

During the deployment process, getting following error:

20:36:57 | CREATE_FAILED        | AWS::ECS::Service        | FargateService/Service
Resource handler returned message: "Invalid request provided: CreateService error: NetworkConfiguration must be null. (Service: AmazonECS; Status Code: 400;
Error Code: InvalidParameterException; Request ID: 8c5d2a4c-9fbc-46c9-ae3e-c85fabc53825; Proxy: null)" (RequestToken: 4b95cca6-5f17-3ebe-3d07-1ea867b6a9e6, HandlerErrorCode: InvalidRequest)

This is because the generated template contains NetworkConfiguration.

Reproduction Steps

It can be reproduced with the following code:

import * as cdk from 'aws-cdk-lib';

const app = new cdk.App();
const stack = new cdk.Stack(app, 'Stack');

const vpc = new cdk.aws_ec2.Vpc(stack, 'Vpc', { maxAzs: 2, restrictDefaultSecurityGroup: false });

const cluster = new cdk.aws_ecs.Cluster(stack, 'Cluster', { vpc });

const taskDefinition = new cdk.aws_ecs.FargateTaskDefinition(stack, 'TaskDef');
taskDefinition.addContainer('web', {
  image: cdk.aws_ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
  memoryLimitMiB: 512,
});

new cdk.aws_ecs.FargateService(stack, 'Service', {
  cluster,
  taskDefinition,
  deploymentController: {
    type: cdk.aws_ecs.DeploymentControllerType.EXTERNAL,
  },
});

Possible Solution

In fargate-service.ts, if deploymentControllerType is external, skip network configuration generation.

The following modifications can be suggested:

if (!props.deploymentController ||
  (props.deploymentController && props.deploymentController?.type !== DeploymentControllerType.EXTERNAL)) {
  this.configureAwsVpcNetworkingWithSecurityGroups(props.cluster.vpc, props.assignPublicIp, props.vpcSubnets, securityGroups);
}

In addition, the following unit tests will need to be modified:

test('ignore task definition and launch type if deployment controller is set to be EXTERNAL', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'MyVpc', {});
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
const taskDefinition = new ecs.FargateTaskDefinition(stack, 'FargateTaskDef');
taskDefinition.addContainer('web', {
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
});
new ecs.FargateService(stack, 'FargateService', {
cluster,
taskDefinition,
deploymentController: {
type: DeploymentControllerType.EXTERNAL,
},
});
// THEN
Annotations.fromStack(stack).hasWarning('/Default/FargateService', 'taskDefinition and launchType are blanked out when using external deployment controller.');
Template.fromStack(stack).hasResourceProperties('AWS::ECS::Service', {
Cluster: {
Ref: 'EcsCluster97242B84',
},
DeploymentConfiguration: {
MaximumPercent: 200,
MinimumHealthyPercent: 50,
},
DeploymentController: {
Type: 'EXTERNAL',
},
EnableECSManagedTags: false,
NetworkConfiguration: {
AwsvpcConfiguration: {
AssignPublicIp: 'DISABLED',
SecurityGroups: [
{
'Fn::GetAtt': [
'FargateServiceSecurityGroup0A0E79CB',
'GroupId',
],
},
],
Subnets: [
{
Ref: 'MyVpcPrivateSubnet1Subnet5057CF7E',
},
{
Ref: 'MyVpcPrivateSubnet2Subnet0040C983',
},
],
},
},
});
});

Additional Information/Context

This problem does not occur with ECS on EC2.

Currently this problem can be avoided with an escape hatches as follows.

const cfnService = service.node.defaultChild as cdk.aws_ecs.CfnService;
cfnService.addDeletionOverride('Properties.NetworkConfiguration');

CDK CLI Version

2.87.0 (build 9fca790)

Framework Version

No response

Node.js Version

v16.14.0

OS

macOS 13.4

Language

Typescript

Language Version

No response

Other information

No response

@msysh msysh added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jul 12, 2023
@github-actions github-actions bot added the @aws-cdk/aws-ecs Related to Amazon Elastic Container label Jul 12, 2023
@pahud
Copy link
Contributor

pahud commented Jul 12, 2023

Thank you for your report and the workaround with addDeletionOverride(). Yes, we should fix that.

@pahud pahud added p2 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Jul 12, 2023
@mergify mergify bot closed this as completed in #26338 Jul 14, 2023
mergify bot pushed a commit that referenced this issue Jul 14, 2023
…using EXTERNAL deployment controller (#26338)

When using an external deployment controller, ECS Service creation fails because the `NetworkConfiguration` is output to the template.
This fix prevents the output of `NetworkConfiguration` when using external deployment controller.

Closes #26335

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

colifran pushed a commit that referenced this issue Jul 17, 2023
…using EXTERNAL deployment controller (#26338)

When using an external deployment controller, ECS Service creation fails because the `NetworkConfiguration` is output to the template.
This fix prevents the output of `NetworkConfiguration` when using external deployment controller.

Closes #26335

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
bmoffatt pushed a commit to bmoffatt/aws-cdk that referenced this issue Jul 29, 2023
…using EXTERNAL deployment controller (aws#26338)

When using an external deployment controller, ECS Service creation fails because the `NetworkConfiguration` is output to the template.
This fix prevents the output of `NetworkConfiguration` when using external deployment controller.

Closes aws#26335

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@analytically
Copy link

could this cause NetworkLoadBalancedFargateService.getService not to return a FargateService instance anymore?

@msysh
Copy link
Contributor Author

msysh commented Aug 5, 2023

@analytically Thank you for notice. I would like to clarify. Is it means that you cannot get an instance with NetworkLoadBalancedFargateService.service when the deployment type is External? In case of my trying by short code, I could get the instance by NetworkLoadBalancedFargateService.service both before and after fixing this issue.
The short code is following:

import * as cdk from 'aws-cdk-lib';
import { NetworkLoadBalancedFargateService } from 'aws-cdk-lib/aws-ecs-patterns';

const app = new cdk.App();
const stack = new cdk.Stack(app, 'Stack');

const taskDefinition = new cdk.aws_ecs.FargateTaskDefinition(stack, 'FargateTaskDef');

taskDefinition.addContainer('web', {
  image: cdk.aws_ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
  memoryLimitMiB: 512,
  portMappings: [ { containerPort: 80, } ],
});

const service = new NetworkLoadBalancedFargateService(stack, 'Service', {
  deploymentController: {
    type: cdk.aws_ecs.DeploymentControllerType.EXTERNAL,
  },
  taskDefinition: taskDefinition,
});

// --------------------------
// I could get the instance.
// --------------------------
console.debug(service.service);

If your mention is not, could you please provide a reproduction step?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-ecs Related to Amazon Elastic Container bug This issue is a bug. effort/medium Medium work item – several days of effort p2
Projects
None yet
3 participants