Assignment 01

1. Install Terraform

Run terraform version in your terminal and you should be able to see the version installed on your system. If you haven't installed it yet, head to the installation page and follow the instructions for your operating system.

Terraform v1.2.8 on darwin_arm64

2. Initialize Terraform

Create a file named main.tf and initialize Terraform so it can work with AWS cloud.


3. What's wrong with this config?

Find out what's wrong with the following configuration, and how to fix it:

terraform { required_providers { aws = { source = "hashicorp/aws" region = "ca-central-1" } } }

4. Create an S3 bucket

Create an S3 bucket using Terraform and enable both versioning and server side encryption. Refer to the Terraform documentation for S3 for help. Use AES256 for the encryption algorithm.


5. Verify your changes in S3 console

Verify that the bucket you created in the previous step has both versioning and server side encryption enabled by going to the S3 console, clicking on your bucket name, and then choosing the Properties tab.

Using the console, upload the same file twice and view all versions.


6. Create a virtual server

Create an EC2 Ubuntu instance of type t2.micro and configure the security group so that you can both SSH into it and access a web server on the HTTP port. After establishing the SSH connection, install an Apache server and change the /var/www/html/index.html file to the following content (You can use Vim or Nano to change the file):

<html> <body> <p>Your Name</p> </body> </html>

Verify that your changes have been applied by visiting the public IP address of your instance in the browser.


7. Use EC2 user-data to automate the process

Instead of SSHing into your instance and change the /var/www/html/index.html, use EC2's user-data to automate the process.


8. Create 2 more instances with the same configuration

Instead of one EC2 instance, create 3, without any code duplication. Print out their public IP addresses at the end.


9. Learn about different EC2 instance types

Learn more about different offerings from AWS EC2, including their resources (vCPU and Memory) and their On-Demand hourly cost.


10. Use a variable for EC2 instance type

Instead of hard-coding t2.micro, read from a variable named instance_type. Explain various ways you can provide Terraform with a value.


11. Use variable validation

Use validation for your instance_type variable to only allow values of t2.micro or t2.nano. Use an appropriate error message if the type is something else. Refer to Terraform documentation for more info.

Hint: you need to use the contains built-in function. Read more here.


12. Structure your files

Separate your variables, outputs, and main configuration and have them in 3 different files: variables.tf, outputs.tf, and main.tf. Run terraform plan and make sure everything works and no change is needed for your infrastructure.