Deploy Your First Cloud Server with Terraform
Use Terraform to provision and destroy a real EC2 server on AWS.
Introduction
30 Second Summary
Every time a company needs a new server, someone has to set it up. Professional cloud engineers skip the manual clicking and write code to describe their infrastructure instead.
In this project, you will use Terraform to write a configuration file that deploys a real cloud server on AWS. You'll bring it to life with a single command and tear it down just as easily.
What You'll Build
A Terraform configuration that provisions and manages an EC2 instance on AWS using the Infrastructure as Code workflow.
By the end of this project, you'll have:
- A Terraform project directory with configuration files that describe your cloud server.
- A live EC2 instance running on AWS, created and destroyed entirely from code.
- An outputs file that displays your server's public IP address after deployment.
- Secret Mission: Add variables to make your configuration reusable and professional-grade.
Are there any prerequisites?
No prior cloud or coding experience is required. This project guides you through installing every tool you need, including VS Code, the Terraform CLI, and the AWS CLI.
Install Your Tools
To deploy a cloud server with code, you need three tools working together on your machine.
Visual Studio Code is where you will write your Terraform configuration files. Terraform is the tool that reads those files and creates real infrastructure on AWS. The AWS CLI lets Terraform authenticate with your AWS account.
In this step, get ready to:
- Install Visual Studio Code.
- Install the Terraform CLI.
- Install the AWS CLI.
Install Visual Studio Code
Visual Studio Code (VS Code) is a free code editor where you will write all your Terraform files.
- Check if VS Code is already on your machine by pressing the Windows key to open your search bar.
- Type Visual Studio Code and look at the results.
✔️ I see Visual Studio Code in the results
- Click it to open VS Code.
- You are all set. Skip ahead to the Terraform section below.
ⓧ I don't see Visual Studio Code
- Open your web browser and go to https://code.visualstudio.com.
- Click the large Download for Windows button to download the User Setup installer.
- Run the downloaded .exe file.
- Follow the installer prompts, accepting the default options.
- Once the installer finishes, press the Windows key, type Visual Studio Code, and press Enter to open it.
You should see the VS Code Welcome tab open in front of you.
VS Code won't open?
Try restarting your computer after installation. Some Windows systems need a restart to register newly installed programs.
help me troubleshoot why VS Code won't open after installation on Windows
Install the Terraform CLI
Terraform is a command-line tool that reads your configuration files and turns them into real cloud infrastructure. You will download the 1.15.6 binary and add it to your system PATH so Windows can find it from any terminal.
- Check if Terraform is already on your machine by opening a Command Prompt.
- Press the Windows key, type cmd, and press Enter.
- Run this command to check if Terraform is available:
terraform -help
✔️ I see a list of Terraform commands
Terraform is already installed. You should see a list of available commands and subcommands.
Since this project uses version 1.15.6, confirm your version is up to date by running:
terraform -version
- If you see 1.15.6 or higher, you are all set. Skip ahead to the AWS CLI section below.
- If you see a lower version number, follow the instructions in the next tab to download and install version 1.15.6.
ⓧ I see an older version
You have Terraform installed, but you need version 1.15.6 or higher for this project. Follow the steps below to update it.
- Open your browser and go to https://developer.hashicorp.com/terraform/install.
- Under the Windows section, download the AMD64 zip file.
- Extract the zip file. You will see a single file called terraform.exe.
- Copy terraform.exe and paste it into your existing Terraform folder, replacing the old file.
- Close your current Command Prompt and open a new one.
- Verify the update by running:
terraform -help
You should see the list of commands again, confirming the new binary is working.
Still seeing the old version?
Make sure you replaced the old terraform.exe file with the new one. Also confirm you opened a brand new Command Prompt window after replacing the file.
help me figure out why my Terraform version is not updating after I replaced the binary on Windows
ⓧ 'terraform' is not recognized
Terraform is not installed yet. Follow all of the steps below to download it and set up your PATH.
- Open your browser and go to https://developer.hashicorp.com/terraform/install.
- Under the Windows section, download the AMD64 zip file.
- Extract the zip file. You will see a single file called terraform.exe.
- Create a new folder at C:\terraform.
- Move terraform.exe into C:\terraform.
Now you need to add C:\terraform to your system PATH so Windows can find the terraform command from any terminal.
- Press the Windows key and type Edit the system environment variables.
- Press Enter to open the System Properties window.
- Click Environment Variables near the bottom of the window.
- In the System variables section (the lower half), find the row called Path and select it.
- Click Edit.
- In the Edit environment variable dialog, click New.
- Type C:\terraform and press Enter.
- Click OK on all three open dialogs to save your changes.
What is PATH?
PATH is a system variable that tells Windows where to look for programs. By adding C:\terraform to PATH, you are telling Windows to check that folder whenever you type a command.
Without this step, you would have to type the full path C:\terraform\terraform.exe every time you wanted to run Terraform.
- Close your current Command Prompt window (PATH changes do not apply to already-open terminals).
- Open a new Command Prompt by pressing the Windows key, typing cmd, and pressing Enter.
- Verify Terraform is accessible by running:
terraform -help
You should see a list of available Terraform commands and subcommands.
Still seeing 'terraform' is not recognized?
Double-check that terraform.exe is inside C:\terraform and that you typed C:\terraform exactly when editing PATH. Make sure you opened a brand new Command Prompt after saving the PATH change.
help me troubleshoot why terraform is not recognized in my Command Prompt after I added it to PATH on Windows
Install the AWS CLI
The AWS CLI is a command-line tool that lets you interact with AWS services. Terraform uses the same credentials that the AWS CLI stores on your machine to authenticate with your account.
- Check if the AWS CLI is already installed by running this in your Command Prompt:
aws --version
✔️ I see a version number
The AWS CLI is already installed. You should see output starting with aws-cli/2. followed by system information.
- You are all set. Move on to the next step.
ⓧ 'aws' is not recognized
The AWS CLI is not installed yet. Follow the steps below.
- Open your browser and go to https://awscli.amazonaws.com/AWSCLIV2.msi to download the installer.
- The download starts automatically. Run the downloaded AWSCLIV2.msi file.
- Follow the installer prompts, accepting the default options.
- Once the installer finishes, close your current Command Prompt and open a new one.
- Verify the install by running:
aws --version
You should see output starting with aws-cli/2. followed by system information.
Still seeing 'aws' is not recognized?
The MSI installer adds the AWS CLI to PATH automatically, but it only applies to new terminal windows. Make sure you closed and reopened your Command Prompt after installation.
If it still does not work after reopening, try restarting your computer.
help me troubleshoot why aws --version is not recognized after I installed the AWS CLI on Windows
Your toolchain is ready. Next up, you will create an AWS account and configure your credentials so Terraform can talk to the cloud.
Create Your AWS Account and Configure Credentials
Your tools are ready. Now you need an AWS account so Terraform has somewhere to create your cloud server.
In this step, you will create a free AWS account, set up a dedicated user for Terraform, and connect your local AWS CLI to that account. This connection is what allows Terraform to talk to AWS on your behalf.
In this step, get ready to:
- Create an AWS Free Plan account with $100 in credits.
- Create an IAM user with permissions for Terraform.
- Configure the AWS CLI to authenticate with your new account.
Create an AWS Free Plan account
AWS offers a Free Plan that gives you $100 in credits at signup. This covers everything you will build in this project at zero cost.
- Open your browser and go to https://aws.amazon.com/free/.
- Click Create a Free Account.
- Select the Free account plan during signup.
- Follow the prompts to enter your email address, set a password, and provide your account details.
- Enter a valid payment method when prompted.
Why does AWS need a payment method?
AWS requires a payment method at signup even for the Free Plan. Your card will not be charged while you are on the Free Plan. The account auto-closes after 6 months or when your $100 in credits runs out, whichever comes first.
- Complete account verification (you may receive an SMS or phone call).
- Once signup is complete, sign in to the AWS Management Console.
You should see the AWS Console home page with a search bar at the top and a list of services.
Create an IAM user for Terraform
Terraform needs credentials to make API calls to AWS. Instead of using your root account (the one you just created), you will create a dedicated IAM user with its own access keys.
- In the AWS Console, click the search bar at the top.
- Type IAM and select IAM from the results.
- In the left sidebar, click Users.
- Click Create user.
- For User name, enter a name like terraform-user.
- Click Next.
- On the Set permissions page, select Attach policies directly.
- In the search box, type AdministratorAccess.
- Check the box next to the AdministratorAccess policy.
- Click Next, then click Create user.
Why AdministratorAccess for a learning project?
In production, you would use a least-privilege policy that grants only the specific permissions needed. For a learning project, AdministratorAccess keeps things simple so you can focus on Terraform without troubleshooting permission errors.
You will destroy all resources at the end of this project, and the Free Plan account auto-closes after 6 months.
Now you need to create an access key for this user so Terraform can authenticate.
- Click on your new user's name (e.g. terraform-user) to open their detail page.
- Click the Security credentials tab.
- Scroll down to Access keys and click Create access key.
- Select Command Line Interface (CLI) as the use case.
- Check the confirmation checkbox at the bottom and click Next.
- Click Create access key.
- Click Download .csv file to save your Access Key ID and Secret Access Key.
Save this file somewhere safe
The Secret Access Key is shown only once at this moment. If you lose it, you will need to delete this access key and create a new one. Do not share this file or commit it to version control.
- Click Done to return to the user page.
Configure the AWS CLI with your credentials
The AWS CLI stores your credentials locally so that Terraform can use them to authenticate. The aws configure command walks you through setting this up.
- Open Command Prompt by pressing the Windows key, typing Command Prompt, and pressing Enter.
- Start the configuration wizard by running this command:
aws configure
What will this command ask?
The aws configure command prompts you for four values, one at a time. Have your downloaded .csv file ready to copy-paste the first two values.
- When prompted for AWS Access Key ID, paste your Access Key ID from the .csv file.
- When prompted for AWS Secret Access Key, paste your Secret Access Key from the .csv file.
- When prompted for Default region name, type us-east-1.
- When prompted for Default output format, type json.
Now verify that your credentials are working correctly.
- Confirm your credentials work by running this command:
aws sts get-caller-identity
You should see a JSON response containing your Account ID, User ARN, and User ID. It looks similar to this:
{
"UserId": "AIDASAMPLEUSERID",
"Account": "123456789012",
"Arn": "arn:aws:iam::123456789012:user/terraform-user"
}
What does this output mean?
This confirms your AWS CLI can successfully authenticate with your account. The Account field is your 12-digit AWS account number. The Arn field shows the full path to your IAM user.
Terraform uses the same credentials file that aws configure just created. You will not need to configure Terraform separately.
Seeing an error instead of JSON output?
- If you see a "could not connect" error, check your internet connection and try again.
- If you see "InvalidClientTokenId" or "SignatureDoesNotMatch", open your .csv file and re-run aws configure with the correct Access Key ID and Secret Access Key. Watch for extra spaces when pasting.
- If you see "Unable to locate credentials", make sure you ran aws configure in the same Command Prompt window.
Help me troubleshoot my AWS CLI credentials
Your AWS account is live and your CLI is connected. Next up, you will write the Terraform configuration that describes your cloud server.
Write Your Terraform Configuration
Your tools are installed and your AWS credentials are configured. Now it's time to write the code that tells Terraform exactly what infrastructure to create.
Terraform uses its own configuration language called HCL (HashiCorp Configuration Language). You describe your desired cloud server in .tf files, and Terraform turns those descriptions into real AWS resources.
In this step, get ready to:
- Create your project directory and open it in VS Code.
- Write the main Terraform configuration for an EC2 instance.
- Create an outputs file to display your server's public IP address.
Create your project directory
Every Terraform project lives in its own folder. You'll create a dedicated folder on your Desktop to keep things organized.
- Return to VS Code.
- Open the integrated terminal by pressing Ctrl+` (that's the backtick key, above Tab).
What is the integrated terminal?
VS Code has a built-in terminal panel at the bottom of the window. It works just like a standalone terminal, but you can see your code and run commands side by side.
- Move to your Desktop by running this command:
cd ~/Desktop
- Create your project folder by running this command:
mkdir my-first-server
- Confirm the folder was created by running:
ls
- You should see my-first-server listed in the output.
Now open this folder as your VS Code workspace so all your files stay organized under one project.
- Click File in the top menu bar.
- Click Open Folder.
- Navigate to your Desktop, select the my-first-server folder, and click Select Folder.
VS Code reloads with your project folder as the workspace. You should see MY-FIRST-SERVER in the Explorer sidebar on the left (it appears in uppercase).
Don't see the Explorer sidebar?
Press Ctrl+Shift+E to toggle the Explorer panel open. If the folder name doesn't appear, try File > Open Folder again and make sure you selected the my-first-server folder itself (not the Desktop).
Help me open a folder in VS Code on Windows.
Create the Terraform configuration file
The main configuration file tells Terraform which cloud provider to use and what resources to create. You'll build this file in three parts: provider setup, AMI lookup, and the server resource.
- In the Explorer sidebar, hover over the MY-FIRST-SERVER heading to reveal the action icons.
- Click the New File icon (a page with a plus sign).
- Type main.tf and press Enter.
The file opens in the editor. Start by adding the provider configuration.
- Paste the following code into main.tf:
# Tell Terraform which provider plugin to download
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.92"
}
}
required_version = ">= 1.2"
}
# Configure the AWS provider to deploy in us-east-1
provider "aws" {
region = "us-east-1"
}
What does this code do?
The terraform block declares which provider plugin Terraform needs to download. Here you're specifying the AWS provider from HashiCorp, pinned to version ~> 5.92 (any patch above 5.92 but below 6.0).
The provider block configures that plugin. Setting region = "us-east-1" tells Terraform to create all resources in the US East (N. Virginia) region. Terraform reads your AWS credentials automatically from the aws configure setup you completed earlier.
- Save main.tf with Ctrl+S.
- Confirm main.tf appears in the Explorer sidebar under your project folder.
File saved in the wrong location?
If main.tf doesn't appear in the sidebar, you may have created it outside your workspace. Delete the misplaced file, then right-click inside the Explorer sidebar and select New File to ensure it's created inside my-first-server.
Help me create a file in the right folder in VS Code.
Next, add a data source that finds the latest Ubuntu server image for your EC2 instance.
- Place your cursor at the end of main.tf (after the closing } of the provider block) and add a blank line.
- Paste the following code below the provider block:
# Look up the latest Ubuntu 24.04 AMI from Canonical
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd-gp3/ubuntu-noble-24.04-amd64-server-*"]
}
owners = ["099720109477"]
}
What is an AMI data source?
An AMI (Amazon Machine Image) is a template that contains the operating system for your server. Instead of hard-coding an AMI ID (which changes over time and across regions), this data source queries AWS for the latest Ubuntu 24.04 image every time you run Terraform.
The owners field ensures you only get official images published by Canonical (Ubuntu's publisher). The filter pattern uses a wildcard (*) to match any release date.
- You won't see a visible change yet. This data source provides the AMI ID that your server resource will use in the next chunk.
Finally, add the resource block that defines your actual EC2 instance.
- Place your cursor at the end of main.tf (after the closing } of the data block) and add a blank line.
- Paste the following code below the data source block:
# Create an EC2 instance using the Ubuntu AMI
resource "aws_instance" "app_server" {
ami = data.aws_ami.ubuntu.id
instance_type = "t2.micro"
tags = {
Name = "my-first-server"
}
}
What does this resource do?
This is the core of your configuration. The resource block tells Terraform to create a real EC2 instance on AWS.
The ami argument references the Ubuntu image your data source finds. The instance_type sets the server size to t2.micro (free tier eligible). The Name tag gives your instance a friendly label in the AWS Console.
- Save main.tf with Ctrl+S.
- You won't see infrastructure changes yet. You'll run terraform plan in the next step to verify everything works.
Seeing red squiggly lines in main.tf?
VS Code may not recognize .tf files by default. Red underlines are from a language extension, not a real error. Your code is fine as long as you copied it exactly.
Double-check that every opening { has a matching closing }. Also confirm that string values are wrapped in double quotes ("), not single quotes.
Help me fix syntax issues in my Terraform main.tf file.
✔️ Awesome, I've got everything!
Great. Double check you've saved main.tf and move on to creating your outputs file.
ⓧ I'd like to double check the full code
Here is the complete main.tf file. Compare it against your code character by character.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.92"
}
}
required_version = ">= 1.2"
}
provider "aws" {
region = "us-east-1"
}
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd-gp3/ubuntu-noble-24.04-amd64-server-*"]
}
owners = ["099720109477"]
}
resource "aws_instance" "app_server" {
ami = data.aws_ami.ubuntu.id
instance_type = "t2.micro"
tags = {
Name = "my-first-server"
}
}
Create an outputs file
Outputs let Terraform display useful information after it finishes creating resources. You'll create a separate file that tells Terraform to print your server's public IP address and instance ID.
- In the Explorer sidebar, hover over MY-FIRST-SERVER and click the New File icon.
- Type outputs.tf and press Enter.
- Paste the following code into outputs.tf:
# Display the server's public IP after deployment
output "instance_public_ip" {
description = "The public IP address of the EC2 instance"
value = aws_instance.app_server.public_ip
}
# Display the server's unique instance ID
output "instance_id" {
description = "The ID of the EC2 instance"
value = aws_instance.app_server.id
}
What do these outputs do?
After Terraform creates your server, it reads these output blocks and prints the values to your terminal. The instance_public_ip output gives you the IP address you could use to connect to the server.
The value field references the resource you defined in main.tf using Terraform's resource_type.resource_name.attribute syntax.
- Save outputs.tf with Ctrl+S.
- In the Explorer sidebar, confirm you now see both main.tf and outputs.tf listed under your project folder.
Only seeing one file in the sidebar?
Make sure you created outputs.tf inside the my-first-server workspace (not at a different level). If the file ended up elsewhere, delete it and use the New File icon directly on the folder name in the Explorer sidebar.
Help me find where my outputs.tf file was saved.
✔️ Awesome, I've got everything!
Great. Double check you've saved outputs.tf and you're ready to deploy.
ⓧ I'd like to double check the full code
Here is the complete outputs.tf file. Compare it against your code character by character.
output "instance_public_ip" {
description = "The public IP address of the EC2 instance"
value = aws_instance.app_server.public_ip
}
output "instance_id" {
description = "The ID of the EC2 instance"
value = aws_instance.app_server.id
}
Your Terraform configuration is complete. Next up, you'll initialize the workspace and deploy your server to AWS with a single command.
Deploy Your Server
Your configuration files are ready. Now it's time to run the Terraform commands that turn your code into a real server on AWS.
Terraform follows a three-command workflow: init downloads the provider plugin, plan previews what will happen, and apply executes the changes. This gives you a chance to review everything before anything touches your cloud account.
In this step, get ready to:
- Initialize the Terraform workspace.
- Preview the execution plan.
- Apply the configuration to create your EC2 instance.
Initialize the Terraform workspace
Before Terraform can create any resources, it needs to download the AWS provider plugin specified in your main.tf file. The terraform init command handles this for you.
- Open the VS Code integrated terminal by pressing Cmd+` (macOS) or Ctrl+` (Windows).
- Download the AWS provider plugin by running this command:
terraform init
What does this command do?
Terraform reads the required_providers block in your main.tf file and downloads the hashicorp/aws provider plugin matching version ~> 5.92.
It creates a .terraform directory to store the plugin and a .terraform.lock.hcl file to lock the exact version. This ensures everyone on a team uses the same provider version.
- Confirm you see the message Terraform has been successfully initialized! in the terminal output.
Don't see the success message?
- Make sure your terminal is open inside the my-first-server folder. Run cd my-first-server if the prompt shows a different directory.
- If you see a "provider not found" error, check that your main.tf file has the correct required_providers block with source = "hashicorp/aws".
- If you see a network error, check your internet connection. Terraform needs to download the plugin from the HashiCorp registry.
Still stuck? Help me troubleshoot terraform init failing
Preview the execution plan
Before creating any real resources, Terraform can show you exactly what it intends to do. This is your safety check.
- Preview what Terraform will create by running this command:
terraform plan
What does the plan output show?
Terraform compares your configuration files against the current state (which is empty, since you haven't created anything yet). It then prints every resource it will create, update, or delete.
The plan includes details like the AMI ID Terraform found, the instance type (t2.micro), and the Name tag (my-first-server). This lets you catch mistakes before they cost time or money.
- Confirm the last line of the output shows Plan: 1 to add, 0 to change, 0 to destroy.
Seeing an error instead of a plan?
- If you see a credentials error, run aws sts get-caller-identity to confirm your AWS CLI is configured correctly.
- If you see a syntax error, open main.tf and check for missing braces, quotation marks, or typos.
- If the plan shows more than 1 resource to add, make sure your main.tf only contains one resource block.
Still stuck? Help me troubleshoot terraform plan errors
Apply the configuration
The plan looks good. Time to create a real EC2 instance on AWS.
- Deploy your EC2 instance by running this command:
terraform apply
What happens when you run apply?
Terraform prints the same execution plan you just saw and asks you to confirm by typing yes. Only after you confirm does it make API calls to AWS to create the instance.
This two-step confirmation exists so you never accidentally create or destroy resources.
- Type yes and press Enter when Terraform asks Do you want to perform these actions?
- Wait about 30-60 seconds while the instance is created.
Once complete, Terraform prints your outputs. Look for the instance_public_ip value. This is your server's public IP address on the internet.
- Inspect the full state of your deployed infrastructure by running:
terraform show
What does terraform show tell you?
This command prints everything Terraform knows about the resources it manages. You can see the instance ID, the AMI that was used, the availability zone, the public IP, and all the tags you defined.
Terraform stores this information in a terraform.tfstate file in your project directory. This state file is how Terraform tracks what exists in the real world and maps it back to your code.
- Confirm the output shows your instance with instance_type = "t2.micro" and a Name tag of my-first-server.
Seeing an error during apply?
- If you see an authorization error, your IAM user may not have the AdministratorAccess policy attached. Go back to the IAM console and verify the policy.
- If you see a timeout, your network may be blocking outbound HTTPS. Try a different network or check firewall settings.
- If you accidentally typed no at the prompt, just run terraform apply again. No resources were created.
Still stuck? Help me troubleshoot terraform apply failing
Your server is live. In the next step, you'll verify it exists in the AWS Console and then tear it down cleanly with a single command.
Inspect and Destroy Your Infrastructure
In the previous step, you deployed a real EC2 server to AWS using a single terminal command. Your infrastructure is live in the cloud right now.
In this step, you will confirm that your server actually exists by viewing it in the AWS Console. Then you will tear it down cleanly using Terraform, completing the full Infrastructure as Code lifecycle.
In this step, get ready to:
- Verify your EC2 instance is running in the AWS Console.
- Destroy your infrastructure with Terraform.
- Review the complete IaC workflow you performed.
Verify your server exists in the AWS Console
You have been running commands in the terminal, but how do you know the server actually exists? The AWS Console gives you a visual confirmation of everything Terraform created.
- Return to the AWS Management Console in your browser.
- In the top search bar, type EC2 and click EC2 from the results.
- In the left navigation pane, click Instances.
- Confirm you see your instance with an Instance state of Running.
- Check the Name column. It should display my-first-server, matching the tag you defined in main.tf.
Why check the console?
The AWS Console is the visual interface for everything running in your AWS account. Seeing your instance here proves that Terraform made real API calls to AWS and created real infrastructure from your code.
The Name tag appearing as my-first-server confirms a direct link between what you wrote in main.tf and what exists in the cloud.
Destroy the infrastructure
One of the biggest advantages of Infrastructure as Code is that tearing down infrastructure is just as easy as creating it. A single command removes everything Terraform manages.
- Switch back to VS Code.
- In the integrated terminal, destroy your infrastructure by running this command:
terraform destroy
What happens when you run this?
Terraform reads your state file to determine which resources it manages. It displays a destroy plan showing what will be removed, then asks for confirmation before proceeding.
You will see a prompt that says: "Do you really want to destroy all resources?" along with "Only 'yes' will be accepted to confirm." This is a safety measure because destruction is irreversible.
- Review the plan output. You should see Plan: 0 to add, 0 to change, 1 to destroy.
- Type yes and press Enter to confirm.
- Wait for the instance to be terminated. This takes about 30 to 60 seconds.
You should see progress messages as Terraform destroys the instance, ending with Destroy complete! Resources: 1 destroyed. in your terminal.
Seeing an error instead of the destroy plan?
If you see a credentials error, your AWS CLI session may have expired. Run aws sts get-caller-identity to check. If that fails, re-run aws configure with your credentials.
If you see a state file error, make sure your terminal is in the my-first-server directory where terraform.tfstate lives.
Help me troubleshoot my terraform destroy error.
- Return to the AWS Console EC2 Instances page in your browser.
- Click the refresh icon at the top of the instances list.
- Confirm your instance now shows Terminated in the Instance state column.
Why does the terminated instance still appear?
After termination, an EC2 instance remains visible in the console briefly with a state of terminated. AWS eventually removes it from the list automatically. You are not charged for instances in a terminated state.
Review the full IaC workflow
You have just completed the entire Infrastructure as Code lifecycle that professional platform engineers use every day.
What did you just accomplish?
- You wrote code in main.tf that described infrastructure declaratively.
- Terraform translated that code into API calls to AWS, creating real cloud resources.
- You can recreate the exact same infrastructure anytime by running terraform apply again. The configuration file is your single source of truth.
- You destroyed everything cleanly with terraform destroy. No clicking through console pages to find and delete resources manually.
Why this workflow matters
Manual infrastructure (clicking through the AWS Console to create servers) does not scale. If you need 50 servers, or need to recreate your environment after a disaster, clicking is slow and error-prone.
With Terraform, your infrastructure is versioned, repeatable, and auditable. This is the foundation of every cloud platform engineering role.
You have proven you can manage AWS infrastructure programmatically. Up next, the Secret Mission challenges you to make your configuration reusable with variables.
Secret mission
Add Variables for a Reusable Configuration
Your configuration works, but the server name and instance type are hardcoded. What if you needed to deploy the same setup with different names for dev, staging, and production? Refactor your Terraform config with input variables to make it reusable across environments.
Clean Up Your Resources
Clean Up Your Resources
This project runs on the AWS Free Plan, so no charges are incurred unless you upgrade to a Paid plan. Your EC2 instance was already terminated via terraform destroy in an earlier step.
Decide whether to keep your project files and AWS setup for future Terraform experiments, or remove everything for a clean slate.
Resources you used:
- EC2 instance (t2.micro) in us-east-1 (already terminated).
- Local project folder (my-first-server).
- IAM user with access keys (in AWS Console).
- AWS CLI credentials (stored in %UserProfile%\.aws).
Keep everything running
No action needed. Choose this if you plan to keep experimenting with Terraform and AWS.
- Your project files stay on disk, ready for you to run terraform apply again anytime.
- Your AWS credentials remain configured so you can provision new infrastructure without re-running aws configure.
- VS Code, Terraform CLI, and AWS CLI stay installed on your machine.
Pause - I'll come back to this later
Your EC2 instance is already terminated from the earlier step, so there is nothing consuming credits right now.
- There is no pause option for EC2. You either run the instance or terminate it.
- Since you already ran terraform destroy, no resources are active.
- Your project files and credentials remain intact. You can re-deploy anytime with terraform apply.
Delete - I don't want to use this again
Remove all project resources and start fresh. Follow each step below to clean everything up.
- Delete your local project folder by opening Command Prompt and running:
rmdir /s /q "%UserProfile%\Desktop\my-first-server"
What if my folder is in a different location?
If you created your my-first-server folder somewhere other than your Desktop, replace the path above with the actual location of your folder.
- Delete your IAM user from the AWS Console:
- Sign in to the AWS Management Console and open the IAM console.
- In the left navigation pane, click Users.
- Select the checkbox next to the IAM user you created for Terraform.
- Click Delete at the top of the page.
- Enter the username in the confirmation dialog and click Delete.
- Remove your local AWS CLI credentials by running:
rmdir /s /q "%UserProfile%\.aws"
What does this remove?
The .aws folder stores your Access Key ID, Secret Access Key, region, and output format. Deleting it removes all locally stored AWS credentials.
- Optionally uninstall the AWS CLI by opening Programs and Features in Control Panel, selecting AWS Command Line Interface, and clicking Uninstall.
- Optionally remove Terraform by deleting the terraform.exe file from the folder you placed it in (e.g., C:\terraform) and removing that folder from your system PATH.
That's a Wrap!
That's a Wrap!
Nice work! You just deployed a real cloud server on AWS using nothing but code.
You've learned how to:
- Write a Terraform configuration in HCL that provisions a real EC2 instance on AWS.
- Deploy and destroy cloud infrastructure using the professional plan/apply/destroy workflow.
- Set up and configure a cloud engineering toolchain including VS Code, Terraform CLI, and AWS CLI.
- Secret Mission: Refactored your configuration with input variables to make it reusable across environments.
Ready to quiz yourself?