phillbaker / terraform-provider-elasticsearch

An elasticsearch provider for terraform
https://registry.terraform.io/providers/phillbaker/elasticsearch
Mozilla Public License 2.0
306 stars 134 forks source link

Read Datastream upon `resource_already_exists_exception` #341

Open applike-ss opened 1 year ago

applike-ss commented 1 year ago

Hello,

i would like to suggest an improvement in the handling of the elasticsearch_data_stream resource. Currently when you try to do a terraform apply and the data_stream already exists, there's an error thrown:

Error: elastic: Error 400 (Bad Request): data_stream [my-data-stream-name] already exists [type=resource_already_exists_exception]

I think it would be desirable to try to read it into the state in this case and only error out (with the original error) if that fails too. If i got it right, then the only thing that needs to be adjusted is to move some code around in resourceElasticsearchDataStreamCreate (https://github.com/phillbaker/terraform-provider-elasticsearch/blob/d18af650f0153647f363bafa304625c7495e3240/es/resource_elasticsearch_data_stream.go#L38)

Our point here is that we want to create the data_stream as part of our monitoring module which runs after we deployed a AWS ECS application. This ECS application does already emit logs to our log router and it gets into a steady state after the first logs get flushed by the log router. So the creation can basically never succeed for us right now. However we want the data_stream to be removed when the application is being removed, which is why we want to manage it in the terraform state.

best, Sebastian Struß

phillbaker commented 1 year ago

Hi @applike-ss, thanks for opening an issue. Unfortunately, I think this would generally break expectations of how terraform is supposed to work, for example, this could result in unintentionally overriding an existing data stream if the name collides.

Have you considered importing the resource into terraform?

applike-ss commented 1 year ago

Hi @phillbaker, yes i considered it. However it is not desirable in an automatically created environment to manually import things into a state. Right now it seems we can only create the datastream before making use of our module that creates all other monitoring related resources or we have to split our module in 2 parts. One part would run before and another one would run after the application was spawned.

Anyhow, i have seen somewhere in the terraform-provider-aws that upon resource creation it was imported when it did already exist. Not sure which one, but might be due to API limitations on aws side (like SNS topics, which can not be fetched by name).

I see that you don't want to break how terraform has worked for you until now and it makes sense to be able to detect when a resource i want to create did exist before (just not in the state). So feel free to close this one if you agree.