Managing AWS Resources with Terraform
This lesson explains how HashiCorp Terraform, which the exam guide names as an example third-party tool, manages AWS resources, and how CloudOps engineers use and manage it.
Learning outcomes
After completing this lesson, you will be able to:
- Describe providers, configuration and the plan and apply workflow.
- Store and lock state safely in S3.
- Resolve drift and common errors.
Providers, configuration and the workflow
Terraform configuration is written in HashiCorp Configuration Language (HCL). The AWS provider translates resources such as aws_instance and aws_s3_bucket into AWS API calls, authenticating with the standard credential chain (environment variables, profiles, instance or task roles, or an assumed role declared in the provider). The workflow is terraform init (download providers and modules, configure the backend), terraform plan (show proposed changes), terraform apply (make them) and terraform destroy. Modules package reusable configurations, and workspaces or separate state files separate environments.
terraform {
required_providers { aws = { source = "hashicorp/aws", version = "~> 6.0" } }
backend "s3" {
bucket = "acme-terraform-state"
key = "network/prod/terraform.tfstate"
region = "eu-west-1"
encrypt = true
use_lockfile = true # S3-native state locking
}
}
provider "aws" {
region = "eu-west-1"
assume_role { role_arn = "arn:aws:iam::111122223333:role/TerraformDeploy" }
}Terraform commands
State management
Terraform records the mapping between configuration and real resources in a state file. State must be shared and protected: store it in an S3 backend with versioning (to recover from corruption), encryption (state can contain secrets) and restrictive bucket policies, and enable state locking so two people cannot apply at once. Current Terraform versions lock with a lock file in S3 (use_lockfile); DynamoDB-based locking is deprecated but still found in older configurations. A stuck lock after a crashed run is removed with terraform force-unlock. Existing resources are adopted with terraform import or import blocks, and resources can be renamed in state with moved blocks to avoid replacement.
The following diagram shows two engineers applying at once against a locked backend.

Managing Terraform state
Drift and troubleshooting
Manual changes appear as differences in terraform plan, which will revert them on apply unless the configuration is updated; terraform plan -refresh-only reconciles state with reality without changing resources. Common problems include AccessDenied from the deployment role (check its policies and SCPs), state lock errors, resources created outside Terraform conflicting with names, and provider version changes. The following table compares Terraform with CloudFormation from an operations viewpoint.
Two engineers apply at once
Two engineers ran terraform apply on the same configuration from their laptops with local state, and each overwrote the other's changes; one apply deleted a security group rule the other had just added. The team moves state to an S3 backend with versioning, SSE-KMS encryption, a bucket policy limited to the deployment role and use_lockfile enabled, and runs Terraform only from a CI job that assumes the deployment role. Concurrent runs now wait for the lock.
How does Terraform adopt an existing resource into its state?
Show answer
With import.
Exam Tips
Remember for the exam
- Store Terraform state in S3 with versioning, encryption and locking.
- S3-native lock files replace DynamoDB-based locking, which is deprecated.
- terraform plan shows changes and drift; import adopts existing resources.
- Use roles, not long-term keys, for Terraform's AWS credentials.