programming
beginner
10 sample questions
Infrastructure As Code (iac) MCQ Practice Test
Using tools like Terraform to manage cloud resources programmatically.
Q1. You're managing a multi-region deployment using Terraform. Your main state file is stored in a remote backend (e.g., S3). A developer accidentally deletes a crucial resource in the 'us-east-1' region. Which Terraform command, executed in the root module directory, will MOST efficiently identify the difference between the desired state (defined in your Terraform configuration) and the actual state in 'us-east-1', *without* applying changes?
-
A. terraform plan -target=us-east-1 ✓
-
B. terraform show -target=us-east-1
-
C. terraform state list -target=us-east-1
-
D. terraform refresh -target=us-east-1
Explanation: While `terraform refresh` updates the state file from the remote backend, it doesn't show the differences between the desired and actual state. `terraform show` displays the current state, not the comparison. `terraform state list` lists all resources, not just the differences. `terraform plan -target=us-east-1` specifically compares the desired state against the actual state in 'us-east-1', pinpointing the discrepancies without applying any changes.
Q2. You're managing infrastructure as code using Terraform for a multi-region application. Your state file is stored in a remote backend using the AWS S3 backend. To ensure that only authorized users can modify the state file and prevent accidental deletion or corruption, which of the following configurations offers the STRONGEST security posture?
-
A. Enable AWS S3 bucket versioning and restrict access to the state bucket using an IAM role with only the necessary permissions (e.g., s3:GetObject, s3:PutObject, s3:DeleteObject). ✓
-
B. Disable public access on the S3 bucket and enable server-side encryption (SSE-S3).
-
C. Use the default configuration provided by Terraform without any additional security measures.
-
D. Enable AWS S3 bucket versioning and restrict access using bucket policies, allowing only specific IP addresses to access the state file.
Explanation: Using IAM roles provides fine-grained access control, limiting actions to only what's needed. While bucket policies and IP restrictions offer some security, IAM roles offer more granular control and are less prone to configuration errors. Server-side encryption is important for data protection at rest, but access control is paramount for preventing unauthorized modifications.
Q3. When using Terraform to manage AWS infrastructure as code, which of the following best describes the purpose of a "module"?
-
A. A reusable collection of Terraform configurations that can be used to provision multiple resources. ✓
-
B. A way to define a single resource, such as an EC2 instance, with its configuration and dependencies.
-
C. A mechanism to manage state and ensure idempotence when provisioning infrastructure.
-
D. A tool to automate the deployment of Terraform configurations to remote environments.
Explanation: In Terraform, a module is a reusable collection of Terraform configurations that can be used to provision multiple resources. This allows developers to break down large infrastructure configurations into smaller, more manageable pieces and reuse them across multiple environments.
Q4. In a Terraform configuration, you want to create a resource that depends on another resource that is created by a different Terraform module. Which of the following Terraform constructs can be used to achieve this?
-
A. Terraform Module Output ✓
-
B. Terraform Resource Import
-
C. Terraform Interpolation Function
-
D. Terraform Data Source
Explanation: Terraform Module Output can be used to expose values from a module to the parent configuration, allowing dependent resources to be created. This is achieved by using the output keyword in the module configuration and then referencing the output value in the parent configuration using the module output name.
Q5. In a Terraform configuration, which statement is used to specify the AWS IAM role that an AWS Lambda function assumes?
-
A. provider "aws" region = "us-west-2"}
-
B. resource "aws_lambda_function" "example" { \n filename = "lambda_function_payload.zip" \n handler = "index.handler" \n role = aws_iam_role.example.arn \n} ✓
-
C. resource "aws_iam_role" "example" { \n name = "example-role" \n assume_role_policy = jsonencode({ \n Version = "2012-10-17" \n Statement = [ \n { \n Action = "sts:AssumeRole" \n Effect = "Allow" \n Principal = { Service = "lambda.amazonaws.com" } \n } \n ] \n }) \n}
-
D. output "lambda_arn" { \n value = aws_lambda_function.example.arn \n}
Explanation: In Terraform, the role of an AWS Lambda function is specified using the 'role' attribute within the 'resource' block for the AWS Lambda function. The 'aws_iam_role' resource is used to create the IAM role, and its ARN is referenced by the Lambda function.
Q6. In Terraform, what is the purpose of the 'terraform state' file?
-
A. 'Terraform state' file is used to store Terraform configuration
-
B. 'Terraform state' file is used to store the current state of the infrastructure ✓
-
C. 'Terraform state' file is used to store the history of changes made to the Terraform configuration
-
D. 'Terraform state' file is used to store the Terraform backend configuration
Explanation: The 'terraform state' file is used to store the current state of the infrastructure, including the IDs of all resources created by Terraform. This allows Terraform to keep track of the resources it has created and make changes to them as needed.
Q7. What is the primary purpose of using a “state file” in a Terraform configuration?
-
A. To define a Terraform backend for remote state storage
-
B. To specify a Terraform version for a specific module
-
C. To manage the state of a Terraform infrastructure as code configuration ✓
-
D. To enable Terraform drift detection
Explanation: A state file in Terraform is used to store the current state of the infrastructure as code configuration, allowing Terraform to track changes and ensure consistency between the desired state and the actual state of the infrastructure.
Q8. In a Terraform configuration, which of the following statements is used to create a new resource, but only if it doesn't already exist?
-
A. resource
-
B. provider
-
C. data
-
D. resource "null" ✓
Explanation: In Terraform, the "resource null" statement is used to create a new resource only if it doesn't already exist. This is known as a 'null resource'. It's often used to create a resource and then delete it immediately, effectively creating the resource only if it doesn't already exist. This is useful for idempotency and ensuring that resources are only created when needed.
Q9. In Terraform, what is the purpose of the "depends_on" argument in a resource block?
-
A. To specify the order of resource creation
-
B. To create a dependency between resources ✓
-
C. To define a variable used by the resource
-
D. To specify a timeout for resource creation
Explanation: The "depends_on" argument in Terraform is used to specify that one resource depends on another resource being created first. This ensures that the dependent resource is created only after the resource it depends on has been created.
Q10. Which Terraform configuration statement is used to create a new AWS Elastic Load Balancer in a VPC?
-
A. Creates a new AWS Elastic Load Balancer and assigns it to a VPC using the aws_lb resource type. ✓
-
B. Creates a new VPC and assigns an Elastic Load Balancer to it using the aws_vpc resource type.
-
C. Creates a new Elastic Load Balancer and assigns it to a VPC using the aws_elastic_lb resource type.
-
D. Creates a new VPC and assigns an Elastic Load Balancer to it using the aws_elb resource type.
Explanation: In Terraform, the aws_lb resource type is used to create and manage Elastic Load Balancers in AWS. When creating a new Elastic Load Balancer in a VPC, the aws_lb resource type is used along with the alb_target_group and alb_listener resources to configure the load balancer. The aws_elastic_lb resource type is deprecated in Terraform 0.12 and later, and should not be used.
That was just a sample. Sign up to unlock the full question bank with timed tests and certificates.
Sign Up Free