Azurerm_shared_image usage within packer pipeline

I got a cicd pipeline where one step is to build an azure image based on packer within a development related subscription. afterwards this image is used within kitchen terraform to run certain tests and ensure its functionality.

after these tests succeed I want to add this image(s) to an azure shared image gallery. This works with data sources within terraform code when the azurerm_shared_image resource already exists and a new azurerm_shared_image_version can be created.
if the azurerm_shared_image does not exist, the terraform code breaks and the pipeline must create it within terraform by azurerm_shared_image resources. But how can this be achieved within one terraform run? If the resource of azurerm_shared_image for this certain image is not available, create it.

And this process must be templated because the cicd pipeline is used as template and not every user of this pipeline template must be in the knowledge how this resources must be created - this has to be done by the pipeline itself!

I do not want to run 2 terraform codes, where I am forced to interpret status/output information if a certain azurerm_shared_image is available so an azurerm_shared_image_version can be created