How to setup and apply Terraform stacks with GitHub Actions.
Using Terraform, GitHub Actions, and DuploCloud together is very straightforward. DuploCloud has created a dedicated provider for Terraform and custom GitHub Actions to orchestrate the flow. It is is surprisingly easy and quick to get setup.
Configuration
There are four main actions to run to get a module properly installed and running.
Setup
Just like all of the other actions, we always start with getting DuploCloud and the underlying cloud all setup and authenticated.
Here is a fully working and reusable GitHub Action Workflow. Simply copy this into your workflows.
name:Single TF Moduleon:workflow_dispatch:inputs:cmd:description:Command to runtype:choicedefault:planoptions: - plan - apply - destroyenvironment:description:Environment to deploy totype:environmentdefault:dev01module:description:Module to runtype:choicedefault:tenantoptions: - tenant - services - appworkflow_call:inputs:cmd:description:Command to runtype:stringdefault:planenvironment:description:Environment to deploy totype:stringdefault:dev01module:description:Module to runtype:stringdefault:tenantsecrets:DUPLO_TOKEN:description:Duplo Tokenrequired:truejobs:module:name:${{ inputs.cmd }} ${{ inputs.environment }} ${{ inputs.module }}runs-on:ubuntu-latestenvironment:name:${{ inputs.environment }}url:${{ vars.DUPLO_HOST }}env:DUPLO_TOKEN:${{ secrets.DUPLO_TOKEN }}DUPLO_HOST:${{ vars.DUPLO_HOST }}DUPLO_TENANT:${{ inputs.environment }}# add any global tf args hereTF_CLI_ARGS_apply:-parallelism=1steps: - name:Checkout source codeuses:actions/checkout@v4 - name:Duplo and AWS Setupuses:with:admin:true - name:Terraform Setupuses: - name:TF Validate Moduleuses:with:module:modules/${{ inputs.module }}test:false - name:TF Execute Moduleuses:with:module:modules/${{ inputs.module }}workspace:${{ inputs.environment }}command:${{ inputs.cmd }}
The workflow_dispatch is for running the job manually. The workflow_call is for running the job in another workflow. This is useful to run a series of modules for one module in order. In this workflow we will reuse the single module three times to orchestrate our environment.
name:Apply My Stackon:workflow_dispatch:inputs:environment:description:Environment to deploy totype:environmentdefault:dev01jobs:tenant:# Name is the same b/c it nests the jobs correctly in the uiname:My Stack# run the single module jobuses:./.github/workflows/module.tfsecrets:inheritwith:cmd:applyenvironment:${{ inputs.environment }}module:tenantservices:name:My Stackuses:./.github/workflows/module.tfsecrets:inheritneeds:tenantwith:cmd:applyenvironment:${{ inputs.environment }}module:servicesapp:name:My Stackuses:./.github/workflows/module.tfsecrets:inheritneeds:serviceswith:cmd:applyenvironment:${{ inputs.environment }}module:app