Bird
Raised Fist0
Terraformcloud~20 mins

Remote state data source for cross-project in Terraform - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Remote State Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding remote state data source usage

What is the primary purpose of using a terraform_remote_state data source in a cross-project setup?

ATo share outputs from one Terraform project with another securely and reliably.
BTo encrypt Terraform state files using a custom key.
CTo automatically backup Terraform state files to a local machine.
DTo deploy resources in multiple cloud providers simultaneously.
Attempts:
2 left
💡 Hint

Think about how Terraform projects can communicate or share information.

Configuration
intermediate
2:00remaining
Correct remote state data source configuration

Which of the following Terraform code snippets correctly configures a remote state data source to read outputs from a different project stored in an S3 backend?

A
data "terraform_remote_state" "vpc" {
  backend = "s3"
  bucket = "my-terraform-states"
  key    = "vpc/terraform.tfstate"
  region = "us-east-1"
}
B
data "terraform_remote_state" "vpc" {
  backend = "gcs"
  config = {
    bucket = "my-terraform-states"
    key    = "vpc/terraform.tfstate"
    region = "us-east-1"
  }
}
C
terraform_remote_state "vpc" {
  backend = "s3"
  config = {
    bucket = "my-terraform-states"
    key    = "vpc/terraform.tfstate"
    region = "us-east-1"
  }
}
D
data "terraform_remote_state" "vpc" {
  backend = "s3"
  config = {
    bucket = "my-terraform-states"
    key    = "vpc/terraform.tfstate"
    region = "us-east-1"
  }
}
Attempts:
2 left
💡 Hint

Check the syntax for declaring a data source and the backend configuration.

Architecture
advanced
2:30remaining
Cross-project remote state access architecture

In a multi-project Terraform setup, what is the best architectural practice to ensure secure and reliable access to remote state data across projects?

AStore all Terraform states in a single shared backend bucket with strict IAM policies limiting access per project.
BUse local state files and manually copy outputs between projects to avoid backend complexity.
CAllow all projects to use the same AWS IAM role with full access to all state files for simplicity.
DStore state files in separate buckets without any access restrictions to speed up deployments.
Attempts:
2 left
💡 Hint

Think about security and access control when multiple projects share state data.

service_behavior
advanced
2:00remaining
Behavior of remote state data source on state changes

What happens to the terraform_remote_state data source when the remote state it references is updated by another project?

ATerraform requires deleting the local state file to detect changes in the remote state.
BTerraform caches the remote state permanently and never updates it unless manually refreshed.
CTerraform fetches the latest remote state data during the next plan or apply operation automatically.
DTerraform throws an error if the remote state changes between runs to prevent inconsistencies.
Attempts:
2 left
💡 Hint

Consider how Terraform keeps data sources up to date during runs.

security
expert
3:00remaining
Preventing unauthorized access to remote state in cross-project setups

Which IAM policy configuration best prevents unauthorized access to a Terraform remote state stored in an S3 bucket used by multiple projects?

A
{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Action": ["s3:*"],
    "Resource": "arn:aws:s3:::terraform-states/*"
  }]
}
B
{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Action": ["s3:GetObject"],
    "Resource": "arn:aws:s3:::terraform-states/project-a/*",
    "Condition": {
      "StringEquals": {"aws:PrincipalTag/Project": "project-a"}
    }
  }]
}
C
{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Deny",
    "Action": ["s3:GetObject"],
    "Resource": "arn:aws:s3:::terraform-states/project-b/*"
  }]
}
D
{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Action": ["s3:GetObject"],
    "Resource": "arn:aws:s3:::terraform-states/*"
  }]
}
Attempts:
2 left
💡 Hint

Focus on restricting access by project tags and resource paths.

Practice

(1/5)
1. What is the main purpose of using a terraform_remote_state data source in Terraform?
easy
A. To store Terraform state files locally
B. To access outputs from another Terraform project's state
C. To create new resources in a different cloud provider
D. To encrypt Terraform state files automatically

Solution

  1. Step 1: Understand remote state data source role

    The terraform_remote_state data source allows one Terraform configuration to read outputs from another configuration's state file.
  2. Step 2: Differentiate from other options

    It does not store state locally, create new resources, or encrypt state automatically; it only reads existing state outputs.
  3. Final Answer:

    To access outputs from another Terraform project's state -> Option B
  4. Quick Check:

    Remote state data source = Access outputs [OK]
Hint: Remote state data source reads outputs from other projects [OK]
Common Mistakes:
  • Confusing remote state with local state storage
  • Thinking it creates resources instead of reading state
  • Assuming it encrypts state automatically
2. Which of the following is the correct syntax to define a terraform_remote_state data source for a backend stored in an S3 bucket named my-terraform-state?
easy
A. data "terraform_remote_state" "example" { backend = "local" config = { path = "my-terraform-state" } }
B. resource "terraform_remote_state" "example" { backend = "s3" bucket = "my-terraform-state" }
C. terraform_remote_state "example" { backend = "s3" bucket = "my-terraform-state" }
D. data "terraform_remote_state" "example" { backend = "s3" config = { bucket = "my-terraform-state" key = "state.tfstate" region = "us-east-1" } }

Solution

  1. Step 1: Identify correct resource type and syntax

    The terraform_remote_state must be declared as a data block, not a resource.
  2. Step 2: Check backend and config structure

    For S3 backend, the config requires bucket, key, and region inside a config map.
  3. Final Answer:

    data "terraform_remote_state" "example" { backend = "s3" config = { bucket = "my-terraform-state" key = "state.tfstate" region = "us-east-1" } } -> Option D
  4. Quick Check:

    Correct syntax = data "terraform_remote_state" "example" { backend = "s3" config = { bucket = "my-terraform-state" key = "state.tfstate" region = "us-east-1" } } [OK]
Hint: Use data block with backend and config map for remote state [OK]
Common Mistakes:
  • Using resource instead of data block
  • Missing required config keys like key or region
  • Using wrong backend type like local for S3
3. Given this Terraform snippet accessing remote state:
data "terraform_remote_state" "network" {
  backend = "gcs"
  config = {
    bucket = "tf-state-bucket"
    prefix = "network"
  }
}

output "vpc_id" {
  value = data.terraform_remote_state.network.outputs.vpc_id
}

What will output.vpc_id contain?
medium
A. The entire remote state file content as a string
B. An error because prefix is not a valid config key for GCS backend
C. The VPC ID output from the remote state stored in the GCS bucket under prefix 'network'
D. Null because outputs cannot be accessed from remote state

Solution

  1. Step 1: Understand remote state data source usage

    The data source reads the remote state from the GCS bucket with the given prefix, making outputs available.
  2. Step 2: Confirm output access

    The output vpc_id is accessed correctly via data.terraform_remote_state.network.outputs.vpc_id, so it returns the VPC ID value.
  3. Final Answer:

    The VPC ID output from the remote state stored in the GCS bucket under prefix 'network' -> Option C
  4. Quick Check:

    Remote output access = VPC ID value [OK]
Hint: Remote state outputs accessed via data.<name>.outputs.<key> [OK]
Common Mistakes:
  • Confusing prefix usage for GCS backend (it is valid)
  • Expecting entire state file instead of outputs
  • Assuming outputs cannot be read remotely
4. You have this Terraform remote state data source:
data "terraform_remote_state" "app" {
  backend = "azurerm"
  config = {
    resource_group_name = "rg-state"
    storage_account_name = "stterraform"
    container_name = "tfstate"
    key = "app.terraform.tfstate"
  }
}

When running terraform plan, you get an error: Failed to load remote state. What is the most likely cause?
medium
A. Incorrect or missing permissions to access the Azure storage account
B. The backend type should be s3 instead of azurerm
C. The key parameter is not supported in azurerm backend
D. Terraform remote state data source cannot be used with Azure

Solution

  1. Step 1: Verify backend and config correctness

    The backend azurerm with given config keys is valid for Azure Blob Storage remote state.
  2. Step 2: Identify common causes of load failure

    Most common cause is missing or incorrect permissions to access the storage account or container.
  3. Final Answer:

    Incorrect or missing permissions to access the Azure storage account -> Option A
  4. Quick Check:

    Access permissions issue = Load failure [OK]
Hint: Check storage permissions if remote state load fails [OK]
Common Mistakes:
  • Assuming backend type is wrong when it is correct
  • Thinking key parameter is unsupported in azurerm backend
  • Believing remote state cannot be used with Azure
5. You manage two Terraform projects: network and app. The network project stores its state remotely in an S3 bucket with key network/terraform.tfstate. You want the app project to use the VPC ID output from network. Which configuration correctly sets up the remote state data source in app to access network outputs securely and follows best practices?
hard
A. data "terraform_remote_state" "network" { backend = "s3" config = { bucket = "my-tf-state-bucket" key = "network/terraform.tfstate" region = "us-west-2" } }
B. data "terraform_remote_state" "network" { backend = "s3" config = { bucket = "my-tf-state-bucket" key = "app/terraform.tfstate" region = "us-west-2" } }
C. data "terraform_remote_state" "network" { backend = "local" config = { path = "../network/terraform.tfstate" } }
D. resource "terraform_remote_state" "network" { backend = "s3" config = { bucket = "my-tf-state-bucket" key = "network/terraform.tfstate" region = "us-west-2" } }

Solution

  1. Step 1: Confirm correct backend and key for remote state

    The remote state is stored in S3 bucket with key network/terraform.tfstate, so the data source must match this.
  2. Step 2: Ensure data source type and security best practices

    Use a data block (not resource) with backend = "s3", specify region, and avoid incorrect keys.
  3. Final Answer:

    Data source with backend s3, correct bucket/key, and region -> Option A
  4. Quick Check:

    Correct backend and secure config = data "terraform_remote_state" "network" { backend = "s3" config = { bucket = "my-tf-state-bucket" key = "network/terraform.tfstate" region = "us-west-2" } } [OK]
Hint: Match backend/key exactly and use data block [OK]
Common Mistakes:
  • Using wrong key path for remote state
  • Using resource block instead of data block
  • Using local backend instead of remote S3
  • Omitting region config