Importing hand-built AWS resources into Terraform
Everything in my blog's AWS account was clicked together in the console in 2020. Here is how I brought it under Terraform with import blocks, the key pair that wanted to be replaced, prevent_destroy on the live server, and a tiny wrapper that keeps credentials off the screen.
The AWS account behind fenixara.com was set up in 2020, by me, in the console. One EC2 instance, an Elastic IP, a key pair, a couple of security groups, two IAM users and the default VPC. No code anywhere. Six years later I wanted to move the blog to a new server, and I didn't want to click together a second server the same way.
So step one was getting what already exist into Terraform, without Terraform deciding to recreate any of it. In this blog we will look at how I did that with import blocks, and the two or three things that would have bitten me.
Credentials without typing them
Before any Terraform, I wanted a way to run it where the credentials never show up on screen or in shell history. My credentials file uses its own variable names, not the ones the AWS provider looks for, so I wrote a tiny wrapper called tf.
#!/bin/bash
# Wrapper: loads credentials from aws.env into the environment (never printed)
# and runs terraform. Usage: ./tf plan | ./tf apply | ...
set -euo pipefail
DIR="$(cd "$(dirname "$0")" && pwd)"
source "$DIR/aws.env" >/dev/null 2>&1
export AWS_ACCESS_KEY_ID="$ACCESS_KEY_ID"
export AWS_SECRET_ACCESS_KEY="$SECRET_ACCESS_KEY"
export AWS_REGION="${REGION:-ap-south-1}"
unset ACCESS_KEY_ID SECRET_ACCESS_KEY
exec terraform -chdir="$DIR" "$@"
aws.env sits next to it, is in .gitignore, and has three lines: ACCESS_KEY_ID, SECRET_ACCESS_KEY and REGION. The wrapper maps them to the standard AWS_* names, drops the originals, and execs Terraform so the wrapper process is replaced and nothing lingers. -chdir means I can run ./tf plan from anywhere.
The provider block then has no credentials at all.
terraform {
required_version = ">= 1.5"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0"
}
}
}
provider "aws" {
region = "ap-south-1"
}
Before going further, add the state files to .gitignore too. With the AWS provider the state will hold a lot of details about your account, and if you ever manage an access key in Terraform, the secret goes in there as well.
aws.env
.terraform/
*.tfstate
*.tfstate.*
*.tfplan
Making a list
First I made a list of every resource in the account. It was a short list, honestly, that's the nice part of a personal account. For each one I wrote the Terraform resource type and the ID that import needs. The ID format changes by resource type, so check the "Import" section at the bottom of each resource's page in the provider docs. A security group imports by its sg- ID, a key pair by its name, an IAM user by its name, a group policy attachment by group/policy-arn.
The default VPC needs no import
The default VPC, its subnets, its route table, network ACL and default security group are special. The provider has aws_default_* resources for them. These don't create anything. On first apply they adopt the existing default and then manage its settings.
resource "aws_default_vpc" "default" {
enable_dns_hostnames = true
enable_dns_support = true
}
resource "aws_default_subnet" "ap_south_1a" {
availability_zone = "ap-south-1a"
map_public_ip_on_launch = true
}
The catch is that they will change the existing resource to match your code. So write the code to match what's live today, and read the plan. The default route table is the same story, I just made sure the route in my code was the one already there.
Import blocks
Since Terraform 1.5 you can write imports as code instead of running terraform import one resource at a time. I put them all in one file, imports.tf.
import {
to = aws_security_group.fenix_lb
id = "sg-0123456789abcdef0"
}
import {
to = aws_key_pair.fenix
id = "fenix"
}
import {
to = aws_eip.blog
id = "eipalloc-0123456789abcdef0"
}
import {
to = aws_iam_user.api
id = "api"
}
import {
to = aws_instance.blog_old
id = "i-0123456789abcdef0"
}
Each block needs a matching resource block with real settings. If you don't want to write those by hand, terraform plan -generate-config-out=generated.tf writes a first draft for any import block that has no resource yet. The draft is noisy, it puts every attribute in, but it's a good place to start from.
Then plan, and save the plan to a file.
./tf plan -out=import.tfplan
The last line is what you care about. It looks something like this (your numbers will differ).
Plan: 12 to import, 0 to add, 0 to change, 0 to destroy.
Anything in "to change" is a place where my code doesn't match reality. Sometimes that's fine (a tag I wanted to add). Often it means I got an attribute wrong, I fixed the code until the change went away. Anything in "to destroy" or "must be replaced", stop and look.
When it's right, apply exactly the plan you just reviewed.
./tf apply import.tfplan
I don't use -auto-approve for this. Applying a saved plan file means Terraform does what I read, and if the state changed in between, the apply refuses because the plan is stale. That's the behaviour I want when the thing on the other end is the only copy of my blog.
The key pair that wanted to be replaced
This one got me. My key pair resource reads the public key from a file, like you'd normally write it.
resource "aws_key_pair" "fenix" {
key_name = "fenix"
public_key = trimspace(file("${path.module}/keys/fenix.pub"))
lifecycle {
# AWS doesn't return the public key on import; avoid a forced replacement.
ignore_changes = [public_key]
}
}
Without that lifecycle block, the plan said the key pair "must be replaced". AWS doesn't give back the public key material when you import a key pair, so Terraform sees an empty value in state against my file and thinks it changed. public_key forces a new resource, so it wanted to delete the key pair and make a new one. On a key pair that's attached to a running instance. No, thank you.
ignore_changes = [public_key] tells Terraform to leave that field alone. The plan went back to a clean import.
Protecting the live server
The old instance was the one thing in the account I really couldn't lose. Node had been uninstalled from it at some point, so the Ghost process running on it was the last working copy. It would not have survived a restart, never mind a replace.
resource "aws_instance" "blog_old" {
ami = "ami-0123456789abcdef0"
instance_type = "t3.small"
key_name = aws_key_pair.fenix.key_name
lifecycle {
prevent_destroy = true
}
}
With prevent_destroy, any plan that would destroy this resource fails with an error, no matter what else is in it. It doesn't protect against the console or the CLI, only against Terraform. But Terraform was the new thing I was worried about.
When the migration was done and I really did want it gone, I took a final snapshot, removed prevent_destroy and the resource in one change, and applied that plan on purpose.
Things I left out on purpose
IAM access keys. The users are in Terraform, their keys are not. An aws_iam_access_key resource stores the secret in state, and I'd rather create keys with the CLI when I need them.
After the import
Once apply finished, I deleted imports.tf. Import blocks are only needed once. Leaving them in is harmless as far as I know, but it's clutter, and a stale ID in there confuses the next person (me, in a year).
Then one more ./tf plan, which should say:
No changes. Your infrastructure matches the configuration.
That line is the whole point. From there, the new server, the backup bucket and everything else got added as plain resources on top.