Terraform Import
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed terraform locals.
https://cloudaffaire.com/terraform-locals/
In this blog post, we will discuss terraform import.
What is terraform import?
You can use terraform import to import your current existing infrastructure. This allows you to take resources you’ve created by some other means and bring it under Terraform management. This is a great way to slowly transition infrastructure to Terraform, or to be able to be confident that you can use Terraform in the future if it potentially doesn’t support every feature you need today.
Note: The current implementation of Terraform import can only import resources into the state. It does not generate configuration.
Next, we are going to explain terraform import with a demo.
Terraform import:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
##----------------------- ## Terraform: Import ## ##----------------------- ## Create a directory and get inside it mkdir terraform && cd terraform ## Create main.tf vi main.tf ------------------ #creates a vpc provider "aws" { access_key = " secret_key = " region = "ap-south-1" } resource "aws_vpc" "myvpc" { cidr_block = "10.0.0.0/16" enable_dns_hostnames = true tags { Name = "myvpc" } } output "vpc_id" { value = "${aws_vpc.myvpc.id}" } ------------------ :wq ## Format code terraform fmt ## Initialize terraform terraform init ## Create the resource (note down vpc id from output) terraform apply ## Create a directory named terraform_import and get inside it mkdir terraform_import && cd terraform_import ## Create a resource import vi main.tf ------------------ provider "aws" { access_key = " secret_key = " region = "ap-south-1" } resource "aws_vpc" "myimportedvpc" { #if you want to change any configuration, #define here. and then apply once configuration is imported #in this demo, we will just import the configurations. } ------------------ :wq ## Format code terraform fmt ## Initialize terraform terraform init #note terraform has craeted an empty terraform.tfstate file ls cat terraform.tfstate #currently empty ## Import the resource to terraform.tfstate file terraform import aws_vpc.myimportedvpc vpc-0cf62221259f35885 ## View state (myvpc configuration saved as myimported vpc) cat terraform.tfstate ## Cleanup cd .. && terraform destroy -auto-approve cd && rm -r terraform |
Hope you have enjoyed this article. In the next blog post, we will discuss terraform refresh.
To get more details on terraform, please refer below terraform documentation.
https://www.terraform.io/docs/index.html
Hi…i tried importing a vpc…but when i try to destroy it gives me an error like Error: aws_vpc.myimportedvpc: “cidr_block”: required field is not set..can you help me with this.
Dear Aakrash,
Can you please provide your tf state file and import command that you have executed.
You can try the import with CIDR block like below
resource “aws_vpc” “myimportedvpc” {
cidr_block = [“your_cidr_block”]
}
Thanks,
Debjeet