---
title: Deployment
description: Bootstrap an AWS account, apply an environment, and ship the first release.
sidebar:
  icon: rocket
---

Every environment lives in its own AWS account and is deployed the same way. The steps below deploy `dev`. For `staging` and `prod`, repeat them with that account's credentials, name and GitHub Environment.

## Before you start

- Terraform 1.10 or newer, the AWS CLI, Docker and `jq`
- Admin credentials for the target AWS account (first run only)
- A GitHub Environment per account (`dev`, `staging`, `prod`) with required reviewers
- The application changes that [Assumptions](/assumptions#application-changes) lists. They are in the code (`/healthz`, the `/api` prefix, same-origin URLs, the production image). The one open application problem is the in-memory database, [Blocker 1](/assumptions#blocker-1-the-in-memory-database).

## First deploy

1. **Bootstrap the account**

    Run this once per account. It creates the state bucket and the GitHub OIDC roles. Bootstrap keeps its state locally, so use one workspace per account:

    ```sh
    cd infrastructure/bootstrap
    terraform init
    terraform workspace select -or-create patent-dev
    terraform apply \
      -var name=patent-dev \
      -var aws_region=eu-west-2 \
      -var github_repository=OWNER/REPO \
      -var github_environment=dev
    terraform output -json backend_config
    ```

    :::warning
    Keep `terraform.tfstate.d/` safe. It is not in git, and it is the only record of the bootstrap resources.
    :::

2. **Point the environment at its state bucket**

    `backend.tf` is a partial configuration, because the bucket name contains the account ID. Pass the rest at init:

    ```sh
    cd ../envs/dev
    terraform init \
      -backend-config="bucket=$(terraform -chdir=../../bootstrap output -raw state_bucket_name)" \
      -backend-config="region=eu-west-2" \
      -backend-config="kms_key_id=$(terraform -chdir=../../bootstrap output -json backend_config | jq -r .kms_key_id)"
    ```

    State locking uses an S3 lock file (`use_lockfile = true`); there is no DynamoDB table.

3. **Plan and apply**

    Replace the [placeholders](/assumptions#placeholders) in `terraform.tfvars`, then:

    ```sh
    terraform plan -out=tfplan
    terraform apply tfplan
    ```

    :::note
    The ECS service is created with the image tag `bootstrap` and an empty OpenAI secret, so tasks cannot start until the next two steps are done. `wait_for_steady_state = false` keeps the apply from hanging while it waits for them.
    :::

4. **Set the OpenAI key**

    Set the key out of band, so that it never reaches tfvars or state. Each environment gets its own key:

    ```sh
    read -rs OPENAI_API_KEY
    printf %s "$OPENAI_API_KEY" | aws secretsmanager put-secret-value \
      --secret-id "$(terraform output -raw openai_secret_arn)" \
      --secret-string file:///dev/stdin
    ```

5. **Push the first image**

    ```sh
    REPO=$(terraform output -raw ecr_repository_url)
    aws ecr get-login-password --region eu-west-2 | docker login --username AWS --password-stdin "${REPO%%/*}"
    docker build --platform linux/amd64 -t "$REPO:bootstrap" ../../../server
    docker push "$REPO:bootstrap"
    aws ecs update-service --cluster "$(terraform output -raw ecs_cluster_name)" \
      --service "$(terraform output -raw ecs_service_name)" --force-new-deployment
    ```

6. **Publish the client**

    ```sh
    (cd ../../../client && npm ci && npm run build)
    aws s3 sync ../../../client/dist "s3://$(terraform output -raw site_bucket_name)" --delete
    aws cloudfront create-invalidation --distribution-id "$(terraform output -raw distribution_id)" --paths "/index.html"
    ```

## Releases after the first deploy

CI deploys with the `deploy_role_arn` output from bootstrap: `deploy.yml` builds once and calls `_release.yml` for each environment. Each release pushes the image by digest, registers a task definition revision in `task_definition_family`, updates the service, verifies the rollout, smoke-tests it, and publishes the client. With the pipeline in place, steps 5 and 6 above are replaced by the first run of `deploy.yml`. See [Pipeline](/pipeline) and [first release](/environments#first-release-of-a-new-environment).

Terraform ignores `task_definition` and `desired_count` on the service, so app releases don't need a Terraform run. Rollback is **not** "the previous revision": Terraform registers revisions too, and its first one points at the missing `bootstrap` tag. `rollback.yml` re-releases a recorded, successful SHA instead. See [Rollback](/rollback).

## GitHub roles

Bootstrap creates three roles that GitHub Actions assumes through OIDC. No long-lived AWS keys exist.

| Role | Who can assume it | What it can do |
|---|---|---|
| `deploy_role_arn` | Jobs in the protected GitHub Environment | Push to ECR, register task definitions (with a tag on registration), update the ECS service, pass the two task roles, write the site bucket, invalidate CloudFront, read and write the deploy freeze parameter under `/<name>/deploy/` |
| `terraform_plan_role_arn` | Pull request jobs | Read-only (`ReadOnlyAccess`), plus read access to state and write access to the lock file |
| `terraform_apply_role_arn` | Jobs in the protected GitHub Environment | `AdministratorAccess`, gated by the Environment's required reviewers |

:::tip
The plan role trusts the `pull_request` subject only. Jobs that set `environment:` carry a different subject, so the plan job must not set one.
:::
