"Streamlining Cloud Management: Importing Existing AWS Infrastructure into Terraform"

Better late than never !

I might be late to the game, but I've recently started working with Terraform and have quickly started to love its capabilities. I thought it would be worthwhile to share some of my learnings and experiences here.

Alright, Definitions First:

AWS (Amazon Web Services) is a leading cloud platform offering an extensive range of services, with hosting and managing infrastructure being one of its primary strengths. If you're involved in IT, it's almost certain you've encountered their offerings in some capacity(https://aws.amazon.com/).

Terraform is an open-source Infrastructure as Code (IaC) tool that allows you to define, provision, and manage infrastructure across multiple cloud providers and on-premises environments using a simple declarative language. It’s a go-to tool for automating and scaling infrastructure efficiently(https://www.terraform.io/).

Here's the scenario: You've been running infrastructure (like EC2, S3, etc.) on AWS, and now you've decided to manage it as code. A quick search highlights Terraform as a popular choice. Before you ask, 'What about CloudFormation?', let me confess—I’m a bit biased. Plus, Terraform is open-source and supports multiple cloud providers, though you’ll find all that and more in the official documentation.

Let’s consider an example: You have an EC2 instance, which we’ll call 'websrv,' running on AWS. Now, you want to import this instance into Terraform so you can manage it as part of your infrastructure code moving forward.

Instance hosted on AWS

Let’s take note of a couple of key details for this instance: the instance type and the AMI ID. In this case, the instance type is t2.micro , the AMI ID is ami-02141377eee7defb9 and instance id i-012ddb56271a7b39b.

Now, let’s walk through the process of importing this resource into Terraform.

As usual, let’s begin by creating a folder and then a providers.tf file, where we’ll specify the provider—in this case, AWS. I typically copy the provider information directly from the official documentation (https://registry.terraform.io/providers/hashicorp/aws/latest/docs).


Terraform Provider Configuration

Under provider block, I have added region to "eu-west-1" as my instance is running there. Save the file. It would look something like this.

Terraform Provider Configuration

Now lets run the "terraform init" command to download the necessary provider plugins and modules.

$ terraform init


By this point, Terraform would have created a .terraform folder, which contains the License.txt file and the provider plugins.

Next, let's create another file called main.tf, where we'll define the resource block for the resource we'll be importing. Remember the key details we collected earlier from the instance? We'll be using them here.

Let's go ahead and add the resource block and add "ami" as well as "instance_type" as shown below.

main.tf

Now that we’re all set to import the instance, let’s run the import command. The syntax is: "$ terraform import aws_instance.<instancename> <instanceid>". In our case, it would be: $ terraform import aws_instance.websrv i-012ddb56271a7b39b.

$ terraform import aws_instance.websrv i-012ddb56271a7b39b

Yay! We’ve successfully imported the EC2 instance. To verify the Terraform state, you can run the command: terraform state list, which will display the instance we just imported.

$ terraform state list

To view all the details of the instance we imported, check the terraform.tfstate file, which contains comprehensive information about the instance.

We can also run terraform plan and terraform apply to see if any changes or updates are required.

$ terraform plan
$ terraform apply -auto-approve

And that concludes the process of importing an EC2 instance into Terraform code. This is just the tip of the iceberg. For more detailed information, refer to the official documentation: Terraform State Import.


Clean Up

You can clean up the instance by running the terraform destroy command, which will remove it from your AWS infrastructure. (terraform destroy -auto-approve)

$ terraform destroy -auto-approve

Verify in AWS Console

I hope you found this article helpful and enjoyed reading it! Feel free to share your thoughts or ask any questions in the comments.

要查看或添加评论,请登录

Vadiraj J.的更多文章

社区洞察

其他会员也浏览了