From 92b434f57e56934ff10ae4507506bcc7271b20ed Mon Sep 17 00:00:00 2001 From: Devansh Tyagi Date: Sat, 20 Jun 2026 21:50:24 +0530 Subject: [PATCH] Isolate Terraform state per module so both can be deployed --- .github/workflows/tf-apply-main.yml | 4 ++-- .github/workflows/tf-destroy-main.yml | 2 +- modules/module-1/main.tf | 4 ++-- modules/module-2/main.tf | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/tf-apply-main.yml b/.github/workflows/tf-apply-main.yml index 33c7b552..76ca4262 100644 --- a/.github/workflows/tf-apply-main.yml +++ b/.github/workflows/tf-apply-main.yml @@ -46,7 +46,7 @@ jobs: id: check run: | echo ${{ env.ACCOUNT_ID }} - aws s3api head-object --bucket do-not-delete-awsgoat-state-files-${{ env.ACCOUNT_ID }} --key terraform.tfstate + aws s3api head-object --bucket do-not-delete-awsgoat-state-files-${{ github.event.inputs.module }}-${{ env.ACCOUNT_ID }} --key terraform.tfstate continue-on-error: true - name: Exit if previous deployment exists @@ -91,7 +91,7 @@ jobs: if: always() run: | cd modules/${{ github.event.inputs.module }} - aws s3 cp ./terraform.tfstate s3://do-not-delete-awsgoat-state-files-${{ env.ACCOUNT_ID }}/terraform.tfstate + aws s3 cp ./terraform.tfstate s3://do-not-delete-awsgoat-state-files-${{ github.event.inputs.module }}-${{ env.ACCOUNT_ID }}/terraform.tfstate # Terraform Output the API Gateway url - name: Application URL diff --git a/.github/workflows/tf-destroy-main.yml b/.github/workflows/tf-destroy-main.yml index e6d76efb..2b6d840d 100644 --- a/.github/workflows/tf-destroy-main.yml +++ b/.github/workflows/tf-destroy-main.yml @@ -46,7 +46,7 @@ jobs: - name: Retrieve tfstate run: | cd modules/${{ github.event.inputs.module }} - aws s3 cp s3://do-not-delete-awsgoat-state-files-${{ env.ACCOUNT_ID }}/terraform.tfstate ./terraform.tfstate + aws s3 cp s3://do-not-delete-awsgoat-state-files-${{ github.event.inputs.module }}-${{ env.ACCOUNT_ID }}/terraform.tfstate ./terraform.tfstate # Initialize a new Terraform working directory - name: Terraform Init diff --git a/modules/module-1/main.tf b/modules/module-1/main.tf index 6f522011..4c1636f2 100644 --- a/modules/module-1/main.tf +++ b/modules/module-1/main.tf @@ -3417,7 +3417,7 @@ resource "aws_s3_object" "upload_temp_object_2" { } /* Creating a S3 Bucket for Terraform state file upload. */ resource "aws_s3_bucket" "bucket_tf_files" { - bucket = "do-not-delete-awsgoat-state-files-${data.aws_caller_identity.current.account_id}" + bucket = "do-not-delete-awsgoat-state-files-module-1-${data.aws_caller_identity.current.account_id}" force_destroy = true tags = { Name = "Do not delete Bucket" @@ -3598,7 +3598,7 @@ data "aws_ami" "goat_ami" { resource "aws_instance" "goat_instance" { ami = data.aws_ami.goat_ami.id - instance_type = "t2.micro" + instance_type = "t3.micro" iam_instance_profile = aws_iam_instance_profile.goat_iam_profile.name subnet_id = aws_subnet.goat_subnet.id security_groups = [aws_security_group.goat_sg.id] diff --git a/modules/module-2/main.tf b/modules/module-2/main.tf index 3ef5ad36..bc9e42b7 100644 --- a/modules/module-2/main.tf +++ b/modules/module-2/main.tf @@ -350,7 +350,7 @@ data "aws_ami" "ecs_optimized_ami" { resource "aws_launch_template" "ecs_launch_template" { name_prefix = "ecs-launch-template-" image_id = data.aws_ami.ecs_optimized_ami.id - instance_type = "t2.micro" + instance_type = "t3.micro" iam_instance_profile { name = aws_iam_instance_profile.ecs-instance-profile.name @@ -514,7 +514,7 @@ EOF /* Creating a S3 Bucket for Terraform state file upload. */ resource "aws_s3_bucket" "bucket_tf_files" { - bucket = "do-not-delete-awsgoat-state-files-${data.aws_caller_identity.current.account_id}" + bucket = "do-not-delete-awsgoat-state-files-module-2-${data.aws_caller_identity.current.account_id}" force_destroy = true tags = { Name = "Do not delete Bucket"