Skip to content

Commit

Permalink
trying cleanup steps
Browse files Browse the repository at this point in the history
  • Loading branch information
proquickly committed Dec 8, 2024
1 parent f079d63 commit e1ad539
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 57 deletions.
89 changes: 40 additions & 49 deletions .github/workflows/terraform.yml
Original file line number Diff line number Diff line change
@@ -1,58 +1,49 @@
name: Terraform
name: Terraform AWS Deployment

on:
push:
branches:
- main
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
terraform:
runs-on: ubuntu-latest

env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: us-west-2

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Setup Terraform
uses: hashicorp/setup-terraform@v3

- name: Verify AWS Secrets Presence (Debug Step)
run: |
if [ -z "${{ secrets.AWS_ACCESS_KEY_ID }}" ]; then
echo "AWS_ACCESS_KEY_ID secret is not set"
exit 1
fi
if [ -z "${{ secrets.AWS_SECRET_ACCESS_KEY }}" ]; then
echo "AWS_SECRET_ACCESS_KEY secret is not set"
exit 1
fi
echo "AWS secrets are set"
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-west-2

- name: Verify AWS credentials
run: aws sts get-caller-identity

- name: Initialize Terraform
run: terraform init
working-directory: terraform

- name: Plan Terraform
run: terraform plan -out=tfplan
working-directory: terraform

- name: Apply Terraform
run: terraform apply -auto-approve tfplan
working-directory: terraform

- name: Wait for 6 minutes
run: sleep 600 # 360 seconds equals 4 minutes

- name: Destroy Terraform
run: terraform destroy -auto-approve
working-directory: terraform
- uses: actions/checkout@v2

- name: Setup Terraform
uses: hashicorp/setup-terraform@v1

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}

- name: Terraform Init
run: terraform init

- name: Terraform Plan
run: terraform plan

- name: Terraform Apply
if: github.ref == 'refs/heads/main'
run: terraform apply -auto-approve

- name: Cleanup old resources
if: always()
run: |
# List and remove old security groups that aren't being used
OLD_SGS=$(aws ec2 describe-security-groups --query 'SecurityGroups[?contains(GroupName, `allow_ssh_`) || contains(GroupName, `allow_http_flask_`)].GroupId' --output text)
for SG_ID in $OLD_SGS; do
aws ec2 delete-security-group --group-id $SG_ID || true
done
continue-on-error: true
1 change: 1 addition & 0 deletions src/tfgha/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from flask import Flask
import chromadb

app = Flask(__name__)

Expand Down
48 changes: 40 additions & 8 deletions terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,60 @@ provider "aws" {
region = "us-west-2"
}

# Add random suffix to avoid conflicts
resource "random_id" "suffix" {
byte_length = 4
}

resource "aws_key_pair" "deployer" {
key_name = "deployer-key"
key_name = "deployer-key-${random_id.suffix.hex}"
public_key = file("id_rsa.pub")
}

resource "aws_security_group" "allow_ssh" {
name = "allow_ssh"
name = "allow_ssh_${random_id.suffix.hex}"
description = "Allow SSH inbound traffic"

ingress {
from_port = 22
to_port = 22
protocol = "tcp"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

egress {
from_port = 0
to_port = 0
protocol = "-1"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}

lifecycle {
create_before_destroy = true
}
}

resource "aws_security_group" "allow_http" {
name = "allow_http_flask_${random_id.suffix.hex}"
description = "Allow inbound HTTP traffic"

ingress {
from_port = 5000
to_port = 5000
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}

lifecycle {
create_before_destroy = true
}
}

resource "aws_instance" "py_server" {
Expand Down

0 comments on commit e1ad539

Please sign in to comment.