# Proton opens the pull request. You own everything after it.

> AWS Proton with Terraform means self-managed provisioning: it renders your template into a repository and submits a PR. What the template bundle looks like, what lands in the branch, and the callback you cannot forget.

- Author: Deyan Peev (https://deyanpeev.com)
- Published: 2024-05-14
- Tags: AWS Proton, Terraform, AWS, Infrastructure as Code, GitHub Actions, ECS, CI/CD
- URL: https://deyanpeev.com/posts/proton-opens-the-pull-request

The [catalog](https://deyanpeev.com/posts/the-button-in-port-and-the-service-it-makes) ends with a
GitHub Actions job calling `aws proton create-service` and exiting. This post is
about what happens on the other side of that call. The first thing to understand
is that Proton didn't build anything for us. It wrote some Terraform, put it on
a branch, opened a pull request, and waited. That is the only way Proton does
Terraform.

# Why a provisioning service at all

We already had modules. Every team could `terraform init` and every team had.
But "we have modules" and "there is one current way to stand up a service" are
different sentences, and we were living in the first one. Seven repositories
with a `terraform/` directory, each pinned to whatever the module registry
offered on the day it was written, and each drifting from the others in ways
you couldn't see without opening all seven.

We wanted three specific things from Proton. Templates versioned in one place,
so that "current" is a number and not an opinion. Environments as first-class
objects, so a service instance belongs to an environment and inherits its
account, VPC and cluster instead of being told about them. And a list. A
queryable list of every service instance in every environment, with the
template major and minor version it's running, because without that you can't
plan a migration.

# Two ways to provision, and Terraform picks one

Proton
[provisions in one of a few ways](https://docs.aws.amazon.com/proton/latest/userguide/ag-works-prov-methods.html),
and the method is decided by the template bundle you register, not by a setting.

With **AWS-managed provisioning**, the bundle contains CloudFormation, Proton
renders it, calls CloudFormation itself, watches the stack, and captures the
outputs. Nothing of yours runs.

With **self-managed provisioning** - the only option for Terraform - Proton
renders your `.tf` files, submits a pull request to a repository you registered,
and then stops. Your automation merges it, runs the provisioning engine, and
tells Proton how it went. The documentation is blunt about the division:
provisioned by *your code*, status tracked by *your code*.

That difference goes further than it looks at first. The pull request is
the API, a closed PR is a cancelled deployment, and Proton's view of your
infrastructure is only as accurate as your callback.

# The template bundle

A manifest, a schema, and a directory of Terraform.

```
service-template/
  manifest.yaml
  schema/
    schema.yaml
  infrastructure/
    main.tf
    outputs.tf
```

`schema/schema.yaml` is the input contract. It draws the form in the Proton
console and validates the spec, and (as the previous post complained at some
length) it's what the catalog form should have been generated from.

```yaml
schema:
  format:
    openapi: '3.0.0'
  service_input_type: HttpServiceInput
  types:
    HttpServiceInput:
      type: object
      description: 'An HTTP service behind the shared ALB'
      properties:
        port:
          type: number
          default: 3000
          minimum: 1
          maximum: 65535
        desired_count:
          type: number
          default: 2
          minimum: 1
          maximum: 20
        cpu:
          type: string
          default: '512'
          enum: ['256', '512', '1024', '2048']
        health_check_path:
          type: string
          default: /healthz
      required:
        - port
```

`infrastructure/` is ordinary Terraform with one unusual rule, which is that the
inputs arrive under namespaces. `var.port` becomes
`var.service_instance.inputs.port`, and `var.vpc_id` becomes
`var.environment.outputs.vpc_id`. The second one is how an instance reaches
values the environment produced when *it* was provisioned.

```hcl
resource "aws_ecs_service" "this" {
  name            = var.service_instance.name
  cluster         = var.environment.outputs.cluster_arn
  desired_count   = var.service_instance.inputs.desired_count
  task_definition = aws_ecs_task_definition.this.arn

  network_configuration {
    subnets         = var.environment.outputs.private_subnet_ids
    security_groups = [aws_security_group.this.id]
  }

  load_balancer {
    target_group_arn = aws_lb_target_group.this.arn
    container_name   = var.service_instance.name
    container_port   = var.service_instance.inputs.port
  }
}
```

Writing against those namespaces takes a day to stop feeling strange. What you
get back is a template that's portable between environments, because it never
names one.

# What lands in the branch

When Proton renders, it compiles your `.tf` files with the inputs from the
create call and writes two extra files next to them: a
`proton.{resource-type}.variables.tf` declaring the namespaced variables, and a
`proton.auto.tfvars.json` holding the values. Terraform picks up
`*.auto.tfvars.json` without being told.

The layout follows the resource hierarchy. Proton looks for an `environments`
folder in the registered repository and, if it finds one, nests everything
under it:

```
/environments
  /env-prod
      main.tf
      proton.environment.variables.tf
      proton.auto.tfvars.json

      /billing-api-prod
          main.tf
          proton.service_instance.variables.tf
          proton.auto.tfvars.json

      /search-indexer-prod
          main.tf
          proton.service_instance.variables.tf
          proton.auto.tfvars.json
```

A service instance is a directory inside its environment's directory. That's a
better default than it sounds, because you can read the blast radius of a plan
off the path. Everything a single `terraform apply` can touch is in one folder,
and the folder is named after one service instance in one environment.

The tfvars file also carries `proton_tags` (the account, template and
environment ARNs), which you can wire into the provider's `default_tags` to
get provenance on every resource for free. We did, and it paid for itself the
first time somebody asked where an unlabelled security group had come from.

# The state key is yours to get right

Proton does not manage Terraform state. The `backend` block is in your template
like anything else, so the same rendered template in twenty directories will
happily share one state key unless you stop it.

```hcl
terraform {
  backend "s3" {}
}
```

The arrangement that survived was an empty backend block plus
`-backend-config` at init time, with the key derived from the path Proton just
rendered into. Hardcoding a key in the template looks fine until the second
instance, and by then the first one is in production.

# Closing the PR is an API call

The documentation says that if the customer closes the PR, Proton recognises it
as closed and marks the deployment as cancelled.

So the pull request *is* the deployment. Merge it and provisioning starts, close
it and the deployment is cancelled. Leave it and Proton waits, and that's a
problem of its own, because a PR that times out leaves the resource in a state
that is neither created nor failed.

Deletion works the same way and is stranger. Proton can't simply delete the
files, because a Terraform directory contains configuration that isn't
resources (providers, backends), and removing it does not destroy anything, it
orphans it. So Proton marks the files for deletion and sets a flag in the PR
metadata, and your automation is expected to read that flag and run
`terraform destroy` instead of treating the PR as an ordinary change.

We got that wrong first time round. The delete PR merged, the files went away,
the resources did not, and the only evidence was an AWS bill that was flat in a
month when things had definately been deleted.

# The callback, and what forgetting it looks like

When your apply finishes, you call
[NotifyResourceDeploymentStatusChange](https://docs.aws.amazon.com/proton/latest/APIReference/API_NotifyResourceDeploymentStatusChange.html)
with the resource ARN, a status, and the outputs.

```bash
aws proton notify-resource-deployment-status-change \
  --resource-arn "$PROTON_RESOURCE_ARN" \
  --status SUCCEEDED \
  --deployment-id "$PROTON_DEPLOYMENT_ID" \
  --outputs "$(terraform output -json | jq -c '
      to_entries | map({key: .key, valueString: (.value.value | tostring)})')"
```

The outputs are how the next thing reads this thing. An environment's `vpc_id`
and `cluster_arn` land in the environment's outputs, and every service instance
rendered into that environment reads them through `var.environment.outputs`.
Skip them on the environment and every service template that depends on them
renders with holes.

Forgetting the call entirely fails silently. Proton keeps showing
**In progress** and nothing errors. The resources exist, the PR is merged, the
ECS service is serving traffic, and the console says the deployment is still
running. So the next update to that instance is refused, and the person who
requested it is told nothing useful. We spent an afternoon on that
once, on a path where the apply succeeded and the notify step had been skipped
by an `if: success()` on a job whose earlier step had a `continue-on-error`.

The status is also the only place a failure can be reported. If the apply
fails, `--status FAILED` with a `--status-message` is what puts the reason in
front of the person who pressed the button. Without it they see a spinner and
ask you.

# What it bought, and what it did not

It bought the three things we asked for. Template versions are numbers,
environments own their accounts and hand their outputs down, and the list
exists. Every instance in every environment with its template version,
queryable. "Who is still on v1 of the HTTP template" went from an afternoon of
grep to a command.

It did not buy speed. A managed-provisioning stack goes from API call to
CloudFormation in seconds. Ours went from API call to a pull request, and then
sat there until a validation pipeline and a merge decided otherwise. For a
developer that's minutes of waiting, and the documentation says so plainly:
with self-managed provisioning, developers might experience slower response,
because someone has to merge the PR before provisioning can start.

Our answer was to make sure the someone wasn't a person, and that needed a good
deal more care than "turn on auto-merge". That's the
[next post](https://deyanpeev.com/posts/auto-merging-a-pull-request-nobody-wrote).

---

*Written in 2024. AWS has since announced that Proton reaches end of support on
7 October 2026, and it has been closed to new customers since 7 October 2025.
The mechanics above are still an accurate description of how self-managed
provisioning works. Most of them - a rendered directory per instance, a pull
request as the deployment, a status callback - I would use again whether or not
the thing rendering them is Proton.*
