Terraform: How to Import Existing Cloud Infrastructure Resources
By Aaron F. February 21st, 2023
An effective tool for managing infrastructure as code is Terraform. You may manage and provision your infrastructure across many cloud providers after defining it in a human-readable configuration file. One of Terraform's important features is the ability to import existing resources into its state file so that you may start using Terraform to manage them. We'll go over many ways to incorporate current cloud infrastructure into Terraform in more depth in this article.
Method 1: Using the Terraform Import Command
Using the terraform import command is the simplest approach to import already-existing resources into Terraform. This command adds the resource to the Terraform state file by passing the resource type and its distinctive ID as parameters. The resource type and ID are particular to the cloud provider and resource, and are often listed in the documentation of the provider. You might execute the command below to import an existing AWS S3 bucket, for instance:
The resource type in this instance is an Amazon Web Services S3 bucket, and the S3 bucket's unique ID is
my-existing-bucket. After the resource type is displayed as an example, the name of the Terraform resource block that will be created or modified using the imported data follows.
The import command will add details about the imported resource to the Terraform state file and add a new resource block to your Terraform configuration file. The rest of your system may then manage and provision this resource block.
When using the import command, it's vital to keep in mind that it only modifies the Terraform state file and configuration file; it leaves the real resource in the cloud provider unchanged. This means that in order to provision changes made to the Terraform resource block to the cloud provider, you must execute the terraform apply command.
Method 2: Using the Terraform Provider's Import Function
Utilizing the import feature offered by the Terraform provider is another way to incorporate current resources into Terraform. It is possible to bulk import resources using this function, which is particular to the resource type and cloud provider. This can be helpful if you wish to import and manage a lot of existing resources using Terraform.
For instance, you might use the following Terraform code to import each and every one of the current AWS S3 buckets:
In this example, we'll use the
aws_s3_bucket.all() function from the AWS Terraform service to acquire a list of every S3 bucket that is currently active in the account. Then, for each bucket, a new Terraform resource block is created using a
for_each loop. Then, the bucket variable is assigned to the bucket's distinctive ID, which is used to import the bucket's data into the Terraform state file.
The benefit of using the provider's import function is that you can import many resources at once as opposed to having to execute the import command for each resource separately. It does, however, have certain restrictions because not all resources or providers provide an import feature.
Method 3: Using a Terraform Module
Finally, you can import current resources into your infrastructure by using a Terraform module. A collection of Terraform code called a module can be applied to several projects. A provider block that directs users to the available resources in a particular cloud provider can be included in a module. You could, for instance, develop a module called "existing-aws-s3-buckets" that imports every S3 bucket that currently exists in your AWS account.
existing-aws-s3-buckets module, which includes the provider block and the import functions, is called in this example. This module will import all of the existing S3 buckets in your AWS account and generate the relevant resource blocks in your Terraform configuration file.
Utilizing a module provides the benefit of increasing the modularity and reusability of your infrastructure. Each resource type and cloud provider can have its own module, which you can subsequently use in different projects. This can speed up the process and make managing your infrastructure as code simpler.
When using modules, bear in mind that you should regard them as an abstraction layer on top of your infrastructure and refrain from making any changes directly to the module's resource blocks. Instead, you should transmit data into and out of the module using variables and outputs.
There are various approaches of importing current cloud infrastructure into Terraform, each with advantages and applications of its own. The simplest option is to utilize the Terraform import command, but you may import resources in bulk and make your infrastructure more modular by using the Terraform provider's import function and modules. These techniques can assist you in getting started, regardless of whether you are new to using Terraform or looking to add existing resources to your infrastructure as code. In order to select the approach that is most appropriate for you, it is crucial to consider your unique use case and requirements.