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-elasticloadbalancingv2] Add support for SubnetMapping to Network Load Balancer #9696

Open
1 of 2 tasks
michaelwiles opened this issue Aug 14, 2020 · 7 comments
Open
1 of 2 tasks
Labels
@aws-cdk/aws-elasticloadbalancingv2 Related to Amazon Elastic Load Balancing V2 effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2

Comments

@michaelwiles
Copy link
Contributor

Add Subnet mappings to the Network Load Balancer construct

Use Case

We have a network load balancer and we need it on a static ip thus we hook up an elastic ip to this network load balancer.

This is possible via the console and it also seems possible via the SubnetMapping property on network load balancer in cloudformation.

Proposed Solution

To add that subnet mapping construct as a property on the NetworkLoadBalancer construct.

Other

I suspect that in the mean time I can use the CfnLoadBalancer either as the primary construct or fetch it and change it after creation of the cdk network load balancer.

Not sure if I should try this or focus on adding the mapping to the network load balancer...

There is already an available SubnetMappingProperty which I'd assume we'd replicate in the primary world (it's in the Cfn world and thus potentially not stable).

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change

This is a 🚀 Feature Request

@michaelwiles michaelwiles added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Aug 14, 2020
@github-actions github-actions bot added the @aws-cdk/aws-elasticloadbalancingv2 Related to Amazon Elastic Load Balancing V2 label Aug 14, 2020
@michaelwiles
Copy link
Contributor Author

hmmm not sure if it's even worth it...

This does the job:


load_balancer = elbv2.NetworkLoadBalancer(
    self, 'LoadBalancer', vpc=vpc, vpc_subnets=SubnetSelection(subnets=[vpc.public_subnets[0]])
)
cfn_load_balancder: CfnLoadBalancer = load_balancer.node.default_child

subnet_mapping = CfnLoadBalancer.SubnetMappingProperty(
    subnet_id=vpc.public_subnets[0].subnet_id, allocation_id='eipalloc-XXXXYYYYxxxxyyyy'
)
cfn_load_balancder.subnet_mappings = [subnet_mapping]

@rix0rrr rix0rrr added effort/medium Medium work item – several days of effort p1 labels Aug 17, 2020
@SomayaB SomayaB assigned njlynch and unassigned rix0rrr Aug 20, 2020
@flemjame-at-amazon
Copy link
Contributor

Also interested in this for the ability to specify the private IPv4 of a NetworkLoadBalancer - see PrivateIPv4Address

@njlynch njlynch removed the needs-triage This issue or PR still needs to be triaged. label Sep 14, 2020
@cfclrk
Copy link

cfclrk commented Dec 21, 2020

@michaelwiles Thanks for the example, it helped me! It doesn't quite work as written though:

You can specify either subnets or subnet mappings, not both

This error is an example of why it's nice to have higher-level constructs for these kinds of things.

@cfclrk
Copy link

cfclrk commented Dec 21, 2020

See also: #7424

@mr-brobot
Copy link

@cfclrk I encountered the same error and this worked for me.

elastic_ip = CfnEIP(self, "EIP")

network_load_balancer = NetworkLoadBalancer(
    self,
    "NetworkLoadBalancer",
    vpc=cluster.vpc,
    internet_facing=True,
)

cfn_nlb = network_load_balancer.node.default_child

subnet_mapping = CfnLoadBalancer.SubnetMappingProperty(
    subnet_id=cluster.vpc.public_subnets[0].subnet_id,
    allocation_id=elastic_ip.attr_allocation_id,
)

cfn_nlb.subnet_mappings = [subnet_mapping]

# i think the higher-level NetworkLoadBalancer construct sets subnets on the lower-level CfnLoadBalancer construct
# clearing subnets on the lower-level construct allowed me to add the subnet mapping without issue
cfn_nlb.subnets = None

Credit to @michaelwiles for the solution!

@forsberg
Copy link

Many thanks to @josh-wiley for the working example. Here extended to also set a fixed IPv6 address on a dualstack NLB:

eip = CfnEIP(self, "ElasticIP")

self.nlb = NetworkLoadBalancer(
    self,
    "LoadBalancer",
    vpc=self.vpc,
    internet_facing=True
)
cfn_lb: CfnLoadBalancer = self.nlb.node.default_child
cfn_lb.ip_address_type = "dualstack"

cfn_subnet: CfnSubnet = self.vpc.public_subnets[0].node.default_child
ipv6_network = Fn.select(0, cfn_subnet.attr_ipv6_cidr_blocks)

ipv6_prefix = Fn.select(0, Fn.split("::", ipv6_network))
# The answer to what the last part of the IPv6 address should be is obviously 42
ipv6_addr = Fn.join("::", [ipv6_prefix, "42"])

subnet_mapping = CfnLoadBalancer.SubnetMappingProperty(
    subnet_id=self.vpc.public_subnets[0].subnet_id,
    allocation_id=eip.attr_allocation_id,
    i_pv6_address=ipv6_addr
)

cfn_lb.subnet_mappings = [subnet_mapping]
cfn_lb.subnets = None

@bkeifer
Copy link

bkeifer commented May 11, 2022

I'm trying to set a pair of Elastic IPs as the public facing addresses for a NetworkLoadBalancer object and running into issues. The console.log("CFN NLB"); line never executes because the load balancer definition throws the following error:

There are no 'Public' subnet groups in this VPC. Available types:

Subprocess exited with error 1

Code:

    const subnet1 = Subnet.fromSubnetId(this, 'subnet1', 'subnet-xxxxx4c499ef987fe');
    const subnet2 = Subnet.fromSubnetId(this, 'subnet2', 'subnet-xxxxx2d40a27dd263');

    console.log("Load Balancer...");
    this.loadBalancer = new NetworkLoadBalancer(this, 'dnsLB', {
      vpc: assets.vpc,
      internetFacing: true,
    });

    console.log("CFN NLB");
    this.cfnNLB = this.loadBalancer.node.defaultChild as CfnLoadBalancer;

    console.log("Mappings");
    const subnetMapping1: CfnLoadBalancer.SubnetMappingProperty = {
      subnetId: 'subnet-xxxxx4c499ef987fe',
      allocationId: assets.elasticIp1.attrAllocationId,
    }
    const subnetMapping2: CfnLoadBalancer.SubnetMappingProperty = {
      subnetId: 'subnet-xxxxx2d40a27dd263',
      allocationId: assets.elasticIp2.attrAllocationId,
    }

    console.log("Mapping assignment");
    this.cfnNLB.subnetMappings = [subnetMapping1, subnetMapping2];

I've found references to CDK wanting a tag of aws-cdk:subnet-type with a value of Public and added that tag to our public subnets, but the error remains unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-elasticloadbalancingv2 Related to Amazon Elastic Load Balancing V2 effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2
Projects
None yet
Development

No branches or pull requests

9 participants