12 Automation Workflows for Cloud Infrastructure
12 cloud infrastructure automation workflows by trigger: plan on PR, drift checks, Ansible dry runs, patching, Spot handling, remediation and teardown.
Cloud infrastructure automation works best as workflows, each tied to a trigger and an outcome: a pull request produces a plan, a merge applies it, a schedule detects drift, an alarm runs a fix, and a calendar patches servers. This guide describes 12 such workflows with the commands and AWS or Terraform details behind them.
Lists of tools are easy to find already, including our own 20 DevOps tools for infrastructure automation and a collection of infrastructure automation scripts. This page takes a different route. It asks what should trigger an action, what the action does, and how you know it worked. Commands were checked against the Terraform, Ansible and AWS documentation in November 2026.
Key takeaways
- A workflow has four parts: a trigger, a check, an action and a record. If any part is missing, the automation is fragile.
- Infrastructure changes should move through a pull request, a reviewed plan and an apply of that same saved plan.
- Scheduled jobs find the problems nobody triggered: drift, missing patches, untagged resources and untested backups.
- Event-driven jobs react to signals such as alarms, Spot interruption notices and scaling events.
- Automate the safe, repeatable steps first, and keep a human approval for anything destructive.
- Every workflow needs an owner and an alert for when it fails, because silent automation is the most dangerous kind.
The 12 workflows at a glance
| # | Workflow | Trigger | Outcome |
|---|---|---|---|
| 1 | Plan on pull request | PR opened or updated | Reviewable plan comment |
| 2 | Apply on merge | Merge to main | Infrastructure matches the reviewed plan |
| 3 | Policy gate | Before apply | Non-compliant changes blocked |
| 4 | Drift detection | Schedule | Ticket or alert for manual changes |
| 5 | Configuration run | Schedule or merge | Servers converge to the declared state |
| 6 | Image bake and rollout | Base image or code change | Fresh immutable instances replace old ones |
| 7 | Patching | Maintenance window | Patch compliance report |
| 8 | Scaling and Spot handling | Metric or interruption notice | Capacity follows demand |
| 9 | Event-driven remediation | Alarm or event | Known fault fixed or escalated |
| 10 | Backup and restore test | Schedule | Proof that recovery works |
| 11 | Tag and cost hygiene | Schedule | Untagged resources found, spend by owner |
| 12 | Non-production shutdown and teardown | Calendar or PR close | Idle environments removed |
Provisioning workflows (1 to 4)
1. Plan on pull request
Every infrastructure change starts as a pull request. The pipeline formats, validates and plans the change, then posts the result so reviewers see exactly what will be created, changed or destroyed.
terraform fmt -check
terraform init -input=false
terraform validate
terraform plan -input=false -out=tfplan
The -input=false flag stops Terraform from prompting for missing variables, which is what you want in automation. -out writes the plan to a file so that the same plan can be applied later. The plan file contains your full configuration and input variable values, so treat it as sensitive and do not publish it as a public build artifact. Managed Terraform services run the same idea as a speculative plan, which cannot change infrastructure.
2. Apply on merge
When the pull request merges, the pipeline applies the reviewed plan, not a fresh one.
terraform apply tfplan
Terraform's documentation describes this two-step plan then apply flow as mainly intended for automation, and it adds a reminder: because other changes may happen between plan and apply, review the final plan before applying. Use state locking, and set -lock-timeout so two pipelines queue rather than fail. Protect the production branch and require approval on the apply job for production environments. For tool choices, see 12 infrastructure as code tools ranked.
3. Policy gate before apply
A policy check reads the plan and rejects changes that break your rules, for example public storage, unencrypted volumes, instances outside approved sizes or Regions, and missing owner tags. Running it in the pipeline means the rule applies to everyone equally and the failure message arrives while the author still has the change open. Cloud-side guardrails complement it by blocking the same actions at the account level. Examples of policy tooling are in 12 tools to automate cloud security policies.
4. Drift detection on a schedule
Drift is the gap between what your code says and what exists, usually caused by a console edit during an incident. Detect it with a scheduled plan that fails when changes are found:
terraform plan -input=false -detailed-exitcode
With -detailed-exitcode, exit code 0 means no changes, 1 means an error, and 2 means changes are present. Have the scheduled job open a ticket or send an alert on exit code 2. Terraform can also run a refresh-only plan (-refresh-only, available from v0.15.4), which updates state to match real resources without proposing code changes, useful when you decide to accept a manual change. Run drift jobs per environment, and decide for each finding whether to revert the resource or update the code.
Configuration and image workflows (5 and 6)
5. Configuration run with a dry run first
Configuration management keeps what runs inside servers consistent: packages, files, services and users. Run it on merge and on a schedule, and use Ansible's check mode as the dry run:
ansible-playbook site.yml --check --diff --limit web01.example.com
Check mode simulates the run without changing remote systems, and --diff shows before and after for modules that support it. Know its limits: modules that do not support check mode report nothing and do nothing, and tasks that depend on registered results of earlier tasks may show no output. A task option, check_mode, lets you override behavior per task. If a diff could expose a secret, set diff: false on that task. Playbook patterns are in automating server configuration with Ansible playbooks.
6. Image bake and rollout
The most reliable server is one nobody logs into. Bake machine images or container images in the pipeline, test them, publish a new version and replace running instances with fresh ones. Patching, base-image updates and application releases then all use the same path. A rollout can be gradual, with part of the fleet replaced first and health checks gating the rest. The idea behind this is immutability: change by replacing, not by editing. See 15 cloud deployment strategies for rollout patterns.
Operations workflows (7 to 10)
7. Patching in a maintenance window
On AWS, Systems Manager Patch Manager automates patching for EC2 instances, on-premises servers and virtual machines. The core concepts are patch baselines, which define which patches are approved, and two operations: Scan, which only reports missing patches, and Scan and install. You can run them through a patch policy in Quick Setup, a maintenance window, or an on-demand Patch now run. A safe rhythm is to scan daily, install in a maintenance window on a canary group first, then on the rest, using the rate controls for concurrency and error thresholds.
Two cautions from the documentation: AWS does not test patches before making them available in Patch Manager, and being compliant with a baseline does not by itself mean a server is secure. Predefined baselines are provided as examples, so create custom baselines for control. For single Linux hosts, automating package updates in Linux shows the native tools.
8. Scaling and Spot interruption handling
Let metrics add and remove capacity instead of paging a person. Scaling policies react to load, and scheduled scaling covers predictable peaks. If you use Spot Instances, automate the interruption path: EC2 issues a two-minute warning before stopping or terminating a Spot Instance, available as an EventBridge event and in instance metadata, and AWS recommends checking the metadata every five seconds. Use that window to drain connections, checkpoint work and deregister from the load balancer. Notices are best effort, so design the application to survive an interruption without a warning. The mechanics are explained in how AWS Auto Scaling works.
9. Event-driven remediation
Some faults have a known, safe fix: restart a stuck service, rotate a full disk, remove a public rule that someone added. Wire the alarm to the fix through an event bus and a function or runbook, and log what ran. Start with notification only, then automate the fix once you trust the signal. Always add a limit so a loop cannot run forever, and escalate to a person when the fix fails twice. A walkthrough is in automating CloudWatch events with Lambda, and alarm design in automating cloud alerts with CloudWatch.
10. Backup and restore testing
Automate the backup and also the proof that it works. A scheduled job takes snapshots, copies them to a second account or Region, applies retention, and then restores a recent one into an isolated environment and runs a smoke test. Alert on a failed backup and on a failed restore test. Record how long the restore took, since that number is your real recovery time. Without the restore step you only know that files exist.
Governance workflows (11 and 12)
11. Tag and cost hygiene
Tags make cost and ownership visible. Enforce required tags at creation through the policy gate in workflow 3, and run a scheduled scan for resources that slipped through. On AWS, tags must be activated as cost allocation tags before they appear in Cost Explorer or cost reports, and the documentation says tags can take up to 24 hours to appear in the Billing and Cost Management console. Send each team a weekly summary of its own spend and its untagged resources. Techniques for acting on that data are in 12 cost optimization strategies for cloud DevOps.
12. Non-production shutdown and teardown
Development and test environments rarely need to run overnight or at weekends. Schedule them to stop outside working hours and start before the team arrives. For pull-request environments, create the environment when the PR opens and destroy it when the PR closes, with a time limit as a safety net for forgotten ones. Make destruction visible: log what was removed and require a tag such as ephemeral=true before any automation may delete a resource.
A practical build order
| Phase | Workflows | Why this order |
|---|---|---|
| First month | 1, 2, 11 | Gets changes under review and ownership visible |
| Second month | 4, 7, 10 | Finds hidden problems on a schedule |
| Third month | 3, 5, 12 | Adds guardrails and savings once the base is stable |
| After that | 6, 8, 9 | Needs trust in monitoring and good images |
Safety rules that apply to every workflow
- Run automation with its own identity and minimum permissions, never a personal admin key.
- Put an approval step before anything that deletes data or touches production.
- Use idempotent steps, so running a job twice gives the same result.
- Log what each run did and alert on failures and on runs that did not happen.
- Keep a manual path that works when the automation is broken, and practise it.
Practise these workflows
Our Terraform course builds workflows 1 to 4 end to end, and the Ansible course covers workflow 5. Classroom batches run in Kharadi, Pune, with live online batches across India.
Official references
- terraform plan command reference
- Ansible validating tasks: check mode and diff mode
- AWS Systems Manager Patch Manager
- Spot Instance interruption notices
- AWS cost allocation tags
FAQs
What is cloud infrastructure automation?
It is the practice of creating, changing, securing and operating cloud resources through code and pipelines instead of manual console work. Triggers such as merges, schedules and alarms start jobs that act on the infrastructure and record what they did.
Which workflow should I automate first?
Start with plan on pull request and apply on merge for your infrastructure code. That puts every change under review and gives you history. Tag enforcement and a drift check are good second steps.
Why apply a saved plan instead of running apply directly?
A saved plan guarantees that what you apply is what the reviewers saw. Terraform notes that other changes may occur between plan and apply, so reviewing the final plan before applying remains good practice.
How do I detect configuration drift?
Run terraform plan -detailed-exitcode on a schedule. Exit code 2 means changes are present, which indicates drift or pending code changes, and your job can raise an alert or ticket on that code.
What does Ansible check mode do?
It runs a playbook as a simulation without changing remote systems. Combined with --diff, it shows what would change. Modules that do not support check mode report nothing, so treat it as a strong hint rather than a guarantee.
How should patching be automated on AWS?
Use Systems Manager Patch Manager with custom patch baselines, scan regularly, and install through a patch policy or maintenance window. Patch a small group first, use rate controls, and review compliance reports afterwards.
Is it safe to automate remediation?
Yes for well-understood faults. Begin with alerts only, add the automated fix once the signal is reliable, cap retries, log every action, and escalate to a person when the fix does not work.
How many workflows do I need before calling it mature?
There is no magic number. A team that has reviewed changes, scheduled drift and patch checks, tested restores and tag-based cost visibility has covered the essentials, even if only half of the twelve are automated.
What's Your Reaction?
Like
0
Dislike
0
Love
0
Funny
0
Angry
0
Sad
0
Wow
0