Schedule destroy infra after 1hr of provioning

what code i can add in terraform files which automatically destroy the resource after 1 hour or schedule time?

Hi @githubvindude5,

Terraform is a one-shot command line tool so it doesn’t have in its scope any features for taking scheduled actions or actions in response to other events.

That means that there are perhaps two different alternative directions for you to consider to meet that requirement:

  • If the remote system you are working with has its own mechanism for taking timed actions then you could potentially use Terraform to configure that mechanism and then let the remote system be the one to actually take the action. For example, in AWS there’s the general solution of deploying an AWS Lambda function containing code to destroy the relevant objects and then configuring CloudWatch Events to run that function.
  • Alternatively, you could choose to solve this by running Terraform itself in some sort of automation system which supports scheduled operations. In that case it would be the automation you choose or build that would be responsible for detecting the elapsed time and then running Terraform; from Terraform’s perspective this would just be the same as if you manually ran terraform destroy at the appropriate time yourself.
1 Like