Skip to main content

Terraform CI/CD Pipeline

Build a complete Terraform CI/CD pipeline with plan-on-PR, apply-on-merge, policy checks, cost estimation, and environment promotion.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt
Build a Terraform CI/CD pipeline using GitHub Actions for multi-service AWS infrastructure with VPC, ECS, RDS, and S3. The team has 4 engineers engineers and follows trunk-based with feature branches workflow. Create: 1) PR pipeline (runs on every PR): terraform fmt --check, terraform validate, tflint with AWS plugin rules plus custom naming convention rules, checkov for security policy violations, terraform plan with output saved as artifact and formatted as PR comment. 2) Apply pipeline (runs on merge to main): retrieve the saved plan, terraform apply -auto-approve with the exact plan from PR review. Add Slack message to #infrastructure with summary of changes notification on success/failure. 3) Environment promotion: dev auto-applies, staging needs team-lead approval, production needs 2 approvals — after successful apply to dev, automatically trigger plan for staging and await GitHub Environment protection rules with required reviewers before applying. 4) State management within the pipeline: GitHub concurrency groups per environment + DynamoDB lock to prevent concurrent applies, with pipeline queuing for sequential execution. 5) Secrets handling: inject AWS credentials, Terraform Cloud token, Slack webhook URL using GitHub Actions OIDC with AWS IAM role assumption without exposing them in logs — mask sensitive values in plan output. 6) Drift detection scheduled pipeline: run every 6 hours to detect manual changes, report to Slack #infra-drift and GitHub issue auto-creation. 7) Rollback strategy: revert the Git commit and re-run apply pipeline — maintain last known good state and provide a manual trigger to revert. 8) Pipeline permissions: use OIDC federation with separate roles for plan (read-only) and apply (write) with short-lived credentials scoped to minimum required permissions per pipeline stage.

What this prompt does

This prompt specifies a complete Terraform CI/CD pipeline on [ci_platform] so infrastructure changes become routine and reviewable. You give it [infrastructure_scope], [team_size], and your [workflow_type], and it designs two pipelines: a PR pipeline running fmt, validate, tflint with [tflint_rules], [security_scanner], and a plan posted as a comment; and an apply pipeline that runs on merge to [main_branch] using the exact saved plan.

The structure works because safe IaC delivery is a sequence of gates. The plan you review is the plan that applies, drift is caught on a schedule via [drift_schedule], and promotion from [first_env] to [next_env] waits on [approval_mechanism]. By also specifying [state_locking_strategy] to prevent concurrent applies, secret handling through [secret_source], a [rollback_approach], and an [iam_strategy] using short-lived OIDC credentials, the prompt covers the failure modes that make naive pipelines dangerous. The [team_size] and [workflow_type] inputs tune how strict the gating should be, since a small trunk-based team needs less ceremony than a large org with formal change control.

When to use it

  • You are moving from local terraform apply to a reviewed, automated pipeline
  • You want plan-on-PR and apply-on-merge so every change is seen before it lands
  • You need policy and security scanning wired in via [security_scanner]
  • You want environment promotion gated by [approval_mechanism] per stage
  • You need short-lived credentials instead of long-lived AWS keys in CI
  • You want scheduled drift detection reporting to [drift_report_destination]
  • You need a clear rollback path when an apply goes wrong

Example output

Expect two pipeline definitions for [ci_platform]: a PR workflow with each check (fmt, validate, tflint with [tflint_rules], [security_scanner]) as a step and the plan formatted as a comment, and an apply workflow that retrieves the saved plan and applies it with notifications via [apply_notification]. You also get a promotion configuration implementing [promotion_workflow], a [state_locking_strategy] setup, a secrets section using [secret_source] with sensitive-value masking, a scheduled drift job reporting to [drift_report_destination], and an [iam_strategy] defining separate read-only plan and write apply roles. The rollback section spells out the [rollback_approach] so reverting is a known procedure, not an improvisation.

Pro tips

  • Use [secret_source] with OIDC federation rather than stored static keys — short-lived role assumption is the single biggest security win here
  • Split [iam_strategy] into a read-only plan role and a write apply role so a compromised PR pipeline cannot mutate infrastructure
  • Make [promotion_workflow] match reality: auto-apply dev, but require the real number of approvals for production
  • Save the plan as an artifact and apply that exact plan on merge — applying a freshly generated plan defeats the review
  • Set [state_locking_strategy] with both CI concurrency groups and a backend lock so two applies cannot race
  • Run [security_scanner] as a blocking check, not an advisory one, or insecure config will sail through under deadline pressure
  • Tune [drift_schedule] to your needs and route results to [drift_report_destination] somewhere people actually look, or the detection is wasted

Frequently Asked Questions

Does it work outside GitHub Actions?
The default `[ci_platform]` is GitHub Actions, but the pipeline structure applies to GitLab CI, CircleCI, or others. You would translate the workflow syntax and adjust `[approval_mechanism]` and `[secret_source]` to that platform's equivalents, keeping the plan-on-PR and apply-on-merge flow intact.
How does it prevent two applies from running at once?
Through `[state_locking_strategy]`, which combines CI concurrency groups per environment with a backend lock such as DynamoDB. The pipeline queues runs so applies execute sequentially, avoiding the state corruption that concurrent applies can cause.
Is the rollback strategy automatic?
No. The `[rollback_approach]` is typically reverting the Git commit and re-running the apply pipeline, which is a manual trigger. Terraform has no true undo, so rollback means applying a previous known-good configuration rather than reversing a transaction.
Why use OIDC instead of stored AWS keys?
OIDC federation issues short-lived credentials scoped per pipeline stage, so there are no long-lived secrets to leak. Combined with separate plan and apply roles in the `[iam_strategy]`, it limits the blast radius if any single workflow is compromised.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in Terraform & Infrastructure as Code Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

AI assistant · trained on my work

👋

Hey there!

Quick Actions

WhatsApp Direct line to me

Chat on WhatsApp

+880 1723 741224 · Replies within the hour on working days

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

mejba.13@gmail.com

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support