# Existing infrastructure and exposure

> Reuse your own VPC, database, cluster, certificate or DNS, and choose between CloudFront and private-only access.

Source: https://diakrin.com/docs/self-hosted/existing-infrastructure

You can reuse your own VPC, database, ECS cluster, certificate, registry or DNS, and choose whether Diakrin is public through CloudFront or private only.

## Before you start

| You supply | Example | Comes from |
| --- | --- | --- |
| The IDs of what you reuse | `vpc-0abc…`, a certificate ARN | Your AWS account |
| A settings file | `diakrin.env` | See [Install](/docs/self-hosted/install#3-write-the-settings) |

Anything you do not set is created. Diakrin never deletes a resource you supplied. `infra up` checks everything you reuse before it changes anything, and each failure prints its fix.

## Reuse what you have

Add the settings you need to `diakrin.env`, one `KEY=VALUE` per line, then run `infra up` as usual.

| Setting | Reuses |
| --- | --- |
| `DIAKRIN_VPC_ID` | A VPC. Needs `DIAKRIN_PRIVATE_SUBNET_IDS` in two or more Availability Zones |
| `DIAKRIN_PRIVATE_SUBNET_IDS` | The private subnets of that VPC |
| `DIAKRIN_PUBLIC_SUBNET_IDS` | Public subnets, only for a public load balancer |
| `DIAKRIN_SECURITY_GROUP_IDS` | Security groups for the workloads |
| `DIAKRIN_ECS_CLUSTER_ARN` | An ECS cluster |
| `DIAKRIN_ALB_LISTENER_ARN` | An HTTPS listener on your load balancer. Needs `DIAKRIN_VPC_ID` |
| `DIAKRIN_CERTIFICATE_ARN` | A regional ACM certificate for the load balancer |
| `DIAKRIN_CLOUDFRONT_CERTIFICATE_ARN` | An ACM certificate for CloudFront. It must be in `us-east-1` |
| `DIAKRIN_KMS_KEY_ARN` | A KMS key |
| `DIAKRIN_ECR_REGISTRY` | An ECR registry host |
| `DIAKRIN_AUDIT_BUCKET` | An S3 audit bucket with Object Lock |
| `DIAKRIN_DATABASE_SECRET_ARN` | A Secrets Manager secret holding an existing database (JSON `host`, `port`, `username`, `password`, `dbname`) |
| `DIAKRIN_DATABASE_URL` | An existing PostgreSQL database. A secret: see below |
| `DIAKRIN_WEBHOOK_URL` | An existing GitHub webhook endpoint instead of the one Diakrin creates |
| `DIAKRIN_DNS` | `external` (any provider, the default) or `route53` |
| `DIAKRIN_ROUTE53_ZONE_ID` | The Route 53 zone for validation and host records |

`DIAKRIN_DATABASE_URL` and `DIAKRIN_DATABASE_SECRET_ARN` are exclusive. Never put the URL in `diakrin.env`; the CLI refuses it. Pass it in the process environment for one run, and the CLI moves it to Secrets Manager:

```sh
DIAKRIN_DATABASE_URL='postgresql://user:password@host:5432/db' diakrin self-hosted infra up --env diakrin.env --plan-only
```

You should see the plan list what is reused and what is created. If you already have everything, you can skip `infra up` and run `deploy` straight away; it validates the same settings.

## Exposure

`DIAKRIN_EXPOSE` decides how people and GitHub reach Diakrin. The load balancer is always internal.

| Value | What you get | Use it when |
| --- | --- | --- |
| `cloudfront` (default) | CloudFront with a VPC origin in front of the internal ALB, AWS WAF, and `https://<host>` on the internet | You want to open the console from anywhere. Needs an internet gateway on the VPC |
| `none` | No CloudFront. People use VPN, peering or PrivateLink. GitHub reaches one API Gateway webhook endpoint | Diakrin must stay private |

With `cloudfront`, AWS WAF lets only GitHub's source ranges reach the webhook path. Use `DIAKRIN_ALLOWED_CIDRS` to limit who can reach everything else.

For `none`, choose how your network reaches Diakrin:

| Setting | Value | Meaning |
| --- | --- | --- |
| `DIAKRIN_PRIVATE_ACCESS` | `peering` (default) | VPC peering, optionally to `DIAKRIN_PEER_VPC_ID` |
| `DIAKRIN_PRIVATE_ACCESS` | `privatelink` | An endpoint service for the IAM principals in `DIAKRIN_PRIVATELINK_PRINCIPALS` |
| `DIAKRIN_EGRESS` | `direct` (default) | Outbound through one NAT gateway |
| `DIAKRIN_EGRESS` | `firewall` | Outbound through your firewall at `DIAKRIN_TRANSIT_GATEWAY_ID`. It must allow GitHub, the Diakrin image registry and Diakrin license checks |

> [!WARNING]
> Stop here before you apply a plan that deletes or replaces stateful resources. The CLI refuses it unless a person adds `--allow-destructive` after reading the plan.

## If it fails

| You see | It means | Do this |
| --- | --- | --- |
| `VALIDATION_FAILED` naming an internet gateway | CloudFront VPC origins need an internet gateway on the VPC | Attach one, or set `DIAKRIN_EXPOSE=none` |
| `EXPOSE_NEEDS_IGW` | Firewall egress with a new VPC creates no internet gateway | Set `DIAKRIN_EXPOSE=none`, or reuse a VPC that has one |
| `EXPOSE_PUBLIC` | A public load balancer cannot be combined with CloudFront | Use `DIAKRIN_INGRESS=private`, or `DIAKRIN_EXPOSE=none` |
| `DATABASE_BOTH` | Both a database URL and a database secret are set | Set only one |
| `SECRET_IN_FILE` | A secret is in `diakrin.env` | Move it to the process environment |
| `BYO_SUBNETS` | `DIAKRIN_VPC_ID` needs private subnets in two zones, and subnets need a VPC id | List two or more private subnet ids with `DIAKRIN_VPC_ID` |

## Next

Continue with [Install](/docs/self-hosted/install).
