I am using GitHub Actions to deploy my code using Terraform. Whenever code is pushed to the Testing
branch, a GitHub Action is triggered that builds the code and runs terraform apply
. This works well.
The problem is that now I want to have a Prod
environment too. Whenever code is pushed to the Prod
branch, it should be built using its own s3 remote backend and its AWS account. The problem I’m having is that I am not sure how to configure my terraform files so that terraform can use the backend for Prod to store the state file.please anyone who is able to help.Im not sure at this point how to set that up. here is the sample of my code
name: “Terraform-Apply-Action”
on:
push:
branches:
- prod
jobs:
terraform:
name: “Terraform”
runs-on: ubuntu-latest
env:
AWS_DEFAULT_REGION: “us-east-1”
AWS_S3_BUCKET: {{ secrets.AWS_S3_BUCKET }}
AWS_ACCESS_KEY_ID: {{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY}}
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
- name: Terraform Init
id: Init
run: terraform init
- name: Terraform Plan
id: plan
if: github.event_name == 'push'
run: terraform plan -no-color
continue-on-error: true
- name: Terraform Plan Status
if: steps.plan.outcome == 'failure'
run: exit 1
- name: Terraform Apply
run: terraform apply -auto-approve