Skip to content

Commit 717f1a4

Browse files
authored
Merge pull request #16 from NgaNaNa/feature/eks-infra
creates separate directories for ECS and EKS TF files, includes restructuring of the files and files references adds in dedicated bucket for storing EKS state file
2 parents b94e23c + 4ede605 commit 717f1a4

27 files changed

+483
-16
lines changed
Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@ name: "Terraform CICD - AWS ECS Instance"
22

33
on:
44
pull_request:
5-
branches:
6-
- main
5+
branches: [main]
6+
paths:
7+
- 'infra/ecs/**'
8+
- 'infra/envs/**'
9+
- '.github/workflows/ecs_terraform.yaml'
710
push:
8-
branches:
9-
- main
11+
branches: [main]
12+
paths:
13+
- 'infra/ecs/**'
14+
- 'infra/envs/**'
15+
- '.github/workflows/ecs_terraform.yaml'
1016

1117
permissions:
1218
contents: read
@@ -30,7 +36,7 @@ jobs:
3036
defaults:
3137
run:
3238
shell: bash
33-
working-directory: infra
39+
working-directory: infra/ecs
3440
environment: dev
3541

3642
steps:
@@ -60,7 +66,7 @@ jobs:
6066
- name: Terraform plan - dev
6167
id: plan-dev
6268
if: github.event_name == 'pull_request'
63-
run: terraform plan -var-file=envs/dev.tfvars -no-color -input=false
69+
run: terraform plan -var-file=../envs/dev.tfvars -no-color -input=false
6470
continue-on-error: true
6571

6672
- uses: actions/github-script@v7
@@ -96,7 +102,7 @@ jobs:
96102

97103
- name: Terraform Apply
98104
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
99-
run: terraform apply -var-file=envs/dev.tfvars -no-color -auto-approve -input=false # TODO: use tf plan file
105+
run: terraform apply -var-file=../envs/dev.tfvars -no-color -auto-approve -input=false # TODO: use tf plan file
100106

101107
# # Terraform Prod CI
102108
# terraform-prod-ci:
@@ -134,7 +140,7 @@ jobs:
134140
# - name: Terraform plan - prod
135141
# id: plan-prod
136142
# if: github.event_name == 'pull_request'
137-
# run: terraform plan -var-file=envs/prod.tfvars -no-color -input=false
143+
# run: terraform plan -var-file=../envs/prod.tfvars -no-color -input=false
138144
# continue-on-error: true
139145

140146
# - uses: actions/github-script@v6
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
name: "Terraform CICD - AWS EKS Cluster"
2+
3+
on:
4+
pull_request:
5+
branches: [main]
6+
paths:
7+
- 'infra/eks/**'
8+
- 'infra/envs/**'
9+
- '.github/workflows/eks_terraform.yaml'
10+
push:
11+
branches: [main]
12+
paths:
13+
- 'infra/eks/**'
14+
- 'infra/envs/**'
15+
- '.github/workflows/eks_terraform.yaml'
16+
17+
permissions:
18+
contents: read
19+
issues: write
20+
pull-requests: write
21+
22+
env:
23+
# Verbosity setting for Terraform logs
24+
TF_LOG: ERROR
25+
# Credentials for deployment to AWS
26+
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
27+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
28+
# S3 bucket for the Terraform state
29+
BUCKET_TF_STATE: ${{ secrets.EKS_BUCKET_TF_STATE}}
30+
31+
jobs:
32+
# Terraform Dev CICD
33+
terraform-dev:
34+
name: "Terraform Infra CICD Dev"
35+
runs-on: ubuntu-latest
36+
defaults:
37+
run:
38+
shell: bash
39+
working-directory: infra/ecs
40+
environment: dev
41+
42+
steps:
43+
- name: Checkout the repository to the runner
44+
uses: actions/checkout@v4
45+
46+
- name: Setup Terraform with specified version on the runner
47+
uses: hashicorp/setup-terraform@v3
48+
with:
49+
terraform_version: 1.11.3
50+
51+
- name: Terraform init dev
52+
id: init-dev
53+
run: terraform init -reconfigure -backend-config=bucket=$BUCKET_TF_STATE
54+
55+
# Quality checks DEV
56+
- name: Terraform format
57+
id: fmt-dev
58+
if: github.event_name == 'pull_request'
59+
run: terraform fmt -check
60+
61+
- name: Terraform validate
62+
id: validate-dev
63+
if: github.event_name == 'pull_request'
64+
run: terraform validate
65+
66+
- name: Terraform plan - dev
67+
id: plan-dev
68+
if: github.event_name == 'pull_request'
69+
run: terraform plan -var-file=../envs/dev.tfvars -no-color -input=false
70+
continue-on-error: true
71+
72+
- uses: actions/github-script@v7
73+
if: github.event_name == 'pull_request'
74+
env:
75+
PLAN: "terraform\n${{ steps.plan-dev.outputs.stdout }}"
76+
with:
77+
script: |
78+
const output = `#### Terraform Format and Style - DEV 🖌\`${{ steps.fmt-dev.outcome }}\`
79+
#### Terraform Initialization - DEV ⚙️\`${{ steps.init-dev.outcome }}\`
80+
#### Terraform Validation - DEV 🤖\`${{ steps.validate-dev.outcome }}\`
81+
#### Terraform Plan - DEV 📖\`${{ steps.plan-dev.outcome }}\`
82+
83+
<details><summary>Show Plan</summary>
84+
85+
\`\`\`\n
86+
${process.env.PLAN}
87+
\`\`\`
88+
89+
</details>
90+
*Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
91+
92+
github.rest.issues.createComment({
93+
issue_number: context.issue.number,
94+
owner: context.repo.owner,
95+
repo: context.repo.repo,
96+
body: output
97+
})
98+
99+
- name: Terraform Plan Status
100+
if: steps.plan-dev.outcome == 'failure'
101+
run: exit 1
102+
103+
- name: Terraform Apply
104+
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
105+
run: terraform apply -var-file=../envs/dev.tfvars -no-color -auto-approve -input=false # TODO: use tf plan file
106+
107+
# # Terraform Prod CI
108+
# terraform-prod-ci:
109+
# name: "Terraform Infra CI Prod"
110+
# runs-on: ubuntu-latest
111+
# defaults:
112+
# run:
113+
# shell: bash
114+
# environment: prod
115+
116+
# steps:
117+
# - name: Checkout the repository to the runner
118+
# uses: actions/checkout@v4
119+
120+
# - name: Setup Terraform with specified version on the runner
121+
# uses: hashicorp/setup-terraform@v3
122+
# with:
123+
# terraform_version: 1.11.3
124+
125+
# - name: Terraform init prod
126+
# id: init-prod
127+
# run: terraform init -reconfigure -backend-config=bucket=$BUCKET_TF_STATE #Create new bucket for prod
128+
129+
# # Quality checks PROD
130+
# - name: Terraform format
131+
# id: fmt-prod
132+
# if: github.event_name == 'pull_request'
133+
# run: terraform fmt -check
134+
135+
# - name: Terraform validate
136+
# id: validate-prod
137+
# if: github.event_name == 'pull_request'
138+
# run: terraform validate
139+
140+
# - name: Terraform plan - prod
141+
# id: plan-prod
142+
# if: github.event_name == 'pull_request'
143+
# run: terraform plan -var-file=../envs/prod.tfvars -no-color -input=false
144+
# continue-on-error: true
145+
146+
# - uses: actions/github-script@v6
147+
# if: github.event_name == 'pull_request'
148+
# env:
149+
# PLAN: "terraform\n${{ steps.plan-prod.outputs.stdout }}"
150+
# with:
151+
# script: |
152+
# const output = `#### Terraform Format and Style - PROD 🖌\`${{ steps.fmt-prod.outcome }}\`
153+
# #### Terraform Initialization - PROD ⚙️\`${{ steps.init-prod.outcome }}\`
154+
# #### Terraform Validation - PROD 🤖\`${{ steps.validate-prod.outcome }}\`
155+
# #### Terraform Plan - PROD 📖\`${{ steps.plan-prod.outcome }}\`
156+
157+
# <details><summary>Show Plan</summary>
158+
159+
# \`\`\`\n
160+
# ${process.env.PLAN}
161+
# \`\`\`
162+
163+
# </details>
164+
# *Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
165+
166+
# github.rest.issues.createComment({
167+
# issue_number: context.issue.number,
168+
# owner: context.repo.owner,
169+
# repo: context.repo.repo,
170+
# body: output
171+
# })
172+
173+
# - name: Terraform Plan Status
174+
# if: steps.plan-prod.outcome == 'failure'
175+
# run: exit 1
176+
177+
# # Terraform PROD CD
178+
# terraform-prod-cd:
179+
# name: "Terraform Infra CD Prod"
180+
# needs: [terraform-dev, terraform-prod-ci]
181+
# runs-on: ubuntu-latest
182+
# defaults:
183+
# run:
184+
# shell: bash
185+
# environment: prod
186+
187+
# steps:
188+
# - name: Checkout the repository to the runner
189+
# uses: actions/checkout@v3
190+
191+
# - name: Setup Terraform with specified version on the runner
192+
# uses: hashicorp/setup-terraform@v2
193+
# with:
194+
# terraform_version: 1.3.9
195+
196+
# - name: Terraform init prod
197+
# id: init-prod
198+
# run: terraform init -reconfigure -backend-config=bucket=$BUCKET_TF_STATE
199+
200+
# - name: Terraform Apply
201+
# if: github.ref == 'refs/heads/main' && github.event_name == 'push' # only on push/merge to main
202+
# run: terraform apply -auto-approve -input=false

README.md

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
# TypeScript Node.js API → Docker → Amazon ECS
2-
*(local build & Terraform deploy – GitHub Actions pipeline coming soon)*
2+
*(local build & Terraform deploy – GitHub Actions EKS Infra CICD pipeline)*
3+
4+
This repo walks you through containerising a simple Node.js API, pushing the image to Docker Hub, and provisioning the infrastructure on **Amazon EKS (EC2 capacity)** with **Terraform**.
5+
The VPC, Public Subnets, Internet Gateway, Route Table and Terraform remote-state bucket (S3 + DynamoDB) are assumed to exist already.
6+
7+
Remote backend: S3 bucket `node-app-eks-tfstate-<env>`
8+
9+
## 1 · Initialise Terraform (one‑time per env)
10+
11+
```bash
12+
cd infra/eks
13+
terraform init -reconfigure -backend-config=bucket=node-app-eks-tfstate-dev -backend-config=profile=node-app-terraform-dev
14+
```
15+
16+
## 3 · Deploy with Terraform from directory infra/eks/
17+
18+
```bash
19+
AWS_PROFILE=node-app-terraform-dev terraform plan -var-file=../envs/dev.tfvars
20+
21+
AWS_PROFILE=node-app-terraform-dev terraform apply -var-file=../envs/dev.tfvars
22+
```
23+
24+
25+
26+
# TypeScript Node.js API → Docker → Amazon ECS
27+
*(local build & Terraform deploy – GitHub Actions ECS Infra CICD pipeline)*
328

429
This repo walks you through containerising a simple Node.js API, pushing the image to Docker Hub, and provisioning the infrastructure on **Amazon ECS (EC2 capacity)** with **Terraform**.
530
The VPC, Public Subnets, Internet Gateway, Route Table and Terraform remote-state bucket (S3 + DynamoDB) are assumed to exist already.
@@ -20,32 +45,34 @@ The VPC, Public Subnets, Internet Gateway, Route Table and Terraform remote-stat
2045
## 1 · Initialise Terraform (one‑time per env)
2146

2247
```bash
23-
cd infra
48+
cd infra/ecs
2449
terraform init -reconfigure -backend-config=bucket=node-app-infra-tfstate-dev -backend-config=profile=node-app-terraform-dev
2550
```
2651

2752
---
2853

29-
## 2 · Build & push the container image (Apply new version tag where appropriate)
54+
## 2 · Local Build & push the container image (Apply new version tag)
3055

3156
```bash
3257
docker buildx build --platform linux/amd64,linux/arm64 -t nrampling/demo-node-app:1.0.2 --push .
3358
```
34-
35-
Update the image tag in `infra/envs/dev.tfvars`:
59+
For ECS workload
60+
Update the image tag in `infra/ecs/envs/dev.tfvars`:
61+
For EKS workload
62+
Update the image tag in `infra/eks/envs/dev.tfvars`:
3663

3764
```hcl
3865
node_app_image = "nrampling/demo-node-app:1.0.2"
3966
```
4067

4168
---
4269

43-
## 3 · Deploy with Terraform
70+
## 3 · Deploy with Terraform from directory infra/ecs/
4471

4572
```bash
46-
AWS_PROFILE=node-app-terraform-dev terraform plan -var-file=envs/dev.tfvars
73+
AWS_PROFILE=node-app-terraform-dev terraform plan -var-file=../envs/dev.tfvars
4774

48-
AWS_PROFILE=node-app-terraform-dev terraform apply -var-file=envs/dev.tfvars
75+
AWS_PROFILE=node-app-terraform-dev terraform apply -var-file=../envs/dev.tfvars
4976
```
5077

5178
### Outputs (example only - plug in aws account)
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)