🌐Terraform with AWS🐧

Today we are going to create a project by using Terrorform, how we will create an instance of AWS, and also how will we create an S3 bucket.

·

5 min read

🌐Terraform with AWS🐧

As a System administrator who is early in my career, I often find it very hard to find hands-on activities that will advance my journey to become a skilled DevOps Engineer. I have recently completed my Terraform course and I have an incredible urge to use these skills and grow my competency in Terraform with AWS.

Terraform: An Overview:

Terraform, developed by HashiCorp, is a leading IaC tool that supports multiple cloud providers, including AWS. Its strength lies in its simple yet powerful syntax, enabling users to define infrastructure in a declarative manner using HashiCorp Configuration Language (HCL).

When Terraform meets AWS, the synergy is unmistakable. AWS offers a vast array of services, from compute and storage to machine learning and serverless computing. Terraform acts as the orchestrator, seamlessly integrating these AWS services into a unified infrastructure.

🐧Now, we will install Terraform in Linux machine

Prerequisites :- AWS CLI installed

AWS CLI means command line interface of Amazon Web Service.
Its function is that you can access all your services simply through the command line.
Now along with AWS-CLI you also need an AWS IAM (Identity Access Management) user. We will use IAM for all user-related services.

✏️Steps :-

1 :- Create a EC2 instance.
2 :- Install Terraform
sudo apt-get update && sudo apt-get install -y gnupg software-properties-common

wget -O-https://apt.releases.hashicorp.com/gpg| gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg

gpg --no-default-keyring --keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg --fingerprint

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-get install terraform

Terraform is used to create cloud infrastructure.
Let's Say, if I want to build an AWS infrastructure then :-

<resource> <aws_instance> {
ami =
instance = "t2.micro" (This is the AWS infra.)

Now if I want to create some resources on AWS Cloud then I have to create a provider block and put it in the Terrorform block.
We have to create a terraform block :-

✍️This is my Terrorform block which is installed. But whether the AWS provider has been installed or not?
The resource will not be created until the provider is installed.
✍️Now, To initialize the Terraform and AWS plugins, we have to run theTerrorform Initcommand.

✍️Now, if you want to configure the provider then you have to write the provider block.

✍️Now, I'm going to create a new file calledinfra.tf, where I'm going to put the AWS infrastructure.
Here I have to create an S3 bucket. The bucket name should be unique.

✍️Now If I runterrorform-applyafter running this command, in whose account S3 buckets will be created? It would need an identifier. also, it will need an account access key or secret key only then, will be able to create the S3 bucket.
Now we will create an IAM User.

Go to AWS console--Search IAM--User--Create User--Username--Next--Attach policies directly--AdministratorAccess--Next--Createuser--Security credentials--Create Access Key--Command Line Interface (CLI)--Next--Create Access Key.

✍️Now configure your system by configuring AWS. But, to configure AWS you must have CLI.
Now search on Google :- install aws cli V2 (use latest)

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"

unzipawscliv2.zip
sudo ./aws/install

✍️Now, your machine is configured for AWS.

✍️Now, if you want you can also keep all the configuration in one file. But don't do this but keep them separate because of their beautifulness.
Terraform's configuration in terraform.tf
Provider's configuration in providers.tf
Now if I runterraform initcommand, it will not make any difference because this folder has already been initialized.

✍️Then run theterraform plancommand, to see what action will be performed by the terraform

✍️Then,terraform apply

✍️Now, Check on AWS S3.

🕸️The S3 bucket has been created successfully.

Terraform | Create AWS EC2 instance with key-pair

🕸️Creating EC2 instance with the help of Terraform

We are familiar with creating an ec2 instance from the console.

Now we can try to create an ec2 instance with a new keypair using Terraform.

When we create a new keypair in AWS, need to specify the key name, key pair type, or the encryption algorithm and private key format file (.pem or .ppk)

Now we will create an EC2 instance. To create an EC2 instance :- First, we will create a resource that will create an instance from the AWS provider.
We will give the name of the instance as my_instance and to run that instance we need AMI-ID. We will also need instance type. Now you can add instance names in the tags. We need an ec2 instance and their name of the ec2 instance should be terra-auto-instance.
A key pair is required to connect the instance.

🎯What are the requirements to create a minimum ec2 instance?
Oneami-Idand 1instance_typeand that will create an instance for you. Also, a security group is required to access that instance.
Then ssh-key would be required, and then default VPC would be required. This is more than enough to create an instance through Terraform.

$vim infra.tf

$ terraform validate

$ terraform apply

🎯Now, terra-auto-instance is initializing.

🎯Now if you want to build a good ec2 instance then first of all we will need to createkey-pair. 🎯Now we will create a key in our Ubuntu server with the help of ssh.

$ ssh-keygen

🎯Now, keys have been created.

🎯Now we will createkey-pairof this ec2 instance. To create akey-pair:- First, we will create a resource that will create akey-pairfrom the AWS provider.

🎯We will create a resource and the resource type name isaws_key_pairand resource name is"key"

$ vim ec2.tf

$ terraform apply

🎯Now, check the key-pair in aws.

🎯Now, we will create an output block for checking the instance IP address.

$ vim output.tf

$ terraform validate

$ terraform apply

🎯Now, we will see the instance IP address.

Resource Block is used to create resources.

Terraform Block is used to create the configuration for your terraform.

Provider Block is used to create the configuration for your providers.

Variable Block is used to create variable that you can use.

Output Block is used to show the output.

###############################################################

✒️🎯Hello Everyone, feel free to learn all about my finished project using Terraform and AWS! I will continue to share my progress in my journey to becoming a DevOps engineer and the projects that I complete. This was a lot of fun to do and I hope you enjoy! 🙂✏️