# Set up load balancing

To have an application accessible from the internet you need a load balancer that can route traffic to the application.

The load balancer set up for your environment is `{Environment}-public`.

## Step 1: Add and configure the `load-balancing` package

```bash title="repo-iac/environments/dev/"
ok pkg add load-balancing-alb load-balancing-alb-main
```

The name `main` is used here because there is only one load balancer. If you have many load balancers, you can replace
`main` with something more descriptive (for instance "payments", "catalog", "orders").

Update `package-config.yml` with your preferences. The `Name` field should be `main` (or whatever you chose above) to
avoid confusion:

```yaml title="repo-iac/environments/dev/load-balancing-alb-main/package-config.yml"
# Setup load balancing
Name: "main"
# ...
```

## Step 2: Install the package

```bash title="repo-iac/environments/dev/load-balancing-alb-main/"
ok pkg install
```

## Step 3: Apply in this order (required)

**Order matters:** Apply **data first**, then **alb**.
The **alb stack depends on resources** created by the data stack.

### 1) Apply the data stack (required first)

Creates S3 buckets for logs and other data resources.

```bash title="repo-iac/environments/dev/load-balancing-alb-main-data/"
terraform init
terraform apply
```

### 2) Apply the alb stack (run second)

Creates the Application Load Balancer, Route53 records, and other resources.

```bash title="repo-iac/environments/dev/load-balancing-alb-main/"
terraform init
terraform apply
```

## Step 4: Verify

=== "AWS CLI"

    Run the following command:

    ```bash
    aws elbv2 describe-load-balancers | jq '.LoadBalancers[].LoadBalancerName'
    ```

    The output list should contain the name of the load balancer you just created.

=== "AWS console"

    Sign in to the AWS console and navigate to **EC2**. Select **Load Balancers** in the left-hand menu.

    The list should contain the name of the load balancer you just created.

---

You're done with the Networking setup.

## Next step

Set up [databases](set-up-databases.md).
