Deployment
Bootstrap an AWS account, apply an environment, and ship the first release.
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 lists. They are in the code (
/healthz, the/apiprefix, same-origin URLs, the production image). The one open application problem is the in-memory database, Blocker 1.
First deploy
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:
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_configPoint 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:
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.
Plan and apply
Replace the placeholders in terraform.tfvars, then:
terraform plan -out=tfplan
terraform apply tfplanSet the OpenAI key
Set the key out of band, so that it never reaches tfvars or state. Each environment gets its own key:
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/stdinPush the first image
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-deploymentPublish the client
(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 and first release.
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.
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 |