Skip to content

Commit

Permalink
feat: Add Infrastructure github action
Browse files Browse the repository at this point in the history
  • Loading branch information
andresousadotpt committed Jun 4, 2024
1 parent 97e7074 commit b00ea97
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
98 changes: 98 additions & 0 deletions .github/workflows/infrastructure.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
name: Apply Infrastructure
on:
workflow_dispatch:
branches:
- main

permissions:
contents: read
pull-requests: write

jobs:
plan:
name: Terraform Plan
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_wrapper: false
terraform_version: "5.52.0"

- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ env.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ env.AWS_SECRET_ACCESS_KEY }}
aws-region: eu-central-1

# Initialize a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc.
- name: Terraform Init
working-directory: ./terraform/
run: |
terraform init
# Generates an execution plan for Terraform
# An exit code of 0 indicated no changes, 1 a terraform failure, 2 there are pending changes.
- name: Terraform Plan
id: tf-plan
working-directory: ./terraform/
run: |
export exitcode=0
terraform plan -var-file $(pwd)/tf_$ENVIRONMENT.tfvars \
-detailed-exitcode -no-color -out tfplan || export exitcode=$?
echo "exitcode=$exitcode" >> $GITHUB_OUTPUT
if [ $exitcode -eq 1 ]; then
echo Terraform Plan Failed!
exit 1
else
exit 0
fi
# Upload plan to artifacts
- name: Publish Terraform Plan
uses: actions/upload-artifact@v4
with:
name: tfplan
path: ./terraform/tfplan

apply:
needs: [plan]
if: ${{ needs.plan.outputs.exitcode == 2 }}
name: Terraform Apply
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_wrapper: false
terraform_version: "5.52.0"

- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ env.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ env.AWS_SECRET_ACCESS_KEY }}
aws-region: eu-central-1

- name: Download Terraform Plan
uses: actions/download-artifact@v4
with:
name: tfplan
path: ./terraform/

# Terraform Apply
- name: Terraform Apply
id: tf-apply-string
working-directory: ./terraform/
run: |
terraform apply -auto-approve tfplan -no-color
3 changes: 2 additions & 1 deletion terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ resource "aws_lambda_function" "reminder_bot_lambda" {
filename = "${path.module}/dummy.jar"
handler = "handler.Handler"
runtime = "java21"
timeout = 60

lifecycle {
ignore_changes = [filename]
Expand All @@ -71,7 +72,7 @@ resource "aws_iam_role_policy_attachment" "lambda_policy_attachment" {
resource "aws_cloudwatch_event_rule" "reminder_bot_schedule" {
name = "reminder-bot-schedule"
description = "Schedule rule for reminder bot Lambda function"
schedule_expression = "cron(0 0 15,28 * ? *)"
schedule_expression = "cron(30 20 15,28 * ? *)"
}

resource "aws_cloudwatch_event_target" "reminder_bot_target" {
Expand Down

0 comments on commit b00ea97

Please sign in to comment.