> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ahmadraza.in/llms.txt
> Use this file to discover all available pages before exploring further.

# CustomErrorPage

# Setting a Custom Error Page in AWS ALB

You can set a custom error page for AWS Application Load Balancer (ALB) using **custom HTTP responses**, **redirects**, or **integrations with external services**. Below are different methods to achieve this:

***

## **1. Use an S3 Bucket as the Error Page Host**

### Steps:

1. **Create an S3 Bucket**:
   * Create a new S3 bucket and enable static website hosting.
   * Upload your custom error page (e.g., `error.html`) to the bucket.

2. **Set Permissions**:
   * Make the S3 object (your error page) publicly accessible.

3. **Configure ALB Target Group**:
   * Create a target group with a dummy backend or no backend.
   * Associate the target group with the ALB listener's default rule or a specific condition.

4. **Add a Custom HTTP Response Rule**:
   * Go to the ALB listener settings and add a rule that returns an HTTP redirect to the S3 error page or directly integrates the custom error page.

***

## **2. Use a Lambda Function with ALB**

### Steps:

1. **Create a Lambda Function**:
   * Write a Lambda function that returns your custom error page.

2. **Configure ALB Target Group**:
   * Use the Lambda function as a target group in the ALB configuration.

3. **Define Routing Rules**:
   * Set up routing rules in ALB to invoke the Lambda function for specific HTTP status codes or requests.

***

## **3. Use Custom HTTP Responses**

AWS ALB supports returning a custom HTTP response for specific conditions.

### Steps:

1. **Access ALB Listener Rules**:
   * Go to the **ALB Listener Rules** section in the AWS Management Console.

2. **Add a Rule**:
   * Define conditions for specific HTTP status codes or requests.

3. **Set HTTP Fixed Response**:
   * Specify the following:
     * **HTTP Status Code**: e.g., `404`, `500`.
     * **Response Body**: Include HTML for your custom error page.
     * **Content Type**: e.g., `text/html`.

### Example Response Body:

```html theme={null}
<html>
<body>
    <h1>Page Not Found</h1>
    <p>Sorry, the page you are looking for does not exist.</p>
</body>
</html>
```

***

## **4. Redirect to an External Service**

If your application has another service hosting custom error pages, you can set up an HTTP redirect to that service.

### Steps:

1. **Access ALB Listener Rules**:
   * Go to the ALB Listener Rules section.

2. **Add a Rule**:
   * Define conditions for error scenarios (e.g., path-based rules or specific status codes).

3. **Set HTTP Redirect**:
   * Redirect the request to the external service hosting your error page.

***

## **Considerations**:

* **Caching**: Ensure caching does not interfere with dynamically served error pages.
* **Error Page Hosting**: Ensure the error page is accessible globally or within the relevant VPC if privately hosted.
* **Cost**: Hosting on S3 is cost-effective; Lambda or external services might incur higher costs.
