aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

ECS: Service should autoconfigure security groups when attached to NLB

Open rix0rrr opened this issue 7 years ago • 36 comments

Right now in ELBv2, security groups are configured for Application Load Balancers, but not for Network Load Balancers.

It seems to be that NLBs don't have security groups themselves (verify), but in that case the target should be autoconfiguring its own SGs correctly upon the LB being attached to them.

@karlpatr has sensible things to say about this in https://github.com/aws/aws-cdk/issues/3667, we should investigate and follow up.

rix0rrr avatar Jan 07 '19 15:01 rix0rrr

@rix0rrr NLBs don't have security groups, no? Access is supposed to be controlled by the resource behind the NLB. Or am I missing something?

tsykora-verimatrix avatar Sep 03 '19 09:09 tsykora-verimatrix

I thought as much, but apparently they do. We get this issue reported a lot these days. Maybe it changed.

rix0rrr avatar Sep 17 '19 08:09 rix0rrr

Oh I see what's going on, it needs to configure that SGs of the Target

rix0rrr avatar Sep 17 '19 08:09 rix0rrr

Just encountered with this problem this morning and took me quite a while to realize that it was the default security group created for ECS service didn't allow NLB to connect. For now I have to create an SG separately for the ECS service and allow any ip to connect, would be great if it works out of the box.

mouyigang avatar Sep 18 '19 19:09 mouyigang

I think the reason this comes up often is that ALB security is handled implicitly during attachment in BaseService (easier since there is a known security group to use) and people want NLB to be just as easy to use. I'm not sure people realize that the origin of the traffic also must be granted access explicitly since that's not true for ALBs, and I'm not sure if there is a one-size-fits-all case that can be inferred at template time since it's not necessary to register NLB callers.

In addition, the health check for an NLB currently needs to be explicitly granted access before it can work properly (see note under recommended rules here for scope: https://docs.aws.amazon.com/elasticloadbalancing/latest/network/target-group-register-targets.html). This tripped me up as I was building since I knew to link the caller SG but had to research where the health check was coming from.

I would not want to see CDK take a posture of weaker default security for convenience, but many people seem to arrive at the shotgun approach of adding any ip to the target ingress to bypass VPC security because it covers both the health check and intended audience cases.

karlpatr avatar Sep 18 '19 21:09 karlpatr

I do not want the NLB to be accessible from the internet, but obviously the Healthcheck must work. The use case I have is that I want to access the NLB from an api gateway vpc link eg.

new apigateway.VpcLink(this, 'MyVPCLink', { targets: [myFargateService.loadBalancer], });

The solution should cater for this use case too.

davidsteed avatar Sep 19 '19 09:09 davidsteed

Can someone bottom line this for me? Are the following statements true or false:

  • The Security Group of the target (let's say the ECS service) must accept traffic from the NLBs subnet (by CIDR?) in order for health checks to work.
  • The Security Group of the target (let's say the ECS service) must specify the traffic source it wants to accept requests from.

Is that an accurate summary?

rix0rrr avatar Sep 19 '19 09:09 rix0rrr

Both statements are true -- opening the target security group to the VPN CIDR is the only way I've found to allow the first one in CDK without multiple creation passes, but the actual required scope is narrower.

karlpatr avatar Sep 19 '19 18:09 karlpatr

I had thought about proposing a proxied security interface to "allow access to the NLB" and have definitions attach to the target instead as a late resolution step; I think that meets user expectations but obfuscates the reality and diverges from what a user would find after template creation.

karlpatr avatar Sep 19 '19 18:09 karlpatr

As long as the documentation is clear - obfuscating templates from CDK code is fine in my opinion. A few examples of how you would set up various use cases would be all I as a user would be after. Eg. Setting up a NLB with Port 80 open, setting up a TLS listener, Setting up a NLB with a VPC link

davidsteed avatar Sep 20 '19 06:09 davidsteed

Public-facing NLB fronted fargate tasks with assignPublicIp enabled should allow all ipv4 ingress traffic.

I am submitting a PR(https://github.com/aws/aws-cdk/pull/4825) with this implementation FYR.

pahud avatar Nov 02 '19 17:11 pahud

Both statements are true -- opening the target security group to the VPN CIDR is the only way I've found to allow the first one in CDK without multiple creation passes, but the actual required scope is narrower.

Agree. The best approach to deal with the problem might be looking up the NLB subnet CIDRs and then setting up inbound rule for target SG allowing ingress traffic for those CIDRs. However, we can only know the CIDRs of the NLB subnets in deploy stage.

iamhopaul123 avatar Nov 13 '19 01:11 iamhopaul123

Can somebody give me a hint on this, how to work around this issue for the moment? I have a network load balanced fargate service that's supposed to run behind an API gateway (NLB not public). After some digging I found that the security group of the service does now allow traffic from the NLB (as described).

sebastian-schlecht avatar Nov 13 '19 15:11 sebastian-schlecht

Can somebody give me a hint on this, how to work around this issue for the moment? I have a network load balanced fargate service that's supposed to run behind an API gateway (NLB not public). After some digging I found that the security group of the service does now allow traffic from the NLB (as described).

  1. Use this approach (opening the port for ingress traffic for every IP though the actual required scope of IPs is narrower).
  2. Manually looking for the NLB subnet IPv4 CIDRs and whitelist them when setting rule for ingress traffic for the target security group.

iamhopaul123 avatar Nov 13 '19 16:11 iamhopaul123

I have written a custom resource provider which might help:

https://www.npmjs.com/package/allow-connections-to-ecs-service-from-network-load-balancer-cdk

winjer avatar Jun 05 '20 08:06 winjer

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

github-actions[bot] avatar Jun 16 '20 06:06 github-actions[bot]

Is this not relevant anymore?

sebastian-schlecht avatar Jun 21 '20 15:06 sebastian-schlecht

@sebastian-schlecht I believe it is. Reopening.

MrArnoldPalmer avatar Jun 24 '20 00:06 MrArnoldPalmer

This issue is still ongoing.

To reproduce this issue I have created a simple Dockerfile for the Fargate service to host: FROM nginx:alpine COPY index.html /usr/share/nginx/html/index.html

I am then creating the ecs_patterns.NetworkLoadBalancedFargateService:

    new ecs_patterns.NetworkLoadBalancedFargateService( this, "Service-Name-Stuff", {
        assignPublicIp: props.isPublic,
        cluster: this.cluster,
        cpu: props.cpuUnits != undefined? props.cpuUnits : 256, // set it to the default 256 if not defined
        desiredCount: props.instanceCount != undefined? props.instanceCount : 1, // only create one instance if not defined
        domainName: props.domainName,
        domainZone: props.hostedZone,
        listenerPort: props.listenerPort,
        memoryLimitMiB: props.memoryLimitMB != undefined? props.memoryLimitMB : 512, // default to 512
        publicLoadBalancer: props.isPublic,
        serviceName: props.name,
        taskImageOptions: { 
            image:containerImage,
            enableLogging: true,
            containerPort: props.containerPort // specify the container port that traffic should be directed to
        },
    });

The Service that is created has a Load Balancer associated with it that I can see in the console. I can see that is is auto-assigning a public IP address. I can see my A record in the Route53 records and I can follow that to the Listener on TCP:80 routing to the FargateService which in the targets has an IP address with port 80. If I look at the SecurityGroup, however, there are no inbound rules so nothing can tell that it is actually running so I'm constantly draining the old instances and creating new ones when the initial healthcheck timeout expires.

If I move this same service over to using an ALB I get two SecurityGroups created. One that listens on 443 and another that listens on port 80. These securitygroups open to 0.0.0.0/0 which is a substantial difference from what happens on the NLB side of the fence.

gregorypierce avatar Jul 31 '20 01:07 gregorypierce

The fix for this is similar to that contained within this push for the examples: https://github.com/aws-samples/aws-cdk-examples/pull/155

Essentially one can resolve this issue by performing an allow on the containerPort:

        nlbFgSvc.service.connections.allowFromAnyIpv4( ec2.Port.tcp(props.containerPort) );

In this scenario, I'm passing it into my construct and specifying that the service should allow anything to the containerPort since that is what the health check is trying to verify it is able to reach. This will allow the healthcheck to work and for the loadbalancer to not mark the instance as Unhealthy. I am calling this line directly the ecs_patterns.NetworkLoadBalancedFargateService(...) line.

gregorypierce avatar Jul 31 '20 04:07 gregorypierce