[toc] ### Problem Statement 1: Provision an EC2 Instance with Security Group #### Description :::info Use Terraform to provision an EC2 instance in AWS along with a security group that allows inbound traffic on specific ports. ::: #### Sample Solution: ````hcl= resource "aws_instance" "example" { ami = "ami-0c94855ba95c71c99" instance_type = "t2.micro" } resource "aws_security_group" "example" { name = "example-security-group" description = "Allow inbound traffic on port 80 and 22" ingress { from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } ingress { from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } } ```` ### Problem Statement 2: Create an S3 Bucket and Enable Versioning #### Description :::info Use Terraform to create an S3 bucket in AWS and enable versioning for the bucket. ::: #### Sample Solution ````hcl= resource "aws_s3_bucket" "example" { bucket = "example-bucket" } resource "aws_s3_bucket_versioning" "example" { bucket = aws_s3_bucket.example.id versioning { enabled = true } } ```` ### Problem Statement 4: Create an RDS Instance #### Description :::info Provision an RDS instance in AWS using Terraform, specifying the database engine, instance type, and storage configuration. ::: ### Sample Solution ````hcl= resource "aws_db_instance" "example" { identifier = "example-db" allocated_storage = 20 engine = "mysql" instance_class = "db.t2.micro" name = "exampledb" username = "admin" password = "password" } ```` ### Problem Statement 4: Create a VPC with Subnets and Internet Gateway #### Description :::info Use Terraform to create a VPC in AWS with public and private subnets, along with an internet gateway. ::: #### Sample Solution ````hcl= resource "aws_vpc" "example" { cidr_block = "10.0.0.0/16" } resource "aws_subnet" "public" { vpc_id = aws_vpc.example.id cidr_block = "10.0.1.0/24" availability_zone = "us-east-1a" } resource "aws_subnet" "private" { vpc_id = aws_vpc.example.id cidr_block = "10.0.2.0/24" availability_zone = "us-east-1b" } resource "aws_internet_gateway" "example" { vpc_id = aws_vpc.example.id } resource "aws_route_table" "public" { vpc_id = aws_vpc.example.id route { cidr_block = "0.0.0.0/0" gateway_id = aws_internet_gateway.example.id ```` ### Problem Statement 6: Provision an EC2 instance with associated resources #### Description :::info Write a Terraform configuration to provision an EC2 instance, along with a VPC, subnet, security group, and an Elastic IP. Ensure that the EC2 instance is accessible via SSH and has the necessary security group rules. ::: #### Sample Solution: ````hcl= resource "aws_vpc" "my_vpc" { cidr_block = "10.0.0.0/16" } resource "aws_subnet" "my_subnet" { vpc_id = aws_vpc.my_vpc.id cidr_block = "10.0.1.0/24" } resource "aws_security_group" "my_sg" { name = "my-security-group" description = "My security group" ingress { from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } } resource "aws_instance" "my_instance" { ami = "ami-12345678" instance_type = "t2.micro" subnet_id = aws_subnet.my_subnet.id security_group_ids = [aws_security_group.my_sg.id] key_name = "my-key-pair" } resource "aws_eip" "my_eip" { vpc = true instance = aws_instance.my_instance.id } ```` ### Problem Statement 7: Create an S3 bucket with versioning enabled #### Description :::info Write a Terraform configuration to create an S3 bucket with versioning enabled and an optional bucket policy to restrict access. ::: #### Sample Solution ````hcl= resource "aws_s3_bucket" "my_bucket" { bucket = "my-bucket" versioning { enabled = true } } resource "aws_s3_bucket_policy" "my_bucket_policy" { bucket = aws_s3_bucket.my_bucket.id policy = <<EOF { "Version": "2012-10-17", "Statement": [ { "Sid": "AllowGetObject", "Effect": "Deny", "Principal": "*", "Action": "s3:GetObject", "Resource": "arn:aws:s3:::my-bucket/*" } ] } EOF } ```` ### Problem Statement 8: Provision an RDS database instance #### Description :::info Write a Terraform configuration to provision an RDS database instance with a specified engine, instance class, and storage size. ::: #### Sample Solution ````hcl= resource "aws_db_instance" "my_db_instance" { engine = "mysql" instance_class = "db.t2.micro" allocated_storage = 20 identifier = "my-db-instance" username = "admin" password = "mypassword" } ```` ### Problem Statement 9: Create an AWS Lambda function with an S3 trigger #### Description :::info Write a Terraform configuration to create an AWS Lambda function that is triggered by object uploads to an S3 bucket. ::: #### Sample Solution ````hcl= resource "aws_lambda_function" "my_lambda_function" { function_name = "my-lambda-function" handler = "index.handler" runtime = "nodejs14.x" filename = "lambda_function.zip" } resource "aws_lambda_permission" "my_lambda_permission" { statement_id = "AllowS3Invoke" action = "lambda:InvokeFunction" function_name = aws_lambda_function.my_lambda_function.arn principal = "s3.amazonaws.com" source_arn = aws_s3_bucket.my_bucket.arn } resource "aws_s3_bucket_notification" "my_bucket_notification" { bucket = aws_s3_bucket.my_bucket.id lambda_function { lambda_function_arn = aws_lambda_function.my_lambda_function.arn events = ["s3:ObjectCreated:*"] } } ```` ### Problem Statement 10: Provision an ECS cluster with a service and task definition #### Description :::info Write a Terraform configuration to provision an ECS cluster, an ECS service, and a task definition for running a containerized application. ::: #### Sample Solution ````hcl= resource "aws_ecs_cluster" "my_cluster" { name = "my-ecs-cluster" } resource "aws_ecs_task_definition" "my_task_definition" { family = "my-task-definition" container_definitions = <<EOF [ { "name": "my-container", "image": "my-image", "cpu": 256, "memory": 512 } ] EOF } resource "aws_ecs_service" "my_service" { name = "my-service" cluster = aws_ecs_cluster.my_cluster.id task_definition = aws_ecs_task_definition.my_task_definition.arn desired_count = 1 deployment_minimum_healthy_percent = 100 deployment_maximum_percent = 200 } ```` :::success :bulb: ***Important Note(s):*** *1. These are sample solutions and you may need to modify them to fit your specific requirements, such as replacing placeholder values with appropriate values for your AWS resources.* *2. Please ensure you have the necessary permissions to create the resources mentioned in the Terraform configurations.* :::