# 9. Infrastructure-as-Code using Terraform [toc] ### Topics :::success - [x] Introduction to Infrastructure-as-Code - [x] Introduction to Terraform - [x] Installation and Configuration - [x] Manage AWS using Terraform - [x] Create and manage an S3 bucket using Terraform ::: ### Introduction to Infrastructure-as-Code #### IaC tools/Software :::warning - Terraform (HCL) - AWS CloudFormation (JSON/YAML) - AWS CDK - Azure ARM Templates - HEAT templates (OpenStack) - Pulumi ::: ### Introduction to Terraform ![](https://i.imgur.com/estWHbg.png) #### How Terraform works? ![](https://i.imgur.com/GmqDxmM.png) #### Terraform Workflow :::warning - terraform init - terraform plan - terraform apply - terraform destroy ::: ![](https://i.imgur.com/0ZF47DF.png) ### Installation and configuration #### Installation steps (tested on Ubuntu 20.04): ````yaml= wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list sudo apt update && sudo apt install terraform Validation command: terraform -v Expected output: Terraform v1.4.6 on linux_amd64 ```` :mag: Above mentioned installation steps are for Ubuntu OS. For other Operating systems checkout the **[Terraform downloads](https://www.terraform.io/downloads)** page. Refer screenshots below: ![](https://hackmd.io/_uploads/BJ7_RZKL2.png) ![](https://hackmd.io/_uploads/SkkK0WtU3.png) #### Classroom activity (Update the Terraform version on Simplilearn lab machine) ````yaml= Put installation commands in a small script: ## vi terra.sh wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list sudo apt update && sudo apt install terraform Make the script executable: chmod +x terra.sh Run the script: sh terra.sh Validation: terraform -v ```` #### Hashicorp Configuration Language ![](https://i.imgur.com/elGKx5g.png) ### Manage AWS using Terraform #### Create an S3 bucket using Terraform ##### Step 1 - Create a project folder for Terraform S3 task ````yaml= cd ~ mkdir terraform && cd terraform mkdir s3-sl && cd s3-sl ```` ##### Step 2 - Create terraform file to create an S3 bucket ````yaml= ## vi main.tf provider "aws" { access_key = "your-key" secret_key = "your-secret-key" token = "your-token" region = "us-east-1" } resource "aws_s3_bucket" "sl-demo-bucket" { bucket = "put-a-unique-bucket-name-here" acl = "private" tags = { Env = "Prod" } } ```` ##### Step 3 - Run Terraform commands to create the bucket ````yaml= terraform init terraform plan terraform apply ```` ##### Step 4 - Validate :::warning - Login to AWS Web console (Via Simplilearn Lab UI) - Go to S3 Dashboard - You should see a new S3 bucket with the name you specified in terraform "*main.tf*" file. ::: ##### Step 5 - Destroy Once the exercise is done you should clean up your AWS environment to make sure you are not being charged unnecessarily. Run the following command to delete the bucket you created in previous steps. ````yaml= terraform destroy ```` ##### Step 6 - Validate :::warning - Login to AWS Web console (Via Simplilearn Lab UI) - Go to S3 Dashboard - The S3 buckets list should be empty and there should be no buckets present. ::: ### References :::success - https://www.terraform.io/ - https://www.pulumi.com/learn/ - https://cdkworkshop.com/ - https://aws.amazon.com/cdk/ - https://hashicorp-terraform.awsworkshop.io/ :::