0
votes

Is it possible to query the Terraform state reliably to identify the most recently created resource of a given type?

In my use case, I have a CI/CD pipeline deploying resources via "terraform apply", I would like to identify the last resource created (virtual machine) and kick off another pipeline to carry out non-terraform configuration on that virtual server.

1
It's not particularly clear what you're trying to do here. Can you provide a minimal reproducible example of what you've got and then explain exactly what you're trying to achieve and what you've tried that isn't working?ydaetskcoR
@ ydaetskcoR - I hope my answer below clarifies the original question.Traiano Welcome

1 Answers

0
votes

The objective behind my original question is to be able to identify a most recently created resource (virtual machine) from a Terraform run and carry out a second phase of configuration on that resource (e.g run an application installation or operating system hardening process).

The solution is not to think of this procedurally but instead handle it with a tool that works idempotently (e.g Ansible) that runs across all resources created after a terraform run and is smart enough to determine if anything should be done.

So: Simply process terraform outputs and pass it to a next stage in the pipeline as inventory to Ansible/Puppet/etc