0
0
Terraformcloud~10 mins

Resource dependencies (implicit) in Terraform - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create an AWS S3 bucket resource named 'my_bucket'.

Terraform
resource "aws_s3_bucket" "my_bucket" {
  bucket = [1]
  acl    = "private"
}
Drag options to blanks, or click blank then click option'
A"s3_bucket"
B"my-unique-bucket-123"
C"bucket_name"
D"my_bucket"
Attempts:
3 left
💡 Hint
Common Mistakes
Using an invalid bucket name without quotes
Using a variable name instead of a string
2fill in blank
medium

Complete the code to create an AWS IAM role that depends on the S3 bucket resource.

Terraform
resource "aws_iam_role" "my_role" {
  name = "my_role"
  assume_role_policy = jsonencode({
    "Version": "2012-10-17",
    "Statement": [{
      "Action": "sts:AssumeRole",
      "Effect": "Allow",
      "Principal": {
        "Service": [1]
      }
    }]
  })
}
Drag options to blanks, or click blank then click option'
A"ec2.amazonaws.com"
B"s3.amazonaws.com"
C"lambda.amazonaws.com"
D"iam.amazonaws.com"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong service principal like "s3.amazonaws.com"
Forgetting to put the service principal in quotes
3fill in blank
hard

Fix the error in the code to create an AWS S3 bucket policy that depends on the bucket resource.

Terraform
resource "aws_s3_bucket_policy" "bucket_policy" {
  bucket = aws_s3_bucket.my_bucket.[1]
  policy = jsonencode({
    "Version": "2012-10-17",
    "Statement": [{
      "Effect": "Allow",
      "Principal": "*",
      "Action": ["s3:GetObject"],
      "Resource": ["arn:aws:s3:::${aws_s3_bucket.my_bucket.bucket}/*"]
    }]
  })
}
Drag options to blanks, or click blank then click option'
Aname
Barn
Cid
Dbucket
Attempts:
3 left
💡 Hint
Common Mistakes
Using aws_s3_bucket.my_bucket.id which is not the bucket name
Using aws_s3_bucket.my_bucket.arn which is the full ARN
4fill in blank
hard

Fill both blanks to create an AWS EC2 instance that implicitly depends on the IAM role and uses the role's ARN.

Terraform
resource "aws_instance" "web" {
  ami           = "ami-12345678"
  instance_type = "t2.micro"
  iam_instance_profile = aws_iam_role.my_role.[1]
  tags = {
    Name = [2]
  }
}
Drag options to blanks, or click blank then click option'
Aarn
B"WebServer"
Cid
D"MyInstance"
Attempts:
3 left
💡 Hint
Common Mistakes
Using arn instead of id for iam_instance_profile
Not quoting the tag value
5fill in blank
hard

Fill all three blanks to create a security group with ingress rules that depend on the VPC and allow HTTP traffic.

Terraform
resource "aws_security_group" "web_sg" {
  name        = [1]
  description = "Allow HTTP traffic"
  vpc_id      = aws_vpc.main.[2]

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = [3]
    cidr_blocks = ["0.0.0.0/0"]
  }
}
Drag options to blanks, or click blank then click option'
A"web_sg"
Bid
C"tcp"
D"main"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the VPC name instead of id for vpc_id
Using protocol "http" instead of "tcp"