Professional Documents
Culture Documents
Module 7 Lecture 2
Module 7 Lecture 2
August 2023
CONFIDENTIAL 1
Agenda
CONFIDENTIAL 2
TERRAFORM IMPORT
OVERVIEW
CONFIDENTIAL 3
Why Import?
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.
CONFIDENTIAL 6
Simple Import: Example
After a successful import, update the target resource block in your configuration with the required attributes,
and set them to the appropriate values.
Validate your configuration and check for any differences between your Terraform configuration and the
imported resource
Apply the required changes according to your updated terraform configuration to match the desired
configuration defined in your Terraform files.
CONFIDENTIAL 7
Importing Modules
CONFIDENTIAL 8
Importing Modules: Example
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.
Run the `terraform init` command to initialize your Terraform working directory, download providers, and retrieve the
specified 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.
CONFIDENTIAL 9
Import Block
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
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.
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
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.
CONFIDENTIAL 12
TERRAFORM TEMPLATEFILE
FUNCTION
OVERVIEW
CONFIDENTIAL 13
Introduction to `templatefile` function
It reads a template file and populates it with the given variables to produce a
string result.
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`):
3. Use the `templatefile` function to render the template with your variables:
CONFIDENTIAL 15
Useful Links
CONFIDENTIAL 16