Lesson 2 of 3 AWS CDK, Terraform and Git

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" }
}
Match the pairs

Terraform commands

Exam skills 3.1.6

Match each Terraform command with what it does.

Log in to try this activity Interactive activities are free with an account.

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.

Terraform remote state with locking. State lives in an S3 backend with versioning and encryption, and use_lockfile creates a lock file in S3 during an apply. When two engineers apply at once, the second cannot take the lock and waits, so the state is not corrupted; terraform force-unlock removes a lock left by a crashed run.
Without locking, both applies would write the state and one set of changes would be lost.
Select all that apply

Managing Terraform state

Exam skills 3.1.6

Identify good practices for Terraform state on AWS.

Log in to try this activity Interactive activities are free with an account.

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.

CloudFormation / CDK
StateManaged by AWS in the stack
PreviewChange sets, cdk diff
DriftDrift detection
LockingBuilt in per stack
ScopeAWS (plus registry extensions)
Terraform
StateState file you store (S3 backend)
Previewterraform plan
Driftplan / refresh-only
LockingS3 lock file (DynamoDB legacy)
ScopeMany providers, including AWS
Worked example

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.

Check yourself

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.