diff --git a/aws/resource_aws_elasticsearch_domain.go b/aws/resource_aws_elasticsearch_domain.go index 34aee1a0fe5..8e5ab7e39a2 100644 --- a/aws/resource_aws_elasticsearch_domain.go +++ b/aws/resource_aws_elasticsearch_domain.go @@ -169,6 +169,38 @@ func resourceAwsElasticSearchDomain() *schema.Resource { }, }, }, + "log_publishing_options": { + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "log_type": { + Type: schema.TypeString, + Required: true, + ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { + value := v.(string) + validLogTypes := []string{"INDEX_SLOW_LOGS", "SEARCH_SLOW_LOGS"} + for _, str := range validLogTypes { + if value == str { + return + } + } + errors = append(errors, fmt.Errorf("expected %s to be one of %v, got %s", k, validLogTypes, value)) + return + }, + }, + "cloudwatch_log_group_arn": { + Type: schema.TypeString, + Required: true, + }, + "enabled": { + Type: schema.TypeBool, + Optional: true, + Default: true, + }, + }, + }, + }, "elasticsearch_version": { Type: schema.TypeString, Optional: true, @@ -308,6 +340,18 @@ func resourceAwsElasticSearchDomainCreate(d *schema.ResourceData, meta interface input.VPCOptions = expandESVPCOptions(s) } + if v, ok := d.GetOk("log_publishing_options"); ok { + input.LogPublishingOptions = make(map[string]*elasticsearch.LogPublishingOption) + options := v.(*schema.Set).List() + for _, vv := range options { + lo := vv.(map[string]interface{}) + input.LogPublishingOptions[lo["log_type"].(string)] = &elasticsearch.LogPublishingOption{ + CloudWatchLogsLogGroupArn: aws.String(lo["cloudwatch_log_group_arn"].(string)), + Enabled: aws.Bool(lo["enabled"].(bool)), + } + } + } + log.Printf("[DEBUG] Creating ElasticSearch domain: %s", input) // IAM Roles can take some time to propagate if set in AccessPolicies and created in the same terraform @@ -448,6 +492,18 @@ func resourceAwsElasticSearchDomainRead(d *schema.ResourceData, meta interface{} } } + if ds.LogPublishingOptions != nil { + m := make([]map[string]interface{}, 0) + for k, val := range ds.LogPublishingOptions { + mm := map[string]interface{}{} + mm["log_type"] = k + mm["cloudwatch_log_group_arn"] = *val.CloudWatchLogsLogGroupArn + mm["enabled"] = *val.Enabled + m = append(m, mm) + } + d.Set("log_publishing_options", m) + } + d.Set("arn", ds.ARN) listOut, err := conn.ListTags(&elasticsearch.ListTagsInput{ @@ -535,6 +591,18 @@ func resourceAwsElasticSearchDomainUpdate(d *schema.ResourceData, meta interface input.VPCOptions = expandESVPCOptions(s) } + if d.HasChange("log_publishing_options") { + input.LogPublishingOptions = make(map[string]*elasticsearch.LogPublishingOption) + options := d.Get("log_publishing_options").(*schema.Set).List() + for _, vv := range options { + lo := vv.(map[string]interface{}) + input.LogPublishingOptions[lo["log_type"].(string)] = &elasticsearch.LogPublishingOption{ + CloudWatchLogsLogGroupArn: aws.String(lo["cloudwatch_log_group_arn"].(string)), + Enabled: aws.Bool(lo["enabled"].(bool)), + } + } + } + _, err := conn.UpdateElasticsearchDomainConfig(&input) if err != nil { return err diff --git a/aws/resource_aws_elasticsearch_domain_test.go b/aws/resource_aws_elasticsearch_domain_test.go index fd6738b1b5a..5640fffb8cd 100644 --- a/aws/resource_aws_elasticsearch_domain_test.go +++ b/aws/resource_aws_elasticsearch_domain_test.go @@ -216,6 +216,23 @@ func TestAccAWSElasticSearchDomain_internetToVpcEndpoint(t *testing.T) { }) } +func TestAccAWSElasticSearchDomain_LogPublishingOptions(t *testing.T) { + var domain elasticsearch.ElasticsearchDomainStatus + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckESDomainDestroy, + Steps: []resource.TestStep{ + { + Config: testAccESDomainConfig_LogPublishingOptions(acctest.RandInt()), + Check: resource.ComposeTestCheckFunc( + testAccCheckESDomainExists("aws_elasticsearch_domain.example", &domain), + ), + }, + }, + }) +} + func testAccCheckESNumberOfSecurityGroups(numberOfSecurityGroups int, status *elasticsearch.ElasticsearchDomainStatus) resource.TestCheckFunc { return func(s *terraform.State) error { count := len(status.VPCOptions.SecurityGroupIds) @@ -414,7 +431,7 @@ resource "aws_elasticsearch_domain" "example" { ebs_options { ebs_enabled = true volume_size = 10 - + } cluster_config { @@ -704,3 +721,46 @@ resource "aws_elasticsearch_domain" "example" { } `, randInt) } + +func testAccESDomainConfig_LogPublishingOptions(randInt int) string { + return fmt.Sprintf(` +resource "aws_cloudwatch_log_group" "example" { + name = "tf-test-%d" +} + +resource "aws_cloudwatch_log_resource_policy" "example" { + policy_name = "tf-cwlp-%d" + policy_document = <