> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tenetlabs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Deploy on ECS with Terraform

> The Terraform module deploys the same ECS-on-EC2 stack as the CloudFormation template — KMS, Aurora vault, Object Lock audit bucket, endpoints, VPC Lattice services with IAM auth policies, detect and rehydrate — with no shared secrets.

**The Terraform module**: the CloudFormation template's architecture as a reusable module — ECS on EC2 (Graviton), separate detect and rehydrate services and roles, each behind its own Amazon VPC Lattice service with an `AWS_IAM` auth policy, an optional Aurora vault, an Object Lock audit bucket, CMK-encrypted log groups and optional VPC endpoints. No NAT, no internet route, no shared secret.

## When to use it

You manage infrastructure with Terraform and want ECS. Requires Terraform ≥ 1.11 with the `hashicorp/aws` provider `~> 6.0` and `hashicorp/time` `~> 0.13`. The module has no backend and no provider block — configure both in your root module.

Wire the module into a root module, plan, and stop for review before apply.

Add the Tenet PII Engine (Enterprise) Terraform module to my infrastructure and plan it, following [https://docs.tenetlabs.com/pii-engine/deploy/terraform](https://docs.tenetlabs.com/pii-engine/deploy/terraform.md).

Ask me for: the root module directory; AWS Region and state backend (reuse the existing one if present); vpc\_id, vpc\_cidr, private\_subnet\_ids (one per AZ, ≥ 2, in AZs VPC Lattice supports), private\_route\_table\_ids; key\_admin\_role\_arn (the role that runs Terraform) or kms\_key\_arn; image\_uri (the Marketplace image by digest, from the verified release record, in every Region — never a copy; outside us-east-1 the role that runs Terraform needs `vpce:AllowMultiRegion` for the cross-Region endpoints the module creates); deployment\_id (3–63 chars, lowercase letters/digits/hyphens, never changes); the IAM role ARNs for detect\_invoker\_role\_arns, rehydrate\_invoker\_role\_arns and (optional) erasure\_invoker\_role\_arns — at most 25 each, role ARNs only; other caller VPCs (allowed\_source\_vpc\_ids); whether rehydration needs the approval tag; whether this is production or a trial; whether the VPC already has the endpoints (then create\_vpc\_endpoints = false) or a VPC Lattice service network (then existing\_service\_network\_arn).

1. Download the module into the root module directory with `curl -sO https://platform.tenetlabs.com/enterprise/current/tenet-pii-enterprise-terraform.tar.gz` and `tar -xzf tenet-pii-enterprise-terraform.tar.gz`; the module is `./tenet-pii-enterprise-terraform/deploy/terraform`. Use that path as the module `source`. Check `terraform version` is ≥ 1.11, the VPC has DNS hostnames and DNS support enabled, and the On-Demand Standard vCPU quota allows at least 8 vCPUs (12 with rehydration). Check `c9g.xlarge` and aurora-postgresql 16.15 are offered in the Region (set instance\_type = "c8g.xlarge" / ask me for aurora\_engine\_version if not).
2. Write a `module "tenet_pii"` block with those inputs and outputs for detect\_endpoint, rehydrate\_endpoint, lattice\_client\_security\_group\_id, detect\_caller\_policy, rehydrate\_caller\_policy, audit\_bucket and deployment\_id. Production: audit\_object\_lock\_mode = "COMPLIANCE", aurora\_deletion\_protection = true, vault\_final\_snapshot = true. Trial: audit\_object\_lock\_mode = "GOVERNANCE", aurora\_deletion\_protection = false, vault\_final\_snapshot = false.
3. Run `terraform init`, `terraform validate`, `terraform plan -out tenet-pii.plan`. Summarize the plan: counts by resource type, and confirm it creates no NAT gateway, no internet gateway and no security-group egress to 0.0.0.0/0, and that the task security groups admit 19991 only from the VPC Lattice prefix list.
4. STOP. Do not run `terraform apply` until I approve the plan explicitly.
   After apply: check both ECS services reach their desired count and every target in the detect and rehydrate Lattice target groups is HEALTHY (`aws vpc-lattice list-targets`). Tell me which caller roles need detect\_caller\_policy / rehydrate\_caller\_policy attached and which workloads need lattice\_client\_security\_group\_id, and ask before attaching. Then, from a host in the VPC running as an invoker role, run one signed /deidentify → /reidentify round trip with the tenet-pii-lattice client.

## Usage

Download the module and extract it next to your configuration. Point `source` at the extracted folder.

```bash theme={null}
curl -sO https://platform.tenetlabs.com/enterprise/current/tenet-pii-enterprise-terraform.tar.gz
tar -xzf tenet-pii-enterprise-terraform.tar.gz   # → ./tenet-pii-enterprise-terraform/deploy/terraform
```

Before you apply: [subscribe and verify the release](https://docs.tenetlabs.com/pii-engine/get-started/marketplace.md), use a VPC with DNS hostnames and DNS support enabled, and check your On-Demand Standard vCPU quota — each detect task fills a `c9g.xlarge`, so the default needs 8 vCPUs, 12 with rehydration.

```hcl main.tf theme={null}
module "tenet_pii" {
  source = "./tenet-pii-enterprise-terraform/deploy/terraform"

  vpc_id                  = "vpc-0123456789abcdef0"
  vpc_cidr                = "10.0.0.0/16"
  private_subnet_ids      = ["subnet-a", "subnet-b", "subnet-c"]   # >= 2 AZs, no NAT route, Lattice-supported AZs
  private_route_table_ids = ["rtb-a", "rtb-b", "rtb-c"]            # S3 gateway endpoint

  key_admin_role_arn = "arn:aws:iam::<account-id>:role/platform-admin"   # runs terraform
  image_uri          = "<repository>@sha256:<digest>"   # the Marketplace image from the verified release record, in every Region
  deployment_id      = "acme-prod-use1"   # bound into every KMS call; never change it

  aurora_engine_version  = "16.15"   # confirm it's offered in your Region
  audit_object_lock_mode = "COMPLIANCE"

  # The auth policies name exactly these roles, per route. At most 25 per list.
  detect_invoker_role_arns    = ["arn:aws:iam::<account-id>:role/claims-app"]           # required
  rehydrate_invoker_role_arns = ["arn:aws:iam::<account-id>:role/claims-reviewer"]      # required with rehydration
  erasure_invoker_role_arns   = ["arn:aws:iam::<account-id>:role/claims-retention-job"] # optional
}

output "detect_endpoint"                  { value = module.tenet_pii.detect_endpoint }
output "rehydrate_endpoint"               { value = module.tenet_pii.rehydrate_endpoint }
output "lattice_client_security_group_id" { value = module.tenet_pii.lattice_client_security_group_id }
output "detect_caller_policy"             { value = module.tenet_pii.detect_caller_policy }
output "rehydrate_caller_policy"          { value = module.tenet_pii.rehydrate_caller_policy }
```

```bash theme={null}
terraform init
terraform plan -out tenet-pii.plan
terraform apply tenet-pii.plan
```

## Calling the services

`detect_endpoint` and `rehydrate_endpoint` are `https://<service>.vpc-lattice-svcs.<region>.on.aws`. For each caller:

1. **Network.** Attach `lattice_client_security_group_id` to the caller (ENI, Lambda, ECS task); its only egress is tcp/443 to the `com.amazonaws.<region>.vpc-lattice` prefix list. The caller's VPC must be associated with the service network — the module associates `vpc_id` when it creates the network; with `existing_service_network_arn` you manage associations yourself — and listed in `allowed_source_vpc_ids` (default `[vpc_id]`).
2. **Identity policy.** Attach `detect_caller_policy` (detect and erasure callers) or `rehydrate_caller_policy` to the caller's role. A same-account caller needs this **and** the service auth policy; either alone is a `403`.
3. **Signing.** SigV4 with service `vpc-lattice-svcs` and `x-amz-content-sha256: UNSIGNED-PAYLOAD`. The `tenet-pii-lattice` client does this (`pip install https://platform.tenetlabs.com/enterprise/current/tenet_pii_lattice.tar.gz`); plain botocore `SigV4Auth` works, no `awscrt` needed. Reuse connections — each new connection pays a TLS handshake.

Routes are exact and case-sensitive: detect accepts `POST /detect`, `/redact`, `/deidentify` and (for erasure invokers) `DELETE /sessions/{session_id}`; rehydrate accepts only `POST /reidentify`. Anything else is a Lattice `404` or `403` and never reaches a task. With `require_rehydrate_approval_tag`, every `/reidentify` must come from a session tagged `tenet-approval-id` — Lattice checks it, because it doesn't forward session tags. The break-glass role (MFA) may call `/reidentify` without the tag.

Both roles audit to the Object Lock bucket: rehydrate under `audit_prefix`, detect (erasures) under `erasure_audit_prefix`. Lattice access logs for both services go to `/<name_prefix>/lattice`; join them to audit records on `upstream_request_id`. They record principal tags only on **denied** requests, so also put the approval id in the role session name. **Other accounts:** the module creates no RAM shares — share the service network or services yourself and add those roles to the invoker lists. The full model: [Rehydration and audit](https://docs.tenetlabs.com/pii-engine/concepts/rehydration-and-audit.md).

## Inputs

| Input | Default | Why |
| - | - | - |
| `vpc_id`, `vpc_cidr` | — | The endpoint SG admits 443 from `vpc_cidr`. |
| `private_subnet_ids` | — | ≥ 2 AZs (3 recommended), no NAT/IGW route, in AZs VPC Lattice supports (not `use1-az3`). |
| `private_route_table_ids` | `[]` | For the S3 gateway endpoint. |
| `deployment_id` | — | `TENET_DEPLOYMENT_ID`, validated `^[a-z0-9][a-z0-9-]{2,62}$`, pinned in every KMS condition. Never change it. |
| `image_uri` | — | The AWS Marketplace image, by digest from the release record, in any Region. Outside `us-east-1` the module's ECR and S3 endpoints reach it across Regions. A copy in your own ECR won't pull. |
| `detect_invoker_role_arns` | `[]` | Required, non-empty, ≤ 25. May `POST /detect`, `/redact`, `/deidentify`. |
| `rehydrate_invoker_role_arns` | `[]` | Required with rehydration, ≤ 25. May `POST /reidentify`; also the runtime's allow-list. |
| `erasure_invoker_role_arns` | `[]` | ≤ 25. May `DELETE /sessions/{session_id}`. Empty = nobody can; sessions expire. |
| `require_rehydrate_approval_tag` | `false` | Require the `tenet-approval-id` session tag on every `/reidentify`. |
| `break_glass_trusted_principal_arn` | `null` | Who may assume the MFA-gated break-glass role. [Break-glass](https://docs.tenetlabs.com/pii-engine/operate/break-glass.md). |
| `existing_service_network_arn` | `null` | `null` creates a service network (`AWS_IAM`, authenticated principals only) and associates `vpc_id`. Set it to use yours; you then own its auth policy and VPC associations. |
| `allowed_source_vpc_ids` | `[]` = `[vpc_id]` | VPCs allowed by `vpc-lattice-svcs:SourceVpc` in every auth-policy statement. |
| `lattice_prefix_list_id`, `s3_prefix_list_id` | `null` = looked up | Override only if the lookup isn't permitted. |
| `kms_key_arn` / `key_admin_role_arn` | `null` / `null` | Bring your own CMK, or let the module create one whose policy names only your roles (then `key_admin_role_arn` is required). With your own key, add the statements from [the CloudFormation page](https://docs.tenetlabs.com/pii-engine/deploy/ecs-cloudformation.md#bringing-your-own-kms-key). |
| `key_policy_include_account_root` | `false` | `false`: only named roles can use the key, and it becomes unmanageable if the admin role is deleted. |
| `create_vault_database` / `existing_vault_*` | `true` | New Aurora PG Serverless v2, or an existing cluster (`existing_vault_endpoint`, `existing_vault_port` = `5432`, `existing_vault_secret_arn` with `username`/`password`, `existing_vault_security_group_id`). |
| `aurora_engine_version` | `"16.15"` | Confirm it's offered in your Region. |
| `aurora_min_capacity`, `aurora_max_capacity`, `vault_database_name` | `0.5`, `8`, `"tenet"` | |
| `aurora_deletion_protection`, `vault_final_snapshot` | `true`, `true` | Production defaults. `false`/`false` for a trial you'll destroy. |
| `enable_rehydration` | `true` | `false` omits every rehydrate resource. The audit bucket stays — detect writes erasure records. |
| `rehydrate_desired_count` | `2` | With `1`, each rollout stops the task before starting its replacement. |
| `existing_audit_bucket_name` | `null` = create | Must have Object Lock and SSE-KMS with the CMK. |
| `audit_prefix`, `erasure_audit_prefix` | `"rehydrate/"`, `"erasure/"` | |
| `audit_object_lock_mode` | `"GOVERNANCE"` | Use `"COMPLIANCE"` in production. |
| `audit_retention_days` | `2190` | Default Object Lock retention (6 years). |
| `audit_reader_role_arn` | `null` | Read access to audit records. |
| `create_vpc_endpoints`, `enable_ssm_endpoints` | `true`, `false` | `false` if the VPC already has private-DNS endpoints. The module points `ecr.api` and `ecr.dkr` at `us-east-1`, where AWS Marketplace serves the image; outside `us-east-1` they're cross-Region (PrivateLink) and an extra `us-east-1` `s3` interface endpoint carries the image layers. Creating cross-Region endpoints needs `vpce:AllowMultiRegion` (Administrator and PowerUser roles have it). With `false`, create those three yourself. |
| `instance_type`, `ecs_ami_ssm_parameter` | `"c9g.xlarge"`, the ECS-optimized AL2023 arm64 parameter | `"c8g.xlarge"` where C9g isn't offered. |
| `asg_min_size`, `asg_max_size` | `2`, `12` | |
| `detect_desired_count`, `enable_detect_autoscaling`, `detect_max_count`, `detect_cpu_target_percent` | `2`, `true`, `10`, `60` | The module ignores later changes to the service's desired count, so applies don't reset autoscaling. |
| `deployment_minimum_healthy_percent`, `deployment_maximum_percent` | `50`, `100` | Replace tasks in place. [Upgrades](https://docs.tenetlabs.com/pii-engine/operate/upgrades.md). |
| `alarm_topic_arns` | `[]` | SNS topics for the alarm on usage that can't be reported to AWS Marketplace. [Billing](https://docs.tenetlabs.com/pii-engine/concepts/billing.md). |
| `telemetry_path` | `null` = stdout | Must be under `/tmp/`. Counts only, kept local. |
| `enable_usage_telemetry_egress` | `false` | Opt in to sending the usage records to Tenet; adds a NAT gateway. Needs `usage_telemetry_api_key_secret_arn` and `usage_telemetry_nat_subnet_id`; see `usage_telemetry_sink_url` (`"https://judge.tenetlabs.com"`) and `usage_telemetry_create_default_route` (`true`). Same behaviour as the [CloudFormation parameters](https://docs.tenetlabs.com/pii-engine/deploy/ecs-cloudformation.md#parameters); the secret holds a Tenet Platform API key ([setup](https://docs.tenetlabs.com/pii-engine/operate/monitoring.md#sending-usage-records-to-tenet)). |
| `log_retention_days` | `365` | |
| `name_prefix`, `tags` | `"tenet-pii"`, `{}` | |

Outputs: `detect_endpoint`, `rehydrate_endpoint`, `lattice_client_security_group_id`, `detect_caller_policy`, `rehydrate_caller_policy`, `service_network_arn`, `detect_service_arn`, `rehydrate_service_arn`, `kms_key_arn`, `audit_bucket`, `vault_endpoint`, `detect_task_role_arn`, `rehydrate_task_role_arn`, `break_glass_role_arn`, `cluster_name`, `deployment_id`. Rehydrate outputs are `null` without rehydration.

## Gotchas

* **A plan against a running install should be empty.** The module declares the container-definition defaults ECS stores, so an unchanged apply doesn't roll the services. If a plan replaces both task definitions without an input change, check your module version.
* **Destroy stops on the audit bucket.** It has `prevent_destroy` — it's an Object Lock record. For a trial: `terraform state rm 'module.tenet_pii.aws_s3_bucket.audit[0]'` (and its configuration resources) first, and in `GOVERNANCE` mode empty the bucket yourself. The KMS key is scheduled for deletion with a 30-day window.
* **Aurora with deletion protection** fails `terraform destroy` until you apply with `aurora_deletion_protection = false`. `vault_final_snapshot = true` leaves `<prefix>-vault-final` for you to delete.
* **Destroy ordering is built in.** The ASG depends on the VPC endpoints, their security-group rules and the instance role's ECS attachment, so services and instances are removed while the ECS agent can still reach ECS. If a destroy still sticks, terminate the ASG's instances, deregister them from the cluster, and run destroy again.
* **A failed apply leaves resources in state**; the next apply converges. Nothing is orphaned.
* **The Aurora secret rotation and health-check notes** on the [CloudFormation page](https://docs.tenetlabs.com/pii-engine/deploy/ecs-cloudformation.md#gotchas) apply here too: tasks read `PGUSER`/`PGPASSWORD` at start.

## Links

* [Prerequisites](https://docs.tenetlabs.com/pii-engine/deploy/prerequisites.md)
* [Deploy on ECS with CloudFormation — same architecture, parameter by parameter](https://docs.tenetlabs.com/pii-engine/deploy/ecs-cloudformation.md)
* [Rehydration and audit](https://docs.tenetlabs.com/pii-engine/concepts/rehydration-and-audit.md)
* [Troubleshooting](https://docs.tenetlabs.com/pii-engine/troubleshooting.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.