Skip to content

Commit c065819

Browse files
Added Github action deploy workflows for CICD pipeline
1 parent 3c48257 commit c065819

6 files changed

Lines changed: 74 additions & 4 deletions

File tree

.github/workflows/deploy.yaml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
name: Terraform Deploy
2+
3+
on:
4+
push:
5+
branches:
6+
- staging
7+
- main
8+
workflow_dispatch:
9+
10+
env:
11+
TF_WORKING_DIR: terraform
12+
13+
jobs:
14+
terraform:
15+
name: Terraform (plan & apply)
16+
runs-on: ubuntu-latest
17+
environment: ${{ github.ref == 'refs/heads/main' && 'prod' || 'staging' }}
18+
permissions:
19+
contents: read
20+
21+
steps:
22+
- name: Checkout
23+
uses: actions/checkout@v4
24+
25+
- name: Setup Terraform
26+
uses: hashicorp/setup-terraform@v2
27+
with:
28+
terraform_version: 1.5.6
29+
30+
- name: Configure AWS Credentials
31+
uses: aws-actions/configure-aws-credentials@v2
32+
with:
33+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
34+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
35+
aws-region: ${{ secrets.AWS_REGION }}
36+
37+
- name: Terraform Init
38+
working-directory: ${{ env.TF_WORKING_DIR }}
39+
run: terraform init -input=false
40+
41+
- name: Terraform Validate & Format
42+
working-directory: ${{ env.TF_WORKING_DIR }}
43+
run: |
44+
terraform fmt -check
45+
terraform validate
46+
47+
- name: Terraform Plan
48+
id: plan
49+
working-directory: ${{ env.TF_WORKING_DIR }}
50+
run: |
51+
if [ "${{ github.ref }}" = "refs/heads/staging" ]; then
52+
terraform plan -var-file="staging.tfvars" -out=tfplan
53+
else
54+
terraform plan -var-file="prod.tfvars" -out=tfplan
55+
fi
56+
57+
- name: Terraform Apply
58+
if: github.ref == 'refs/heads/staging'
59+
working-directory: ${{ env.TF_WORKING_DIR }}
60+
run: terraform apply -input=false -auto-approve tfplan
61+
62+
- name: Terraform Apply (prod) - requires env approval
63+
if: github.ref == 'refs/heads/main'
64+
working-directory: ${{ env.TF_WORKING_DIR }}
65+
run: terraform apply -input=false -auto-approve tfplan
66+
67+
- name: Show outputs
68+
if: success()
69+
working-directory: ${{ env.TF_WORKING_DIR }}
70+
run: terraform output -json

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ Run the python funtion locally using VS Code Run Button
3131

3232
- Create the terraform folder structure
3333

34-
- imp
34+
- Deploy with: terraform init then terraform apply -var-file="staging.tfvars" (or prod.tfvars)

terraform/modules/api-gateway/main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
resource "aws_apigatewayv2_api" "health_api" {
2-
name = "${var.environment}-health-check-api"
2+
name = "${var.environment}-serverless-health-check-api"
33
protocol_type = "HTTP"
44

55
cors_configuration {
@@ -55,7 +55,7 @@ resource "aws_apigatewayv2_stage" "default" {
5555
}
5656

5757
resource "aws_cloudwatch_log_group" "api_gateway_logs" {
58-
name = "/aws/apigateway/${var.environment}-health-check-api"
58+
name = "/aws/apigateway/${var.environment}-serverless-health-check-api"
5959
retention_in_days = 7
6060

6161
tags = var.common_tags

terraform/modules/lambda/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ data "archive_file" "lambda_zip" {
55
}
66

77
resource "aws_lambda_function" "health_check" {
8-
function_name = "${var.environment}-health-check-function"
8+
function_name = "${var.environment}-serverless-health-check-api"
99
role = var.lambda_role_arn
1010
handler = "lambda_function.lambda_handler"
1111
source_code_hash = data.archive_file.lambda_zip.output_base64sha256
-745 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)