Complete the code to define a secure AWS S3 bucket with versioning enabled.
resource "aws_s3_bucket" "my_bucket" { bucket = "my-secure-bucket" versioning { enabled = [1] } }
Enabling versioning helps keep previous versions of objects, which is important for security and recovery.
Complete the code to restrict public access to the S3 bucket.
resource "aws_s3_bucket_public_access_block" "block_public" { bucket = aws_s3_bucket.my_bucket.id [1] = true }
Setting block_public_acls to true blocks public ACLs, improving security.
Complete the code to create an IAM policy that denies all S3 actions.
data "aws_iam_policy_document" "s3_deny_policy" { statement { effect = "Deny" actions = ["[1]"] resources = ["*"] } }
Denying s3:* denies all S3 actions, enforcing a secure deny-by-default posture.
Fill both blanks to create a secure security group allowing only SSH from a specific IP.
resource "aws_security_group" "ssh_access" { name = "ssh_access" description = "Allow SSH only from office" ingress { from_port = [1] to_port = [2] protocol = "tcp" cidr_blocks = ["203.0.113.0/32"] } }
SSH uses port 22, so both from_port and to_port should be 22 to allow only SSH.
Fill all three blanks to define an IAM role with a trust policy for EC2 and attach a read-only policy.
resource "aws_iam_role" "ec2_role" { name = "ec2_read_only_role" assume_role_policy = jsonencode({ Version = "2012-10-17", Statement = [{ Effect = "Allow", Principal = { Service = "[1]" }, Action = "sts:AssumeRole" }] }) } resource "aws_iam_role_policy_attachment" "attach_read_only" { role = aws_iam_role.ec2_role.name policy_arn = "[2]" depends_on = [aws_iam_role.ec2_role] } resource "aws_iam_policy" "read_only_policy" { name = "ReadOnlyAccessCustom" description = "Custom read-only policy" policy = jsonencode({ Version = "2012-10-17", Statement = [{ Effect = "Allow", Action = ["[3]"], Resource = "*" }] }) }
The trust policy must allow EC2 service to assume the role (ec2.amazonaws.com).
The managed policy ARN for read-only access is arn:aws:iam::aws:policy/ReadOnlyAccess.
The policy action s3:GetObject allows read-only access to S3 objects.