terraform: Apply changes to a specific resource

When updating resources using terraform we might notice that infraestructure might have drifted for multiple reason: from developers creating or updating infrastructure through the web console without telling anyone, to uncontrolled updates on the cloud provider side.

If we really need to apply a change but there are other changes that need reviewing, we can tell terraform to update just a specific resource.

To do so we just need to add the -target option to the terraform plan to get the list of changes, excluding other resources. On the plan we will get a warning as follows:

Plan: 3 to add, 0 to change, 0 to destroy.

Warning: Resource targeting is in effect

You are creating a plan with the -target option, which means that the result
of this plan may not represent all of the changes requested by the current
configuration.

The -target option is not for routine use, and is provided only for
exceptional situations such as recovering from errors or mistakes, or when
Terraform specifically suggests to use it as part of an error message.

We can also apply these changes using terraform apply:

$ terraform apply -target=module.ampa

We can get even more specific than that:

$ terraform apply -target=module.ampa.aws_autoscaling_group.ampa_push

Posted on 12/02/2021

Categories