How to create an ephemeral test environment with Terraform and GitHub Actions
This tutorial shows how to create an ephemeral (temporary) test environment in Azure using Terraform and GitHub Actions, useful to validate isolated changes in pull requests and reduce interference in shared environments. We will explain the why and provide a practical flow to create, test and destroy infrastructures automatically.
Prerequisites
- Azure account with permissions to create resources (Contributor on a Subscription).
- Install Azure CLI and Terraform locally for testing.
- GitHub repository with permissions for Actions and configured secrets (AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_TENANT_ID, AZURE_SUBSCRIPTION_ID).
- Basic knowledge of Terraform, Git and YAML.
Step 1: Why an ephemeral environment for PRs
Ephemeral environments allow each pull request to have its own copy of infra to test features without affecting the common environment. This avoids conflicts, facilitates manual/automated validation and reduces costs because the infra is removed after testing.
Step 2: Minimal Terraform structure
Create a folder in the repository, for example infra/pr-env, with a simple main.tf that creates a Resource Group and an App Service Plan + Web App as an example.
provider "azurerm" {
features {}
}
variable "env_name" {}
variable "location" { default = "northeurope" }
resource "azurerm_resource_group" "rg" {
name = "rg-${var.env_name}"
location = var.location
}
resource "azurerm_app_service_plan" "plan" {
name = "plan-${var.env_name}"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
sku {
tier = "Basic"
size = "B1"
}
}
resource "azurerm_app_service" "app" {
name = "web-${var.env_name}"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
app_service_plan_id = azurerm_app_service_plan.plan.id
}
Step 3: Outputs and remote state
Define useful outputs (e.g.: URL) and configure Terraform remote backend (e.g.: Azure Storage Account) to share state between runs. Here is an example output.
output "app_default_site_hostname" {
value = azurerm_app_service.app.default_site_hostname
}
Step 4: GitHub Actions to create and destroy (workflow)
Create a YAML workflow that runs for pull_request: create the environment on the opened or synchronize event and destroy on the closed event. Use login with a service principal via Azure/login and run terraform init/apply/destroy.
name: PR ephemeral env
on:
pull_request:
types: [opened, synchronize, closed]
jobs:
terraform:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Login Azure
uses: azure/login@v1
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
client-secret: ${{ secrets.AZURE_CLIENT_SECRET }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_version: 1.5.0
- name: Terraform Init
working-directory: infra/pr-env
run: terraform init -input=false
- name: Terraform Apply for PR
if: ${{ github.event.action != 'closed' }}
working-directory: infra/pr-env
run: |
ENV_NAME=pr-${{ github.event.number }}
terraform plan -var "env_name=${ENV_NAME}" -out=plan.tfplan
terraform apply -auto-approve plan.tfplan
- name: Terraform Destroy for closed PR
if: ${{ github.event.action == 'closed' }}
working-directory: infra/pr-env
run: |
ENV_NAME=pr-${{ github.event.number }}
terraform destroy -auto-approve -var "env_name=${ENV_NAME}"
Step 5: Variables, naming and resource cleanup
Use a consistent prefix and limit the lifetime: you can combine immediate destruction when a PR closes and a nightly job that removes old environments by age. Check quotas and naming to avoid collisions.
Verify the result
Open a pull request and watch the Actions run: there should be a run that does terraform init/plan/apply and, at the end, outputs appear in the logs (hostname). After closing the PR, confirm the destroy run executes and that the Resource Group is removed in the Azure portal or via az cli:
az group show -n rg-pr-123 --subscription $AZURE_SUBSCRIPTION_ID
# should fail if removed
Conclusion
With this flow you have a practical example of how to create ephemeral environments with Terraform and GitHub Actions to validate PRs automatically. Next steps: add automated tests (e.g.: integration tests), secure secrets with OIDC (replace service principal) and use a lifecycle policy for critical resources. Tip: start with low-cost resources (B1) and validate quotas before scaling up.