This file describes the general flow I use for starting and creating a aws-based teraform infrastructure
Author: Chisom Ukaegbu
-
Install Terraform (ubuntu debian ver.)
-
wget -O - https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings hashicorp-archive-keyring.gpg -
echo "deb [arch=$(dpkg --print-architecture) 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
-
For our example we will focus on using aws as the cloud provider aka the place where out machines will be created & hosted
AWS will be our cloud provider and set up an account on aws cloud provider
-
You will need AWS Access KEY ID & AWS Secret Key ID This is gathered from the aws web console. Create a user or use a preexisting user.
Grab there access and secret key ids.
-
Run
Aws Configurein your terminal. Input your keys
blueteam@cyber-range tf-tuts % aws configure
AWS Access Key ID [****************PYVK]: ****PYVK
AWS Secret Access Key [****************duMt]: ****duMt
Default region name [eu-central-1]: <same region where you want to place the machines>
Default output format [None]:
blueteam@cyber-range tf-tuts %-
Setup Cloud Provider
Convention says to place this config in a file name provider.tf. It does not matter aslong as the file has the .tf extension and is unique in name.
terraform {
required_providers {
aws = {
source = hashicorp/aws
version = " ~> 4.19.0"
}
}
}-
Create instances
Create a main.tf file. Convention is to name the file "main.tf"
This is where the block of the cofiguration for the virtual machines will be deployed
# creating the code to create an EC2 instance in AWS using Terraform.
resource "aws_instance" "my_vm" {
ami = "ami-065deacbcaac64cf2" //Ubuntu AMI
instance_type = "t2.micro"
tags = {
Name = "My EC2 instance",
}
###########
# declared a resource block of type “aws_instance”.
### This instructs Terraform that we want to create an EC2 instance resource in AWS with the given attributes
# second parameter is “`my_vm`”, an internal identifier that refers to this ##particular EC2 instance elsewhere in the code. We can assign any name to this identifier
# assigned a `tag` “Name” with the value “My EC2 Instance”.-
Intialize terraform
Run this command in your terminal of the same directory your provider is.
terraform init
You should see these hidden files. when running
ls -l . .. .terraform .terraform.lock.hcl provider.tf -
Format the code This command will auto fixed syntax and indentation of your configuration code
terraform fmt
This command will output 2 scenarios
output: identify and highlight resources that will be created, updated, or deleted if we choose to execute the current version of the code
or
Show issues regarding your terraform file
```sh
terraform plan`
```


Running the command terraform apply will begin to create
terraform applyNow if you navigate to aws, you will see the instances created.
Make sure you are in the same region as the provider you selected.
terraform destoryWill delete any resources provisioned by your terraform script. Virtual machines, vpcs, subnets etc are considered resources
There is more you can do with terraform but this is a quick start guide for creating an instance or network for the first time.

