Skip navigation links

Package software.amazon.awscdk.services.elasticsearch

Amazon Elasticsearch Service Construct Library

See: Description

Package software.amazon.awscdk.services.elasticsearch Description

Amazon Elasticsearch Service Construct Library

---

Features | Stability -----------------------------------|---------------------------------------------------------------- CFN Resources | Stable Higher level constructs for Domain | Stable

CFN Resources: All classes with the Cfn prefix in this module (CFN Resources) are always stable and safe to use.

Stable: Higher level constructs in this module that are marked stable will not undergo any breaking changes. They will strictly follow the Semantic Versioning model.


Quick start

Create a development cluster by simply specifying the version:

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 import software.amazon.awscdk.aws_elasticsearch;
 
 
 Domain devDomain = new Domain(this, "Domain", new DomainProps()
         .version(es.ElasticsearchVersion.getV7_1()));
 

To perform version upgrades without replacing the entire domain, specify the enableVersionUpgrade property.

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 import software.amazon.awscdk.aws_elasticsearch;
 
 
 Domain devDomain = new Domain(this, "Domain", new DomainProps()
         .version(es.ElasticsearchVersion.getV7_10())
         .enableVersionUpgrade(true));
 

Create a production grade cluster by also specifying things like capacity and az distribution

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 Object prodDomain = Domain.Builder.create(this, "Domain")
         .version(es.ElasticsearchVersion.getV7_1())
         .capacity(Map.of(
                 "masterNodes", 5,
                 "dataNodes", 20))
         .ebs(Map.of(
                 "volumeSize", 20))
         .zoneAwareness(Map.of(
                 "availabilityZoneCount", 3))
         .logging(Map.of(
                 "slowSearchLogEnabled", true,
                 "appLogEnabled", true,
                 "slowIndexLogEnabled", true))
         .build();
 

This creates an Elasticsearch cluster and automatically sets up log groups for logging the domain logs and slow search logs.

A note about SLR

Some cluster configurations (e.g VPC access) require the existence of the AWSServiceRoleForAmazonElasticsearchService Service-Linked Role.

When performing such operations via the AWS Console, this SLR is created automatically when needed. However, this is not the behavior when using CloudFormation. If an SLR is needed, but doesn't exist, you will encounter a failure message simlar to:

 Before you can proceed, you must enable a service-linked role to give Amazon ES...
 

To resolve this, you need to create the SLR. We recommend using the AWS CLI:

 aws iam create-service-linked-role --aws-service-name es.amazonaws.com
 

You can also create it using the CDK, but note that only the first application deploying this will succeed:

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 CfnServiceLinkedRole slr = new CfnServiceLinkedRole(this, "ElasticSLR", new CfnServiceLinkedRoleProps()
         .awsServiceName("es.amazonaws.com"));
 

Importing existing domains

To import an existing domain into your CDK application, use the Domain.fromDomainEndpoint factory method. This method accepts a domain endpoint of an already existing domain:

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 String domainEndpoint = "https://my-domain-jcjotrt6f7otem4sqcwbch3c4u.us-east-1.es.amazonaws.com";
 Object domain = Domain.fromDomainEndpoint(this, "ImportedDomain", domainEndpoint);
 

Permissions

IAM

Helper methods also exist for managing access to the domain.

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 Object lambda = Function.Builder.create(this, "Lambda").build();
 
 // Grant write access to the app-search index
 domain.grantIndexWrite("app-search", lambda);
 
 // Grant read access to the 'app-search/_search' path
 domain.grantPathRead("app-search/_search", lambda);
 

Encryption

The domain can also be created with encryption enabled:

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 Object domain = Domain.Builder.create(this, "Domain")
         .version(es.ElasticsearchVersion.getV7_4())
         .ebs(Map.of(
                 "volumeSize", 100,
                 "volumeType", EbsDeviceVolumeType.getGENERAL_PURPOSE_SSD()))
         .nodeToNodeEncryption(true)
         .encryptionAtRest(Map.of(
                 "enabled", true))
         .build();
 

This sets up the domain with node to node encryption and encryption at rest. You can also choose to supply your own KMS key to use for encryption at rest.

VPC Support

Elasticsearch domains can be placed inside a VPC, providing a secure communication between Amazon ES and other services within the VPC without the need for an internet gateway, NAT device, or VPN connection.

Visit VPC Support for Amazon Elasticsearch Service Domains for more details.

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 Object vpc = new Vpc(this, "Vpc");
 Object domainProps = Map.of(
         "version", es.ElasticsearchVersion.getV7_1(),
         "removalPolicy", RemovalPolicy.getDESTROY(),
         "vpc", vpc,
         // must be enabled since our VPC contains multiple private subnets.
         "zoneAwareness", Map.of(
                 "enabled", true),
         "capacity", Map.of(
                 // must be an even number since the default az count is 2.
                 "dataNodes", 2));
 new Domain(this, "Domain", domainProps);
 

In addition, you can use the vpcSubnets property to control which specific subnets will be used, and the securityGroups property to control which security groups will be attached to the domain. By default, CDK will select all private subnets in the VPC, and create one dedicated security group.

Metrics

Helper methods exist to access common domain metrics for example:

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 Object freeStorageSpace = domain.metricFreeStorageSpace();
 Object masterSysMemoryUtilization = domain.metric("MasterSysMemoryUtilization");
 

This module is part of the AWS Cloud Development Kit project.

Fine grained access control

The domain can also be created with a master user configured. The password can be supplied or dynamically created if not supplied.

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 Object domain = Domain.Builder.create(this, "Domain")
         .version(es.ElasticsearchVersion.getV7_1())
         .enforceHttps(true)
         .nodeToNodeEncryption(true)
         .encryptionAtRest(Map.of(
                 "enabled", true))
         .fineGrainedAccessControl(Map.of(
                 "masterUserName", "master-user"))
         .build();
 
 Object masterUserPassword = domain.getMasterUserPassword();
 

Using unsigned basic auth

For convenience, the domain can be configured to allow unsigned HTTP requests that use basic auth. Unless the domain is configured to be part of a VPC this means anyone can access the domain using the configured master username and password.

To enable unsigned basic auth access the domain is configured with an access policy that allows anyonmous requests, HTTPS required, node to node encryption, encryption at rest and fine grained access control.

If the above settings are not set they will be configured as part of enabling unsigned basic auth. If they are set with conflicting values, an error will be thrown.

If no master user is configured a default master user is created with the username admin.

If no password is configured a default master user password is created and stored in the AWS Secrets Manager as secret. The secret has the prefix <domain id>MasterUser.

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 Object domain = Domain.Builder.create(this, "Domain")
         .version(es.ElasticsearchVersion.getV7_1())
         .useUnsignedBasicAuth(true)
         .build();
 
 Object masterUserPassword = domain.getMasterUserPassword();
 

Audit logs

Audit logs can be enabled for a domain, but only when fine grained access control is enabled.

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 Object domain = Domain.Builder.create(this, "Domain")
         .version(es.ElasticsearchVersion.getV7_1())
         .enforceHttps(true)
         .nodeToNodeEncryption(true)
         .encryptionAtRest(Map.of(
                 "enabled", true))
         .fineGrainedAccessControl(Map.of(
                 "masterUserName", "master-user"))
         .logging(Map.of(
                 "auditLogEnabled", true,
                 "slowSearchLogEnabled", true,
                 "appLogEnabled", true,
                 "slowIndexLogEnabled", true))
         .build();
 

UltraWarm

UltraWarm nodes can be enabled to provide a cost-effective way to store large amounts of read-only data.

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 Object domain = Domain.Builder.create(this, "Domain")
         .version(es.ElasticsearchVersion.getV7_10())
         .capacity(Map.of(
                 "masterNodes", 2,
                 "warmNodes", 2,
                 "warmInstanceType", "ultrawarm1.medium.elasticsearch"))
         .build();
 

Custom endpoint

Custom endpoints can be configured to reach the ES domain under a custom domain name.

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 Domain.Builder.create(stack, "Domain")
         .version(ElasticsearchVersion.getV7_7())
         .customEndpoint(Map.of(
                 "domainName", "search.example.com"))
         .build();
 

It is also possible to specify a custom certificate instead of the auto-generated one.

Additionally, an automatic CNAME-Record is created if a hosted zone is provided for the custom endpoint

Skip navigation links

Copyright © 2021. All rights reserved.