Skip to content

Commit

Permalink
Merge pull request #14 from aws-educate-tw/SCRUM-22-Build-a-CI-CD-pip…
Browse files Browse the repository at this point in the history
…eline-to-ensure-continuous-delivery

Scrum 22 build a ci cd pipeline to ensure continuous delivery
  • Loading branch information
sh1un committed Jun 10, 2024
2 parents 40f9cb9 + cc4dce0 commit 578f77d
Show file tree
Hide file tree
Showing 45 changed files with 1,137 additions and 261 deletions.
117 changes: 117 additions & 0 deletions .github/workflows/terraform.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
name: "Terraform Infrastructure Change Management Pipeline with GitHub Actions"

on:
push:
branches:
- main
- dev
- poc
paths:
- terraform/**
pull_request:
branches:
- main
- dev
- poc
paths:
- terraform/**

env:
# verbosity setting for Terraform logs
TF_LOG: INFO
# Credentials for deployment to AWS
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
# S3 bucket for the Terraform state

jobs:
terraform:
name: "Terraform Infrastructure Change Management"
runs-on: ubuntu-latest
defaults:
run:
shell: bash

steps:
- name: Checkout the repository to the runner
uses: actions/checkout@v2

- name: Setup Terraform with specified version on the runner
uses: hashicorp/setup-terraform@v2
with:
terraform_version: 1.8.3

- name: Set environment variables
id: set-env
run: |
if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
echo "::set-output name=env::prod"
echo "::set-output name=dir::./terraform/prod"
echo "::set-output name=tfvars::variables_prod.tfvars"
elif [[ "${{ github.ref }}" == "refs/heads/dev" ]]; then
echo "::set-output name=env::dev"
echo "::set-output name=dir::./terraform/dev"
echo "::set-output name=tfvars::variables_dev.tfvars"
elif [[ "${{ github.ref }}" == "refs/heads/poc" ]]; then
echo "::set-output name=env::poc"
echo "::set-output name=dir::./terraform/poc"
echo "::set-output name=tfvars::variables_poc.tfvars"
fi
- name: Terraform init
id: init
working-directory: ${{ steps.set-env.outputs.dir }}
run: terraform init"

- name: Terraform format
id: fmt
working-directory: ${{ steps.set-env.outputs.dir }}
run: terraform fmt -check

- name: Terraform validate
id: validate
working-directory: ${{ steps.set-env.outputs.dir }}
run: terraform validate

- name: Terraform plan
id: plan
if: github.event_name == 'pull_request'
working-directory: ${{ steps.set-env.outputs.dir }}
run: terraform plan -no-color -input=false -var-file="${{ steps.set-env.outputs.tfvars }}"
continue-on-error: true

- uses: actions/github-script@v6
if: github.event_name == 'pull_request'
env:
PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
with:
script: |
const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\`
#### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
#### Terraform Validation 🤖\`${{ steps.validate.outcome }}\`
#### Terraform Plan 📖\`${{ steps.plan.outcome }}\`
<details><summary>Show Plan</summary>
\`\`\`\n
${process.env.PLAN}
\`\`\`
</details>
*Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
- name: Terraform Plan Status
if: steps.plan.outcome == 'failure'
run: exit 1

- name: Terraform Apply
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
working-directory: ${{ steps.set-env.outputs.dir }}
run: terraform apply -auto-approve -input=false -var-file="${{ steps.set-env.outputs.tfvars }}"
File renamed without changes.
File renamed without changes.
7 changes: 2 additions & 5 deletions src/file_service/terraform/iam.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "aws_iam_role" "lambda_exec_role" {
name = "lambda_exec_role"
resource "aws_iam_role" "list_files_lambda_exec_role" {
name = "list_files_lambda_exec_role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Expand All @@ -16,7 +16,4 @@ resource "aws_iam_role" "lambda_exec_role" {
"arn:aws:iam::aws:policy/AmazonDynamoDBReadOnlyAccess"
]

lifecycle {
prevent_destroy = true
}
}
2 changes: 1 addition & 1 deletion src/file_service/terraform/lambda.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ data "archive_file" "lambda_zip" {
resource "aws_lambda_function" "list_files" {
filename = data.archive_file.lambda_zip.output_path
function_name = "list_files"
role = aws_iam_role.lambda_exec_role.arn
role = aws_iam_role.list_files_lambda_exec_role.arn
handler = "list_files_function.lambda_handler"
source_code_hash = filebase64sha256(data.archive_file.lambda_zip.output_path)
runtime = "python3.11"
Expand Down
Binary file not shown.
Loading

0 comments on commit 578f77d

Please sign in to comment.