Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Terraform “Import” functionality

and ”templatefile” function

August 2023

CONFIDENTIAL 1
Agenda

• Terraform Import overview


• Terraform templatefile Function overview

CONFIDENTIAL 2
TERRAFORM IMPORT
OVERVIEW

CONFIDENTIAL 3
Why Import?

Terraform can import existing infrastructure resources.


This functionality lets you bring existing resources under Terraform management.

Getting the pre-existing cloud resources under the Terraform management is


facilitated by Terraform import. import is a Terraform CLI command which is
used to read real-world infrastructure and update the state, so that future
updates to the same set of infrastructure can be applied via IaC.

The import functionality helps update the state locally and it does not create
the corresponding configuration automatically. However, the Terraform team is
working hard to improve this function in upcoming releases.

CONFIDENTIAL 4
Simple Import

Terraform expects that each remote object is bound to a single resource address.
You should import each remote object to one Terraform resource address. If you
import the same object multiple times, Terraform may exhibit unwanted behavior.

The terraform import CLI command can only import resources into the state.
Importing via the CLI does not generate configuration. If you want to generate the
accompanying configuration for imported resources, use the import block instead.

Before you run terraform import you must manually write a resource configuration
block for the resource. The resource block describes where Terraform should map
the imported object.

CONFIDENTIAL 5
Simple Import: Example

To import resource into an existing Terraform configuration using the Terraform Import CLI, follow these steps.

1. Identify the exact required resource ID for concrete resource type


If the target resource supports import, examples of its format and how the command can be executed can be found in
Terraform’s official documentation,

2. Prepare the Terraform configuration.


Create or update your Terraform configuration with a target resource block. Don’t include attributes. Use an appropriate alias
instead.

CONFIDENTIAL 6
Simple Import: Example

3. Run `terraform init`

4. Perform the `terraform import`

5. Update the Terraform configuration.

After a successful import, update the target resource block in your configuration with the required attributes,
and set them to the appropriate values.

6. Run `terraform plan`

Validate your configuration and check for any differences between your Terraform configuration and the
imported resource

7. Run `terraform apply`

Apply the required changes according to your updated terraform configuration to match the desired
configuration defined in your Terraform files.

CONFIDENTIAL 7
Importing Modules

Modules wrap multiple resources into a single package that can be


reused in various projects. You should expect a large codebase and
many resources to be part of the module.

The process of importing resources created using a module is very


similar to single import, with a little difference in running the
command. In case of module import you need to execute `terraform
import` command against each resource defined inside the module

CONFIDENTIAL 8
Importing Modules: Example

1. Define the module source in your Terraform configuration.

In your Terraform configuration file (usually named main.tf), add a module block to define the module's source and any
required variables. The source can be a local directory, a remote Git repository, or hosted on the Terraform registry.

2. Retrieve the module using `terraform init`

Run the `terraform init` command to initialize your Terraform working directory, download providers, and retrieve the
specified module

3. Import the existing resource into the module.

You can now import the existing resources into Terraform. Note that when importing a resource into a module, you'll
need to specify the module path within the import command. This includes the module name as defined in the module
block.

terraform import module.azure_vnet.azurerm_virtual_network.vnet {VnetResourceId}


terraform import module.azure_vnet.azurerm_subnet.subnet {SubnetResourceId}

4. Run `terraform plan` and `terraform apply`

CONFIDENTIAL 9
Import Block

In Terraform 1.5, there is a new import mechanism available. A new top-level


import block can be defined in your code to allow import operations. As this
block is added in the code, import will not be a state operation, from now
on, as for every other resource, it becomes a plannable operation.

Before importing, you must add configuration for every resource you want
Terraform to import. Otherwise, Terraform throws an error during planning,
insisting you add resource configuration before it can successfully import.
You can create resource configuration manually or generate it using
Terraform (experimental feature).

CONFIDENTIAL 10
Import Block: Example

1. Add the `import` block

Add an import block to your configuration. This import block can be in a separate file (e.g., import.tf) or an existing
configuration file.

The import block's to argument points to the address a resource will have in your state file. If a resource address in
your state matches an import block's to argument, Terraform attempts to import into that resource. In future planning,
Terraform knows it doesn't need to generate configuration for resources that already exist in your state.

The import block's id argument uses that resource’s import ID.

2. Plan and generate configuration

To instruct Terraform to generate configuration for the import blocks you defined, run `terraform plan` with the `-
generate-config-out=` flag and a new file path. Terraform displays its plan for importing your resource and the file
where Terraform generated configuration based on this plan.

CONFIDENTIAL 11
Import Block: Example

3. Review generated configuration

Review the generated configuration and update it as needed. You may wish to move the generated configuration to
another file, add or remove resource arguments, or update it to reference input variables or other resources in your
configuration.

4. Run `terraform apply` to import your infrastructure

CONFIDENTIAL 12
TERRAFORM TEMPLATEFILE
FUNCTION
OVERVIEW

CONFIDENTIAL 13
Introduction to `templatefile` function

The `templatefile` function in Terraform is a powerful tool to render templates


with dynamic data.

It reads a template file and populates it with the given variables to produce a
string result.

Useful for creating configuration files, setting up metadata, and generating


custom scripts like shell bash scripts.

This function can be used only with files that already exist on disk at the
beginning of a Terraform run. Functions do not participate in the dependency
graph, so this function cannot be used with files that are generated dynamically
during a Terraform operation.

CONFIDENTIAL 14
Configuring the `templatefile` function to use as VM custom_data

1. Create a Bash script template file with placeholders for variables (e.g., `custom_setup.sh.tftpl`):

2. Define variables in your Terraform configuration file (e.g., `variables.tf`):

3. Use the `templatefile` function to render the template with your variables:

CONFIDENTIAL 15
Useful Links

1. Terraform CLI import command:


https://developer.hashicorp.com/terraform/cli/import
2. Terraform Import block:
https://developer.hashicorp.com/terraform/language/import
3. Terraform templatefile function:
https://developer.hashicorp.com/terraform/language/functions/templatefile

CONFIDENTIAL 16

You might also like