How to use Terraform import? - Part 22

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello there Rahul this side and this is one of the most requested videos from many of you guys and like how to import the resource back into our terraform code so I have prepared this session where I'll be taking two examples in the first example I'll be just creating an easy to instance on AWS manually from the web GUI console so where I'll go and create an ec2 instance and then I'll use the terraform import command to import that easy to instance back to our terraform code and once I have imported that resource then I'm just gonna write the terraform code or reconstruct the terraform code for that particular ec2 instance so that is going to be one basic example so that you know like how terraform import Works second we are just gonna take one more example where we will be creating an S3 bucket manually from again from the GUI console of AWS and then we are just gonna run the terraform import command to re-import the whole S3 bucket back into our terraform code and reconstruct the terraform code for our S3 bucket so so these are the two examples which I'll be covering this session but these Concepts under command are pretty much common which you can use whenever you are working with any other resource on AWS as well as on a Google cloud or Azure so let's switch over to my desktop and let's see some demo in action let's try to understand the situation over here so on the right hand side you will see this is our AWS environment and in this AWS environment we have already started an ec2 machine which is running and for that ec2 instance we have got a unique ID associated with that ec2 instance but that easy to instance has been started manually or someone has already done in the past but if you take a look on the left hand side then we don't have the terraform code for that particular ec2 machine so whenever we talk about terraform then we should have a infrastructure as a code for each resource which we are trying to start on cloud environment but in this scenario we don't have the code or the terraform code for that particular ec2 instance so I'm just talking about this code this code is completely missing so in that kind of a scenario we need to import the resource back and reconstruct our terraform file so that we can later on use the terraform init plan and apply command now you might be wondering like what's the syntax or what's the import command to import any resource from the cloud environment so here if you look look carefully over here into this screen then I have written and Syntax for importing a resource so first of all you need to write terraform then import and here you need to mention the resource type over here after that you need to mention the resource name and the resource ID so if you compare with the current example so this resource ID we are going to put i12345 this is just an arbitrary example I'm just taking but I'm just trying to refer to this example over here on to the top and the resource name this resource name you can keep anything whichever you want so it's up to you you can keep any resource name of your choice but the resource type you need to mention the exact resource type if it's easy to instance then you need to use AWS instance over here and if you are using S3 bucket then you need to use that S3 bucket name over here so you need to carefully Define or put the AWS instance type over here so here is the example command which is I have written considering the scenario which I have explained to you so here this is terraform this is import and here I'm just writing the type of the instance that is AWS instance then the name which is easy to example and the ID which I want to import back now we have a little background on how to import the resource back let's try to simulate the scenario so first of all I'm just gonna do I'm just gonna manually create an easy to instance onto my AWS console so this is my AWS account I'll just go and click on launch instances I'll put the name of the instance that is easy to example I'm just gonna use any Ubuntu machine and here I'm just gonna verify all the details so I'm just gonna use T2 micro because I'm just doing it for demo that's why and here let's keep the key pair name I just don't want to associate any key pair and I will just go with the default configuration actually I just wanted to set up something uh for ec2 instance and I'll just click on the launch instance oh I need to create the key pair so I will just go over here and I will just select this one which I have already created and I'll just click on launch it stars so this will set up an easy to instance for me and I think it's already done it so if you go on instances so you can see over here so this instance is just getting started so the key thing which I you need to remember over here is the instance ID which you can see over here I'll zoom in a bit so this is the instance ID which is important for us because that instance ID we will be needing for importing our resources so now I'll switch back to my ID and we will run some terraform import commands okay so here is my ID and this is the terraform code which I need to write so first of all you if you look carefully so I have written only the resource block over here the resource block is empty because we have not imported anything yet so first of all just create a provider since I'm using AWS that's why the provider over here is AWS and the resource type so here I'm just trying to import the ec2 instance so that's why the resource type is AWS instance and here ec2 example this is just my own defined name which you can keep by your own choice so and after that the body of the resource should be empty because we are just trying to import the resource all right now we need to create the terraform import command so just this is an example I have already written for terraform import command which I have commented so here we need to replace this instance ID so I will just go back to my AWS console and here I will just copy this ID go back to my IDE I'll just replace the command okay so here I'm just trying to write the terraform import on AWS instance the instance is ec2 example which is a user defined name and the instance ID so this is the command which we are going to use for importing the resource let's copy the whole command and go to your terminal so this is my terminal and just paste the command and hit enter and here you can see import success pool which means you have imported the ec2 instance from AWS console or AWS environment to your local terraform workspace now once you have imported the resource from AWS console you need to reconstruct the terraform code for your ec2 instance so as of now you can see our body is still empty but we have already imported our resource so what we are gonna do we are just gonna insert the missing tags or missing attributes which is necessary for creating an ec2 instance so the important attribute which we are talking about is the Ami the machine ID which is needed for provisioning and ec2 instance the instance type which is T2 dot micro if you remember previously from the UI console we have set up this T2 dot micro and the tag name tag names are kind of optional if you have inserted over there then just try to keep the same tag name all right so now we have reconstructed the body of our ec2 instance and we have already imported the resource now we need to verify that our terraform code is correct in accordance with our ec2 instance which is running on AWS environment so for that what we can do we just need to run the terraform plan and after that we can just run the terraform apply command so both the command should be successful and it should not create any new ec2 instance because our ec2 instance is already running our aim is to recreate the whole terraform code not to restart or not to recreate the resource so here once we will run the terraform plan and once we will run the terraform apply command then nothing will get provisioned it will just complete successfully without adding or removing any new resources let's open the terminal over here and the next command which I'm just going to run is terraform plan just to verify that whether we have imported the resource successfully or not so the command which I'm just gonna run is terraform plan and here you can see after importing the resource we are able to run the terraform plan command and it says like no changes your infrastructure matches the configuration so that means we have successfully imported the resource pack and similarly if we are gonna run the terraform apply command then also it's just it's just gonna run successful without adding or destroying any resource so I'm just gonna run terraform apply and here you can see uh all apply complete without any resource has been added or destroyed so that means you have successfully imported the resource back into your terraform code now we have seen in the previous example how to import an ec2 instance now the next example we are going to see how to import an S3 bucket back into our terraform code so for that I'm just gonna first create an S3 bucket so I'm just gonna go over here onto AWS console and S3 and then click on create bucket and here you can mention the name of the bucket so I'm just gonna keep my demo J hook uh bucket just for an example I'm just gonna correct the typo name over here sorry it should be bucket yeah I'll just go with the default configuration over here and I'll just uh create a tab also so I'll just put the name and I'll just put the name of the tag that is Trust sorry bucket and after that I'm just gonna click on create bucket it might take few time and here you can see our test bucket has been created now we are just gonna re-import this test bucket into our terraform code and now I'll switch over to my ID and then we will run the terraform import commands so this is my ID and here we need to reconstruct the terraform import command with the correct bucket name so if you go back to our browser and here you will see the name of the bucket is my demo J hook bucket that's the name of my bucket so if you go back to IDE then you will find I have written the command with the bucket name so the command is again pretty simple terraform import the resource name is AWS S3 bucket this is the resource type name for AWS you need to be really careful and then the resource type name this can be user defined name but this one should be AWS S3 bucket because it signifies the resource type and this one signifies the name which you are trying to assign to that particular type and this is the actual bucket which we have created over there onto GUI so and also here I'm just creating the resource block which is empty because first of all I just want to import and also whenever you're trying to import the bucket then you need to create the AWS S3 bucket ACL resource block also otherwise you will not be import all the attributes of S3 Bucket from AWS all right so now we have placed everything uh so I'm just gonna copy this command from here go to my terminal and paste the command over here and hit enter and here you can see we have imported the S3 bucket uh successfully onto our uh local workspace now we have only imported the one part of the S3 bucket the next part which we need to import is the AWS S3 bucket ACL and the command is again pretty simple terraform import type of the resource that is AWS S3 bucket ACL and the name which is I am taking over here is example so I'm just kept the same thing over here and the name of the bucket for which this AWS S3 bucket ACL belongs so that's the My Demo G hook bucket which is same as over here so I'm just gonna rerun this command also so that we can import the second part of this AWS S3 bucket ACL also so I'm just gonna head over to terminal I'm just gonna paste the command over here and hit enter and here you can see it has been imported successfully now the next thing what we are going to do we are just gonna reconstruct the piece of code in our terraform file coming back to my code so here first thing which we need to insert the attribute is the bucket and that should match with the name that is bucket name and this is the bucket name which we have we are trying to import then secondly the tax which is the important attribute so this is the tag which we have created at the time of creation of a SQ bucket manually from the GUI console so these are the two important attributes which we need to put in over here and secondly for AWS S3 scl bucket we need to I have just copied it already so you need to reference the original bucket so here AWS S3 which is this one the my test bucket which is this one and the ID so the whatever ID which we have got in AWS the same ID we are going to refer over here so this way this is going to point to this particular bucket so these are the block which or the terraform code which you need to fill in after importing the resources after that we are just gonna run the terraform plan and then we are just gonna run the terraform apply command and again same this terraform plan and terraform apply command will not do anything but it should complete successfully so that you can verify that you have important imported everything successfully all right so this is my terminal and I'm just gonna clean the previous output the next command which I'm just gonna run is terraform plan and here you can see it says no changes and your infrastructure matches the configuration which means we have successfully imported all the resources related to our S3 bucket and here you can see the resources name that is our S3 bucket and AWS S3 bucket ACL so these two resources we have imported successfully and at the same time if you are trying to run the terraform apply command also then also it's gonna execute successfully without adding or changing any kind of a resources so here you can see no changes your configuration matches the configuration so this is how you are gonna import the resource I have just shown a very basic example for ec2 instance and S3 bucket but if you have any kind of a resource which is already provision then you need to look for that resource definition or like how you can you create that particular resource using terraform and then you need to run the terraform import command and reconstruct the terraform code for that particular resource I hope this tutorial will help you to understand the concept of a terraform import and these terraform import concept can be used for any Cloud environment whether it's a Google cloud or whether it's Azure so you can just look for the like actual code like what kind of a code or what kind of a resource you are trying to provision and then try to look for the IDS which are associated with that the resource and try to run the terraform import command and then you need to reconstruct the whole code of your terraform and that you can reconstruct by referring to the terraform documentation because they have a really good documentation for all these so you just need to reconstruct the body of your terraform code and then you should be able to reconstruct the whole terraform code for already provisioned resources so I hope you liked the today's session on terraform import and if you are interested into the similar concept on terraform or devops then please consider subscribing to this Channel and if you have any comments or suggestion then please put down into the comment section below and I'll try to answer those so see you into the next session of a terraform till then take care and bye bye
Info
Channel: Rahul Wagh
Views: 16,708
Rating: undefined out of 5
Keywords: terraform, terraform import resource, terraform import
Id: miz6jTSHn7s
Channel Id: undefined
Length: 16min 22sec (982 seconds)
Published: Tue Jan 10 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.