# workload-identity / aws / t0 "Quick start"
#
# Lets one GitHub repository's main branch sign in to AWS with a short-lived
# token, so no access key is stored in GitHub at all.
#
# Change the repository below, then:
#   terraform init && terraform apply
#   terraform output workflow
#
# Paste the printed workflow into .github/workflows/ in that repository and
# run it. It signs in and prints which role it became. Starting from any other
# branch or repository is refused.
#
# The role only gets read-only access here, which is enough to prove the
# sign-in works. The Standard version takes the policies you actually need.

locals {
  repository = "your-org/your-repo" # change me
  branch     = "main"
}

terraform {
  required_version = ">= 1.6"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 6.0"
    }
  }
}

provider "aws" {
  default_tags {
    tags = {
      owner               = "platform-engineering"
      environment         = "prod"
      data_classification = "internal"
      managed_by          = "terraform"
    }
  }
}

# One per account. If another tool already created it, apply fails with
# EntityAlreadyExists. The Standard version can look the existing one up.
resource "aws_iam_openid_connect_provider" "github" {
  url            = "https://token.actions.githubusercontent.com"
  client_id_list = ["sts.amazonaws.com"]
}

# The trust policy is the whole control. The audience check alone would let
# every workflow in every repository on GitHub assume this role, because they
# all get tokens from the same issuer. The subject check names one repository
# and one branch.
data "aws_iam_policy_document" "trust" {
  statement {
    actions = ["sts:AssumeRoleWithWebIdentity"]
    principals {
      type        = "Federated"
      identifiers = [aws_iam_openid_connect_provider.github.arn]
    }
    condition {
      test     = "StringEquals"
      variable = "token.actions.githubusercontent.com:aud"
      values   = ["sts.amazonaws.com"]
    }
    condition {
      test     = "StringEquals"
      variable = "token.actions.githubusercontent.com:sub"
      values   = ["repo:${local.repository}:ref:refs/heads/${local.branch}"]
    }
  }
}

resource "aws_iam_role" "github" {
  name                 = "github-${replace(local.repository, "/", "-")}"
  assume_role_policy   = data.aws_iam_policy_document.trust.json
  max_session_duration = 3600
}

resource "aws_iam_role_policy_attachment" "read_only" {
  role       = aws_iam_role.github.name
  policy_arn = "arn:aws:iam::aws:policy/ReadOnlyAccess"
}

data "aws_region" "current" {}

output "role_arn" {
  description = "The role the workflow becomes."
  value       = aws_iam_role.github.arn
}

output "workflow" {
  description = "A workflow that proves the sign-in. id-token: write is what lets it ask GitHub for a token."
  value       = <<-EOT
    name: prove-aws-sign-in
    on: workflow_dispatch
    permissions:
      id-token: write
      contents: read
    jobs:
      whoami:
        runs-on: ubuntu-latest
        steps:
          - uses: aws-actions/configure-aws-credentials@v4
            with:
              role-to-assume: ${aws_iam_role.github.arn}
              aws-region: ${data.aws_region.current.region}
          - run: aws sts get-caller-identity
  EOT
}
