Using the AWS SDKs with Amazon Elasticsearch Service - Amazon Elasticsearch Service

Using the AWS SDKs with Amazon Elasticsearch Service

This section includes examples of how to use the AWS SDKs to interact with the Amazon Elasticsearch Service configuration API. These code samples show how to create, update, and delete Amazon ES domains.

Important

For examples of how to interact with the Elasticsearch APIs, such as _index, _bulk, _search, and _snapshot, see Signing HTTP Requests to Amazon Elasticsearch Service.

Java

This example uses the AWS SDK for Java to create a domain, update its configuration, and delete it. Uncomment the calls to waitForDomainProcessing (and comment the call to deleteDomain) to allow the domain to come online and be useable.

package com.amazonaws.samples; import java.util.concurrent.TimeUnit; import com.amazonaws.auth.DefaultAWSCredentialsProviderChain; import com.amazonaws.regions.Regions; import com.amazonaws.services.elasticsearch.AWSElasticsearch; import com.amazonaws.services.elasticsearch.AWSElasticsearchClientBuilder; import com.amazonaws.services.elasticsearch.model.CreateElasticsearchDomainRequest; import com.amazonaws.services.elasticsearch.model.CreateElasticsearchDomainResult; import com.amazonaws.services.elasticsearch.model.DeleteElasticsearchDomainRequest; import com.amazonaws.services.elasticsearch.model.DeleteElasticsearchDomainResult; import com.amazonaws.services.elasticsearch.model.DescribeElasticsearchDomainRequest; import com.amazonaws.services.elasticsearch.model.DescribeElasticsearchDomainResult; import com.amazonaws.services.elasticsearch.model.EBSOptions; import com.amazonaws.services.elasticsearch.model.ElasticsearchClusterConfig; import com.amazonaws.services.elasticsearch.model.NodeToNodeEncryptionOptions; import com.amazonaws.services.elasticsearch.model.ResourceNotFoundException; import com.amazonaws.services.elasticsearch.model.SnapshotOptions; import com.amazonaws.services.elasticsearch.model.UpdateElasticsearchDomainConfigRequest; import com.amazonaws.services.elasticsearch.model.UpdateElasticsearchDomainConfigResult; import com.amazonaws.services.elasticsearch.model.VolumeType; /** * Sample class demonstrating how to use the AWS SDK for Java to create, update, * and delete Amazon Elasticsearch Service domains. */ public class AESSample { public static void main(String[] args) { final String domainName = "my-test-domain"; // Build the client using the default credentials chain. // You can use the AWS CLI and run `aws configure` to set access key, secret // key, and default region. final AWSElasticsearch client = AWSElasticsearchClientBuilder .standard() // Unnecessary, but lets you use a region different than your default. .withRegion(Regions.US_WEST_2) // Unnecessary, but if desired, you can use a different provider chain. .withCredentials(new DefaultAWSCredentialsProviderChain()) .build(); // Create a new domain, update its configuration, and delete it. createDomain(client, domainName); // waitForDomainProcessing(client, domainName); updateDomain(client, domainName); // waitForDomainProcessing(client, domainName); deleteDomain(client, domainName); } /** * Creates an Amazon Elasticsearch Service domain with the specified options. * Some options require other AWS resources, such as an Amazon Cognito user pool * and identity pool, whereas others require just an instance type or instance * count. * * @param client * The AWSElasticsearch client to use for the requests to Amazon * Elasticsearch Service * @param domainName * The name of the domain you want to create */ private static void createDomain(final AWSElasticsearch client, final String domainName) { // Create the request and set the desired configuration options CreateElasticsearchDomainRequest createRequest = new CreateElasticsearchDomainRequest() .withDomainName(domainName) .withElasticsearchVersion("6.3") .withElasticsearchClusterConfig(new ElasticsearchClusterConfig() .withDedicatedMasterEnabled(true) .withDedicatedMasterCount(3) // Small, inexpensive instance types for testing. Not recommended for production // domains. .withDedicatedMasterType("t2.small.elasticsearch") .withInstanceType("t2.small.elasticsearch") .withInstanceCount(5)) // Many instance types require EBS storage. .withEBSOptions(new EBSOptions() .withEBSEnabled(true) .withVolumeSize(10) .withVolumeType(VolumeType.Gp2)) // You can uncomment this line and add your account ID, a user name, and the // domain name to add an access policy. // .withAccessPolicies("{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"arn:aws:iam::123456789012:user/user-name\"]},\"Action\":[\"es:*\"],\"Resource\":\"arn:aws:es:region:123456789012:domain/domain-name/*\"}]}") .withNodeToNodeEncryptionOptions(new NodeToNodeEncryptionOptions() .withEnabled(true)); // Make the request. System.out.println("Sending domain creation request..."); CreateElasticsearchDomainResult createResponse = client.createElasticsearchDomain(createRequest); System.out.println("Domain creation response from Amazon Elasticsearch Service:"); System.out.println(createResponse.getDomainStatus().toString()); } /** * Updates the configuration of an Amazon Elasticsearch Service domain with the * specified options. Some options require other AWS resources, such as an * Amazon Cognito user pool and identity pool, whereas others require just an * instance type or instance count. * * @param client * The AWSElasticsearch client to use for the requests to Amazon * Elasticsearch Service * @param domainName * The name of the domain to update */ private static void updateDomain(final AWSElasticsearch client, final String domainName) { try { // Updates the domain to use three data instances instead of five. // You can uncomment the Cognito lines and fill in the strings to enable Cognito // authentication for Kibana. final UpdateElasticsearchDomainConfigRequest updateRequest = new UpdateElasticsearchDomainConfigRequest() .withDomainName(domainName) // .withCognitoOptions(new CognitoOptions() // .withEnabled(true) // .withUserPoolId("user-pool-id") // .withIdentityPoolId("identity-pool-id") // .withRoleArn("role-arn")) .withElasticsearchClusterConfig(new ElasticsearchClusterConfig() .withInstanceCount(3)); System.out.println("Sending domain update request..."); final UpdateElasticsearchDomainConfigResult updateResponse = client .updateElasticsearchDomainConfig(updateRequest); System.out.println("Domain update response from Amazon Elasticsearch Service:"); System.out.println(updateResponse.toString()); } catch (ResourceNotFoundException e) { System.out.println("Domain not found. Please check the domain name."); } } /** * Deletes an Amazon Elasticsearch Service domain. Deleting a domain can take * several minutes. * * @param client * The AWSElasticsearch client to use for the requests to Amazon * Elasticsearch Service * @param domainName * The name of the domain that you want to delete */ private static void deleteDomain(final AWSElasticsearch client, final String domainName) { try { final DeleteElasticsearchDomainRequest deleteRequest = new DeleteElasticsearchDomainRequest() .withDomainName(domainName); System.out.println("Sending domain deletion request..."); final DeleteElasticsearchDomainResult deleteResponse = client.deleteElasticsearchDomain(deleteRequest); System.out.println("Domain deletion response from Amazon Elasticsearch Service:"); System.out.println(deleteResponse.toString()); } catch (ResourceNotFoundException e) { System.out.println("Domain not found. Please check the domain name."); } } /** * Waits for the domain to finish processing changes. New domains typically take 15-30 minutes * to initialize, but can take longer depending on the configuration. Most updates to existing domains * take a similar amount of time. This method checks every 15 seconds and finishes only when * the domain's processing status changes to false. * * @param client * The AWSElasticsearch client to use for the requests to Amazon * Elasticsearch Service * @param domainName * The name of the domain that you want to check */ private static void waitForDomainProcessing(final AWSElasticsearch client, final String domainName) { // Create a new request to check the domain status. final DescribeElasticsearchDomainRequest describeRequest = new DescribeElasticsearchDomainRequest() .withDomainName(domainName); // Every 15 seconds, check whether the domain is processing. DescribeElasticsearchDomainResult describeResponse = client.describeElasticsearchDomain(describeRequest); while (describeResponse.getDomainStatus().isProcessing()) { try { System.out.println("Domain still processing..."); TimeUnit.SECONDS.sleep(15); describeResponse = client.describeElasticsearchDomain(describeRequest); } catch (InterruptedException e) { e.printStackTrace(); } } // Once we exit that loop, the domain is available System.out.println("Amazon Elasticsearch Service has finished processing changes for your domain."); System.out.println("Domain description response from Amazon Elasticsearch Service:"); System.out.println(describeResponse.toString()); } }