Managing Existing and New EC2 Instances with Terraform

Managing Existing and New EC2 Instances with Terraform

In this blog post, we'll explore how to use terraform import to manage EC2 instances created both through Terraform and manually via the AWS console. This is a common scenario where you want to bring manually created resources under Terraform's control.

Scenario

  1. Initial Setup: An EC2 instance created using a Terraform script.

  2. Manual Addition: Another EC2 instance created manually through the AWS console with different configurations.

  3. Objective: Manage both EC2 instances using Terraform.

Step-by-Step Guide

1. Prerequisites

  • Terraform installed on your machine.

  • AWS CLI configured with appropriate credentials.

  • Existing Terraform configuration for the first EC2 instance.

2. Existing Terraform Configuration

Assume you have the following Terraform configuration for your first EC2 instance (main.tf):

provider "aws" {
  region = "us-west-2"
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  tags = {
    Name = "ExampleInstance"
  }
}

3. Create a Second EC2 Instance Manually

Using the AWS Management Console, launch a new EC2 instance with different configurations. Note down the instance ID (e.g., i-0123456789abcdef0).

4. Update Terraform Configuration

Modify your main.tf to include the second EC2 instance:

provider "aws" {
  region = "us-west-2"
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  tags = {
    Name = "ExampleInstance"
  }
}

resource "aws_instance" "manual" {
  ami           = "ami-0c55b159cbfafe1f0"  # Adjust according to your instance
  instance_type = "t2.medium"              # Adjust according to your instance

  tags = {
    Name = "ManualInstance"
  }
}

5. Initialize Terraform

Run terraform init to initialize your Terraform configuration.

terraform init

6. Import the Manually Created Instance

Use the terraform import command to import the manually created instance into the state file.

terraform import aws_instance.manual i-0123456789abcdef0

7. Verify the Import

Run terraform plan to ensure that Terraform recognizes the manually created instance and sees no changes needed for it.

terraform plan

You should see output indicating that no changes are required for the manually imported instance.

8. Manage Both Instances

Now, both instances are under Terraform's management. You can make changes to their configurations in your main.tf file and apply them using terraform apply.

For example, to change the instance type of the manually created instance:

resource "aws_instance" "manual" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.small"  # Changed instance type

  tags = {
    Name = "ManualInstance"
  }
}

Apply the changes:

terraform apply

Conclusion

By using terraform import, you can seamlessly manage resources created outside of Terraform. This approach helps in maintaining a consistent and automated infrastructure management process.

This step-by-step guide should help you manage both existing and new EC2 instances using Terraform effectively. If you have any questions or run into issues, feel free to leave a comment below!