Advanced Terraform Import of existing S3 Bucket

Angelos Alexopoulos
1 min readMar 3, 2022

--

We all have faced the issue of trying to use Terraform to code our infrastructure. A very common issue that we definitely need to tackle is how to include in our terraform code existing resources.

To do so we can use terraform import functionality. The official documentation is really great and quite explanatory. However, I just wanted to add a small example here for anyone wanting to see some more use cases.

So let's say we have a bucket named usw2-datalake-bucket and we want to import to our terraform module code then we have to use terraform import command. After that, we mention the whole logical path to our resource. So a quite realistic use case is keeping our terraform code in some module, which is why we have to mention the whole path joined with ‘.’: module.name_of_module.aws_s3_bucket.resource_name

terraform init
terraform import module.my-custom-tf-module.aws_s3_bucket.kinesis_firehose_bucket usw2-datalake-bucket
terraform plan --out importing_planThat's it :-)

In case we have a count in our resource definition we can import the existing bucket by enclosing our import statement with quotes and indexing desired bucket

terraform import 'module.my-custom-tf-module.aws_s3_bucket.kinesis_firehose_bucket[0]' usw2-ddatalake-bucket

For a step-by-step tutorial on how we can import pre-existing cloud resources before we continue to develop the IaC in Terraform please visit this excellent guide.

--

--

Angelos Alexopoulos
Angelos Alexopoulos

No responses yet