Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Terraform and AWS CLI Safe Local Workflow: Install to Plan

· 6 min read · Home Tech

In this article
  1. Why a Local Workflow Matters
  2. Prerequisites and Installation
  3. Creating an Isolated IAM User
  4. Configuring AWS Credentials Safely
  5. Initializing a Terraform Project
  6. Running a Safe Plan
  7. Common Pitfalls and How to Avoid Them
  8. Comparing Local Workflow with Managed Alternatives
  9. Frequently Asked Questions

Setting up a safe local Terraform and AWS CLI workflow from installation to plan means using version-pinned tools, isolated credentials, and a read-only plan step before any changes are applied. This article walks through the complete setup so you can experiment with infrastructure-as-code without risking your AWS account.

Why a Local Workflow Matters

Terraform is an infrastructure-as-code tool that lets you define cloud resources in declarative configuration files. The AWS CLI is a command-line tool for interacting with AWS services. Together they allow you to create, modify, and destroy AWS resources reproducibly. A safe local workflow is essential because Terraform can make real changes to your AWS account, and mistakes—like using the wrong credentials or running apply without reviewing a plan—can lead to unexpected costs or resource deletion.

By keeping everything local, you can test configurations, review plans, and validate changes before they hit production. This is especially important for learning, development, and small projects where you want full control and visibility.

Prerequisites and Installation

Before starting, you need a computer with internet access and an AWS account. You also need permission to create IAM users and policies, since we'll use a dedicated IAM user for Terraform.

  1. Install Terraform: Download the appropriate binary for your operating system from the official Terraform website, or use a package manager like Homebrew (macOS), Chocolatey (Windows), or apt (Ubuntu). Verify the installation with terraform version.
  2. Install AWS CLI: Install the AWS CLI version 2 using the official installer or your package manager. Verify with aws --version.
  3. Install Git (optional but recommended): Git helps you version your Terraform configurations. Most systems have it pre-installed or available via package manager.

Keep both tools updated, but pin versions in your project files (see below) to ensure reproducibility.

Creating an Isolated IAM User

Never use your root AWS account credentials for Terraform. Instead, create a dedicated IAM user with least-privilege permissions. This limits the blast radius if credentials are leaked.

  1. Log into the AWS Management Console as root.
  2. Go to IAM > Users > Add user. Choose a name like terraform-local.
  3. Select "Access key - Programmatic access" as the access type.
  4. Attach a policy. For learning, you can attach AdministratorAccess, but for production you should create a custom policy that only allows the actions Terraform needs for your specific resources.
  5. After creation, you'll get an access key ID and secret access key. Download the CSV file and store it securely (e.g., in a password manager). Never share it.

For the safest local workflow, also enable MFA on this IAM user if you plan to use it for anything beyond local experimentation.

Configuring AWS Credentials Safely

The AWS CLI looks for credentials in several places. The recommended method is to use the aws configure command, which stores credentials in ~/.aws/credentials (on Linux/macOS) or %USERPROFILE%\.aws\credentials (on Windows).

  • Run aws configure and enter your access key ID, secret access key, region (e.g., us-east-1), and output format (e.g., json).
  • Set the region to avoid specifying it in every command.
  • For multiple profiles, use aws configure --profile terraform-local and then reference the profile with --profile terraform-local or set the AWS_PROFILE environment variable.

Never hardcode credentials in Terraform files or commit them to Git. Use environment variables or the shared credentials file.

Verify your credentials with aws sts get-caller-identity to confirm you're using the intended IAM user.

Initializing a Terraform Project

Create a directory for your Terraform configuration. Inside, create a file named main.tf with a basic configuration, for example:

provider "aws" {
  region = "us-east-1"
}

resource "aws_s3_bucket" "example" {
  bucket = "my-unique-bucket-name-12345"
}

Then run terraform init. This downloads the AWS provider plugin and sets up the backend. By default, Terraform uses the local backend, storing state in a file named terraform.tfstate. This is fine for local experimentation, but for team use you should configure a remote backend (like S3 with DynamoDB locking) later.

Running a Safe Plan

The terraform plan command creates an execution plan without making any changes. It shows what actions Terraform would take (create, modify, destroy). This is your safety net.

  1. Run terraform plan in your project directory.
  2. Review the output carefully. Check that the resources and actions match your expectations.
  3. If the plan looks correct, you can apply it with terraform apply. But for a truly safe workflow, you should also use terraform plan -out=plan.out to save the plan, then review it, and finally apply that exact plan with terraform apply plan.out. This ensures no drift between plan and apply.

Always run plan before apply—even for small changes. This catches typos, missing dependencies, and unintended deletions.

Common Pitfalls and How to Avoid Them

  • Using the wrong region: Always specify the region in the provider block or via environment variable to avoid creating resources in unexpected locations.
  • Accidentally using root credentials: Check aws sts get-caller-identity to confirm you're using the dedicated IAM user.
  • State file exposure: The terraform.tfstate file contains sensitive information. Add it to .gitignore and never commit it.
  • Running apply without plan: Always plan first. The plan is your preview.
  • Not pinning provider versions: Specify a version constraint in the provider block, e.g., version = "~> 5.0", to avoid unexpected upgrades.

If you make a mistake, you can use terraform destroy to remove all resources defined in your configuration—but only after reviewing the plan for that command.

Comparing Local Workflow with Managed Alternatives

Many teams use Terraform Cloud or Terraform Enterprise for remote state management, policy controls, and collaboration. These services offer additional safety features like run approvals and audit logs. However, for individual learning or small projects, a local workflow is simpler and free. The choice depends on your needs: if you need team collaboration or robust state locking, consider a remote backend. If you're just experimenting, local is fine.

Frequently Asked Questions

How do I check which AWS credentials Terraform is using?

Run aws sts get-caller-identity to see the IAM user or role. Terraform uses the same credential chain as the AWS CLI: environment variables, shared credentials file, and then IAM roles (in that order). You can also set AWS_PROFILE to force a specific profile.

What is the difference between terraform plan and terraform apply?

plan shows what changes Terraform will make without actually making them. apply executes the changes. Always run plan first to review the proposed changes, and use plan -out to save the plan for a consistent apply.

Can I use environment variables for AWS credentials instead of aws configure?

Yes. Set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY in your shell. This is useful for CI/CD pipelines. However, be careful not to expose them in logs or commit them to version control.

How do I prevent Terraform from destroying resources unintentionally?

Review the plan output carefully. Look for any destroy actions. You can also use the prevent_destroy lifecycle argument on critical resources, but this is a last resort. The best practice is to always review the plan and use a separate state file for each environment (dev, prod).

What should I do if my state file is corrupted or lost?

If you lose the local state file, Terraform will not know about existing resources, so it may try to create duplicates. To recover, you can import existing resources using terraform import or manually recreate the state. For critical infrastructure, always use a remote backend with versioning.

Is it safe to run terraform apply without a plan?

It's not recommended. Terraform will generate a plan and ask for confirmation, but you might miss important details. Always run plan separately and review it, especially when you're learning or working with production resources.