- Published on
Day 05: Deploying a VM in AWS Using the Terraform Workflow
- Authors
- Name
- Surya Harahap
- @suryaharahap18
Introduction
In this blog, we will be following the Terraform workflow — Write > Plan > Apply — to deploy a virtual machine (VM) in AWS. After a successful deployment, we will then clean up our infrastructure and destroy the resource we created.
Lab practice:
Log in to the lab server using the credentials provided:
ssh cloud_user@Terraform-Controller
In a web browser, log in to the AWS Management Console using the credentials provided.
Create a Directory and Write Your Terraform Code (Write)
- In the CLI, create a new directory in the cloud_user's home directory called terraform_code to house your Terraform code: terminalSwitch to the new directory:
mkdir terraform_code
terminalUsing vi or nano, create a new file called main.tf where you will write your code:cd terraform_code
terminalvi main.tf
- In the file, paste the provided code that will be used to create the required VM (EC2 instance) in AWS: terminalPress Escape and enter
provider "aws" { region = "us-east-1" } resource "aws_instance" "vm" { ami = "DUMMY_VALUE_AMI_ID" subnet_id = "DUMMY_VALUE_SUBNET_ID" instance_type = "t3.micro" tags = { Name = "my-first-tf-node" } }
:wq
to save and exit the file.
Plug the Provided AMI and Subnet ID Values Into Your Code
View the contents of the resource_ids.txt file that has been saved on the lab server:
cat /home/cloud_user/resource_ids.txt
The ami and subnet_id values that have been saved in this file will be displayed.
Copy the
ami
value.Open the main.tf file that houses your code:
terminalvi main.tf
Paste the
ami
value into your code for theami
parameter, replacing theDUMMY_VALUE_AMI_ID
placeholder text. (Note: You can add the subnet_id value at this step as well if you wish).Press Escape and enter
:wq
to save and exit the file.Copy the subnet_id value.
Open the file with your code again:
terminalvi main.tf
Paste the
subnet_id
value into your code for thesubnet_id
parameter, replacing theDUMMY_VALUE_SUBNET_ID
placeholder text.
Press Escape and enter :wq
to save and exit the file.
Initialize and Review Your Terraform Code (Plan)
Initialize the Terraform configuration and download the required providers:
terminalterraform init
Review the actions that will be performed when you deploy your code:
terminalterraform plan
In this case, it will create 1 resource: the EC2 instance you configured in your code.
If you scroll up, you will notice that only the
ami
,instance_type
,subnet_id
, andtags
properties are configured, as that was included in your code.Everything else, denoted with a
+
sign, will be created from scratch or will be populated when Terraform creates the resource upon deployment of your code.
Deploy Your Terraform Code (Apply), Verify Your Resources, and Clean Up
Deploy the code:
terraform apply
When prompted, type yes and press Enter.
Once the code has executed successfully, note in the output that 1 resource has been created.
Note: You could also use the terraform output command at any time in the CLI to view the output on demand.
Verify that the resource was created correctly in the AWS Management Console:
- Navigate to the AWS Management Console in your browser.
- Type EC2 in the search bar and select EC2 from the contextual menu.
- On the Resources page, click Instances (running).
- Verify that the instance, named my-first-tf-node (as configured in your code), appears in the list.
Back in the CLI, remove the infrastructure you just created:
terraform destroy
In the plan output, notice that it will destroy 1 resource: the EC2 instance you just created.
Note: You can also scroll through the rest of the plan output and view the properties of the resource that will be destroyed, if desired.
When prompted, type yes and press Enter.
In the notifications displayed in the CLI, note that the aws_instance.vm
resource you created is now being destroyed.
In the AWS Management Console, click the refresh button inside the Instances page and verify that the my-first-tf-node
instance no longer appears in the list.