Question 1mediummultiple choice
Read the full Implement and maintain state explanation →TF-004 Implement and maintain state • Complete Question Bank
Complete TF-004 Implement and maintain state question bank — all 0 questions with answers and detailed explanations.
Drag a concept onto its matching description — or click a concept then click the description.
List resources in the state
Show details of a single resource in state
Move an item in the state
Remove an item from the state
Download current state from backend
$ terraform state list aws_instance.web[0] aws_instance.web[1] aws_instance.web[2] aws_s3_bucket.data
$ terraform state list aws_instance.web aws_security_group.sg
Error: Backend initialization required: please run "terraform init" Reason: Backend configuration has changed. The configured backend "s3" has changed since the last "terraform init". Previous configuration: bucket = "prod-terraform-state" key = "network/terraform.tfstate" region = "us-east-1" New configuration: bucket = "prod-terraform-state" key = "network/terraform.tfstate" region = "us-west-2"
$ terraform apply -auto-approve
aws_instance.web_server: Modifying... [id=i-0a1b2c3d4e5f6g7h8]
aws_instance.web_server: Modifications complete after 5s [id=i-0a1b2c3d4e5f6g7h8]
Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
$ terraform state show aws_instance.web_server
# aws_instance.web_server:
resource "aws_instance" "web_server" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
"Name" = "WebServer"
}
subnet_id = "subnet-0a1b2c3d4e5f6g7h8"
vpc_security_group_ids = [
"sg-0a1b2c3d4e5f6g7h8",
]
}
$ terraform state pull | jq '.resources[0].instances[0].attributes_flat'
{
"ami": "ami-0c55b159cbfafe1f0",
"instance_type": "t2.micro"
}Drag steps to the numbered slots on the right, or tap a step then tap a slot.
$ terraform state list aws_vpc.main aws_subnet.public aws_instance.web
terraform {
backend "s3" {
bucket = "my-company-terraform-state"
key = "prod/terraform.tfstate"
region = "us-east-1"
encrypt = true
}
}Error: Error acquiring the state lock Error message: ConditionalCheckFailedException: The conditional request failed Lock Info: ID: 123456 Path: my-company-terraform-state/prod/terraform.tfstate Operation: OperationTypeApply Who: jenkins@ci Version: 1.0.0 Created: 2023-01-15 10:00:00.123456789 +0000 UTC Info: terraform apply from pipeline
terraform {
backend "s3" {
bucket = "my-company-state-bucket"
key = "dev/terraform.tfstate"
region = "us-east-1"
encrypt = true
dynamodb_table = "tf-lock-table"
}
}Error: Error loading state:
Terraform state file does not exist.Refer to the exhibit.
```
$ terraform show
# data.aws_ami.ubuntu
id = "ami-0c55b159cbfafe1f0"
# aws_instance.web
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "WebServer"
}
}
```Error: Error acquiring the state lock Lock Info: ID: 1234567890abcdef Path: terraform.tfstate Operation: OperationTypeInvalid Who: user@hostname Version: 1.0.0 Created: 2023-01-15 10:30:00.000000000 +0000 UTC Info: Terraform acquires a state lock to protect the state from being written by multiple users at the same time. Please resolve the issue and try again. If you have access to the backend, you may be able to unlock it manually by running "terraform force-unlock <lock-id>".
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "WebServer"
}
}
# Remote state data source
data "terraform_remote_state" "network" {
backend = "s3"
config = {
bucket = "mycompany-terraform-state"
key = "network/terraform.tfstate"
region = "us-east-1"
}
}
resource "aws_security_group" "web_sg" {
name_prefix = "web-sg-"
vpc_id = data.terraform_remote_state.network.outputs.vpc_id
}Error: Backend configuration block requires a backend type
on main.tf line 1, in terraform:
1: terraform {
2: backend "s3" {
3: }
4: }
An argument named "region" or "bucket" is required.$ terraform init Initializing the backend... Successfully configured the backend "s3"! Terraform will automatically use this backend for state storage. Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! $ terraform plan No changes. Your infrastructure matches the configuration. Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.
$ terraform state list
aws_instance.web_server
data.aws_ami.ubuntu
aws_security_group.web_sg
$ terraform state show aws_instance.web_server
# aws_instance.web_server:
resource "aws_instance" "web_server" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
"Name" = "WebServer"
}
subnet_id = "subnet-0a1b2c3d4e5f6g7h8"
vpc_security_group_ids = [
"sg-0a1b2c3d4e5f6g7h8",
]
}
$ terraform plan
No changes. Your infrastructure matches the configuration.
$ terraform state rm aws_instance.web_server
Removed aws_instance.web_server from state.
$ terraform plan
+ create
Terraform will perform the following actions:
# aws_instance.web_server will be created
...$ terraform import aws_instance.web_server i-0a1b2c3d4e5f6g7h8
aws_instance.web_server: Importing from ID "i-0a1b2c3d4e5f6g7h8"...
aws_instance.web_server: Import prepared!
Prepared aws_instance for import
aws_instance.web_server: Refreshing state... [id=i-0a1b2c3d4e5f6g7h8]
Import successful!
The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.
$ terraform state pull
{
"version": 4,
"terraform_version": "1.5.0",
"serial": 1,
"lineage": "abc123",
"outputs": {},
"resources": [
{
"mode": "managed",
"type": "aws_instance",
"name": "web_server",
"provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
"instances": [
{
"schema_version": 1,
"attributes": {
"id": "i-0a1b2c3d4e5f6g7h8",
"ami": "ami-0c55b159cbfafe1f0",
"instance_type": "t2.micro"
}
}
]
}
]
}$ terraform plan -out=tfplan
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# aws_instance.web_server will be created
+ resource "aws_instance" "web_server" {
+ ami = "ami-0c55b159cbfafe1f0"
+ instance_type = "t2.micro"
+ subnet_id = "subnet-0a1b2c3d4e5f6g7h8"
+ vpc_security_group_ids = [
+ "sg-0a1b2c3d4e5f6g7h8",
]
+ tags = {
+ "Name" = "WebServer"
}
}
Plan: 1 to add, 0 to change, 0 to destroy.
─────────────────────────────────────────────────────────────────────────────
Saved the plan to: tfplan
To perform exactly these actions, run the following command to apply:
terraform apply "tfplan"
$ terraform apply tfplan
aws_instance.web_server: Creating...
aws_instance.web_server: Still creating... [10s elapsed]
aws_instance.web_server: Creation complete after 12s [id=i-0a1b2c3d4e5f6g7h8]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
$ terraform state pull | jq '.serial'
2Error: Error acquiring the state lock Error message: 2 errors occurred: * state lock was already acquired by another process * error acquiring the state lock: ConditionalCheckFailedException: The conditional request failed Lock Info: ID: 12345 Path: terraform/state/default.tfstate Operation: OperationTypeApply Who: user@hostname Version: 1.5.0 Created: 2024-01-15 10:00:00 UTC Info: Terraform acquires a state lock to protect the state from being written by multiple users at the same time. Please resolve the issue and try again. $ terraform force-unlock 12345 Do you really want to force unlock the state? [y/N]: y Terraform state unlock has been successful!