Skip to content

Commit

Permalink
Update terraform-apply.yml
Browse files Browse the repository at this point in the history
try some magic with statefiles and buckets
  • Loading branch information
mihai-satmarean authored Dec 5, 2024
1 parent 2c69490 commit 0ed4092
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions .github/workflows/terraform-apply.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,42 @@
name: Terraform apply
name: Terraform Apply with Remote State
run-name: ${{ github.actor }} is deploying on AWS 🚀
on:
push:
branches:
- feature/aws-s3-module
- mihai-satmarean-patch-1
jobs:
Deploy-AWS-Ec2:
runs-on: ubuntu-latest
steps:
# Step 1: Configure AWS credentials
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v3
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_MARIUS }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_MARIUS }}
aws-region: eu-central-1

# Step 2: Checkout the repository code
- name: Checkout Repository
uses: actions/checkout@v4
- name: Deploy Infrastructure

# Step 3: Ensure Terraform state S3 bucket exists
- name: Create Terraform State Bucket
run: |
cd terraform/terraform-modules/tf-ec2-module/
cd terraform/terraform-modules/state-bucket
terraform init
terraform destroy -auto-approve
terraform apply -auto-approve
# Step 4: Reconfigure Backend to Use S3
- name: Reconfigure Backend to S3
run: |
cd terraform/terraform-modules/tf-ec2-module/
BUCKET_NAME=$(terraform output -raw backend_config | grep bucket | cut -d '=' -f2 | tr -d ' ')
terraform init -backend-config="bucket=${BUCKET_NAME}" \
-backend-config="key=state/${GITHUB_REF#refs/heads/}/terraform.tfstate" \
-backend-config="region=eu-central-1"
# Step 5: Apply Terraform with S3 backend
- name: Finalize Infrastructure Deployment
run: |
cd terraform/terraform-modules/tf-ec2-module/
terraform apply -auto-approve

0 comments on commit 0ed4092

Please sign in to comment.