Infrastructure as Code
Defining infrastructure in version-controlled configuration instead of clicking through a console: reproducible, reviewable, and diffable.
- Minutes to read
- 4
- Practice MCQs
- 25
- Interview Q&A
- 25
- Edition
- v7
- Editorial status
- Reviewed
Scope: Terraform language and CLI documentation accessed 2026-08-30; OpenTofu 1.13 state encryption; current AWS CloudFormation change-set and drift behavior..
Curated: · Written: · Reviewed:
MCQ-1A platform team's deploy script creates 30 AWS resources with ordered CLI calls. A run that fails at resource 12 leaves the operator unable to tell what exists, and re-running the script fails on the resources already created. The team rewrites the same resources as a declarative Terraform configuration. Which property lets an unchanged re-run finish the remaining resources?
Answer to see the discussion.
MCQ-2A team's Terraform 1.x configuration declares four resources. One of them, `aws_instance.web`, is renamed to `aws_instance.app`; its arguments (AMI, instance type, subnet) are unchanged, the other three resources are untouched, and no `moved` block is added. They run `terraform plan` against the state written by the last apply. What does the plan summary report?
Answer to see the discussion.
MCQ-15A team renames the resource block `aws_instance.app` to `aws_instance.web` in the same root module and runs `terraform plan`. The plan reports `Plan: 1 to add, 0 to change, 1 to destroy`, and the instance's root EBS volume holds 380 GB of data with no snapshot from the last 24 hours. All configuration changes must go through pull-request review and CI, and engineers have no direct access to the state backend. What should the team do?
Answer to see the discussion.
MCQ-14An S3 bucket in production, `acme-logs-prod`, was created by hand and carries the tags `team=platform` and `owner=ops`. A team adds this block: ```hcl resource "aws_s3_bucket" "logs" { bucket = "acme-logs-prod" tags = { team = "platform" } } ``` then runs `terraform import aws_s3_bucket.logs acme-logs-prod` and `terraform plan`. What does the plan report?
Answer to see the discussion.
MCQ-13A production security group is managed by Terraform. The config declares exactly two ingress rules: 443 from 0.0.0.0/0 and 8080 from 10.0.0.0/8. The state file records those same two rules. During an incident, an engineer added a third rule through the AWS console: 22 from 10.20.0.0/16, and nobody touched the config or the state file. The team then runs `terraform plan` with default settings. What does the plan report?
Answer to see the discussion.
MCQ-21A stack `media-prod` defines 9 resources. An engineer changes one resource's configuration directly in its service console. The team runs drift detection on the stack. CloudFormation reports that resource as `NOT_CHECKED` and the stack-level drift status as `IN_SYNC`, though the engineer expected `DRIFTED`. Why is the stack `IN_SYNC`?
Answer to see the discussion.
MCQ-5A team's pipeline runs `terraform plan -out=tfplan` on merge, posts the plan output for review, and runs `terraform apply tfplan` in a separate job once a reviewer approves. A teammate argues the saved file is pointless: the apply job could re-run `terraform plan` and apply the fresh result. What does applying the saved file actually buy the team?
Answer to see the discussion.
MCQ-18A team runs `terraform plan -out=prod.tfplan` on Monday against a workspace whose state serial is 41. The plan records three changes: an EC2 instance's `instance_type` from `t3.large` to `m5.large`, and two tag updates. On Tuesday an on-call engineer changes that instance's `instance_type` to `c5.xlarge` in the AWS console. No other Terraform run touches the workspace. On Wednesday the team runs `terraform apply prod.tfplan`. What happens?
Answer to see the discussion.
MCQ-3A team runs Terraform from CI against an S3 backend with DynamoDB state locking. Job A starts `terraform apply` at 10:00:00 and holds the lock for 12 minutes. Job B starts at 10:00:03 with `-lock-timeout=5m` and no other lock flags. What is Job B's outcome?
Answer to see the discussion.
MCQ-24Terraform 1.7.2, S3 backend `acme-tf/prod/terraform.tfstate` with DynamoDB locking. At 09:14:02 the CI job `deploy-prod-4821` acquired the state lock and began an apply. At 09:14:31 an engineer's `terraform plan` fails with `Error acquiring the state lock`, reporting lock ID `9d1f7c2a-...`, `Who: deploy-prod-4821@ip-10-0-3-77`, `Created: 09:14:02`. `kubectl get pod deploy-prod-4821` shows the pod still `Running`. What should the engineer do?
Answer to see the discussion.
MCQ-23Terraform 1.7 with an S3 backend. A refactor commit deleted `aws_db_instance.orders` and added `aws_db_instance.orders_v2` with `identifier = "orders-prod-2"`, and dropped `aws_iam_role.legacy_sync` from the config. The apply destroyed `orders-prod` after writing final snapshot `orders-prod-final`, created `orders-prod-2`, and destroyed the IAM role. Production must return to the pre-apply configuration with the 400 GB of order data intact. Which sequence does that?
Answer to see the discussion.
MCQ-6A team manages `aws_db_instance.orders` with Terraform. The instance's `identifier` is `orders-prod`, it holds 1.8 TB, and it is unencrypted. To meet a new compliance rule, the team sets `storage_encrypted = true`. `terraform plan` shows: ``` # aws_db_instance.orders must be replaced -/+ resource "aws_db_instance" "orders" { ~ storage_encrypted = false -> true # forces replacement # (12 unchanged attributes hidden) } ``` The `identifier` stays `orders-prod`. To avoid the downtime of a destroy-then-create, the team adds `lifecycle { create_before_destroy = true }` and runs `terraform apply`. What happens?
Answer to see the discussion.
MCQ-7A team manages an `aws_iam_role` in Terraform with `create_before_destroy = true` and a literal `name = "app-task-role"` rather than `name_prefix`. Which condition makes the next apply that changes the role fail?
Answer to see the discussion.
MCQ-8A Terraform configuration declares an ECS service with `desired_count = 4` and `lifecycle { ignore_changes = [desired_count] }`. During a traffic spike an autoscaler raises the service to 20 tasks, and the next `terraform refresh` records 20 in state. A developer then runs `terraform apply` to ship a new task definition image. What does that apply do to `desired_count`?
Answer to see the discussion.
MCQ-22A team runs a production stack containing an AWS::RDS::DBInstance that holds 400 GB of customer data and an AWS::AutoScaling::AutoScalingGroup. To rename the database instance, an engineer changes DBInstanceIdentifier and creates a change set, which reports: | Resource | Action | Replacement | | --- | --- | --- | | AWS::RDS::DBInstance | Modify | True | | AWS::AutoScaling::AutoScalingGroup | Modify | False | The team's requirement is that no customer data is lost. What will executing this change set do to the DB instance?
Answer to see the discussion.
MCQ-4An engineer marks a database password output `sensitive = true`. The password stops appearing in `terraform plan` output, and the team now plans to give a contractor read-only access to the state bucket. Which statement about the marker is correct?
Answer to see the discussion.
MCQ-16A CI job running Terraform 1.x runs `terraform init` and `terraform plan -out=plan.bin`, then uploads `plan.bin` as the deploy artifact. The S3 backend block in `backend.tf` sets `access_key` and `secret_key` as literal strings. A scanner finds the literal secret key in exactly two files in the workspace: `.terraform/terraform.tfstate` and `plan.bin`. The team must keep the S3 backend and keep uploading `plan.bin`. Which change removes the secret from both files?
Answer to see the discussion.
MCQ-25A platform team encrypts Terraform state at rest with a customer-managed KMS key and restricts kms:Decrypt to the CI runner role. Which exposure does that control not address?
Answer to see the discussion.
MCQ-12A platform team runs one root module with two CLI workspaces, `dev` and `prod`. The S3 backend uses its default settings, so both workspaces' state files live in the same bucket. The provider block is identical in both: `provider "aws" { region = "us-east-1" }`, with credentials read from the runner's environment. The dev pipeline's IAM user carries `AdministratorAccess` because the dev stack creates IAM roles. A developer on the dev team runs `terraform workspace select prod && terraform destroy -auto-approve` on the dev pipeline's runner. What happens?
Answer to see the discussion.
MCQ-9A platform team and an app team share one Terraform module, `stack`, that creates a VPC, its subnets, an RDS instance, and the app's ECS service: 210 resources in one state. The app team deploys twice a day; the network team edits subnets about once a quarter. Every app deploy plans all 210 resources, takes about 3 minutes, and blocks on the network team's approval because the module is a single protected CI unit. The teams want app deploys to stop refreshing and planning the network resources, and to stop needing the network team's approval. Which change achieves that?
Answer to see the discussion.
MCQ-17A production Terraform state holds 420 resources: VPC, subnets, security groups, an RDS instance, an EKS cluster, IAM roles, and Route 53 records. A full plan refreshes all 420 resources, takes 38 minutes, and often hits AWS API throttling. Last week, editing one security group rule produced a plan that also proposed replacing the RDS instance, because both lived in the same state and the RDS subnet group referenced the security group. The team wants to keep cross-component dependencies explicit. Which change best fits this failure mode?
Answer to see the discussion.
MCQ-19The pipeline for `infra/` runs `terraform validate` and then `terraform plan` on every PR; a green plan is merged and applied. Four PRs merged last week: - #409 adds `Environment = "staging"` tags to three `aws_s3_bucket` resources. - #410 changes an `aws_instance` type from `t3.medium` to `t3.large`. - #411 adds an `aws_security_group_rule` ingress from `0.0.0.0/0` on port 443 (the public web listener). - #412 adds an `aws_security_group_rule` ingress from `0.0.0.0/0` on port 22. `terraform validate` was green on all four and each plan showed the expected change. Nine days later the security scanner flags #412. The team wants one added gate that fails on #412 and passes on #409, #410 and #411. Which gate does that?
Answer to see the discussion.
MCQ-10@acme/auth-core is an internal npm package consumed by 60 services. Each service declares "@acme/auth-core": "^2.4.0", imports verifyToken, and commits no lockfile, so every CI build runs a fresh `npm install` against the registry. On Tuesday a maintainer deleted the deprecated `verifyToken(token, cb)` export and published the change as 2.5.0. By Wednesday all 60 services were failing their builds with `TypeError: verifyToken is not a function`. What explains the failures?
Answer to see the discussion.
MCQ-11Production and staging share one AWS account. Staging's CI role carries an inline policy granting `s3:DeleteObject` on `arn:aws:s3:::*/*`. A nightly staging cleanup job calls `ListBuckets`, keeps names matching `-staging`, and deletes objects older than 30 days. A regex bug makes the filter match every bucket. Production's bucket holds 40 TB of customer uploads and has versioning disabled. What does the next run do?
Answer to see the discussion.
MCQ-20Role `prod-deploy` in AWS account `111122223333` trusts GitHub's OIDC provider with two conditions: - `StringEquals` on `token.actions.githubusercontent.com:aud`: `sts.amazonaws.com` - `StringLike` on `token.actions.githubusercontent.com:sub`: `repo:acme/infra:ref:refs/heads/release/*` Four workflows in `acme/infra` each hold `permissions: id-token: write`, call `core.getIDToken('sts.amazonaws.com')` for one token, and pass it to `sts:AssumeRoleWithWebIdentity` for that role with no session policy or duration override: 1. `push` to branch `release/2024-06`; the job has no `environment:` key. 2. `push` to branch `releases/2024-06`; the job has no `environment:` key. 3. `push` to branch `release/2024-06`; the job declares `environment: prod`. 4. `push` to tag `release/2024-06`; the job has no `environment:` key. Which calls return credentials?
Answer to see the discussion.