Blog

How to run Terraform with a specific resource only?

TERRAFORM-target-CLOUDISHSOFT-COM
Terraform

How to run Terraform with a specific resource only?

The Terraform script can build the infrastrcture in any Cloud platform, this post will explain you how to execute specific resource in AWS cloud, that would be created an EC2 instance.

To run Terraform for a specific resource, you can use the -target option followed by the name of the resource you want to apply changes to.

For example, let’s say you have the following Terraform configuration:

resource "aws_instance" "example" {
  ami                = "ami-0c12c59vabcf1ac30"
  instance_type      = "t3a.medium"
  security_groups    = [aws_security_group.myteam-web.id]
  subnet_id          = "${var.private-subnets}"
}

resource "aws_vpc" "my_vpc" {
  cidr_block = "10.110.0.0/16"

  tags = {
    Name = "tag_example"
  }
}

We have two resources in the Terraform script, here will define one specific resource by the terraform script using
-target option followed by the name of the resource you want to apply changes to.

To apply changes only to the aws_instance.example resource, you can run the following command:

$ terraform init

$ erraform apply -target=aws_instance.example

This will apply changes only to the specified resource and not to any other resources defined in the Terraform configuration.

Keep in mind that using -target can cause problems if the resource you are targeting has dependencies on other resources that haven’t been created yet or have been modified since the last Terraform apply.

Spread the love

Leave your thought here

Your email address will not be published. Required fields are marked *