Import a Resource to a Terraform Module

Importing an existing AWS instance into a Terraform module is straightforward, but the syntax trips people up because you need to specify the full module path. terraform import module.foo.aws_instance.bar i-abcd1234 The format is module.<module_name>.<resource_type>.<resource_name>, followed by the resource ID. In this case, foo is the module name, aws_instance is the resource type, bar is the resource name inside that module, and i-abcd1234 is the AWS instance ID. Once imported, Terraform will manage the instance and track any configuration drift going forward.

Terraform Modules in Subdirectories

Terraform modules are usually at the root of a repository. Sometimes they’re nested in a subdirectory, and you need to point Terraform at the right one. Terraform handles this with a double-slash (//) in the source path. Everything after the // is treated as a subdirectory within the package. How It Works The syntax is straightforward: module "consul" { source = "hashicorp/consul/aws//modules/consul-cluster" } The hashicorp/consul/aws part is the module registry path. The //modules/consul-cluster tells Terraform to look inside that package for the actual module. ...