diff --git a/.github/workflows/apply-all.yml b/.github/workflows/apply-all.yml
index 76e5736..0c64f9a 100644
--- a/.github/workflows/apply-all.yml
+++ b/.github/workflows/apply-all.yml
@@ -7,26 +7,32 @@ on:
inputs:
input_environment:
description: 'Input environment'
- required: true
+ required: true
type: choice
options:
- dev
- stag
- prod
+permissions: {}
+
jobs:
vpc:
- uses: bombsdontbite/multi-environment-infrastructure-provisioning/.github/workflows/terraform-stack.yml@main
+ uses: ./.github/workflows/terraform-stack.yml
+ permissions:
+ contents: read
+ id-token: write
with:
input_environment: ${{ inputs.input_environment }}
input_stack: vpc
input_mode: apply
- secrets: inherit
ec2:
- uses: bombsdontbite/multi-environment-infrastructure-provisioning/.github/workflows/terraform-stack.yml@main
+ needs: vpc
+ uses: ./.github/workflows/terraform-stack.yml
+ permissions:
+ contents: read
+ id-token: write
with:
input_environment: ${{ inputs.input_environment }}
input_stack: ec2
input_mode: apply
- secrets: inherit
- needs: vpc
diff --git a/.github/workflows/destroy-all.yml b/.github/workflows/destroy-all.yml
index 97d6b92..5e97c9e 100644
--- a/.github/workflows/destroy-all.yml
+++ b/.github/workflows/destroy-all.yml
@@ -7,26 +7,32 @@ on:
inputs:
input_environment:
description: 'Input environment'
- required: true
+ required: true
type: choice
options:
- dev
- stag
- prod
+permissions: {}
+
jobs:
ec2:
- uses: bombsdontbite/multi-environment-infrastructure-provisioning/.github/workflows/terraform-stack.yml@main
+ uses: ./.github/workflows/terraform-stack.yml
+ permissions:
+ contents: read
+ id-token: write
with:
input_environment: ${{ inputs.input_environment }}
input_stack: ec2
input_mode: destroy
- secrets: inherit
vpc:
- uses: bombsdontbite/multi-environment-infrastructure-provisioning/.github/workflows/terraform-stack.yml@main
+ needs: ec2
+ uses: ./.github/workflows/terraform-stack.yml
+ permissions:
+ contents: read
+ id-token: write
with:
input_environment: ${{ inputs.input_environment }}
input_stack: vpc
input_mode: destroy
- secrets: inherit
- needs: ec2
diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml
new file mode 100644
index 0000000..b85e329
--- /dev/null
+++ b/.github/workflows/pull-request.yml
@@ -0,0 +1,63 @@
+name: pull-request
+
+run-name: checks for pull request #${{ github.event.pull_request.number }}
+
+on:
+ pull_request:
+ branches:
+ - main
+
+permissions: {}
+
+# The checks touch no state, so a newer push may cancel an older run.
+concurrency:
+ group: pull-request-${{ github.event.pull_request.number }}
+ cancel-in-progress: true
+
+jobs:
+ checks:
+ name: fmt, validate, trivy
+ runs-on: ubuntu-24.04
+ timeout-minutes: 20
+ permissions:
+ contents: read
+ security-events: write
+ steps:
+ - name: Checkout
+ uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+ with:
+ persist-credentials: false
+ - name: Install Terraform
+ uses: hashicorp/setup-terraform@dfe3c3f87815947d99a8997f908cb6525fc44e9e # v4.0.1
+ with:
+ terraform_version: 1.17.1
+ terraform_wrapper: false
+ - name: Terraform Format
+ run: terraform fmt -check -recursive -diff
+ - name: Terraform Validate
+ run: |
+ for dir in main/vpc main/ec2 bootstrap/management bootstrap/target; do
+ echo "::group::$dir"
+ terraform -chdir="$dir" init -backend=false -input=false
+ terraform -chdir="$dir" validate
+ echo "::endgroup::"
+ done
+ # Runs after init so the downloaded modules are scanned too; prod's inputs switch on the most.
+ - name: Trivy
+ uses: aquasecurity/trivy-action@ed142fd0673e97e23eac54620cfb913e5ce36c25 # v0.36.0
+ with:
+ scan-type: config
+ scan-ref: .
+ tf-vars: ./inputs/prod.tfvars
+ severity: HIGH,CRITICAL
+ limit-severities-for-sarif: true
+ format: sarif
+ output: trivy-results.sarif
+ exit-code: '1'
+ version: v0.76.1
+ - name: Upload Trivy results to code scanning
+ if: ${{ !cancelled() && hashFiles('trivy-results.sarif') != '' }}
+ uses: github/codeql-action/upload-sarif@9b3254085494b55a0f7c4c2b3d2d52cc712ce370 # v4.39.1
+ with:
+ sarif_file: trivy-results.sarif
+ category: trivy-config
diff --git a/.github/workflows/terraform-stack.yml b/.github/workflows/terraform-stack.yml
index c0c2ce9..589d973 100644
--- a/.github/workflows/terraform-stack.yml
+++ b/.github/workflows/terraform-stack.yml
@@ -7,21 +7,21 @@ on:
inputs:
input_environment:
description: 'Input environment'
- required: true
+ required: true
type: string
input_stack:
description: 'Input the Terraform stack'
- required: true
+ required: true
type: string
input_mode:
- description: 'Whether to deploy or destroy the infrastructure'
- required: true
+ description: 'Whether to plan, deploy or destroy the infrastructure'
+ required: true
type: string
workflow_dispatch:
inputs:
input_environment:
description: 'Input environment'
- required: true
+ required: true
type: choice
options:
- dev
@@ -29,14 +29,14 @@ on:
- prod
input_stack:
description: 'Input the Terraform stack'
- required: true
+ required: true
type: choice
options:
- vpc
- ec2
input_mode:
- description: 'Whether to deploy or destroy the infrastructure'
- required: true
+ description: 'Whether to plan, deploy or destroy the infrastructure'
+ required: true
type: choice
default: plan
options:
@@ -44,50 +44,163 @@ on:
- apply
- destroy
+# Nothing by default; each job asks for what it needs.
+permissions: {}
+
+# One run per environment and stack. Never cancel a running one: an apply stopped halfway
+# leaves a lock and a partly changed environment behind.
+concurrency:
+ group: terraform-${{ inputs.input_environment }}-${{ inputs.input_stack }}
+ cancel-in-progress: false
+
+env:
+ TF_IN_AUTOMATION: 'true'
+ TF_INPUT: 'false'
+ TF_ENVIRONMENT: ${{ inputs.input_environment }}
+ TF_STACK: ${{ inputs.input_stack }}
+ TF_MODE: ${{ inputs.input_mode }}
+
jobs:
- terraform:
- name: ${{ inputs.input_stack }}
- runs-on: ubuntu-latest
+ plan:
+ name: plan ${{ inputs.input_stack }}
+ runs-on: ubuntu-24.04
+ timeout-minutes: 30
environment: ${{ inputs.input_environment }}
- concurrency: ${{ inputs.input_environment }}-${{ inputs.input_stack }}-terraform-stack
+ permissions:
+ contents: read
+ id-token: write
+ outputs:
+ changes: ${{ steps.plan.outputs.changes }}
defaults:
run:
shell: bash
working-directory: ./main/${{ inputs.input_stack }}
+ env:
+ TF_VAR_target_account: ${{ vars.TARGET_ACCOUNT }}
+ TF_VAR_target_region: ${{ vars.TARGET_REGION }}
+ TF_VAR_target_role: ${{ vars.TARGET_ROLE }}
steps:
+ - name: Check inputs
+ working-directory: .
+ run: |
+ [[ "$TF_ENVIRONMENT" =~ ^(dev|stag|prod)$ ]] || { echo "::error::unknown environment: $TF_ENVIRONMENT"; exit 1; }
+ [[ "$TF_STACK" =~ ^(vpc|ec2)$ ]] || { echo "::error::unknown stack: $TF_STACK"; exit 1; }
+ [[ "$TF_MODE" =~ ^(plan|apply|destroy)$ ]] || { echo "::error::unknown mode: $TF_MODE"; exit 1; }
- name: Checkout
- uses: actions/checkout@v3
- - name: Configure AWS Credentials
- uses: aws-actions/configure-aws-credentials@v1-node16
+ uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
- aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
- aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- aws-region: ${{ secrets.AWS_DEFAULT_REGION }}
+ persist-credentials: false
+ - name: Configure AWS credentials
+ uses: aws-actions/configure-aws-credentials@e1253824e5c10ff9df46874f81ed3ec929e19cfd # v6.3.0
+ with:
+ role-to-assume: ${{ vars.AWS_ROLE_ARN }}
+ role-session-name: gha-${{ inputs.input_environment }}-${{ inputs.input_stack }}-${{ github.run_id }}
+ role-duration-seconds: 3600
+ aws-region: ${{ vars.AWS_REGION }}
- name: Install Terraform
- uses: hashicorp/setup-terraform@v2
+ uses: hashicorp/setup-terraform@dfe3c3f87815947d99a8997f908cb6525fc44e9e # v4.0.1
+ with:
+ terraform_version: 1.17.1
+ terraform_wrapper: false
+ - name: Terraform Init
+ run: terraform init
- name: Trivy
- uses: aquasecurity/trivy-action@master
if: inputs.input_mode != 'destroy'
+ uses: aquasecurity/trivy-action@ed142fd0673e97e23eac54620cfb913e5ce36c25 # v0.36.0
with:
+ scan-type: config
scan-ref: ./main/${{ inputs.input_stack }}
- scan-type: 'config'
- - name: Terraform Init
- run: terraform init
+ tf-vars: ./inputs/${{ inputs.input_environment }}.tfvars
+ severity: HIGH,CRITICAL
+ exit-code: '1'
+ version: v0.76.1
+ - name: Terraform Workspace
+ run: terraform workspace select -or-create "$TF_ENVIRONMENT"
+ - name: Terraform Validate
+ run: terraform validate
- name: Terraform Plan
- env:
- TF_VAR_target_account: ${{ secrets.TARGET_ACCOUNT }}
- TF_VAR_target_region: ${{ secrets.TARGET_REGION }}
- TF_VAR_target_role: ${{ secrets.TARGET_ROLE }}
+ id: plan
run: |
- terraform workspace select -or-create ${{ inputs.input_environment }}
- terraform validate
- if [[ "${{ inputs.input_mode }}" == "destroy" ]]; then
- terraform plan -destroy -var-file="../../inputs/${{ inputs.input_environment }}.tfvars" -out "${{ inputs.input_environment }}.txt"
- else
- terraform plan -var-file="../../inputs/${{ inputs.input_environment }}.tfvars" -out "${{ inputs.input_environment }}.txt"
+ args=(-var-file="../../inputs/${TF_ENVIRONMENT}.tfvars" -out=tfplan -lock-timeout=5m -detailed-exitcode)
+ if [[ "$TF_MODE" == "destroy" ]]; then
+ args+=(-destroy)
fi
- - name: Terraform Apply
- if: inputs.input_mode != 'plan'
+ set +e
+ terraform plan "${args[@]}"
+ code=$?
+ set -e
+ case "$code" in
+ 0) echo "changes=false" >> "$GITHUB_OUTPUT" ;;
+ 2) echo "changes=true" >> "$GITHUB_OUTPUT" ;;
+ *) exit "$code" ;;
+ esac
+ - name: Plan summary
run: |
- terraform workspace select -or-create ${{ inputs.input_environment }}
- terraform apply -input=false "${{ inputs.input_environment }}.txt"
+ {
+ echo "### ${TF_MODE} ${TF_STACK} on ${TF_ENVIRONMENT}"
+ echo
+ terraform show -no-color tfplan | grep -E '^(Plan:|No changes\.)' || true
+ echo
+ echo 'Full plan
'
+ echo
+ echo '```'
+ terraform show -no-color tfplan | head -c 900000
+ echo '```'
+ echo ' '
+ } >> "$GITHUB_STEP_SUMMARY"
+ # A plan file holds every input value in plain text, sensitive ones included, and anyone who
+ # can read the repository can download its artifacts. Keep secrets out of variables, or encrypt
+ # the file before uploading it.
+ - name: Upload plan
+ if: inputs.input_mode != 'plan' && steps.plan.outputs.changes == 'true'
+ uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
+ with:
+ name: tfplan-${{ inputs.input_environment }}-${{ inputs.input_stack }}
+ path: main/${{ inputs.input_stack }}/tfplan
+ if-no-files-found: error
+ retention-days: 1
+
+ apply:
+ name: ${{ inputs.input_mode }} ${{ inputs.input_stack }}
+ needs: plan
+ if: inputs.input_mode != 'plan' && needs.plan.outputs.changes == 'true'
+ runs-on: ubuntu-24.04
+ # The chained target-account session ends with the OIDC session, after an hour.
+ timeout-minutes: 55
+ environment: ${{ inputs.input_environment }}
+ permissions:
+ contents: read
+ id-token: write
+ defaults:
+ run:
+ shell: bash
+ working-directory: ./main/${{ inputs.input_stack }}
+ steps:
+ - name: Checkout
+ uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+ with:
+ persist-credentials: false
+ - name: Configure AWS credentials
+ uses: aws-actions/configure-aws-credentials@e1253824e5c10ff9df46874f81ed3ec929e19cfd # v6.3.0
+ with:
+ role-to-assume: ${{ vars.AWS_ROLE_ARN }}
+ role-session-name: gha-${{ inputs.input_environment }}-${{ inputs.input_stack }}-${{ github.run_id }}
+ role-duration-seconds: 3600
+ aws-region: ${{ vars.AWS_REGION }}
+ - name: Install Terraform
+ uses: hashicorp/setup-terraform@dfe3c3f87815947d99a8997f908cb6525fc44e9e # v4.0.1
+ with:
+ terraform_version: 1.17.1
+ terraform_wrapper: false
+ - name: Download plan
+ uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
+ with:
+ name: tfplan-${{ inputs.input_environment }}-${{ inputs.input_stack }}
+ path: main/${{ inputs.input_stack }}
+ - name: Terraform Init
+ run: terraform init
+ - name: Terraform Workspace
+ run: terraform workspace select "$TF_ENVIRONMENT"
+ # Applies exactly what the plan job showed; Terraform refuses the file if the state moved since.
+ - name: Terraform Apply
+ run: terraform apply -lock-timeout=5m tfplan
diff --git a/.gitignore b/.gitignore
index 7a3e2fd..226b662 100644
--- a/.gitignore
+++ b/.gitignore
@@ -25,5 +25,9 @@ override.tf.json
#
# !example_override.tf
-# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
-# example: *tfplan*
+# Plan files (terraform plan -out=tfplan): binary, and they can hold secrets in plain text
+tfplan
+*.tfplan
+
+# Bootstrap inputs: account IDs, copied from terraform.tfvars.example
+terraform.tfvars
diff --git a/README.md b/README.md
index a9a0562..90d332a 100644
--- a/README.md
+++ b/README.md
@@ -2,39 +2,129 @@
## Description
-This project demonstrates the potential of reusable workflows using GitHub Actions to provision multi-environment AWS infrastructure. You can read more about this project in [my article on Medium](https://medium.com/@bombsdontbite/multi-environment-infrastructure-provisioning-using-terraform-workspaces-github-actions-bb898330b6e).
+This project demonstrates reusable GitHub Actions workflows that provision the same AWS infrastructure into several environments: one code base, one set of inputs per environment, one Terraform CLI workspace per environment. You can read more about it in [my article on Medium](https://medium.com/@bombsdontbite/multi-environment-infrastructure-provisioning-using-terraform-workspaces-github-actions-bb898330b6e).
-> **Please note:** I'm not focusing on infrastructure security in terms of AWS best practices and recommendations, this project is just a demonstration of GitHub Actions features.
+The article was written in March 2023. The code was brought up to date in October 2026; [what changed since 2023](#what-changed-since-2023) lists the differences.
+
+> **Please note:** this is still a demonstration of GitHub Actions and Terraform features, not a production blueprint. For example, the load balancer serves plain HTTP to the internet, because HTTPS needs a domain and a certificate.
-----
-## Environments
+## How it works
-| Name | Desctiption |
-|------|-------------|
-| dev | Development environment. |
-| stag | Staging environment. |
-| prod | Production environment. |
+```
+inputs/{dev,stag,prod}.tfvars ──┐
+ ├─► main/vpc ──(terraform_remote_state, same workspace)──► main/ec2
+terraform workspace ──────┘
+```
+
+- **Two stacks**, `main/vpc` and `main/ec2`, built from [terraform-aws-modules](https://github.com/terraform-aws-modules). The EC2 stack reads the VPC stack's outputs with `terraform_remote_state` and `workspace = terraform.workspace`, so `dev` reads `dev`.
+- **One workspace per environment.** The S3 backend keeps each workspace's state at `workspaces///terraform.tfstate` and locks it with a `.tflock` object beside it (`use_lockfile`, no DynamoDB).
+- **Two providers.** The default `aws` provider is the management account, where the state lives; the workflow reaches it through GitHub OIDC. The `target` alias assumes a role in the environment's own account (role chaining), and every resource uses it.
+- **One reusable workflow**, `terraform-stack.yml`, takes an environment, a stack and a mode (`plan`, `apply`, `destroy`). `apply-all.yml` and `destroy-all.yml` call it for both stacks in order. `pull-request.yml` only runs checks and never touches AWS.
-----
-## Secrets
+## Setup
+
+Nothing here creates AWS resources until you apply it. The bootstrap is applied once, by hand, with administrator credentials; its state stays local (keep it somewhere safe, or move it into the bucket afterwards with a `backend "s3"` block and `terraform init -migrate-state`).
+
+### 1. Management account: OIDC provider, state bucket, one role per environment
+
+```bash
+cd bootstrap/management
+cp terraform.tfvars.example terraform.tfvars # put in the target account IDs
+gh api repos///actions/oidc/customization/sub # check sub_claim_prefix
+terraform init && terraform apply
+```
+
+Each role `github-terraform-` trusts only a job of this repository running in GitHub environment ``: the token's `sub` must equal `repo:/:environment:` and its `aud` `sts.amazonaws.com`. If GitHub issues the repository an immutable subject (`use_immutable_subject: true` in the call above, with ids in the prefix), set `github_subject_prefix` to the `sub_claim_prefix` it prints. A role may read and write only `workspaces//*` in the state bucket, delete only its own `.tflock` objects, and assume only ``'s target role.
+
+If the account already has a GitHub OIDC provider, set `create_oidc_provider = false`. If you change the bucket name, region or `workspace_key_prefix`, change them in `main/*/backend.tf` and `main/ec2/data.tf` as well; backend blocks cannot take variables.
+
+### 2. Each target account: the role Terraform works in
+
+```bash
+cd bootstrap/target
+cp terraform.tfvars.example terraform.tfvars # the management role ARNs from step 1
+terraform init && terraform apply # with that account's administrator credentials
+```
+
+The role `terraform-target` trusts the management roles of the environments that deploy into the account. It has `PowerUserAccess` plus the IAM the stacks create (the web servers' role under `/ec2/` and the Auto Scaling service-linked role). The chained session lasts at most an hour, which bounds a single apply.
+
+### 3. GitHub environments
+
+Create `dev`, `stag` and `prod` (*Settings → Environments*) and give each these **environment variables**. Nothing in them is a credential, so they are variables, not secrets; if you would rather hide the account IDs, store them as environment secrets and read `secrets.` instead of `vars.` in `terraform-stack.yml`.
+
+| Variable | Value |
+|----------|-------|
+| `AWS_ROLE_ARN` | `github-terraform-` from step 1 (output `environment_role_arns`). |
+| `AWS_REGION` | The state bucket's region (`eu-north-1`). |
+| `TARGET_ACCOUNT` | The environment's AWS account ID. |
+| `TARGET_REGION` | The region its resources go to. |
+| `TARGET_ROLE` | `terraform-target`. |
+
+Protection rules:
+
+| Environment | Required reviewers | Deployment branches |
+|-------------|--------------------|---------------------|
+| `dev` | yes | all branches, for manual runs from a feature branch |
+| `stag` | yes | `main` only |
+| `prod` | yes, and prevent self-review | `main` only |
+
+The plan and the apply are separate jobs and both run in the environment, so in every environment a reviewer approves twice: once to let the plan run, then again after reading it in the run's summary. Because `dev` accepts any branch, anyone who can push a branch can use the `dev` role from it; keep `dev` a sandbox in its own account.
-### Repository secrets
+### 4. First run
+
+Run **apply-all** for `dev` once to check the whole setup end to end. The EC2 stack reads the VPC stack's applied state, so `apply-all` applies `vpc` first.
+
+-----
+
+## Running
+
+| Workflow | Trigger | What it does |
+|----------|---------|--------------|
+| `pull-request` | pull request to `main` | `terraform fmt -check`, `init -backend=false` and `validate` for every root module, Trivy (fails on HIGH/CRITICAL, results in *Security → Code scanning*). It needs no AWS access. |
+| `terraform-stack` | manual, or called | Plans one stack in one environment; in `apply` or `destroy` mode a second job applies that exact plan. |
+| `apply-all` | manual | `vpc`, then `ec2`. |
+| `destroy-all` | manual | `ec2`, then `vpc`. |
+
+Applying happens only from a manual run, in the environment's protection rules. Runs on the same environment and stack queue behind each other (`cancel-in-progress: false`, since a cancelled apply leaves a lock and a half-changed environment). GitHub keeps one waiting run per group: a third run replaces the one waiting, not the one running.
+
+> **Plan files can contain secrets.** `tfplan` holds every input value in plain text, sensitive ones included, and anyone who can read a public repository can download its artifacts. Here the plan lives one day and the inputs hold no secrets. If yours do, encrypt the file before uploading it, or plan and apply in one job.
+
+-----
+
+## Workspaces and credentials
+
+HashiCorp [recommends against](https://developer.hashicorp.com/terraform/cli/workspaces#when-not-to-use-multiple-workspaces) CLI workspaces when deployments need strong separation, such as different credentials and access controls per environment, because every workspace in a working directory shares one backend. Separate root modules (a directory per environment calling a shared module) are its suggested answer.
+
+This repository keeps workspaces, which is the article's idea, and narrows the gap:
+
+- each environment has its own role, which only a job in that GitHub environment can assume;
+- that role can read and write only its own state prefix and assume only its own target role;
+- each environment's resources live in its own account, and `allowed_account_ids` stops a run that points at the wrong one.
+
+What stays shared: one bucket and one backend configuration, bucket listing (key names, not contents), and the code itself, so a change reaches every environment the moment it is applied there. Switch to separate root modules when environments need different backends or state accounts, different module versions promoted one environment at a time, different owners, or more environments and regions than a `choice` input holds comfortably.
+
+-----
-| Secret name | Desctiption | Required |
-|-------------|-------------|----------|
-| AWS_ACCESS_KEY_ID | Specifies an AWS access key associated with an IAM user. | yes |
-| AWS_SECRET_ACCESS_KEY | Specifies the secret key associated with the access key. This is essentially the "password" for the access key. | yes |
-| AWS_DEFAULT_REGION | Specifies the AWS Region to send the request to. | yes |
+## What changed since 2023
-### Environment secrets
+| Then | Now | Why |
+|------|-----|-----|
+| IAM user access keys as repository secrets | GitHub OIDC, one role per environment (`bootstrap/`) | No long-lived credential to leak or rotate. |
+| DynamoDB lock table | `use_lockfile = true` | Native S3 locking since Terraform 1.11; one resource fewer. |
+| Default `env:/` workspace prefix | `workspace_key_prefix = "workspaces"` | Lets IAM scope each environment to its own state. |
+| Modules from GitHub `master`, provider `>= 4.5` | Registry modules at exact versions, provider pinned, lock file committed | The same commit plans the same way next year. |
+| `http_tcp_listeners`, `target_group_arns`, `computed_ingress_*` | `listeners`/`target_groups` maps, `traffic_source_attachments`, structured rules | Inputs renamed in alb v9, autoscaling v8, security-group v6. |
+| Amazon Linux 2, metadata without a token | Amazon Linux 2023, IMDSv2 | AL2 support ended 2026-06-30; the autoscaling module enforces IMDSv2. |
+| Actions by tag or `@master`, `owner/repo@main` | Actions by commit SHA, reusable workflow by local path | Tags can be moved (trivy-action's were, in March 2026); a local path runs the commit being tested. |
+| Default token permissions | `permissions: {}` and per-job grants | Least privilege. |
+| Plan and apply in one job, plan named `.txt` | Plan job → `tfplan` artifact → apply job | The apply runs exactly what was shown. |
+| No pull-request checks | fmt, validate, Trivy with SARIF | Problems surface before merge. |
-| Secret name | Desctiption | Required |
-|-------------|-------------|----------|
-| TARGET_ACCOUNT | Target AWS account ID. | yes |
-| TARGET_REGION | Target AWS region name. | yes |
-| TARGET_ROLE | Target AM role to be assumed. | yes |
+Pinned versions: Terraform 1.17.1, AWS provider 6.71.0; modules vpc 6.7.3, s3-bucket 5.17.0, security-group 6.0.0, alb 10.5.1, autoscaling 9.3.2; actions checkout v7.0.1, configure-aws-credentials v6.3.0, setup-terraform v4.0.1, trivy-action v0.36.0 (Trivy v0.76.1), upload-artifact v7.0.2, download-artifact v8.0.2, codeql-action v4.39.1.
-----
@@ -44,9 +134,9 @@ This project demonstrates the potential of reusable workflows using GitHub Actio
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|----------|
-| target_account | Target AWS account ID. | `string` | `null` | yes (auto-filled) |
-| target_region | Target AWS region name. | `string` | `null` | yes (auto-filled) |
-| target_role | Target AWS IAM role name to be assumed. | `string` | `null` | yes (auto-filled) |
+| target_account | Target AWS account ID. | `string` | `null` | yes (from the environment's `TARGET_ACCOUNT`) |
+| target_region | Target AWS region name. | `string` | `null` | yes (from `TARGET_REGION`) |
+| target_role | Target AWS IAM role name to be assumed. | `string` | `null` | yes (from `TARGET_ROLE`) |
| project | Project name. | `string` | `null` | yes |
| tags | A map of tags to assign to the resources. | `map(string)` | `{}` | no |
@@ -56,9 +146,9 @@ This project demonstrates the potential of reusable workflows using GitHub Actio
|------|-------------|------|---------|----------|
| vpc_cidr | The IPv4 CIDR block for the VPC. | `string` | `null` | yes |
| vpc_azs | A list of availability zones names or IDs in the target region. | `list(string)` | `[]` | no |
-| vpc_public_subnets | A list of public subnets CIDR bloks inside the VPC. | `list(string)` | `[]` | no |
-| vpc_private_subnets | A list of private subnets CIDR bloks inside the VPC. | `list(string)` | `[]` | no |
-| vpc_database_subnets | A list of database subnets CIDR bloks inside the VPC. | `list(string)` | `[]` | no |
+| vpc_public_subnets | A list of public subnets CIDR blocks inside the VPC. | `list(string)` | `[]` | no |
+| vpc_private_subnets | A list of private subnets CIDR blocks inside the VPC. | `list(string)` | `[]` | no |
+| vpc_database_subnets | A list of database subnets CIDR blocks inside the VPC. | `list(string)` | `[]` | no |
| vpc_enable_nat_gateway | Should be true if you want to provision NAT Gateways for each of your private networks. | `bool` | `false` | no |
| vpc_single_nat_gateway | Should be true if you want to provision a single shared NAT Gateway across all of your private networks. | `bool` | `false` | no |
| vpc_one_nat_gateway_per_az | Should be true if you want only one NAT Gateway per availability zone. Requires the number of `vpc_public_subnets` created to be greater than or equal to the number of availability zones. | `bool` | `false` | no |
@@ -73,7 +163,7 @@ This project demonstrates the potential of reusable workflows using GitHub Actio
| vpc_flow_log_destination_arn | The ARN of S3 bucket where VPC Flow Logs will be pushed. If this ARN is a S3 bucket the appropriate permissions need to be set on that bucket's policy. | `string` | `null` | no |
| vpc_flow_log_log_format | The fields to include in the flow log record, in the order in which they should appear. | `string` | `null` | no |
| vpc_flow_log_traffic_type | The type of traffic to capture. Valid values: ACCEPT, REJECT, ALL. | `string` | `"ALL"` | no |
-| flow_log_max_aggregation_interval | The maximum interval of time during which a flow of packets is captured and aggregated into a flow log record. Valid Values: `60` seconds or `600` seconds. | `number` | `600` | no |
+| vpc_flow_log_max_aggregation_interval | The maximum interval of time during which a flow of packets is captured and aggregated into a flow log record. Valid Values: `60` seconds or `600` seconds. | `number` | `600` | no |
### EC2 stack inputs
@@ -81,6 +171,7 @@ This project demonstrates the potential of reusable workflows using GitHub Actio
|------|-------------|------|---------|----------|
| alb_load_balancer_type | The type of load balancer to create. Possible values are application or network. **Please note: Only `"application"` loadbalancer type can be used within this project!** | `string` | `"application"` | no |
| alb_internal | Boolean determining if the load balancer is internal or externally facing. | `bool` | `false` | no |
+| alb_enable_deletion_protection | Whether deletion of the load balancer is disabled. The ALB module defaults to `true` since v9; `false` keeps destroy-all working. | `bool` | `false` | no |
| alb_idle_timeout | The time in seconds that the connection is allowed to be idle. | `number` | `60` | no |
| alb_load_balancer_create_timeout | Timeout value when creating the ALB. | `string` | `"10m"` | no |
| alb_load_balancer_update_timeout | Timeout value when updating the ALB. | `string` | `"10m"` | no |
@@ -92,11 +183,11 @@ This project demonstrates the potential of reusable workflows using GitHub Actio
| autoscaling_desired_capacity | The number of Amazon EC2 instances that should be running in the autoscaling group. | `number` | `null` | no |
| autoscaling_wait_for_capacity_timeout | A maximum duration that Terraform should wait for ASG instances to be healthy before timing out. | `string` | `"10m"` | no |
| autoscaling_default_instance_warmup | Amount of time, in seconds, until a newly launched instance can contribute to the Amazon CloudWatch metrics. This delay lets an instance finish initializing before Amazon EC2 Auto Scaling aggregates instance metrics, resulting in more reliable usage data. Set this value equal to the amount of time that it takes for resource consumption to become stable after an instance reaches the InService state. | `number` | `null` | no |
-| autoscaling_update_default_version | Whether to update Default Version each update. **Please note: Only the `true` value available value within this project!** | `bool` | `true` | no |
+| autoscaling_update_default_version | Whether to update Default Version each update. **Please note: only `true` is supported within this project.** | `bool` | `true` | no |
| autoscaling_instance_type | The type of the instance. | `string` | `null` | no |
| autoscaling_ebs_optimized | If true, the launched EC2 instance will be EBS-optimized. | `bool` | `null` | no |
| autoscaling_enable_monitoring | Enables/disables detailed monitoring. | `bool` | `true` | no |
-| autoscaling_create_iam_instance_profile | Determines whether an IAM instance profile is created or to use an existing IAM instance profile. **Please note: Only the `true` value available value within this project!** | `bool` | `true` | no |
+| autoscaling_create_iam_instance_profile | Determines whether an IAM instance profile is created or to use an existing IAM instance profile. **Please note: only `true` is supported within this project.** | `bool` | `true` | no |
| autoscaling_delete_on_termination | Whether the volume should be destroyed on instance termination. | `bool` | `true` | no |
| autoscaling_encrypted | Enables EBS encryption on the volume. | `bool` | `true` | no |
| autoscaling_volume_size | The size of the root volume in gigabytes. | `number` | `null` | no |
diff --git a/bootstrap/management/.terraform.lock.hcl b/bootstrap/management/.terraform.lock.hcl
new file mode 100644
index 0000000..6f34d54
--- /dev/null
+++ b/bootstrap/management/.terraform.lock.hcl
@@ -0,0 +1,26 @@
+# This file is maintained automatically by "terraform init".
+# Manual edits may be lost in future updates.
+
+provider "registry.terraform.io/hashicorp/aws" {
+ version = "6.71.0"
+ constraints = "6.71.0"
+ hashes = [
+ "h1:sZCupscga0Grk22+04FNY1RYDeOcVbCTd6ff8+hsF94=",
+ "zh:09aa14408c5ab26915fb2a469b5eb67c06393a2054d78592ba4c32f7a882e918",
+ "zh:1d1138128dc4866f6c16b1ef3b871b3fd9a3dc648e9056e06e210d1817aa052b",
+ "zh:2c17a79821a0e06fbf1834a12e92227db29c2001fef7ef0b347af2e6dad83f14",
+ "zh:55616730a485c9c7283546a84f1ac5f8341152c5572338a54fc7af5f69ebfb92",
+ "zh:7e6461ec2976b5961a1d4f3c08c68fae0645d7f5e0a404f7647baa3b88d337d8",
+ "zh:7ec935166b2251d075fea980012f5267078785399585e8b443dca484fc57df0f",
+ "zh:885b3290cd48738cbf9ee8a6d47a4452889843f8f53b8cf67fc8c9b16e553721",
+ "zh:8930fb33c6c965f7cc82230cb7fbb0cfff9bb30becdfce5e0c2242512d849894",
+ "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425",
+ "zh:a702f9a4dc7340182a8daf5b6ee562f14657fe14752f7d54dc799cf2fd91a868",
+ "zh:b624cb320d787fbfa6dc94be5a1fd52683d1ebd11b42646262b221a694e6ffb8",
+ "zh:bcbaded68b553afeea93604ec22c254b98f3951862bdeb68ae89c179c7dda117",
+ "zh:cb57bbcaf0fa3a70ea29b60d183d3a0dd020f041b18da5d1725d2acd17fa7d59",
+ "zh:d4ca164b1b6039557d8f5f5f911c4aaad1a187e9b3758f6885c95f8954f7de58",
+ "zh:eb0c705337cef7b96086e8b84702ba965eeeace7a4aa3235b2c8aad5f50f2fca",
+ "zh:f2787f40ce873d8c1308c92adec9b6adbd223506fe9d79bba447729e422f9146",
+ ]
+}
diff --git a/bootstrap/management/data.tf b/bootstrap/management/data.tf
new file mode 100644
index 0000000..4f27e13
--- /dev/null
+++ b/bootstrap/management/data.tf
@@ -0,0 +1,81 @@
+# DATA
+
+data "aws_partition" "current" {}
+
+data "aws_iam_openid_connect_provider" "github" {
+ count = var.create_oidc_provider ? 0 : 1
+ url = "https://token.actions.githubusercontent.com"
+}
+
+# Only a job running in GitHub environment of this repository gets 's role.
+data "aws_iam_policy_document" "github_trust" {
+ for_each = var.environments
+ statement {
+ actions = ["sts:AssumeRoleWithWebIdentity"]
+ principals {
+ type = "Federated"
+ identifiers = [local.oidc_provider_arn]
+ }
+ condition {
+ test = "StringEquals"
+ variable = "token.actions.githubusercontent.com:aud"
+ values = ["sts.amazonaws.com"]
+ }
+ condition {
+ test = "StringEquals"
+ variable = "token.actions.githubusercontent.com:sub"
+ values = ["${var.github_subject_prefix}:environment:${each.key}"]
+ }
+ }
+}
+
+data "aws_iam_policy_document" "environment" {
+ for_each = var.environments
+
+ # Listing shows key names of every environment, never their contents. The S3 backend lists
+ # to find workspaces, and init reads the default workspace's (absent) state, which S3
+ # answers with 404 only to a caller allowed to list.
+ statement {
+ sid = "ListStateBucket"
+ actions = ["s3:ListBucket"]
+ resources = [aws_s3_bucket.state.arn]
+ }
+ statement {
+ sid = "ReadWriteOwnWorkspaceState"
+ actions = ["s3:GetObject", "s3:PutObject"]
+ resources = ["${aws_s3_bucket.state.arn}/${var.workspace_key_prefix}/${each.key}/*"]
+ }
+ statement {
+ sid = "ReleaseOwnLocks"
+ actions = ["s3:DeleteObject"]
+ resources = ["${aws_s3_bucket.state.arn}/${var.workspace_key_prefix}/${each.key}/*.tflock"]
+ }
+ statement {
+ sid = "ReadDefaultWorkspaceKeys"
+ actions = ["s3:GetObject"]
+ resources = [for stack in var.stacks : "${aws_s3_bucket.state.arn}/${stack}/terraform.tfstate"]
+ }
+ statement {
+ sid = "AssumeOwnTargetRole"
+ actions = ["sts:AssumeRole"]
+ resources = ["arn:${data.aws_partition.current.partition}:iam::${each.value.target_account_id}:role/${var.target_role_name}"]
+ }
+}
+
+data "aws_iam_policy_document" "state_bucket" {
+ statement {
+ sid = "DenyInsecureTransport"
+ effect = "Deny"
+ actions = ["s3:*"]
+ principals {
+ type = "*"
+ identifiers = ["*"]
+ }
+ resources = [aws_s3_bucket.state.arn, "${aws_s3_bucket.state.arn}/*"]
+ condition {
+ test = "Bool"
+ variable = "aws:SecureTransport"
+ values = ["false"]
+ }
+ }
+}
diff --git a/bootstrap/management/main.tf b/bootstrap/management/main.tf
new file mode 100644
index 0000000..a128437
--- /dev/null
+++ b/bootstrap/management/main.tf
@@ -0,0 +1,95 @@
+# LOCALS
+
+locals {
+ oidc_provider_arn = var.create_oidc_provider ? aws_iam_openid_connect_provider.github[0].arn : data.aws_iam_openid_connect_provider.github[0].arn
+}
+
+# GITHUB OIDC
+
+# AWS trusts GitHub's OIDC certificate chain itself, so no thumbprint is pinned.
+resource "aws_iam_openid_connect_provider" "github" {
+ count = var.create_oidc_provider ? 1 : 0
+ url = "https://token.actions.githubusercontent.com"
+ client_id_list = ["sts.amazonaws.com"]
+ tags = var.tags
+}
+
+resource "aws_iam_role" "environment" {
+ for_each = var.environments
+ name = "github-terraform-${each.key}"
+ description = "GitHub Actions, environment ${each.key}: own state and own target role only."
+ assume_role_policy = data.aws_iam_policy_document.github_trust[each.key].json
+ max_session_duration = 3600
+ tags = var.tags
+}
+
+resource "aws_iam_role_policy" "environment" {
+ for_each = var.environments
+ name = "state-and-target"
+ role = aws_iam_role.environment[each.key].id
+ policy = data.aws_iam_policy_document.environment[each.key].json
+}
+
+# STATE BUCKET
+
+resource "aws_s3_bucket" "state" {
+ bucket = var.state_bucket_name
+ tags = var.tags
+ lifecycle {
+ prevent_destroy = true
+ }
+}
+
+resource "aws_s3_bucket_versioning" "state" {
+ bucket = aws_s3_bucket.state.id
+ versioning_configuration {
+ status = "Enabled"
+ }
+}
+
+# SSE-S3 keeps the setup free of a key policy; access is already limited per environment by IAM.
+#trivy:ignore:AWS-0132
+resource "aws_s3_bucket_server_side_encryption_configuration" "state" {
+ bucket = aws_s3_bucket.state.id
+ rule {
+ apply_server_side_encryption_by_default {
+ sse_algorithm = "AES256"
+ }
+ }
+}
+
+resource "aws_s3_bucket_public_access_block" "state" {
+ bucket = aws_s3_bucket.state.id
+ block_public_acls = true
+ block_public_policy = true
+ ignore_public_acls = true
+ restrict_public_buckets = true
+}
+
+resource "aws_s3_bucket_ownership_controls" "state" {
+ bucket = aws_s3_bucket.state.id
+ rule {
+ object_ownership = "BucketOwnerEnforced"
+ }
+}
+
+resource "aws_s3_bucket_policy" "state" {
+ bucket = aws_s3_bucket.state.id
+ policy = data.aws_iam_policy_document.state_bucket.json
+}
+
+# Every lock leaves a noncurrent version and a delete marker behind, as does every state write.
+resource "aws_s3_bucket_lifecycle_configuration" "state" {
+ bucket = aws_s3_bucket.state.id
+ rule {
+ id = "noncurrent-versions"
+ status = "Enabled"
+ filter {}
+ noncurrent_version_expiration {
+ noncurrent_days = 90
+ }
+ expiration {
+ expired_object_delete_marker = true
+ }
+ }
+}
diff --git a/bootstrap/management/outputs.tf b/bootstrap/management/outputs.tf
new file mode 100644
index 0000000..10fbcc0
--- /dev/null
+++ b/bootstrap/management/outputs.tf
@@ -0,0 +1,16 @@
+# OUTPUTS
+
+output "environment_role_arns" {
+ description = "Per GitHub environment: the value of its AWS_ROLE_ARN variable."
+ value = { for env, role in aws_iam_role.environment : env => role.arn }
+}
+
+output "state_bucket" {
+ description = "The state bucket name."
+ value = aws_s3_bucket.state.id
+}
+
+output "oidc_provider_arn" {
+ description = "The GitHub OIDC provider ARN."
+ value = local.oidc_provider_arn
+}
diff --git a/bootstrap/management/providers.tf b/bootstrap/management/providers.tf
new file mode 100644
index 0000000..8dc6da7
--- /dev/null
+++ b/bootstrap/management/providers.tf
@@ -0,0 +1,7 @@
+# PROVIDERS
+
+# Applied once by a person with administrator credentials for this account.
+# State stays local: this stack creates what the remote state needs.
+provider "aws" {
+ region = var.region
+}
diff --git a/bootstrap/management/terraform.tfvars.example b/bootstrap/management/terraform.tfvars.example
new file mode 100644
index 0000000..a25d64b
--- /dev/null
+++ b/bootstrap/management/terraform.tfvars.example
@@ -0,0 +1,7 @@
+# Copy to terraform.tfvars and put in your account IDs.
+# One account per environment is what keeps them apart; see README, "Workspaces and credentials".
+environments = {
+ dev = { target_account_id = "111111111111" }
+ stag = { target_account_id = "222222222222" }
+ prod = { target_account_id = "333333333333" }
+}
diff --git a/bootstrap/management/variables.tf b/bootstrap/management/variables.tf
new file mode 100644
index 0000000..e0b89fc
--- /dev/null
+++ b/bootstrap/management/variables.tf
@@ -0,0 +1,59 @@
+# VARIABLES
+
+variable "region" {
+ description = "Region of the state bucket. Must match `region` in main/*/backend.tf."
+ type = string
+ default = "eu-north-1"
+}
+
+variable "state_bucket_name" {
+ description = "Name of the Terraform state bucket. Must match `bucket` in main/*/backend.tf."
+ type = string
+ default = "multi-environment-infrastructure-provisioning-state-bucket"
+}
+
+variable "workspace_key_prefix" {
+ description = "Must match `workspace_key_prefix` in main/*/backend.tf."
+ type = string
+ default = "workspaces"
+}
+
+variable "stacks" {
+ description = "Stack directories under main/. Their backend `key` is `/terraform.tfstate`."
+ type = list(string)
+ default = ["vpc", "ec2"]
+}
+
+variable "github_subject_prefix" {
+ description = "The `sub` claim prefix GitHub issues for the repository: `gh api repos///actions/oidc/customization/sub` prints it as `sub_claim_prefix`."
+ type = string
+ default = "repo:bombsdontbite/terraform-workspaces-github-actions"
+}
+
+variable "environments" {
+ description = "GitHub environment name => the AWS account its resources live in."
+ type = map(object({
+ target_account_id = string
+ }))
+}
+
+variable "target_role_name" {
+ description = "Name of the role bootstrap/target creates in each target account."
+ type = string
+ default = "terraform-target"
+}
+
+variable "create_oidc_provider" {
+ description = "An account holds one GitHub OIDC provider; set false if it already exists."
+ type = bool
+ default = true
+}
+
+variable "tags" {
+ description = "Tags for every resource."
+ type = map(string)
+ default = {
+ "Provisioner" = "Terraform"
+ "Stack" = "bootstrap/management"
+ }
+}
diff --git a/bootstrap/management/versions.tf b/bootstrap/management/versions.tf
new file mode 100644
index 0000000..c58b7ce
--- /dev/null
+++ b/bootstrap/management/versions.tf
@@ -0,0 +1,11 @@
+# VERSIONS
+
+terraform {
+ required_version = "~> 1.17"
+ required_providers {
+ aws = {
+ source = "hashicorp/aws"
+ version = "6.71.0"
+ }
+ }
+}
diff --git a/bootstrap/target/.terraform.lock.hcl b/bootstrap/target/.terraform.lock.hcl
new file mode 100644
index 0000000..6f34d54
--- /dev/null
+++ b/bootstrap/target/.terraform.lock.hcl
@@ -0,0 +1,26 @@
+# This file is maintained automatically by "terraform init".
+# Manual edits may be lost in future updates.
+
+provider "registry.terraform.io/hashicorp/aws" {
+ version = "6.71.0"
+ constraints = "6.71.0"
+ hashes = [
+ "h1:sZCupscga0Grk22+04FNY1RYDeOcVbCTd6ff8+hsF94=",
+ "zh:09aa14408c5ab26915fb2a469b5eb67c06393a2054d78592ba4c32f7a882e918",
+ "zh:1d1138128dc4866f6c16b1ef3b871b3fd9a3dc648e9056e06e210d1817aa052b",
+ "zh:2c17a79821a0e06fbf1834a12e92227db29c2001fef7ef0b347af2e6dad83f14",
+ "zh:55616730a485c9c7283546a84f1ac5f8341152c5572338a54fc7af5f69ebfb92",
+ "zh:7e6461ec2976b5961a1d4f3c08c68fae0645d7f5e0a404f7647baa3b88d337d8",
+ "zh:7ec935166b2251d075fea980012f5267078785399585e8b443dca484fc57df0f",
+ "zh:885b3290cd48738cbf9ee8a6d47a4452889843f8f53b8cf67fc8c9b16e553721",
+ "zh:8930fb33c6c965f7cc82230cb7fbb0cfff9bb30becdfce5e0c2242512d849894",
+ "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425",
+ "zh:a702f9a4dc7340182a8daf5b6ee562f14657fe14752f7d54dc799cf2fd91a868",
+ "zh:b624cb320d787fbfa6dc94be5a1fd52683d1ebd11b42646262b221a694e6ffb8",
+ "zh:bcbaded68b553afeea93604ec22c254b98f3951862bdeb68ae89c179c7dda117",
+ "zh:cb57bbcaf0fa3a70ea29b60d183d3a0dd020f041b18da5d1725d2acd17fa7d59",
+ "zh:d4ca164b1b6039557d8f5f5f911c4aaad1a187e9b3758f6885c95f8954f7de58",
+ "zh:eb0c705337cef7b96086e8b84702ba965eeeace7a4aa3235b2c8aad5f50f2fca",
+ "zh:f2787f40ce873d8c1308c92adec9b6adbd223506fe9d79bba447729e422f9146",
+ ]
+}
diff --git a/bootstrap/target/data.tf b/bootstrap/target/data.tf
new file mode 100644
index 0000000..c9aa21a
--- /dev/null
+++ b/bootstrap/target/data.tf
@@ -0,0 +1,69 @@
+# DATA
+
+data "aws_caller_identity" "current" {}
+
+data "aws_partition" "current" {}
+
+# Role chaining: the management role an environment's workflow assumed through OIDC.
+data "aws_iam_policy_document" "trust" {
+ statement {
+ actions = ["sts:AssumeRole"]
+ principals {
+ type = "AWS"
+ identifiers = var.management_role_arns
+ }
+ }
+}
+
+# The IAM the stacks create: the web servers' role and instance profile under /ec2/,
+# and the Auto Scaling service-linked role.
+data "aws_iam_policy_document" "stack_iam" {
+ statement {
+ sid = "WebServerRole"
+ actions = [
+ "iam:AddRoleToInstanceProfile",
+ "iam:AttachRolePolicy",
+ "iam:CreateInstanceProfile",
+ "iam:CreateRole",
+ "iam:DeleteInstanceProfile",
+ "iam:DeleteRole",
+ "iam:DetachRolePolicy",
+ "iam:GetInstanceProfile",
+ "iam:GetRole",
+ "iam:ListAttachedRolePolicies",
+ "iam:ListInstanceProfilesForRole",
+ "iam:ListRolePolicies",
+ "iam:RemoveRoleFromInstanceProfile",
+ "iam:TagInstanceProfile",
+ "iam:TagRole",
+ "iam:UntagInstanceProfile",
+ "iam:UntagRole",
+ "iam:UpdateRoleDescription",
+ ]
+ resources = [
+ "arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:role/ec2/*",
+ "arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:instance-profile/ec2/*",
+ ]
+ }
+ statement {
+ sid = "PassWebServerRoleToEc2"
+ actions = ["iam:PassRole"]
+ resources = ["arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:role/ec2/*"]
+ condition {
+ test = "StringEquals"
+ variable = "iam:PassedToService"
+ values = ["ec2.amazonaws.com"]
+ }
+ }
+ statement {
+ sid = "AutoScalingServiceLinkedRole"
+ actions = [
+ "iam:CreateServiceLinkedRole",
+ "iam:DeleteServiceLinkedRole",
+ "iam:GetRole",
+ "iam:GetServiceLinkedRoleDeletionStatus",
+ "iam:PassRole",
+ ]
+ resources = ["arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling_*"]
+ }
+}
diff --git a/bootstrap/target/main.tf b/bootstrap/target/main.tf
new file mode 100644
index 0000000..ee545e4
--- /dev/null
+++ b/bootstrap/target/main.tf
@@ -0,0 +1,21 @@
+# TARGET ROLE
+
+resource "aws_iam_role" "target" {
+ name = var.role_name
+ description = "Terraform in this account, reached by role chaining from the management account."
+ assume_role_policy = data.aws_iam_policy_document.trust.json
+ max_session_duration = 3600
+ tags = var.tags
+}
+
+resource "aws_iam_role_policy_attachment" "managed" {
+ for_each = toset(var.managed_policy_arns)
+ role = aws_iam_role.target.name
+ policy_arn = each.value
+}
+
+resource "aws_iam_role_policy" "stack_iam" {
+ name = "stack-iam"
+ role = aws_iam_role.target.id
+ policy = data.aws_iam_policy_document.stack_iam.json
+}
diff --git a/bootstrap/target/outputs.tf b/bootstrap/target/outputs.tf
new file mode 100644
index 0000000..c57d075
--- /dev/null
+++ b/bootstrap/target/outputs.tf
@@ -0,0 +1,6 @@
+# OUTPUTS
+
+output "role_arn" {
+ description = "The role Terraform assumes in this account."
+ value = aws_iam_role.target.arn
+}
diff --git a/bootstrap/target/providers.tf b/bootstrap/target/providers.tf
new file mode 100644
index 0000000..8dc6da7
--- /dev/null
+++ b/bootstrap/target/providers.tf
@@ -0,0 +1,7 @@
+# PROVIDERS
+
+# Applied once by a person with administrator credentials for this account.
+# State stays local: this stack creates what the remote state needs.
+provider "aws" {
+ region = var.region
+}
diff --git a/bootstrap/target/terraform.tfvars.example b/bootstrap/target/terraform.tfvars.example
new file mode 100644
index 0000000..088ea47
--- /dev/null
+++ b/bootstrap/target/terraform.tfvars.example
@@ -0,0 +1,4 @@
+# Copy to terraform.tfvars. List every environment that deploys into this account.
+management_role_arns = [
+ "arn:aws:iam::000000000000:role/github-terraform-dev",
+]
diff --git a/bootstrap/target/variables.tf b/bootstrap/target/variables.tf
new file mode 100644
index 0000000..d57359a
--- /dev/null
+++ b/bootstrap/target/variables.tf
@@ -0,0 +1,33 @@
+# VARIABLES
+
+variable "region" {
+ description = "Any region: IAM is global, the provider just needs one."
+ type = string
+ default = "eu-north-1"
+}
+
+variable "management_role_arns" {
+ description = "The management-account roles (bootstrap/management output `environment_role_arns`) of the environments that deploy into this account."
+ type = list(string)
+}
+
+variable "role_name" {
+ description = "Name of the role Terraform assumes here; the TARGET_ROLE variable of the environment."
+ type = string
+ default = "terraform-target"
+}
+
+variable "managed_policy_arns" {
+ description = "AWS managed policies for the role. PowerUserAccess covers everything but IAM; the IAM the stacks need is granted below."
+ type = list(string)
+ default = ["arn:aws:iam::aws:policy/PowerUserAccess"]
+}
+
+variable "tags" {
+ description = "Tags for every resource."
+ type = map(string)
+ default = {
+ "Provisioner" = "Terraform"
+ "Stack" = "bootstrap/target"
+ }
+}
diff --git a/bootstrap/target/versions.tf b/bootstrap/target/versions.tf
new file mode 100644
index 0000000..c58b7ce
--- /dev/null
+++ b/bootstrap/target/versions.tf
@@ -0,0 +1,11 @@
+# VERSIONS
+
+terraform {
+ required_version = "~> 1.17"
+ required_providers {
+ aws = {
+ source = "hashicorp/aws"
+ version = "6.71.0"
+ }
+ }
+}
diff --git a/inputs/dev.tfvars b/inputs/dev.tfvars
index 7f35361..709e9f0 100644
--- a/inputs/dev.tfvars
+++ b/inputs/dev.tfvars
@@ -25,5 +25,5 @@ autoscaling_default_instance_warmup = 300
autoscaling_instance_type = "t3.micro"
autoscaling_ebs_optimized = true
autoscaling_enable_monitoring = false
-autoscaling_volume_size = 8
+autoscaling_volume_size = 8
autoscaling_volume_type = "gp2"
diff --git a/inputs/stag.tfvars b/inputs/stag.tfvars
index c6d9247..7fbd71b 100644
--- a/inputs/stag.tfvars
+++ b/inputs/stag.tfvars
@@ -26,5 +26,5 @@ autoscaling_default_instance_warmup = 300
autoscaling_instance_type = "t3.small"
autoscaling_ebs_optimized = true
autoscaling_enable_monitoring = false
-autoscaling_volume_size = 8
+autoscaling_volume_size = 8
autoscaling_volume_type = "gp2"
diff --git a/main/ec2/.terraform.lock.hcl b/main/ec2/.terraform.lock.hcl
new file mode 100644
index 0000000..15c8e43
--- /dev/null
+++ b/main/ec2/.terraform.lock.hcl
@@ -0,0 +1,26 @@
+# This file is maintained automatically by "terraform init".
+# Manual edits may be lost in future updates.
+
+provider "registry.terraform.io/hashicorp/aws" {
+ version = "6.71.0"
+ constraints = ">= 6.28.0, >= 6.29.0, >= 6.56.0, 6.71.0"
+ hashes = [
+ "h1:sZCupscga0Grk22+04FNY1RYDeOcVbCTd6ff8+hsF94=",
+ "zh:09aa14408c5ab26915fb2a469b5eb67c06393a2054d78592ba4c32f7a882e918",
+ "zh:1d1138128dc4866f6c16b1ef3b871b3fd9a3dc648e9056e06e210d1817aa052b",
+ "zh:2c17a79821a0e06fbf1834a12e92227db29c2001fef7ef0b347af2e6dad83f14",
+ "zh:55616730a485c9c7283546a84f1ac5f8341152c5572338a54fc7af5f69ebfb92",
+ "zh:7e6461ec2976b5961a1d4f3c08c68fae0645d7f5e0a404f7647baa3b88d337d8",
+ "zh:7ec935166b2251d075fea980012f5267078785399585e8b443dca484fc57df0f",
+ "zh:885b3290cd48738cbf9ee8a6d47a4452889843f8f53b8cf67fc8c9b16e553721",
+ "zh:8930fb33c6c965f7cc82230cb7fbb0cfff9bb30becdfce5e0c2242512d849894",
+ "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425",
+ "zh:a702f9a4dc7340182a8daf5b6ee562f14657fe14752f7d54dc799cf2fd91a868",
+ "zh:b624cb320d787fbfa6dc94be5a1fd52683d1ebd11b42646262b221a694e6ffb8",
+ "zh:bcbaded68b553afeea93604ec22c254b98f3951862bdeb68ae89c179c7dda117",
+ "zh:cb57bbcaf0fa3a70ea29b60d183d3a0dd020f041b18da5d1725d2acd17fa7d59",
+ "zh:d4ca164b1b6039557d8f5f5f911c4aaad1a187e9b3758f6885c95f8954f7de58",
+ "zh:eb0c705337cef7b96086e8b84702ba965eeeace7a4aa3235b2c8aad5f50f2fca",
+ "zh:f2787f40ce873d8c1308c92adec9b6adbd223506fe9d79bba447729e422f9146",
+ ]
+}
diff --git a/main/ec2/backend.tf b/main/ec2/backend.tf
index a3ebed4..99c596d 100644
--- a/main/ec2/backend.tf
+++ b/main/ec2/backend.tf
@@ -2,10 +2,13 @@
terraform {
backend "s3" {
- bucket = "multi-environment-infrastructure-provisioning-state-bucket"
- key = "ec2/terraform.tfstate"
- region = "eu-north-1"
- dynamodb_table = "multi-environment-infrastructure-provisioning-locktable"
- encrypt = true
+ bucket = "multi-environment-infrastructure-provisioning-state-bucket"
+ key = "ec2/terraform.tfstate"
+ # Each workspace's state lives at workspaces//ec2/terraform.tfstate,
+ # so an environment's role can be limited to its own prefix.
+ workspace_key_prefix = "workspaces"
+ region = "eu-north-1" # The state bucket's region, not the target environment's.
+ use_lockfile = true # Native S3 locking (Terraform >= 1.11): no DynamoDB table.
+ encrypt = true
}
}
diff --git a/main/ec2/data.tf b/main/ec2/data.tf
index 2ddfa48..752b351 100644
--- a/main/ec2/data.tf
+++ b/main/ec2/data.tf
@@ -4,18 +4,24 @@ data "terraform_remote_state" "vpc" {
backend = "s3"
workspace = terraform.workspace
config = {
- bucket = "multi-environment-infrastructure-provisioning-state-bucket"
- key = "vpc/terraform.tfstate"
- region = "eu-north-1"
+ bucket = "multi-environment-infrastructure-provisioning-state-bucket"
+ key = "vpc/terraform.tfstate"
+ workspace_key_prefix = "workspaces"
+ region = "eu-north-1" # The state bucket's region.
}
}
+# Amazon Linux 2 reached end of support on 2026-06-30; Amazon Linux 2023 replaces it.
data "aws_ami" "amazon_linux" {
provider = aws.target
most_recent = true
owners = ["amazon"]
filter {
name = "name"
- values = ["amzn2-ami-hvm*"]
+ values = ["al2023-ami-2023.*-x86_64"]
+ }
+ filter {
+ name = "architecture"
+ values = ["x86_64"]
}
}
diff --git a/main/ec2/main.tf b/main/ec2/main.tf
index a5a36ac..77e8b09 100644
--- a/main/ec2/main.tf
+++ b/main/ec2/main.tf
@@ -2,77 +2,113 @@
locals {
tags = merge(var.tags, { "Project" = var.project, "Environment" = terraform.workspace })
+ vpc = data.terraform_remote_state.vpc.outputs
}
# MODULES & RESOURCES
module "alb_security_group" {
- source = "github.com/terraform-aws-modules/terraform-aws-security-group/modules/http-80"
+ source = "terraform-aws-modules/security-group/aws//modules/http-80"
+ version = "6.0.0"
providers = {
aws = aws.target
}
- name = "${terraform.workspace}-${var.project}-alb"
- use_name_prefix = false
- vpc_id = data.terraform_remote_state.vpc.outputs.vpc_id
- ingress_cidr_blocks = ["0.0.0.0/0"]
- tags = local.tags
+ name = "${terraform.workspace}-${var.project}-alb"
+ use_name_prefix = false
+ vpc_id = local.vpc.vpc_id
+ ingress_cidr_ipv4 = {
+ internet = "0.0.0.0/0"
+ }
+ # Security group module v6 no longer adds an implicit allow-all egress rule.
+ egress_rules = {
+ http-to-vpc = {
+ description = "HTTP to the targets and their health checks"
+ from_port = 80
+ ip_protocol = "tcp"
+ cidr_ipv4 = local.vpc.vpc_cidr_block
+ }
+ }
+ tags = local.tags
}
+# Package repositories and SSM have no fixed addresses: egress is HTTPS only, through the NAT gateway.
+#trivy:ignore:AWS-0104
module "autoscaling_security_group" {
- source = "github.com/terraform-aws-modules/terraform-aws-security-group/"
+ source = "terraform-aws-modules/security-group/aws"
+ version = "6.0.0"
providers = {
aws = aws.target
}
name = "${terraform.workspace}-${var.project}-autoscaling"
use_name_prefix = false
- vpc_id = data.terraform_remote_state.vpc.outputs.vpc_id
- computed_ingress_with_source_security_group_id = [
- {
- rule = "http-80-tcp"
- source_security_group_id = module.alb_security_group.security_group_id
+ vpc_id = local.vpc.vpc_id
+ ingress_rules = {
+ http-from-alb = {
+ description = "HTTP from the load balancer"
+ from_port = 80
+ ip_protocol = "tcp"
+ referenced_security_group_id = module.alb_security_group.id
}
- ]
- number_of_computed_ingress_with_source_security_group_id = 1
- egress_rules = ["all-all"]
- tags = local.tags
+ }
+ egress_rules = {
+ https = {
+ description = "Package updates and SSM through the NAT gateway"
+ from_port = 443
+ ip_protocol = "tcp"
+ cidr_ipv4 = "0.0.0.0/0"
+ }
+ }
+ tags = local.tags
}
+# A public HTTP endpoint is the demo; HTTPS needs a domain and an ACM certificate.
+#trivy:ignore:AWS-0053
+#trivy:ignore:AWS-0054
module "alb" {
- source = "github.com/terraform-aws-modules/terraform-aws-alb"
+ source = "terraform-aws-modules/alb/aws"
+ version = "10.5.1"
providers = {
aws = aws.target
}
- name = "${terraform.workspace}-${var.project}-lb"
- load_balancer_type = var.alb_load_balancer_type # Only application loadbalancer type can be used within this project.
- internal = var.alb_internal
- vpc_id = data.terraform_remote_state.vpc.outputs.vpc_id
- subnets = data.terraform_remote_state.vpc.outputs.public_subnets
- security_groups = [module.alb_security_group.security_group_id]
- create_security_group = false
- http_tcp_listeners = [
- {
- port = 80
- protocol = "HTTP"
- target_group_index = 0
+ name = "${terraform.workspace}-${var.project}-lb"
+ load_balancer_type = var.alb_load_balancer_type # Only application loadbalancer type can be used within this project.
+ internal = var.alb_internal
+ vpc_id = local.vpc.vpc_id
+ subnets = local.vpc.public_subnets
+ security_groups = [module.alb_security_group.id]
+ create_security_group = false
+ enable_deletion_protection = var.alb_enable_deletion_protection
+ listeners = {
+ http = {
+ port = 80
+ protocol = "HTTP"
+ forward = {
+ target_group_key = "web"
+ }
}
- ]
- target_groups = [
- {
- name = "${terraform.workspace}-${var.project}-tg"
- backend_protocol = "HTTP"
- backend_port = 80
- target_type = "instance"
+ }
+ target_groups = {
+ web = {
+ name = "${terraform.workspace}-${var.project}-tg"
+ protocol = "HTTP"
+ port = 80
+ target_type = "instance"
+ # The autoscaling group registers its instances.
+ create_attachment = false
}
- ]
- idle_timeout = var.alb_idle_timeout
- load_balancer_create_timeout = var.alb_load_balancer_create_timeout
- load_balancer_update_timeout = var.alb_load_balancer_update_timeout
- load_balancer_delete_timeout = var.alb_load_balancer_delete_timeout
- tags = local.tags
+ }
+ idle_timeout = var.alb_idle_timeout
+ timeouts = {
+ create = var.alb_load_balancer_create_timeout
+ update = var.alb_load_balancer_update_timeout
+ delete = var.alb_load_balancer_delete_timeout
+ }
+ tags = local.tags
}
module "autoscaling" {
- source = "github.com/terraform-aws-modules/terraform-aws-autoscaling"
+ source = "terraform-aws-modules/autoscaling/aws"
+ version = "9.3.2"
providers = {
aws = aws.target
}
@@ -87,13 +123,13 @@ module "autoscaling" {
wait_for_capacity_timeout = var.autoscaling_wait_for_capacity_timeout
default_instance_warmup = var.autoscaling_default_instance_warmup
health_check_type = "EC2"
- vpc_zone_identifier = data.terraform_remote_state.vpc.outputs.private_subnets
+ vpc_zone_identifier = local.vpc.private_subnets
service_linked_role_arn = aws_iam_service_linked_role.this.arn
launch_template_name = "${terraform.workspace}-${var.project}-launch-template"
update_default_version = var.autoscaling_update_default_version
image_id = data.aws_ami.amazon_linux.id
instance_type = var.autoscaling_instance_type
- user_data = base64encode(templatefile("../../templates/user_data.sh.tpl", { secret_phrase = "Hello from ${upper(terraform.workspace)} environment!" }))
+ user_data = base64encode(templatefile("${path.module}/../../templates/user_data.sh.tpl", { secret_phrase = "Hello from ${upper(terraform.workspace)} environment!" }))
ebs_optimized = var.autoscaling_ebs_optimized
enable_monitoring = var.autoscaling_enable_monitoring
create_iam_instance_profile = var.autoscaling_create_iam_instance_profile
@@ -102,8 +138,12 @@ module "autoscaling" {
iam_role_policies = {
AmazonSSMManagedInstanceCore = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}
- security_groups = [module.autoscaling_security_group.security_group_id]
- target_group_arns = module.alb.target_group_arns
+ security_groups = [module.autoscaling_security_group.id]
+ traffic_source_attachments = {
+ alb = {
+ traffic_source_identifier = module.alb.target_groups["web"].arn
+ }
+ }
block_device_mappings = [
{
# Root volume only
@@ -125,18 +165,17 @@ module "autoscaling" {
predefined_metric_specification = {
predefined_metric_type = "ASGAverageCPUUtilization"
}
- target_value = 70.0
- scale_in_cooldown = 300
- scale_out_cooldown = 300
+ target_value = 70.0
}
}
}
tags = local.tags
}
-
+
# SUPPORTING RESOURCES
-
+
resource "aws_iam_service_linked_role" "this" {
+ provider = aws.target
aws_service_name = "autoscaling.amazonaws.com"
description = "A service linked role for autoscaling."
custom_suffix = "${terraform.workspace}-${var.project}"
diff --git a/main/ec2/outputs.tf b/main/ec2/outputs.tf
index bd16119..ffbfe5e 100644
--- a/main/ec2/outputs.tf
+++ b/main/ec2/outputs.tf
@@ -2,5 +2,5 @@
output "alb_dns_name" {
description = "The DNS name of the load balancer."
- value = module.alb.lb_dns_name
+ value = module.alb.dns_name
}
diff --git a/main/ec2/providers.tf b/main/ec2/providers.tf
index 028d63c..cc420ef 100644
--- a/main/ec2/providers.tf
+++ b/main/ec2/providers.tf
@@ -1,12 +1,16 @@
# PROVIDERS
+# Management account: the credentials the workflow got through GitHub OIDC.
+# The backend and terraform_remote_state use the same credentials.
provider "aws" {}
+# Target account: role chaining from the management role into the environment's own account.
provider "aws" {
- alias = "target"
- region = var.target_region
+ alias = "target"
+ region = var.target_region
+ allowed_account_ids = [var.target_account]
assume_role {
role_arn = "arn:aws:iam::${var.target_account}:role/${var.target_role}"
- session_name = "terraform"
+ session_name = "terraform-${terraform.workspace}"
}
}
diff --git a/main/ec2/variables.tf b/main/ec2/variables.tf
index 1a447ac..25e6225 100644
--- a/main/ec2/variables.tf
+++ b/main/ec2/variables.tf
@@ -47,6 +47,12 @@ variable "alb_internal" {
default = false
}
+variable "alb_enable_deletion_protection" {
+ description = "Whether deletion of the load balancer is disabled. The ALB module defaults to true since v9; false keeps destroy-all working."
+ type = bool
+ default = false
+}
+
variable "alb_idle_timeout" {
description = "The time in seconds that the connection is allowed to be idle."
type = number
diff --git a/main/ec2/versions.tf b/main/ec2/versions.tf
index bb336d8..d3f6635 100644
--- a/main/ec2/versions.tf
+++ b/main/ec2/versions.tf
@@ -1,11 +1,11 @@
# VERSIONS
terraform {
- required_version = ">= 1.0.0"
+ required_version = "~> 1.17" # CI runs exactly 1.17.1 (setup-terraform); S3 native locking needs >= 1.11.
required_providers {
aws = {
source = "hashicorp/aws"
- version = ">= 4.5"
+ version = "6.71.0"
}
}
}
diff --git a/main/vpc/.terraform.lock.hcl b/main/vpc/.terraform.lock.hcl
new file mode 100644
index 0000000..41f3970
--- /dev/null
+++ b/main/vpc/.terraform.lock.hcl
@@ -0,0 +1,26 @@
+# This file is maintained automatically by "terraform init".
+# Manual edits may be lost in future updates.
+
+provider "registry.terraform.io/hashicorp/aws" {
+ version = "6.71.0"
+ constraints = ">= 6.28.0, >= 6.44.0, 6.71.0"
+ hashes = [
+ "h1:sZCupscga0Grk22+04FNY1RYDeOcVbCTd6ff8+hsF94=",
+ "zh:09aa14408c5ab26915fb2a469b5eb67c06393a2054d78592ba4c32f7a882e918",
+ "zh:1d1138128dc4866f6c16b1ef3b871b3fd9a3dc648e9056e06e210d1817aa052b",
+ "zh:2c17a79821a0e06fbf1834a12e92227db29c2001fef7ef0b347af2e6dad83f14",
+ "zh:55616730a485c9c7283546a84f1ac5f8341152c5572338a54fc7af5f69ebfb92",
+ "zh:7e6461ec2976b5961a1d4f3c08c68fae0645d7f5e0a404f7647baa3b88d337d8",
+ "zh:7ec935166b2251d075fea980012f5267078785399585e8b443dca484fc57df0f",
+ "zh:885b3290cd48738cbf9ee8a6d47a4452889843f8f53b8cf67fc8c9b16e553721",
+ "zh:8930fb33c6c965f7cc82230cb7fbb0cfff9bb30becdfce5e0c2242512d849894",
+ "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425",
+ "zh:a702f9a4dc7340182a8daf5b6ee562f14657fe14752f7d54dc799cf2fd91a868",
+ "zh:b624cb320d787fbfa6dc94be5a1fd52683d1ebd11b42646262b221a694e6ffb8",
+ "zh:bcbaded68b553afeea93604ec22c254b98f3951862bdeb68ae89c179c7dda117",
+ "zh:cb57bbcaf0fa3a70ea29b60d183d3a0dd020f041b18da5d1725d2acd17fa7d59",
+ "zh:d4ca164b1b6039557d8f5f5f911c4aaad1a187e9b3758f6885c95f8954f7de58",
+ "zh:eb0c705337cef7b96086e8b84702ba965eeeace7a4aa3235b2c8aad5f50f2fca",
+ "zh:f2787f40ce873d8c1308c92adec9b6adbd223506fe9d79bba447729e422f9146",
+ ]
+}
diff --git a/main/vpc/backend.tf b/main/vpc/backend.tf
index a7dff29..0af6f5e 100644
--- a/main/vpc/backend.tf
+++ b/main/vpc/backend.tf
@@ -2,10 +2,13 @@
terraform {
backend "s3" {
- bucket = "multi-environment-infrastructure-provisioning-state-bucket"
- key = "vpc/terraform.tfstate"
- region = "eu-north-1"
- dynamodb_table = "multi-environment-infrastructure-provisioning-locktable"
- encrypt = true
+ bucket = "multi-environment-infrastructure-provisioning-state-bucket"
+ key = "vpc/terraform.tfstate"
+ # Each workspace's state lives at workspaces//vpc/terraform.tfstate,
+ # so an environment's role can be limited to its own prefix.
+ workspace_key_prefix = "workspaces"
+ region = "eu-north-1" # The state bucket's region, not the target environment's.
+ use_lockfile = true # Native S3 locking (Terraform >= 1.11): no DynamoDB table.
+ encrypt = true
}
}
diff --git a/main/vpc/data.tf b/main/vpc/data.tf
index ad63035..68409b7 100644
--- a/main/vpc/data.tf
+++ b/main/vpc/data.tf
@@ -21,7 +21,7 @@ data "aws_iam_policy_document" "flow_log_s3" {
identifiers = ["delivery.logs.amazonaws.com"]
}
actions = ["s3:PutObject"]
- resources = ["arn:aws:s3:::${terraform.workspace}-${var.project}-flow-logs-${data.aws_caller_identity.target.account_id}-${data.aws_region.target.name}/AWSLogs/*"]
+ resources = ["arn:aws:s3:::${terraform.workspace}-${var.project}-flow-logs-${data.aws_caller_identity.target.account_id}-${data.aws_region.target.region}/AWSLogs/*"]
}
statement {
sid = "AWSLogDeliveryAclCheck"
@@ -30,6 +30,6 @@ data "aws_iam_policy_document" "flow_log_s3" {
identifiers = ["delivery.logs.amazonaws.com"]
}
actions = ["s3:GetBucketAcl"]
- resources = ["arn:aws:s3:::${terraform.workspace}-${var.project}-flow-logs-${data.aws_caller_identity.target.account_id}-${data.aws_region.target.name}"]
+ resources = ["arn:aws:s3:::${terraform.workspace}-${var.project}-flow-logs-${data.aws_caller_identity.target.account_id}-${data.aws_region.target.region}"]
}
-}
\ No newline at end of file
+}
diff --git a/main/vpc/main.tf b/main/vpc/main.tf
index bb1d6ca..344d0bb 100644
--- a/main/vpc/main.tf
+++ b/main/vpc/main.tf
@@ -7,7 +7,8 @@ locals {
# MODULES & RESOURCES
module "vpc" {
- source = "github.com/terraform-aws-modules/terraform-aws-vpc"
+ source = "terraform-aws-modules/vpc/aws"
+ version = "6.7.3"
providers = {
aws = aws.target
}
@@ -38,15 +39,19 @@ module "vpc" {
# If VPC flow logs are enabled and the destination type is `s3`
# but no destination ARN has been specified,
# an S3 bucket will be created automatically and set as the flow log bucket for the VPC.
-
+
+# Default SSE-S3 encryption; a customer managed key would need its own key policy for log delivery.
+#trivy:ignore:AWS-0132
module "s3_flow_log_bucket" {
- count = var.vpc_enable_flow_log && var.vpc_flow_log_destination_type == "s3" && var.vpc_flow_log_destination_arn == null ? 1 : 0
- source = "github.com/terraform-aws-modules/terraform-aws-s3-bucket"
+ count = var.vpc_enable_flow_log && var.vpc_flow_log_destination_type == "s3" && var.vpc_flow_log_destination_arn == null ? 1 : 0
+ source = "terraform-aws-modules/s3-bucket/aws"
+ version = "5.17.0"
providers = {
aws = aws.target
}
- bucket = "${terraform.workspace}-${var.project}-flow-logs-${data.aws_caller_identity.target.account_id}-${data.aws_region.target.name}"
- policy = data.aws_iam_policy_document.flow_log_s3.json
+ bucket = "${terraform.workspace}-${var.project}-flow-logs-${data.aws_caller_identity.target.account_id}-${data.aws_region.target.region}"
+ attach_policy = true # Without it the module ignores `policy`.
+ policy = data.aws_iam_policy_document.flow_log_s3.json
lifecycle_rule = [
{
id = "default"
diff --git a/main/vpc/outputs.tf b/main/vpc/outputs.tf
index cd912fa..d1ce8b6 100644
--- a/main/vpc/outputs.tf
+++ b/main/vpc/outputs.tf
@@ -54,7 +54,7 @@ output "nat_public_ips" {
description = "List of public Elastic IPs created for AWS NAT Gateway."
value = module.vpc.nat_public_ips
}
-
+
output "igw_id" {
description = "The ID of the Internet Gateway."
value = module.vpc.igw_id
diff --git a/main/vpc/providers.tf b/main/vpc/providers.tf
index 028d63c..cc420ef 100644
--- a/main/vpc/providers.tf
+++ b/main/vpc/providers.tf
@@ -1,12 +1,16 @@
# PROVIDERS
+# Management account: the credentials the workflow got through GitHub OIDC.
+# The backend and terraform_remote_state use the same credentials.
provider "aws" {}
+# Target account: role chaining from the management role into the environment's own account.
provider "aws" {
- alias = "target"
- region = var.target_region
+ alias = "target"
+ region = var.target_region
+ allowed_account_ids = [var.target_account]
assume_role {
role_arn = "arn:aws:iam::${var.target_account}:role/${var.target_role}"
- session_name = "terraform"
+ session_name = "terraform-${terraform.workspace}"
}
}
diff --git a/main/vpc/versions.tf b/main/vpc/versions.tf
index bb336d8..d3f6635 100644
--- a/main/vpc/versions.tf
+++ b/main/vpc/versions.tf
@@ -1,11 +1,11 @@
# VERSIONS
terraform {
- required_version = ">= 1.0.0"
+ required_version = "~> 1.17" # CI runs exactly 1.17.1 (setup-terraform); S3 native locking needs >= 1.11.
required_providers {
aws = {
source = "hashicorp/aws"
- version = ">= 4.5"
+ version = "6.71.0"
}
}
}
diff --git a/templates/user_data.sh.tpl b/templates/user_data.sh.tpl
index 380b36f..daf99c5 100644
--- a/templates/user_data.sh.tpl
+++ b/templates/user_data.sh.tpl
@@ -1,8 +1,10 @@
#!/bin/bash
-yum -y update
-yum -y install httpd
+dnf -y update
+dnf -y install httpd
-INSTANCE_IP=$(curl http://169.254.169.254/latest/meta-data/local-ipv4)
+# Instance metadata needs an IMDSv2 session token: the launch template requires it.
+TOKEN=$(curl -sS -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 300")
+INSTANCE_IP=$(curl -sS -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/local-ipv4)
cat < /var/www/html/index.html
@@ -11,5 +13,4 @@ Instance IP address: $INSTANCE_IP
EOF
-service httpd start
-chkconfig httpd on
+systemctl enable --now httpd