Deploying an AWS Virtual Machine with Terraform: A Step-by-Step Guide
Introduction
Terraform is an open-source infrastructure as code (IaC) tool that allows you to define and provision data center infrastructure using a high-level configuration language. In this blog, we’ll walk through the steps to deploy a virtual machine (VM) in AWS using Terraform.
Prerequisites
Before we begin, ensure you have the following:
An AWS account
Terraform installed on your local machine
AWS CLI configured with your credentials
Setup Terraform for AWS
To configure AWS credentials and set up Terraform to work with AWS, you'll need to follow these steps:
1. Install AWS CLI (Command Line Interface):
Make sure you have the AWS CLI installed on your machine. You can download and install it from the [AWS CLI download page](https://aws.amazon.com/cli/).
2. Create an AWS IAM User:
To interact with AWS programmatically, you should create an IAM (Identity and Access Management) user with appropriate permissions. Here's how to create one:
a. Log in to the AWS Management Console with an account that has administrative privileges.
b. Navigate to the IAM service.
c. Click on "Users" in the left navigation pane and then click "Add user."
- Choose a username, select "Programmatic access" as the access type, and click "Next: Permissions."
- Attach policies to this user based on your requirements. At a minimum, you should attach the "AmazonEC2FullAccess" policy for basic EC2 operations. If you need access to other AWS services, attach the relevant policies accordingly.
- Review the user's configuration and create the user. Be sure to save the Access Key ID and Secret Access Key that are displayed after user creation; you'll need these for Terraform.
3. Configure AWS CLI Credentials:
Use the AWS CLI to configure your credentials. Open a terminal and run:
```
aws configure
```
It will prompt you to enter your AWS Access Key ID, Secret Access Key, default region, and default output format. Enter the credentials you obtained in the previous step.
Step 1: Set Up Your Terraform Configuration
First, create a new directory for your Terraform configuration files. Inside this directory, create a file named main.tf. This file will contain the configuration for your VM.
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "vm" {
ami = "ami-0c55b159cbfafe1f0" # Amazon Linux 2 AMI
instance_type = "t3.micro"
tags = {
Name = "tf_test_vm"
}
}
Step 2: Initialise Terraform
From the directory and run the command below to initialise Terraform, init command downloads the necessary provider plugins.
terraform init
Step 3: Plan Your Deployment
Next, run the terraform plan command to create an execution plan. This command shows you what actions Terraform will take to achieve the desired state defined in your configuration.
terraform plan
Step 4: Apply Your Configuration
Review the plan to make sure that only the components you intend to deploy will be configured. Install VM using the command below.
terraform apply
Step 5: Verify Your Deployment
Log in to the AWS Management Console and navigate to the EC2 dashboard. You should see your new instance running.
Subscribe to my newsletter
Read articles from Ravi Kumar Srivastava directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by