Terraform Imports

April 19, 2021

Another short post

Recently I did some work manually in the AWS Console. After some consideration and reflecting, I found that it would have been better if this infrastructure was documented and in code.

To set it up I just need to set up a terraform file with the resources I wanted like so:

resource "aws_s3_bucket" "the-bucket" {
 # random comment
}

resource "aws_cloudfront_distribution" "the-cloudfront-distribution" {
 # random comment
}

Then to import the resouce into a terraform state file and convert to terraform syntax:

terraform init
terraform import aws_s3_bucket.the-bucket name-of-bucket
terraform import aws_cloudfront_distribution.the-cloudfront-distribution cf-name
terraform show -no-color > my-resource.tf

Now if you check my-resource.tf you will see your imported resources along with any related resources needed to fully provision the resource.

All Done!