Terraform intro,setup,create s3bucket,ec2 instance in its own network
Tags: terraform, aws, vscode, devops
Published: 2023-05-18 16:08:09


Terraform is written in go and more important in building infrastructure compare to chef and ansible which are better with configurations.
Terraform can be use with all cloud services. Unlike cloud formation that covers AWS platform only.

use these three commands to perform terraform deployments.. 
**terraform “init”
terraform "plan"
terraform "apply"** (sometime you add** “--auto-approve”** to avoid the Yes/No confirmation response.)


## INSTALLING TERRAFORM

- download Terraform from the terraform page or use already made shell file from my [GitHub](https://github.com/hesblac/installation_scripts/blob/master/terraform.sh) 
- that has all the commands to install and get terraform up and running in seconds.
- Use the commands terraform --version to confirm that terraform is installed.
- Use vscode or any editor you like for convenience. We’ll use Vscode.
- In the vscode, install the terraform extension for easy coding and template helps.
  
![terraform installed](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8wqzzkyx76pfthvzecn1.png)




## Configuring our “AWS secret key and Access key”


**STEPS:**
    • launch your aws console 
    • search for IAM
    • click on user
    • click on Add user
    • create a username by entering any meaningful name you desire
from the next section click on the "attach policy directly"
    • in this section _Permissions policies_, search and check/choose your prefered permission to perform what you need.
    • We’ll use the the Admin full access “AdministratorAccess” then click the “Next Tags”
    • click next again
    • click on “create user”
    • your user is ready, click on it 
go to the security credential tab
in the access key section , click on create access key
click the Command Line Interface, accept the terms and click next, in the next section,input a tag name or leave it empty then click **create access key**
 save them both.

![access keys](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/35ricf9ghbn3dhjurpx2.png)


We will show you set them up securely for use in your Terraform codes.


CREATING AN EC2 INSTANCE WITH TERRAFORM

for beginners, go to Google and search for “aws instance terraform”
copy the sample basic code for the instance to a file in your workspace named main.tf
note: every file in terraform uses the extension **“.tf”**  except some.
when you have that set in your vscode,
go to the search tab again and search for “terraform aws provider”
this block will authenticate you to use terraform with aws.
then copy the basic provider block of code into your main.tf file.

Now , arrange your code so we have the provider block before the instance block.

![aws terra code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/brk0gyb8pgmw848jnt7a.png)

we can just leave our credential in the code, but with security in mind, it's a bad practice.

In your vscode, we’ll want to use the terminal to configure the credentials we got from AWS (Access key and Secret key) so as to get it ready for terraform usage.
right click on the main.tf then choose terminal  to open the terminal or just use Ctrl + back-tick to access the Terminal from vscode or just use the normal terminal like me.
i'm using an ec2 instance for this since i already have terraform set up on my local.

## CONFIGURING TERRAFORM TO USE AWS CREDENTIALS

first we'll install AWScli on our server to use aws command line. 
we'll go to our [github](https://github.com/hesblac/installation_scripts/blob/master/Awscli) and get the ready shell script. you can use the main aws site also and get the installation commands.


![aws cli](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1msgh3wqwoupcfh0sqs6.png)



in the terminal, run the command **“aws configure”**
you will be prompted for both keys,a region, enter the keys as requested and your preferred region.(where you want your deployment). Also, use json for the output prompt.



![aws configure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aqa0725748q05d04f1pj.png)




To confirm that your credentials are saved :

in your home folder, there is a file named “.aws”
this folder should have these files “credentials and config”
you can edit the credential file and change the default to “myaws”  or a name you like
The config has the default settings.

![aws profile](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wqm90z2n2v4u0t4h45i5.png)

You can also confirm that your credentials are set and they are working by completing these terraform setup;

In your terraform code, remove everything in the provider block, you can leave it blank or set your profile as “myaws” or to the name you set in the setup, and set your region to what you want like so:

![profile set](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pxdgxussp1kmy1egshmh.png)



terraform will automatically use the credentials we saved earlier.

Use “terraform init” >>>> this will initialize the code and install all the plugins needed, if the credential  is wrong, we’ll get an error.
If it succeed , we’ll get a ready response.

![terraform init](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0zk43620vbq91o3npmqg.png)



Use “terraform plan” >>> this will show us what we are deploying . And if there’s any problem with our code, this is the place where we’ll know and rectify/debug it

![terraform plan](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p8otswc6fsfinnjkqldm.png)


Use “terraform apply” >>>> this will apply our code and deploy our infrastructure as stated in our code and we will also see the detail of our deployment just as we see after the plan command.
After this command, terraform will prompt us to type yes to continue(after this , there’s no going back unless you use Ctr l C). Instead of typing yes in the prompt, we can add “--auto-approve” to the terraform apply command. And we won’t be prompted, terraform will just run the commands and show us success or failure of our deployment.

![terraform apply](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/won1p0cied95yx3ejhz8.png)


After deployment, that is , when terraform shows us that the run is complete,

![success](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4uwmu8qif0ogise0nr0k.png)
to see the infrastructure we just deploy,
go to the aws console, 
change your region to the one you choose or used in your aws default configuration or in your provider block. In my case, I used us-west-2

![region](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qi7xzx7a91s00ny6ls2s.png)


When you are in that region, use the search box on the console to search for _ec2 instance_.
you should Immediately  see your instance, either already deployed or its starting up.

![instance running](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/41q6tv8y847avoreqvzl.png)

If you’ve confirmed your deployment , to destroy or delete your infrastructure, it’s better to destroy from the same place you apply from and not in the console, so as to delete all created components,

in our case , we only have an ec2 instance. So it’s easy to destroy.     
In deployments where many components are involved, its not a good practice to go into the console and delete each one after the other. 

using “terraform destroy” or “terraform destroy --auto-approve” will delete all the infrastructure you just created.
Ensure you’re still in the terminal and also inside the folder where your files are located.

![terraform destroy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h5551mnva8gouhsud1dt.png)




TERRAFORM VARIABLES

type contraints are created from a mixture of type keywords and type contructors.
The supported type keywords are:
string, number, bool
type constructors allow you to specify complex types such as collections:
list, set, map,object, tuple

to perform this lab,
use the google again to search for terraform aws s3bucket, copy the s3bucket basic block code to a main.tf file.

Ensure your provider block is already set and configured as explained earlier.


![s3bucket](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mzjsi3g9xn2so9pj90gl.png)
After your components are ready, extract each of  the variables from it, with the type, description and default all stated, like so

![variables](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/louy0x0aumvkh0nf7hab.png)

copy all the variables to a file name “variable.tf”

![var ready](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oa2lmu91hnidf1d6mk29.png)


you should do this for all resources 

deploy and  confirm your s3buckets just the same way we confirmed our ec2 instance earlier (search on the console for s3bucket) you should see your bucket.

![bucket ready](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/80qxr8rlsaaxxpdlh3h2.png)


destroy the s3bucket when you're done.



TERRAFORM (MULTIPLE BLOCKS IN A SINGLE TERRAFORM CODE)
in this lab, We’ll be launching :
EC2 instance
Vpc
Subnet
Nic ==> network interface card


we’ll be creating all this in a main.tf . No variables.
The work-flow:

VPC===>subnet==>NIC==>EC2

assuming we already have our provider block.
We will start with our vpc,
as usual, search for your vpc code block, and copy the basic vpc block of code to main.tf file.
This comes with a cidr_block.
Do the same for the subnet block and copy that to our code into vscode.
Edit the vpc in the subnet block to use the vpc we added first. Like so.==>

![vpc and subnet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6hq12sihb8rh7ikt1e73.png)


also search for the nic block of code. And copy that to our vscode .
From the nic block, change the subnet to use ours, also change the private_ips to anyone, just pick from the subnet cidr_block

remember to remove the components not needed or the optional ones, since this is just a simple deployment,we shouldn’t complicate things..

![network interface](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t16jv8xoepj3qfr84bdh.png)


Finally,
Search for ec2 instance and copy its basic to  your code and
edit your ec2 like so,===>

![ec2 instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xg97qld59zj5v5hduu9m.png)



after these,
use the **terraform init,plan and apply** commands to initialize and deploy your infrastructure

![instance ready](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4xo26edyw0b62q2hjqxb.png)


use the **terraform destroy --auto-approve** to destroy it.

this is the end of this part. thanks for reading.