Skip to content

Commit 23af917

Browse files
abhirajadhikary06quetzalliwritesremotesynth
authored
Updated documentation for elb-load-balancing (#267)
Co-authored-by: Quetzalli <[email protected]> Co-authored-by: Brian Rinaldi <[email protected]>
1 parent baaa0e5 commit 23af917

File tree

2 files changed

+73
-34
lines changed

2 files changed

+73
-34
lines changed
442 KB
Loading

src/content/docs/aws/tutorials/elb-load-balancing.mdx

Lines changed: 73 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@ pro: true
1212
leadimage: "elb-load-balancing-featured-image.png"
1313
---
1414

15-
[Elastic Load Balancer (ELB)](https://aws.amazon.com/elasticloadbalancing/) is a service that distributes incoming application traffic across multiple targets, such as EC2 instances, containers, IP addresses, and Lambda functions.
16-
ELBs can be physical hardware or virtual software components.
17-
They accept incoming traffic and distribute it across multiple targets in one or more Availability Zones.
15+
## Introduction
16+
17+
[Elastic Load Balancer (ELB)](https://aws.amazon.com/elasticloadbalancing/)is a service that distributes incoming application traffic across multiple targets, such as EC2 instances, containers, IP addresses, and Lambda functions.
18+
ELBs can be physical hardware or virtual software components. They accept incoming traffic and distribute it across multiple targets in one or more Availability Zones.
1819
Using ELB, you can quickly scale your load balancer to accommodate changes in traffic over time, ensuring optimal performance for your application and workloads running on the AWS infrastructure.
1920

20-
ELB provides three types of load balancers: [Application Load Balancer](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/introduction.html), [Network Load Balancer](https://docs.aws.amazon.com/elasticloadbalancing/latest/network/introduction.html), [Classic Load Balancer](https://docs.aws.amazon.com/elasticloadbalancing/latest/gateway/introduction.html), and [Application Load Balancer](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/introduction.html).
21+
ELB provides four types of load balancers:
22+
- **[Application Load Balancer](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/introduction.html)**: Manages HTTP/HTTPS traffic, offering advanced routing features at the application layer.
23+
- **[Network Load Balancer](https://docs.aws.amazon.com/elasticloadbalancing/latest/network/introduction.html)**: Handles TCP traffic with high performance and low latency at the transport layer.
24+
- **[Gateway Load Balancer](https://docs.aws.amazon.com/elasticloadbalancing/latest/gateway/introduction.html)**: Deploys, scales, and manages third-party virtual appliances with a transparent network gateway.
25+
- **[Classic Load Balancer](https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/introduction.html)**: Provides basic load balancing for both HTTP/HTTPS and TCP traffic.
2126

22-
In this tutorial we focus on the Application Load Balancer (ALB), which operates at the Application layer of the OSI model and is specifically designed for load balancing HTTP and HTTPS traffic for web applications.
23-
ALB works at the request level, allowing advanced load-balancing features for HTTP and HTTPS requests.
24-
It also enables you to register Lambda functions as targets.
27+
In this tutorial, we focus on the Application Load Balancer (ALB), which operates at Layer 7 (Application layer) of the OSI model and is specifically designed for load balancing HTTP and HTTPS traffic for web applications.
28+
ALB works at the request level, allowing advanced load-balancing features for HTTP and HTTPS requests. It also enables you to register Lambda functions as targets.
2529
You can configure a listener rule that forwards requests to a target group for your Lambda function, triggering its execution to process the request.
2630

2731
[LocalStack Pro](https://localstack.cloud) extends support for ELB Application Load Balancers and the configuration of target groups, including Lambda functions.
@@ -37,17 +41,34 @@ Additionally, we will demonstrate how to set up ELB endpoints to efficiently for
3741
- [awslocal](https://github.com/localstack/awscli-local)
3842
- [curl](https://curl.se/) and [jq](https://jqlang.github.io/jq/)
3943

44+
## Architecture
45+
46+
The architecture emulates a scalable AWS setup locally: Clients send HTTP/HTTPS requests to the ALB's DNS endpoint.
47+
The ALB listener (e.g., on port 80) routes traffic based on path rules to target groups, which forward to registered Lambda functions.
48+
These functions process requests and return responses. The setup runs within a VPC and subnet for networking isolation, all emulated in LocalStack.
49+
50+
### Key components
51+
- Clients/Users: Initiate traffic via browsers or tools like curl.
52+
- ALB Listener: Receives and routes based on rules (e.g., /hello1 → hello1 Lambda).
53+
- Target Group: Manages health checks and forwards to Lambda targets.
54+
- Lambda Functions: Handle business logic (e.g., return "Hello 1").
55+
- VPC/Subnet: Provides network boundaries.
56+
57+
![Architecture diagram for ELB Application Load Balancer with Lambda targets](/src/assets/images/aws/tutorials/elb-load-balancing-architecture-image.png)
58+
4059
## Setup a Serverless project
4160

4261
Serverless is an open-source framework that enables you to build, package, and deploy serverless applications seamlessly across various cloud providers and platforms.
4362
With the Serverless framework, you can easily set up your serverless development environment, define your applications as functions and events, and deploy your entire infrastructure to the cloud using a single command.
63+
4464
To start using the Serverless framework, install the Serverless framework globally by executing the following command using `npm`:
4565

4666
```bash
4767
npm install -g serverless
4868
```
4969

5070
The above command installs the Serverless framework globally on your machine.
71+
5172
After the installation is complete, you can verify it by running the following command:
5273

5374
```bash
@@ -59,6 +80,7 @@ SDK: 4.3.2
5980
```
6081

6182
This command displays the version numbers of the Serverless framework's core, plugins, and SDK you installed.
83+
6284
Now, let's proceed with creating a new Serverless project using the `serverless` command:
6385

6486
```bash
@@ -91,6 +113,7 @@ The `serverless-localstack` plugin enables your Serverless project to redirect A
91113
This bucket is responsible for storing the deployment artifacts and ensuring that old deployment buckets are properly cleaned up after each deployment.
92114

93115
We have a `serverless.yml` file in the directory to define our Serverless project's configuration, which includes information such as the service name, the provider (AWS in this case), the functions, and example events that trigger those functions.
116+
94117
To set up the plugins we installed earlier, you need to add the following properties to your `serverless.yml` file:
95118

96119
```yaml showLineNumbers
@@ -119,8 +142,8 @@ custom:
119142
120143
To configure Serverless to use the LocalStack plugin specifically for the `local` stage and ensure that your Serverless project only deploys to LocalStack instead of the real AWS Cloud, you need to set the `--stage` flag when using the `serverless deploy` command and specify the flag variable as `local`.
121144

122-
Configure a `deploy` script in your `package.json` file to simplify the deployment process.
123-
It lets you run the `serverless deploy` command directly over your local infrastructure.
145+
Configure a `deploy` script in your `package.json` file to simplify the deployment process. It lets you run the `serverless deploy` command directly over your local infrastructure.
146+
124147
Update your `package.json` file to include the following:
125148

126149
```json showLineNumbers
@@ -186,11 +209,9 @@ module.exports.hello2 = async (event) => {
186209
};
187210
```
188211

189-
We have defined the `hello1` and `hello2` Lambda functions in the updated code.
190-
Each function receives an event parameter and logs it to the console.
191-
The function then returns a response with a status code of 200 and a plain text body containing the respective `"Hello"` message.
192-
It's important to note that the `isBase64Encoded` property is not required for plain text responses.
193-
It is typically used when you need to include binary content in the response body and want to indicate that the content is Base64 encoded.
212+
We have defined the `hello1` and `hello2` Lambda functions in the updated code. Each function receives an event parameter and logs it to the console. The function then returns a response with a status code of 200 and a plain text body containing the respective `"Hello"` message.
213+
214+
It's important to note that the `isBase64Encoded` property is not required for plain text responses. It is typically used when you need to include binary content in the response body and want to indicate that the content is Base64 encoded.
194215

195216
Let us now configure the `serverless.yml` file to create an Application Load Balancer (ALB) and attach the Lambda functions to it.
196217

@@ -231,12 +252,12 @@ custom:
231252
- local
232253
```
233254

234-
In the above configuration, we specify the service name (`serverless-elb` in this case) and set the provider to AWS with the Node.js 12.x runtime.
235-
We include the necessary plugins, `serverless-localstack` and `serverless-deployment-bucket`, for LocalStack support and deployment bucket management.
236-
Next, we define the `hello1` and `hello2` functions with their respective handlers and event triggers.
237-
In this example, both functions are triggered by HTTP GET requests to the `/hello1` and `/hello2` paths.
255+
In the above configuration, we specify the service name (`serverless-elb` in this case) and set the provider to AWS with the Node.js 12.x runtime. We include the necessary plugins, `serverless-localstack` and `serverless-deployment-bucket`, for LocalStack support and deployment bucket management.
256+
257+
Next, we define the `hello1` and `hello2` functions with their respective handlers and event triggers. In this example, both functions are triggered by HTTP GET requests to the `/hello1` and `/hello2` paths.
238258

239259
Lastly, let's create a VPC, a subnet, an Application Load Balancer, and an HTTP listener on the load balancer that redirects traffic to the target group.
260+
240261
To do this, add the following resources to your `serverless.yml` file:
241262

242263
```yaml showLineNumbers
@@ -276,27 +297,28 @@ resources:
276297
CidrBlock: 12.2.1.0/24
277298
```
278299

279-
With these resource definitions, you have completed the configuration of your Serverless project.
280-
Now you can create your local AWS infrastructure on LocalStack and deploy your Application Load Balancers with the two Lambda functions as targets.
300+
You have completed the configuration of your Serverless project! Now you can create your local AWS infrastructure on LocalStack and deploy your Application Load Balancers with the two Lambda functions as targets.
281301

282302
## Creating the infrastructure on LocalStack
283303

284-
Now that we have completed the initial setup let's run LocalStack's AWS emulation on our local machine.
304+
Now that we have completed the initial setup, let's run LocalStack's AWS emulation on our local machine.
305+
285306
Start LocalStack by running the following command:
286307

287308
```bash
288309
LOCALSTACK_AUTH_TOKEN=<your-auth-token> localstack start -d
289310
```
290311

291-
This command launches LocalStack in the background, enabling you to use the AWS services locally.
292-
Now, let's deploy our Serverless project and verify the resources created in LocalStack.
312+
This command launches LocalStack in the background, enabling you to use the AWS services locally. Now, let's deploy our Serverless project and verify the resources created in LocalStack.
313+
293314
Run the following command:
294315

295316
```bash
296317
npm run deploy
297318
```
298319

299320
This command deploys your Serverless project using the "local" stage.
321+
300322
The output will resemble the following:
301323

302324
```bash
@@ -317,8 +339,8 @@ functions:
317339
hello2: test-elb-load-balancing-local-hello2 (157 kB)
318340
```
319341

320-
This output confirms the successful deployment of your Serverless service to the `local` stage in LocalStack.
321-
It also displays information about the deployed Lambda functions (`hello1` and `hello2`).
342+
This output confirms the successful deployment of your Serverless service to the `local` stage in LocalStack. It also displays information about the deployed Lambda functions (`hello1` and `hello2`).
343+
322344
You can run the following command to verify that the functions and the load balancers have been deployed:
323345

324346
```bash showLineNumbers
@@ -365,25 +387,42 @@ The ALB endpoints for the two Lambda functions, hello1 and hello2, are accessibl
365387
- [`http://lb-test-1.elb.localhost.localstack.cloud:4566/hello1`](http://lb-test-1.elb.localhost.localstack.cloud:4566/hello1)
366388
- [`http://lb-test-1.elb.localhost.localstack.cloud:4566/hello2`](http://lb-test-1.elb.localhost.localstack.cloud:4566/hello2)
367389

368-
To test these endpoints, you can use the curl command along with the jq tool for better formatting.
369-
Run the following commands:
390+
## Testing
391+
392+
Here in the testing phase we will test endpoints, do a validation check which includes health check and error handling. To test these endpoints, you can use the curl command along with the jq tool for better formatting.
393+
394+
1. **Verify Deployment:** Use the commands `awslocal lambda list-functions` and `awslocal elbv2 describe-load-balancers` respectively to confirm the existince of Lambda and ALB respectively
395+
2. **Test Endpoints:** Run the following commands:
370396

371397
```bash
372398
curl http://lb-test-1.elb.localhost.localstack.cloud:4566/hello1 | jq
373399
"Hello 1"
374400
curl http://lb-test-1.elb.localhost.localstack.cloud:4566/hello2 | jq
375401
"Hello 2"
376402
```
403+
Both commands send an HTTP GET request to the endpoints and uses `jq` to format the response. The expected outputs are `Hello 1` & `Hello 2`, representing the Lambda functions' response.
404+
405+
3. **Health Checks:** Describe target health:
406+
```bash
407+
awslocal elbv2 describe-target-health --target-group-arn $(awslocal elbv2 describe-target-groups --load-balancer-arn $(awslocal elbv2 describe-load-balancers --names lb-test-1 --query 'LoadBalancers[0].LoadBalancerArn' --output text) --query 'TargetGroups[0].TargetGroupArn' --output text)
408+
```
377409

378-
Both commands send an HTTP GET request to the endpoints and uses `jq` to format the response.
379-
The expected outputs are `Hello 1` & `Hello 2`, representing the Lambda functions' response.
410+
4. **Invalid Path:** Test fallback/redirect:
411+
```bash
412+
curl -I http://lb-test-1.elb.localhost.localstack.cloud:4566/invalid
413+
```
414+
415+
5. **Logs Validation:** Check Lambda logs for invocations:
416+
```bash
417+
awslocal logs describe-log-groups --query 'logGroups[].logGroupName' | jq -r '.[] | select(contains("hello1"))' | xargs -I {} awslocal logs tail {} --follow
418+
```
419+
420+
If tests fail, you can ensure LocalStack is healthy (`localstack status services`), check ports (default `4566`), and restart if needed.
380421

381422
## Conclusion
382423

383424
In this tutorial, we have learned how to create an Application Load Balancer (ALB) with two Lambda functions as targets using LocalStack.
384-
We have also explored creating, configuring, and deploying a Serverless project with LocalStack.
385-
This enables developers to develop and test Cloud and Serverless applications locally conveniently.
386425

387-
LocalStack offers integrations with various popular tools such as Terraform, Pulumi, Serverless Application Model (SAM), and more.
388-
For more information about LocalStack integrations, you can refer to our [Integration documentation]().
389-
To further explore and experiment with the concepts covered in this tutorial, you can access the code and resources on our [LocalStack Pro samples over GitHub](https://github.com/localstack/localstack-pro-samples/tree/master/elb-load-balancing) along with a `Makefile` for step-by-step execution.
426+
We have also explored creating, configuring, and deploying a Serverless project with LocalStack, enabling developers to develop and test Cloud and Serverless applications locally without AWS costs—accelerating iteration for cloud-native workloads.
427+
428+
LocalStack offers integrations with various popular tools such as Terraform, Pulumi, Serverless Application Model (SAM), and more. For more information about LocalStack integrations, you can refer to our [Integration documentation](https://docs.localstack.cloud/aws/integrations). To further explore and experiment with the concepts covered in this tutorial, you can access the code and resources on our [LocalStack Pro samples over GitHub](https://github.com/localstack/localstack-pro-samples/tree/master/elb-load-balancing) along with a Makefile for step-by-step execution.

0 commit comments

Comments
 (0)