Looking for advice on configuring a secure and reliable CI/CD pipeline for Genesys Cloud CX as Code using Terraform. We are attempting to implement a workflow where terraform plan runs automatically on every Pull Request to validate infrastructure changes, and terraform apply executes only upon merge to the main branch. The challenge lies in managing the state file and backend locking in a way that prevents race conditions between multiple concurrent PRs while ensuring the CI runner has the necessary OAuth tokens to authenticate with the Genesys Cloud API.
Background
Our infrastructure is defined using the Genesys Cloud Terraform Provider. We are using GitHub Actions for our CI/CD pipeline. The goal is to catch drift and configuration errors early in the development process without requiring manual intervention for every change. The team is located in Africa/Lagos, so we need to ensure the pipeline is robust against network latency and potential timeout issues with the Genesys Cloud API endpoints.
Issue
When multiple developers open PRs simultaneously, the terraform plan steps fail with a 409 Conflict error or a state lock acquisition failure. The error message indicates that the state file is locked by another process, but we are not using a remote backend with locking capabilities configured correctly. Additionally, we are seeing intermittent 401 Unauthorized errors during the plan phase, suggesting that the OAuth token generated for the CI runner might be expiring or not being refreshed correctly between the authentication step and the Terraform execution.
Troubleshooting
I have attempted to configure the Terraform backend to use S3 with DynamoDB for state locking, but the GitHub Actions runner lacks the necessary IAM permissions. I have also tried using environment variables for the OAuth token, but the token lifecycle management is proving difficult. Here is a snippet of our current GitHub Actions workflow:
name: Terraform Plan
on: pull_request
jobs:
plan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
- name: Terraform Init
run: terraform init
- name: Terraform Plan
run: terraform plan -out=tfplan
env:
GC_CLIENT_ID: ${{ secrets.GC_CLIENT_ID }}
GC_CLIENT_SECRET: ${{ secrets.GC_CLIENT_SECRET }}
How can we properly configure the backend and token management to support concurrent PRs and reliable apply on merge?