TeKanAid

Dark

Table of Contents
HashiCorp Vault 101 - Certified Vault Associate
Get started with HashiCorp Vault and prepare for your Vault Associate Exam
Terraform 101 - Certified Terraform Associate
Learn all you need to know to ace the Terraform Associate Exam and go beyond the certification

Terraform Import Example - AWS EC2 Instance


Do you need to import an EC2 instance into Terraform? Learn how to do this the right way without accidentally deleting your resources.

IaCHashiCorpTerraform
Created: July 27, 2022 | Updated: January 27, 2023

Video

Below is a video explanation and demo.

Terraform Import Example - AWS EC2 Instance Video

Video Chapters

This video lecture has been taken from my course: Terraform 101 - Certified Terraform Associate

You can skip to the relevant chapters below:

  • 00:00 - Introduction
  • 02:22 - Terraform Import Docs
  • 04:30 - Create EC2 Instance from Console
  • 07:18 - Run Terraform Import Command
  • 08:26 - Create the Terraform Configuration
  • 11:39 - Wrap-up

Terraform Import Example in AWS

Overview

Terraform is able to import existing infrastructure. In this blog post, you'll learn how to use Terraform import the right way. You will import existing AWS resources into Terraform. You will see a sample terraform script for AWS.

Suppose a DevOps engineer on your team decided to create an EC2 instance in AWS directly via the console and without using Terraform. Terraform doesn't know anything about this resource and therefore won't know to add it to its state file.

Now there is a way to terraform import this resource into our state file, but you'll still need to manually write the configuration for it. If you don't, you could delete that resource!

You can see this in the documentation.

Code

Get FREE access to the source code by subscribing to my newsletter
You only need to subscribe once. Already subscribed? Enter your email to get instant access to the code.

Pre-requisites

The following is required to follow along:

  • Terraform
  • Access to an AWS account to build the AWS infrastructure. We'll be running within the 12 months free tier

Create an EC2 Instance with Terraform

First, go ahead and run the terraform sample code to create an EC2 instance with terraform. Take a look at the main.tf file. You can find the code here.

Make sure you have your AWS credentials defined in the ~/.aws/credentials file. You can also run aws configure if you have the aws cli installed.

Run the following commands

terraform init
terraform apply --auto-approve

Check the AWS console to see the VM instance.

Create a new EC2 Instance via the Console

Now go ahead and create a new EC2 instance via the AWS console. Use Amazon Linux to be a bit different. You can find how to do this in the video.

Import the new EC2 Instance to Terraform

Now check the documentation for the import command for aws_instance resources

Then run the terraform import command below replacing the <instance_id> below with the one you created from the AWS console and you can find it in the image below:

Where to find the Instance ID in AWS

terraform import aws_instance.console <instance_id>
terraform plan

Notice that you get an error message that we're missing the configuration. This is good so we don't accidentally delete the resource. Take a look at the Terraform State Scenarios table below and notice the fourth one highlighted with a red box. This is our import scenario where we import the aws_instance into the state file. Now if we don't have the configuration, we're telling Terraform that we don't want the aws_instance and Terraform will go out and delete it from AWS and remove it from the state file.

Terraform State Scenarios

Uncomment the resource block below in the main.tf file to add the proper configuration. Notice that the ami below corresponds to Amazon Linux.

resource "aws_instance" "console" {
  ami           = "ami-0cff7528ff583bf9a"
  instance_type = "t2.micro"
}

Now run terraform import again:

terraform import aws_instance.console <instance_id>

This time the aws_instance resource will be imported successfully. You can now run the following:

terraform plan
terraform apply --auto-approve

You should see that there are no changes to the infrastructure so Terraform won't apply. Finally, run terraform state list and notice we have two aws_instances in the state file. This shows you that Terraform is now managing both EC2 instances.

Cleanup

Go ahead and clean up the environment with the following command:

terraform destroy --auto-approve

Check the AWS console to verify that the AWS instances are terminated.

Conclusion

In this post, we learned how to correctly import terraform resources. Terraform as of today doesn't have a mechanism to create the configuration for the resource we want to import. The terraform import command only imports the resource to the state file. If you forget to create the resource, you may risk deleting that resource when running the terraform apply command.

You can learn other Terraform commands and much more in my Terraform 101 course called Terraform 101 - Certified Terraform Associate.

If Infrastructure as Code (Iac) is of interest to you, then I recommend you take a look at building a Ubuntu 20.04 image in vSphere. Then another post to use this image to Build a Kubernetes k3s Cluster in vSphere with Terraform and Packer.

References


Other Posts
Terraform for Beginners - A Beginner's Guide to Automating Cloud Infrastructure
Terraform vs Ansible - Demo the Differences - Part 2
Terraform vs Ansible - Learn the Differences - Part 1
HashiCorp Vault Backup and Restore Raft Snapshots from Kubernetes to AWS S3
AWS Lambda - Terraform Configuration Example with API Gateway
Securing the Future - DevSecOps Trends for 2023
36 Top DevOps Questions to Get You Started in 2023
Terraform to Create a Ubuntu 22.04 VM in VMware vSphere ESXi
HashiCorp Packer to Build a Ubuntu 22.04 Image Template in VMware vSphere
Migrate Secrets from AWS Secrets Manager to HashiCorp Vault with Python, Docker, and GitLab
Migrate Secrets from AWS Secrets Manager to HashiCorp Vault with Terraform
env0 - A Terraform Cloud Alternative
Terraform Import Example - AWS EC2 Instance
DevOps Engineer NOT on Linux? You're MISSING OUT!
HashiCorp Vault API Tutorial and Pro Tips
HashiCorp Vault Tutorial for Beginners
Create a Pihole Docker Ad Blocker with Ansible and Terraform
Terraform vSphere Windows Example to Join an AD Domain
Build a Kubernetes k3s Cluster in vSphere with Terraform and Packer
HashiCorp Packer to Build a Ubuntu 20.04 Image Template in VMware
Consul-Template to Automate Certificate Management for HashiCorp Vault PKI
HashiCorp Vault PKI Secrets Engine Demo for Certificate Management
Jenkins, Vault, Terraform, Ansible, and Consul Delivering an End-to-End CI/CD Pipeline
Secret Zero Problem Solved for HashiCorp Vault
Hashicorp Packer, Terraform, and Ansible to Set Up Jenkins
Hashicorp Vault Azure Secrets Engine - Secure Your Azure Resources
HashiCorp Waypoint - Will it Replace Your CI/CD?
HashiCorp Boundary - Make Sure Your Human To Machine Access Is Secure
HashiCorp Packer for VMware Ubuntu Templates and Terraform for building VMs
HashiCorp Packer VMware Windows Templates and Terraform for VMs
Webblog App Part 4 – HashiStack – Nomad Consul Vault Terraform
Webblog App Part 3 - Consul Connect Service Mesh
Webblog App Part 2 - Secrets Development Phases with Vault
Webblog App Part 1 - Infrastructure as Code with Terraform
Microservices Applications'​ Life Cycle
HashiCorp Vault 201 - Vault for Apps in Kubernetes
Learn how to use HashiCorp Vault for your applications in Kubernetes