Scheduled destroy and apply of the same resource in Azure

Hi, my goal is to schedule a Cron Job using GitHub Actions to destroy and reapply the same resource at specific times — for example, apply at 8 a.m. and destroy at 5 p.m.

Based on my research, I would run the command terraform destroy -target=xyz.resource_block_name.

Now, if I want to redeploy the exact same resource, would I run terraform apply -target=xyz.resource_block_name? Will this command apply only the Terraform code for that specific resource block (which configures the bastion host) and nothing else?

Also, will this create a new resource ID in Azure?

The Terraform code remains in the repository with the same resource block configuration — the only difference is that terraform destroy removed that resource from the state file.

Thank you.

Hi @xyz99,

Using -target will also include any dependencies, so if there are resources which depend on xyz, they would also need to be destroyed. Unless you need to take some action in between the destroy an create operations, it sounds like the -replace flag would be more suitable for your use case.