Day 5 of 5
⏱ ~60 minutes
Terraform in 5 Days — Day 5

CI/CD with Terraform

Run Terraform in GitHub Actions, use Terraform Cloud for remote state, and implement plan-on-PR, apply-on-merge.

GitHub Actions Workflow

.github/workflows/terraform.yml
name: Terraform

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  terraform:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4

    - uses: hashicorp/setup-terraform@v3
      with:
        terraform_version: 1.7.0
        cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}

    - name: Terraform Init
      run: terraform init

    - name: Terraform Plan
      run: terraform plan -no-color
      # On PRs, plan output is posted as a comment

    - name: Terraform Apply
      if: github.ref == 'refs/heads/main' && github.event_name == 'push'
      run: terraform apply -auto-approve
Terraform Cloud Setup
# 1. Create account at app.terraform.io
# 2. Create organization and workspace
# 3. Add backend config:
terraform {
  cloud {
    organization = "your-org"
    workspaces {
      name = "production"
    }
  }
}
# 4. Store AWS credentials as workspace variables
# 5. Generate API token → store as GitHub secret TF_API_TOKEN
ℹ️
The pull-request → plan, merge → apply workflow is the gold standard for infrastructure changes. Every change is reviewed before it lands. The plan output on the PR shows exactly what will change. This prevents accidental destroys.
📝 Day 5 Exercise
Set Up Terraform CI/CD
  1. C
  2. r
  3. e
  4. a
  5. t
  6. e
  7. a
  8. G
  9. i
  10. t
  11. H
  12. u
  13. b
  14. r
  15. e
  16. p
  17. o
  18. w
  19. i
  20. t
  21. h
  22. y
  23. o
  24. u
  25. r
  26. T
  27. e
  28. r
  29. r
  30. a
  31. f
  32. o
  33. r
  34. m
  35. c
  36. o
  37. n
  38. f
  39. i
  40. g
  41. .
  42. S
  43. e
  44. t
  45. u
  46. p
  47. T
  48. e
  49. r
  50. r
  51. a
  52. f
  53. o
  54. r
  55. m
  56. C
  57. l
  58. o
  59. u
  60. d
  61. f
  62. o
  63. r
  64. r
  65. e
  66. m
  67. o
  68. t
  69. e
  70. s
  71. t
  72. a
  73. t
  74. e
  75. .
  76. C
  77. r
  78. e
  79. a
  80. t
  81. e
  82. t
  83. h
  84. e
  85. G
  86. i
  87. t
  88. H
  89. u
  90. b
  91. A
  92. c
  93. t
  94. i
  95. o
  96. n
  97. s
  98. w
  99. o
  100. r
  101. k
  102. f
  103. l
  104. o
  105. w
  106. .
  107. O
  108. p
  109. e
  110. n
  111. a
  112. P
  113. R
  114. ,
  115. v
  116. e
  117. r
  118. i
  119. f
  120. y
  121. t
  122. h
  123. e
  124. p
  125. l
  126. a
  127. n
  128. r
  129. u
  130. n
  131. s
  132. .
  133. M
  134. e
  135. r
  136. g
  137. e
  138. t
  139. o
  140. m
  141. a
  142. i
  143. n
  144. ,
  145. v
  146. e
  147. r
  148. i
  149. f
  150. y
  151. a
  152. p
  153. p
  154. l
  155. y
  156. r
  157. u
  158. n
  159. s
  160. .

Day 5 Summary

  • terraform plan on every PR = infrastructure changes reviewed before merge.
  • terraform apply -auto-approve on main merge = automatic deployment.
  • Terraform Cloud manages state remotely and stores credentials securely.
  • TF_API_TOKEN secret authenticates the CI runner to Terraform Cloud.
Finished this lesson?