{"id":61764,"date":"2022-04-08T14:00:00","date_gmt":"2022-04-08T18:00:00","guid":{"rendered":"https:\/\/jumpcloud.com\/?p=61764"},"modified":"2024-08-14T17:24:01","modified_gmt":"2024-08-14T21:24:01","slug":"how-to-launch-amazon-linux-ec2-instance-terraform","status":"publish","type":"post","link":"https:\/\/jumpcloud.com\/blog\/how-to-launch-amazon-linux-ec2-instance-terraform","title":{"rendered":"How to Launch an Amazon Linux EC2 Instance Using Terraform"},"content":{"rendered":"\n
Jump to Tutorial<\/a><\/p>\n\n\n\n Cloud computing has revolutionized the way organizations deploy and manage resources, and provides numerous benefits to businesses of all sizes. These benefits include:<\/p>\n\n\n\n The advancement of cloud computing has gone hand in hand with transformational technologies that aim to boost the efficiency and speed of deploying and managing resources. One of these technologies is Infrastructure as Code (IaC).<\/p>\n\n\n\n IaC is the practice of deploying, managing, and provisioning resources using configuration files instead of using interactive graphical tools or manual hardware configuration. Simply put, it is the management of your infrastructure using code that provides a blueprint for the desired state of the resources.<\/p>\n\n\n\n Before IaC, operation teams would manually make configuration changes to their infrastructure, which was a tedious and time-consuming task often peppered with inconsistencies and errors.<\/p>\n\n\n\n IaC provides consistency and error reduction in the deployment and management of resources. In addition, it increases the speed of deployment, eliminates configuration drift, and reduces the costs that come with manual deployment.<\/p>\n\n\n\n One of the most popular IaC utilities is Terraform<\/a>. Developed by HashiCorp, Terraform is an open source IaC utility that uses a declarative configuration language known as HashiCorp Configuration Language (HCL) to manage or deploy hundreds of cloud resources. <\/p>\n\n\n\n Terraform takes away a huge chunk of deploying resources from developers who only need to run a few commands to have their resources ready. It also forms a critical part of DevOps and CI\/CD implementation<\/a>.<\/p>\n\n\n\n In this guide, you will learn how to deploy an EC2 instance using Terraform.<\/p>\n\n\n\n To realize the power of Terraform in deploying resources, the first step is to install it. Follow the steps outlined below for your operating system. <\/p>\n\n\n\n Note that although the apt-key is deprecated<\/a>, as of the publishing of this tutorial, these instructions are provided by HashiCorp<\/a> and will still work.<\/p>\n\n\n\n First, add the HashiCorp GPG key.<\/p>\n\n\n\n Next, add the HashiCorp repository.<\/p>\n\n\n\n Once the repository is added, update the package lists to notify your system about the newly added repository and then install Terraform using the APT package manager.<\/p>\n\n\n\n If you are running a modern Red Hat distribution such as RHEL 8, CentOS 8 and 9, Rocky Linux, or AlmaLinux, follow the steps shown.<\/p>\n\n\n\n Install the prerequisite packages:<\/p>\n\n\n\n Next, add the HashiCorp repository<\/p>\n\n\n\n Lastly, install Terraform using the DNF package manager.<\/p>\n\n\n\n Install the prerequisite packages first.<\/p>\n\n\n\n Next, add the HashiCorp repository.<\/p>\n\n\n\n And finally, install Terraform using the DNF package manager.<\/p>\n\n\n\n When the installation of Terraform is complete, verify the version installed as follows.<\/p>\n\n\n\n From the output, you can clearly see the latest version of Terraform is installed.<\/p>\n\n\n\n As discussed earlier in the introduction, Terraform is an IaC utility. As such, you will create a configuration file, which you will then leverage to deploy an EC2 instance.<\/p>\n\n\n\n First, you will create a Terraform directory and navigate to it. <\/p>\n\n\n\n Next, you will create a configuration file with the In the first block, specify your cloud provider, in this case If you don\u2019t have these keys, you can create them by following the steps below:<\/p>\n\n\n\n With the Access keys in hand, specify them in the configuration block below.<\/p>\n\n\n\n In the second block, specify the EC2 instance details such as the resource type, AMI, instance type, and tags.<\/p>\n\n\n\n The complete configuration file should resemble the following:<\/p>\n\n\n\n Some important points to note:<\/p>\n\n\n\n With the configuration file in place, initialize the configuration as follows:<\/p>\n\n\n\n The command initializes the working directory containing the Terraform configuration file. This is the first command that should be executed right after writing a new Terraform configuration file.<\/p>\n\n\n\n Thereafter, run the Simply put, the If everything goes well, you should get some output that resembles the screenshot below. If you get an error, head back to the Terraform configuration file and check for any syntax errors or invalid Access keys.<\/p>\n\n\n\n To deploy the EC2 instance, run the When prompted to perform the actions specified in the configuration file, type \u2018 To confirm that the EC2 instance was created, head back to your AWS account and navigate to the EC2 section. You should see your EC2 instance up and running.<\/p>\n\n\n\n Since this is a test, you can destroy your instance by running the command:<\/p>\n\n\n\n This is the opposite of And there you have it! This article demonstrated how you can provision an EC2 instance on AWS using Terraform. As you may have noticed, it\u2019s an easy and efficient way to provision resources in the cloud.<\/p>\n\n\n\n Once resources have been provisioned, the next question is: how do you easily and securely provision user access to those resources? If you\u2019re an IT admin, the answer is simple: Use JumpCloud.<\/p>\n\n\n\n Our cloud directory platform is an official identity provider (IdP) for AWS Single Sign-On (SSO), which means you can simplify onboarding and secure group management<\/a> from a unified cloud-based admin console.<\/p>\n\n\n\n\n
Step 1: Install Terraform on Linux<\/strong><\/h2>\n\n\n\n
For Debian\/Ubuntu<\/h3>\n\n\n\n
$ curl -fsSL https:\/\/apt.releases.hashicorp.com\/gpg | sudo apt-key add –<\/code><\/p>\n\n\n\n
$ sudo apt-add-repository “deb [arch=amd64] https:\/\/apt.releases.hashicorp.com $(lsb_release -cs) main”<\/code><\/p>\n\n\n\n
$ sudo apt-get update && sudo apt-get install terraform -y<\/code><\/p>\n\n\n\n
For CentOS and RHEL 8\/Rocky Linux and AlmaLinux<\/h3>\n\n\n\n
$ sudo yum clean all<\/code><\/p>\n\n\n\n
$ sudo yum update<\/code><\/p>\n\n\n\n
$ sudo yum install -y yum-utils<\/code><\/p>\n\n\n\n
$ sudo yum-config-manager –add-repo https:\/\/rpm.releases.hashicorp.com\/RHEL\/hashicorp.repo<\/code><\/p>\n\n\n\n
$ sudo dnf install terraform -y<\/code><\/p>\n\n\n\n
For Fedora<\/h3>\n\n\n\n
$ sudo dnf install -y dnf-plugins-core<\/code><\/p>\n\n\n\n
$ sudo dnf config-manager –add-repo https:\/\/rpm.releases.hashicorp.com\/fedora\/hashicorp.repo<\/code><\/p>\n\n\n\n
$ sudo dnf install terraform -y<\/code><\/p>\n\n\n\n
$ terraform version<\/code><\/p>\n\n\n\n
<\/figure>\n\n\n\n
Step 2: Create a Terraform configuration file for provisioning the EC2 instance<\/strong><\/h2>\n\n\n\n
$ mkdir terraform && cd terraform<\/code><\/p>\n\n\n\n
.tf<\/code> file extension. In this example, it is called
config.tf<\/code>. This is an arbitrary name, so feel free to name it whatever you like.<\/p>\n\n\n\n
$ sudo vim config.tf<\/code><\/p>\n\n\n\n
“aws”<\/code> followed by your Access keys (Access key ID and Secret access key). These are keys used to sign programmatic requests made to AWS.<\/p>\n\n\n\n
\n
\n
<\/figure>\n\n\n\n
\n
\n
\n
Access key ID: AXKDUNTIEDSEXAMPLE<\/code><\/p>\n\n\n\n
Secret access key: wLulrXUtnY\/8KNED\/pYkjKYEXAMPLEKEY<\/code><\/p>\n\n\n\n
\n
#Access & secret keys<\/code><\/p>\n\n\n\n
provider “aws” {<\/code><\/p>\n\n\n\n
access_key = “<\/code>
AKXXXXXXXXXXXGI<\/code>
“<\/code><\/p>\n\n\n\n
secret_key = “<\/code>
acXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXm<\/code>
“<\/code><\/p>\n\n\n\n
region = “us-east-2”<\/code><\/p>\n\n\n\n
}<\/code><\/p>\n\n\n\n
#EC2 instance details<\/code><\/p>\n\n\n\n
resource “aws_instance” “instance1” {<\/code><\/p>\n\n\n\n
ami = “ami-0ba62214afa52bec7”<\/code><\/p>\n\n\n\n
instance_type = “t2.micro”<\/code><\/p>\n\n\n\n
tags = {<\/code><\/p>\n\n\n\n
Name = “RHEL-8”<\/code><\/p>\n\n\n\n
}<\/code><\/p>\n\n\n\n
}<\/code><\/p>\n\n\n\n
<\/figure>\n\n\n\n
\n
\n
Step 3: Launch the EC2 instance using Terraform<\/strong><\/h2>\n\n\n\n
$ terraform init<\/code><\/p>\n\n\n\n
<\/figure>\n\n\n\n
terraform plan <\/code>command. The command runs a simulation of the expected outcome after running the configuration file. It determines the desired state of all the resources defined in the configuration file.<\/p>\n\n\n\n
$ terraform plan<\/code><\/p>\n\n\n\n
terraform plan <\/code>command performs a dry run and no resources are provisioned.<\/p>\n\n\n\n
<\/figure>\n\n\n\n
terraform apply <\/code>command. The command carries out the changes specified in the configuration file. In this example, it launches an EC2 instance of the
t2.micro<\/code> instance type in the
us-east-2<\/code> region using the specified ami image and the
RHEL-8<\/code> tag,<\/p>\n\n\n\n
$ terraform apply<\/code><\/p>\n\n\n\n
<\/figure>\n\n\n\n
yes<\/code>\u2019. Right after, Terraform will provision the EC2 instance and you will get a confirmation message on the terminal printed in green.<\/p>\n\n\n\n
<\/figure>\n\n\n\n
<\/figure>\n\n\n\n
$ terraform destroy<\/code><\/p>\n\n\n\n
terraform apply<\/code> and it terminates all the resources specified in your Terraform configuration file.<\/p>\n\n\n\n
Conclusion<\/strong><\/h2>\n\n\n\n