Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ updates:

- package-ecosystem: opentofu
directories:
- /terraform/catalog/modules/**/*
- /terraform/modules/**/*
schedule:
interval: weekly
cooldown:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci-terraform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ jobs:

- uses: jdx/mise-action@1648a7812b9aeae629881980618f079932869151 # v4.0.1

- name: Validate all catalog modules
- name: Validate all modules
run: |
find terraform/catalog/modules -mindepth 1 -maxdepth 1 -type d | while read -r dir; do
find terraform/modules -mindepth 1 -maxdepth 1 -type d | while read -r dir; do
echo "--- Validating $dir ---"
tofu -chdir="$dir" init -backend=false
tofu -chdir="$dir" validate
Expand Down
43 changes: 43 additions & 0 deletions .github/workflows/docker-push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Build and Push

on:
push:
branches: [main]
paths:
- 'applications/hello-world/**'

concurrency:
group: docker-push-hello-world
cancel-in-progress: false

jobs:
build-and-push:
name: Build and push (dev)
runs-on: ubuntu-latest
environment: dev
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
persist-credentials: false

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@99214aa6889fcddfa57764031d71add364327e59 # v6.1.3
with:
role-to-assume: ${{ vars.HELLO_WORLD_PUSH_ROLE_ARN }}
aws-region: ${{ vars.AWS_REGION }}

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@fa648b43de3d4d023bcb3f89ed6940096949c419 # v2.1.5

- name: Build and push
working-directory: applications/hello-world
env:
REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ github.sha }}
run: |
docker build -t $REGISTRY/hello-world:$IMAGE_TAG .
docker push $REGISTRY/hello-world:$IMAGE_TAG
2 changes: 1 addition & 1 deletion .github/workflows/terragrunt-apply.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
uses: aws-actions/configure-aws-credentials@99214aa6889fcddfa57764031d71add364327e59 # v6.1.3
with:
role-to-assume: ${{ vars.APPLY_ROLE_ARN }}
aws-region: ap-southeast-2
aws-region: ${{ vars.AWS_REGION }}
- name: Terragrunt Apply
uses: gruntwork-io/terragrunt-action@4ed5b7344c80315e5357f28f36159fc980bc2d5a # v3.4.0
with:
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/terragrunt-plan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ jobs:
run: |
CHANGED=$(git diff --name-only "origin/$BASE_REF...HEAD")

# A catalog change (module/unit/stack) can affect every environment,
# A module change can affect every environment,
# so plan them all. Otherwise, only plan environments with direct changes.
if echo "$CHANGED" | grep -q '^terraform/catalog/'; then
if echo "$CHANGED" | grep -q '^terraform/modules/'; then
ENVS=$(find terraform/environments \
-maxdepth 1 -mindepth 1 -type d \
-printf '%f\n' | sort \
Expand Down Expand Up @@ -76,7 +76,7 @@ jobs:
uses: aws-actions/configure-aws-credentials@99214aa6889fcddfa57764031d71add364327e59 # v6.1.3
with:
role-to-assume: ${{ vars.PLAN_ROLE_ARN }}
aws-region: ap-southeast-2
aws-region: ${{ vars.AWS_REGION }}

- name: Terragrunt Plan
id: plan
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ repos:

- id: tofu-validate
name: tofu validate
entry: bash -c 'find terraform/catalog/modules -mindepth 1 -maxdepth 1 -type d | while read -r dir; do
entry: bash -c 'find terraform/modules -mindepth 1 -maxdepth 1 -type d | while read -r dir; do
echo "--- Validating $dir ---";
tofu -chdir="$dir" init -backend=false;
tofu -chdir="$dir" validate;
Expand Down
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ A personal sandbox for learning Terraform and Kubernetes, using Terragrunt to ma

```
terraform/
├── catalog/
│ ├── modules/ # Raw Terraform modules
│ ├── units/ # Terragrunt wrappers around modules
│ └── stacks/ # Compositions of units
├── modules/ # Raw OpenTofu modules
└── environments/
└── dev/ # Environment-specific stack instantiations
└── dev/ # Environment-specific Terragrunt units
```

## Prerequisites
Expand Down Expand Up @@ -44,7 +41,7 @@ task tf:destroy:dev # destroy all units in dev

```bash
task tf:fmt # format all Terraform files
task tf:validate # validate all catalog modules
task tf:validate # validate all modules
task tf:clean # remove local Terraform and Terragrunt caches
```

Expand Down
4 changes: 2 additions & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ tasks:
- tofu fmt -recursive terraform/

tf:validate:
desc: Validate all catalog modules
desc: Validate all modules
cmds:
- |
find terraform/catalog/modules -mindepth 1 -maxdepth 1 -type d | while read -r dir; do
find terraform/modules -mindepth 1 -maxdepth 1 -type d | while read -r dir; do
echo "--- Validating $dir ---"
tofu -chdir="$dir" init -backend=false
tofu -chdir="$dir" validate
Expand Down
6 changes: 6 additions & 0 deletions applications/hello-world/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM nginx:alpine

COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY index.html /usr/share/nginx/html/index.html

EXPOSE 80
Comment thread
AdamJHall marked this conversation as resolved.
10 changes: 10 additions & 0 deletions applications/hello-world/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello, World!</title>
</head>
<body>
<h1>Hello, World!</h1>
</body>
</html>
8 changes: 8 additions & 0 deletions applications/hello-world/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
server {
listen 80;

location / {
root /usr/share/nginx/html;
index index.html;
}
}
15 changes: 0 additions & 15 deletions terraform/catalog/stacks/github-oidc/terragrunt.stack.hcl

This file was deleted.

20 changes: 0 additions & 20 deletions terraform/catalog/stacks/network/terragrunt.stack.hcl

This file was deleted.

14 changes: 0 additions & 14 deletions terraform/catalog/units/github-oidc/terragrunt.hcl

This file was deleted.

19 changes: 0 additions & 19 deletions terraform/catalog/units/vpc/terragrunt.hcl

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
include "root" {
path = find_in_parent_folders("root.hcl")
}

terraform {
source = "${get_repo_root()}//terraform/modules/github-oidc"
}

locals {
stacks_path = "${get_repo_root()}/terraform/catalog/stacks"
environment = "dev"
account_vars = read_terragrunt_config(find_in_parent_folders("account.hcl"))
region_vars = read_terragrunt_config(find_in_parent_folders("region.hcl"))
state_bucket = "terragrunt-tf-state-${local.account_vars.locals.account_name}-${local.region_vars.locals.aws_region}"
}

stack "dev-oidc" {
source = "${local.stacks_path}/github-oidc"
path = "dev/oidc"

values = {
github_org = "AdamJHall"
github_repo = "platform-lab"
environment = local.environment
state_bucket = local.state_bucket
}
}
inputs = {
github_org = "AdamJHall"
github_repo = "platform-lab"
environment = "dev"
state_bucket = local.state_bucket
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
include "root" {
path = find_in_parent_folders("root.hcl")
}

terraform {
source = "${get_repo_root()}//terraform/modules/ecr"
}

dependency "github_oidc" {
config_path = "../github-oidc"

mock_outputs = {
oidc_provider_arn = "arn:aws:iam::000000000000:oidc-provider/token.actions.githubusercontent.com"
}
mock_outputs_allowed_terraform_commands = ["validate", "plan"]
}

inputs = {
name = "hello-world"
environment = "dev"
oidc_provider_arn = dependency.github_oidc.outputs.oidc_provider_arn
github_repository = "AdamJHall/platform-lab"
}
30 changes: 30 additions & 0 deletions terraform/environments/dev/ap-southeast-2/network/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
include "root" {
path = find_in_parent_folders("root.hcl")
}

terraform {
source = "${get_repo_root()}//terraform/modules/vpc"
}

inputs = {
name = "dev-network"
environment = "dev"
cidr_block = "10.0.0.0/16"
az_count = 2
subnet_cidrs = {
public = ["10.0.0.0/20", "10.0.16.0/20"]
private = ["10.0.32.0/20", "10.0.48.0/20"]
private_with_egress = ["10.0.64.0/20", "10.0.80.0/20"]
}
enable_flow_logs = false
nat_use_spot_instances = true
nat_instance_type = "t4g.nano"
subnet_tags = {
public = {
"kubernetes.io/role/elb" = "1"
}
private_with_egress = {
"kubernetes.io/role/internal-elb" = "1"
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ locals {

resource "aws_ecr_repository" "this" {
name = var.name
image_tag_mutability = var.mutability
image_tag_mutability = var.mutable ? "MUTABLE" : "IMMUTABLE"
image_scanning_configuration {
scan_on_push = var.image_scanning_on_push
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ output "repository_url" {
output "repository_arn" {
description = "ARN of the ECR repo."
value = aws_ecr_repository.this.arn
}

output "push_role_arn" {
description = "ARN of the IAM role that can push images to this repo."
value = local.create_push_role ? aws_iam_role.push[0].arn : null
}
Loading