· Techtribe team · Tutorials · 4 min read
tep-by-Step Guide to Deploying AWS EC2 with Terraform
Master the process of deploying an EC2 instance on AWS using Terraform in this detailed guide.
Deploy an AWS EC2 Instance with terraform
Steps:
Brief concept on what is IaC
Terraform provider
Terraform init, plan, apply and destroy
Configurar a máquina no Security group
Create IAM admin user, save the key and secret
Create the config file
Deploy
Hello and welcome. The topic of today will be about IaC and how to deploy an EC2 instance on Amazon AWS using Terraform. We will cover some key aspects of this technology and how it can enhance your tech stack.
🔗 If you want to watch the whole video:
https://www.youtube.com/watch?v=od7kZ0WpFLk&t=751s
What is IaC
Infrastructure as a code gives you and your projects the power to spin up and maintain your infrastructure using code and avoid the manual work overhead. As all your infrastructure will be kept on a codebase, seeing the big picture of your environments and what your architecture is like will be an easy task.
Manually managing your infrastructure takes a lot of time and is prone to numerous problems, with IaC you can manage the machines operating systems, networking architecture, environment variables, databases and much more, so it’s a good solution to easily manage different development environments.
Because you have control on the components of your infrastructure, it will help your projects scale as needed much more easily, due to a simplicity of management on all the components of it.
Not only that, it is also possible to configure multiple cloud infrastructures on your code, that will allow your projects to be more resilient, reliable and of course, profitable. If one cloud happens to be down, you can ship your entire codebase to another cloud instantly, it’s also common to manage different environments on different cloud providers.
If your company needs to change cloud providers for any reason, be it pricing or is needing a better provider, you can do it at the speed of light.
Now that we have talk more a little bit on the benefits of using IaC, let’s see what we will be doing today
Steps to deploy an EC2 instances with Terraform
Let’s first step on the AWS steps, we will be doing the following:
- We need an AWS account
The first step of course is to have an AWS account, so if you happen to don’t have one, make one for yourself, it’s free and you get 500$ to use for a while and test their services.
- We will create an IAM admin user
The second step would be to create an IAM admin user. We will use an admin user for that because the Terraform backend tries to make calls to specific resources on your account, if we don’t have the all needed permissions our deploy will fail.
You don’t actually need an admin user for that, but for the sake of simplicity we will do like that.
- Add the machine IP on security group
We must add the IP of our deployment machine in our security group related to the VPC we will use. We will do that on the default security group and on the default VPC that comes on the account.
Terraform Concepts
Now that we have our AWS account configured, let’s talk about some key concepts of terraform.
Also, there are plenty of terraform commands, on this video we will stick only on the ones we will be using.
Now let’s jump into the code:
provider.tf:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.68.0"
}
}
}
provider "aws" {
region = "sa-east-1"
access_key = ""
secret_key = ""
}
Set your the created IAM admin user access key and secret key on the above variables.
ec2.tf:
resource "aws_instance" "FirstEC2Terraform" {
ami = "ami-046cfe7738e92bfba"
instance_type = "t2.micro"
tags = {
Name = "FirstEC2TerraformInstance"
}
}
Now you can run the following commands on your terminal:
terraform init
terraform plan
terraform deploy
Don’t forget to destroy your infrastructure (if that’s the case):
terraform destroy