Assistance with deploying EC2 instance via terraform

Viewed 22

I am new to terraform and I am just working in a lab environment at the moment so I hope someone can help me and point me in the right direction as to where I am going wrong. I am following this video for reference https://www.youtube.com/watch?v=SLB_c_ayRMo&t=2336s. I am running terraform V1.2.9 I run a terraform init command and everything initialises, but when I run a terraform plan I get this error "No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed."

This is my code for reference

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.16"
    }
  }

  required_version = ">= 1.2.0"
}

provider "aws" {
  region =     "us-east-1"
  access_key = "access key"
  secret_key = "secretkey"
}

resource "aws_instance" "my-first-server" {
  ami             = "ami-052efd3df9dad4825"
  instance_type   = "t2.micro"
  
tags = {
  Name = "MyFirstServer"
} 

}

Any help would be gratefully appreciated to help me on my learning journey.

1 Answers

Terraform creates resources (for example, in AWS) and tracks them in a state file. The state file is one large object - nothing complex.

If you look in AWS - EC2 region us-east-1 see if your instance is there. If it isn't you should see the terraform plan saying it was deleted outside of Terraform.

One way to check if it's even being tracked properly is to do a terraform state list. That command should show you aws_instance_my-first-server if you've plan & applied this configuration in the past.

If you're still stuck one way to "start from fresh" since it's only a learning exercise and not production level at work, you can delete the entire state file and start fresh.

However I trust Terraform... you must have the instance running somewhere ;)

Related