Code examples for Elastic Load Balancing - Version 2 using AWS SDKs - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Code examples for Elastic Load Balancing - Version 2 using AWS SDKs

The following code examples show you how to use Elastic Load Balancing - Version 2 with an AWS software development kit (SDK).

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.

Scenarios are code examples that show you how to accomplish specific tasks by calling multiple functions within a service or combined with other AWS services.

More resources

Get started

The following code examples show how to get started using Elastic Load Balancing.

Java
SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

public class HelloLoadBalancer { public static void main(String[] args) { ElasticLoadBalancingV2Client loadBalancingV2Client = ElasticLoadBalancingV2Client.builder() .region(Region.US_EAST_1) .build(); DescribeLoadBalancersResponse loadBalancersResponse = loadBalancingV2Client .describeLoadBalancers(r -> r.pageSize(10)); List<LoadBalancer> loadBalancerList = loadBalancersResponse.loadBalancers(); for (LoadBalancer lb : loadBalancerList) System.out.println("Load Balancer DNS name = " + lb.dnsName()); } }
JavaScript
SDK for JavaScript (v3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

import { ElasticLoadBalancingV2Client, DescribeLoadBalancersCommand, } from "@aws-sdk/client-elastic-load-balancing-v2"; export async function main() { const client = new ElasticLoadBalancingV2Client({}); const { LoadBalancers } = await client.send( new DescribeLoadBalancersCommand({}), ); const loadBalancersList = LoadBalancers.map( (lb) => `• ${lb.LoadBalancerName}: ${lb.DNSName}`, ).join("\n"); console.log( "Hello, Elastic Load Balancing! Let's list some of your load balancers:\n", loadBalancersList, ); } // Call function if run directly import { fileURLToPath } from "node:url"; if (process.argv[1] === fileURLToPath(import.meta.url)) { main(); }
Python
SDK for Python (Boto3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

import boto3 def hello_elbv2(elbv2_client): """ Use the AWS SDK for Python (Boto3) to create an Elastic Load Balancing V2 client and list up to ten of the load balancers for your account. This example uses the default settings specified in your shared credentials and config files. :param elbv2_client: A Boto3 Elastic Load Balancing V2 client object. """ print("Hello, Elastic Load Balancing! Let's list some of your load balancers:") load_balancers = elbv2_client.describe_load_balancers(PageSize=10).get( "LoadBalancers", [] ) if load_balancers: for lb in load_balancers: print(f"\t{lb['LoadBalancerName']}: {lb['DNSName']}") else: print("Your account doesn't have any load balancers.") if __name__ == "__main__": hello_elbv2(boto3.client("elbv2"))