# Set up DNS

All applications in an environment are deployed to a subdomain of `oslo.systems`.

The default domain for your environment is `{Environment}.oslo.systems`. Each new application uses a subdomain of
this domain.

## Step 1: Add and configure the `dns` package

```bash title="repo-iac/environments/dev/"
ok pkg add dns
```

## Step 2: Install and apply the package

```bash title="repo-iac/environments/dev/dns/"
ok pkg install
terraform init
terraform apply
```

When `terraform apply` completes, name servers are printed to the console.

```hcl title="Output from terraform apply"
name_servers = tolist([
  "ns-123.awsdns-44.org",
  "ns-321.awsdns-29.co.uk",
  "ns-213.awsdns-35.com",
  "ns-231.awsdns-18.net",
])
```

## Step 3: Verify

=== "AWS CLI"

    ```bash
    aws route53 list-hosted-zones | jq '.HostedZones[].Name'
    ```

    Output should contain the domain you created.

=== "AWS console"

    Sign in to the **AWS console** and navigate to **Route 53**. Select **Hosted zones**.

    List should contain the domain you created.

---

## Step 4: Add DNS Name Servers

The output from Step 2 are the name servers for the root domain specified in `_gp_dns.tf`. Because `oslo.systems` was specified as the root domain, follow the guide [Adding a subdomain to `oslo.systems`](https://github.com/oslokommune/origo-aws-infrastructure/tree/master/infrastructure/production/dns/oslo.systems/auto_applied_subdomains#readme){:target="_blank"} to complete the setup.

To override default configuration, set the `root_domain` variable in `dns/config_override.tf`.

The domain `pirates-dev.oslo.systems` should now resolve to applications you deploy.

## Step 5: Verify

To verify the subdomain is properly registered with a name server (after completing step 4):

```bash
dig NS +short {Environment}.oslo.systems
```

This should return name servers corresponding to the list printed after `terraform apply` in step 2.

If you don't have `dig` installed, use [this DNS lookup tool](https://dns-lookup.jvns.ca/#|NS){:target="_blank"} to check `{your-subdomain}.oslo.systems`.

## Step 6: Add DNS for `oslo.kommune.no` (optional)

All solutions visible to residents of Oslo and Oslo municipality employees should always use `oslo.kommune.no`. If you require `oslo.kommune.no` as subdomain, you need to follow these steps.

### Step 6a: Create a Route53 zone for subdomain

```bash title="repo-iac/environments/prod/dns/config.tf"
locals {
    my_product_root_domain = "my-product-root-domain.oslo.kommune.no"
}
```

```bash title="repo-iac/environments/prod/dns/dns-oslo-kommune-no.tf"
resource "aws_route53_zone" "my_product_root_domain" {
  name = local.my_product_root_domain
}

output "name_servers" {
  value       = aws_route53_zone.my_product_root_domain.name_servers
  description = "A list of name servers in associated delegation set. The root domain zone needs these name servers as NS records for your domain."
}
```

### Step 6b: Request DNS changes on `oslo.kommune.no`

Use the output from 6a to request a DNS change:

- Go to the `#utviklerflyt-support` channel on Slack
- Paste the value from the output in step 6a together with the URL for your IaC repository
- Team Utviklerflyt is going to request the DNS change on your behalf

### Step 6c: Verify

After team Utviklerflyt completes the DNS change, you are contacted on Slack. Then, verify the new subdomain with:

```bash
dig NS +short my-product-root-domain.oslo.kommune.no
```

If an update or change results in an updated `name_servers` output, submit a new DNS change request to team Utviklerflyt.

## Next step

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