From 75f05f80879297c4bb0e7650501678288ffd280f Mon Sep 17 00:00:00 2001 From: Charles Ferguson Date: Sun, 13 Aug 2017 20:37:12 +0100 Subject: [PATCH] Apply tags to Elastic Search domain resources after creation. The Elastic Search Domain resource takes about 10 minutes to create. During this time, many things may go wrong to break the behaviour of the Terraform system - network drops, token expiry, system crashes, accidentally closing the wrong terminal, etc... In such cases, the resource will have been created but not be tagged. So, as well as Terraform losing track of it, the tracking tags would not give an indication of why it existed in the first place. The current code creates the domain and then waits until it has completed construction before setting the tags. It also seems to use the partial completion interface, which seems redundant as it immediately completes the operation. In any case, moving the tag setting operations before the wait means that the tags are present as early as possible, allowing the resource to be identified. And that tiny amount of time is absorbed into the long domain creation, making things marginally faster. --- aws/resource_aws_elasticsearch_domain.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/aws/resource_aws_elasticsearch_domain.go b/aws/resource_aws_elasticsearch_domain.go index bee71bc9359..68ebbb33558 100644 --- a/aws/resource_aws_elasticsearch_domain.go +++ b/aws/resource_aws_elasticsearch_domain.go @@ -245,6 +245,19 @@ func resourceAwsElasticSearchDomainCreate(d *schema.ResourceData, meta interface d.SetId(*out.DomainStatus.ARN) + // Whilst the domain is being created, we can initialise the tags. + // This should mean that if the creation fails (eg because your token expired + // whilst the operation is being performed), we still get the required tags on + // the resources. + tags := tagsFromMapElasticsearchService(d.Get("tags").(map[string]interface{})) + + if err := setTagsElasticsearchService(conn, d, *out.DomainStatus.ARN); err != nil { + return err + } + + d.Set("tags", tagsToMapElasticsearchService(tags)) + d.SetPartial("tags") + log.Printf("[DEBUG] Waiting for ElasticSearch domain %q to be created", d.Id()) err = resource.Retry(60*time.Minute, func() *resource.RetryError { out, err := conn.DescribeElasticsearchDomain(&elasticsearch.DescribeElasticsearchDomainInput{ @@ -264,15 +277,6 @@ func resourceAwsElasticSearchDomainCreate(d *schema.ResourceData, meta interface if err != nil { return err } - - tags := tagsFromMapElasticsearchService(d.Get("tags").(map[string]interface{})) - - if err := setTagsElasticsearchService(conn, d, *out.DomainStatus.ARN); err != nil { - return err - } - - d.Set("tags", tagsToMapElasticsearchService(tags)) - d.SetPartial("tags") d.Partial(false) log.Printf("[DEBUG] ElasticSearch domain %q created", d.Id())