aws-cdk-aws-opensearchservice 1.204.0


pip install aws-cdk-aws-opensearchservice

  Latest version

Released: Jun 19, 2023

Project Links

Meta
Author: Amazon Web Services
Requires Python: ~=3.7

Classifiers

Intended Audience
  • Developers

Operating System
  • OS Independent

Programming Language
  • JavaScript
  • Python :: 3 :: Only
  • Python :: 3.7
  • Python :: 3.8
  • Python :: 3.9
  • Python :: 3.10
  • Python :: 3.11

Typing
  • Typed

Development Status
  • 7 - Inactive

License
  • OSI Approved

Framework
  • AWS CDK
  • AWS CDK :: 1

Amazon OpenSearch Service Construct Library

---

End-of-Support

AWS CDK v1 has reached End-of-Support on 2023-06-01. This package is no longer being updated, and users should migrate to AWS CDK v2.

For more information on how to migrate, see the Migrating to AWS CDK v2 guide.


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.


See Migrating to OpenSearch for migration instructions from @aws-cdk/aws-elasticsearch to this module, @aws-cdk/aws-opensearchservice.

Quick start

Create a development cluster by simply specifying the version:

dev_domain = opensearch.Domain(self, "Domain",
    version=opensearch.EngineVersion.OPENSEARCH_1_0
)

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

dev_domain = opensearch.Domain(self, "Domain",
    version=opensearch.EngineVersion.OPENSEARCH_1_0,
    enable_version_upgrade=True
)

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

prod_domain = opensearch.Domain(self, "Domain",
    version=opensearch.EngineVersion.OPENSEARCH_1_0,
    capacity=opensearch.CapacityConfig(
        master_nodes=5,
        data_nodes=20
    ),
    ebs=opensearch.EbsOptions(
        volume_size=20
    ),
    zone_awareness=opensearch.ZoneAwarenessConfig(
        availability_zone_count=3
    ),
    logging=opensearch.LoggingOptions(
        slow_search_log_enabled=True,
        app_log_enabled=True,
        slow_index_log_enabled=True
    )
)

This creates an Amazon OpenSearch Service 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 OpenSearch Service...

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:

slr = iam.CfnServiceLinkedRole(self, "Service Linked Role",
    aws_service_name="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:

domain_endpoint = "https://my-domain-jcjotrt6f7otem4sqcwbch3c4u.us-east-1.es.amazonaws.com"
domain = opensearch.Domain.from_domain_endpoint(self, "ImportedDomain", domain_endpoint)

Permissions

IAM

Helper methods also exist for managing access to the domain.

# fn: lambda.Function
# domain: opensearch.Domain


# Grant write access to the app-search index
domain.grant_index_write("app-search", fn)

# Grant read access to the 'app-search/_search' path
domain.grant_path_read("app-search/_search", fn)

Encryption

The domain can also be created with encryption enabled:

domain = opensearch.Domain(self, "Domain",
    version=opensearch.EngineVersion.OPENSEARCH_1_0,
    ebs=opensearch.EbsOptions(
        volume_size=100,
        volume_type=ec2.EbsDeviceVolumeType.GENERAL_PURPOSE_SSD
    ),
    node_to_node_encryption=True,
    encryption_at_rest=opensearch.EncryptionAtRestOptions(
        enabled=True
    )
)

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

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

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

vpc = ec2.Vpc(self, "Vpc")
domain_props = opensearch.DomainProps(
    version=opensearch.EngineVersion.OPENSEARCH_1_0,
    removal_policy=RemovalPolicy.DESTROY,
    vpc=vpc,
    # must be enabled since our VPC contains multiple private subnets.
    zone_awareness=opensearch.ZoneAwarenessConfig(
        enabled=True
    ),
    capacity=opensearch.CapacityConfig(
        # must be an even number since the default az count is 2.
        data_nodes=2
    )
)
opensearch.Domain(self, "Domain", domain_props)

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:

# domain: opensearch.Domain

free_storage_space = domain.metric_free_storage_space()
master_sys_memory_utilization = 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.

domain = opensearch.Domain(self, "Domain",
    version=opensearch.EngineVersion.OPENSEARCH_1_0,
    enforce_https=True,
    node_to_node_encryption=True,
    encryption_at_rest=opensearch.EncryptionAtRestOptions(
        enabled=True
    ),
    fine_grained_access_control=opensearch.AdvancedSecurityOptions(
        master_user_name="master-user"
    )
)

master_user_password = domain.master_user_password

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.

domain = opensearch.Domain(self, "Domain",
    version=opensearch.EngineVersion.OPENSEARCH_1_0,
    use_unsigned_basic_auth=True
)

master_user_password = domain.master_user_password

Custom access policies

If the domain requires custom access control it can be configured either as a constructor property, or later by means of a helper method.

For simple permissions the accessPolicies constructor may be sufficient:

domain = opensearch.Domain(self, "Domain",
    version=opensearch.EngineVersion.OPENSEARCH_1_0,
    access_policies=[
        iam.PolicyStatement(
            actions=["es:*ESHttpPost", "es:ESHttpPut*"],
            effect=iam.Effect.ALLOW,
            principals=[iam.AccountPrincipal("123456789012")],
            resources=["*"]
        )
    ]
)

For more complex use-cases, for example, to set the domain up to receive data from a cross-account Kinesis Firehose the addAccessPolicies helper method allows for policies that include the explicit domain ARN.

domain = opensearch.Domain(self, "Domain",
    version=opensearch.EngineVersion.OPENSEARCH_1_0
)
domain.add_access_policies(
    iam.PolicyStatement(
        actions=["es:ESHttpPost", "es:ESHttpPut"],
        effect=iam.Effect.ALLOW,
        principals=[iam.AccountPrincipal("123456789012")],
        resources=[domain.domain_arn, f"{domain.domainArn}/*"]
    ),
    iam.PolicyStatement(
        actions=["es:ESHttpGet"],
        effect=iam.Effect.ALLOW,
        principals=[iam.AccountPrincipal("123456789012")],
        resources=[f"{domain.domainArn}/_all/_settings", f"{domain.domainArn}/_cluster/stats", f"{domain.domainArn}/index-name*/_mapping/type-name", f"{domain.domainArn}/roletest*/_mapping/roletest", f"{domain.domainArn}/_nodes", f"{domain.domainArn}/_nodes/stats", f"{domain.domainArn}/_nodes/*/stats", f"{domain.domainArn}/_stats", f"{domain.domainArn}/index-name*/_stats", f"{domain.domainArn}/roletest*/_stat"
        ]
    ))

Audit logs

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

domain = opensearch.Domain(self, "Domain",
    version=opensearch.EngineVersion.OPENSEARCH_1_0,
    enforce_https=True,
    node_to_node_encryption=True,
    encryption_at_rest=opensearch.EncryptionAtRestOptions(
        enabled=True
    ),
    fine_grained_access_control=opensearch.AdvancedSecurityOptions(
        master_user_name="master-user"
    ),
    logging=opensearch.LoggingOptions(
        audit_log_enabled=True,
        slow_search_log_enabled=True,
        app_log_enabled=True,
        slow_index_log_enabled=True
    )
)

UltraWarm

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

domain = opensearch.Domain(self, "Domain",
    version=opensearch.EngineVersion.OPENSEARCH_1_0,
    capacity=opensearch.CapacityConfig(
        master_nodes=2,
        warm_nodes=2,
        warm_instance_type="ultrawarm1.medium.search"
    )
)

Custom endpoint

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

opensearch.Domain(self, "Domain",
    version=opensearch.EngineVersion.OPENSEARCH_1_0,
    custom_endpoint=opensearch.CustomEndpointOptions(
        domain_name="search.example.com"
    )
)

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

Advanced options

Advanced options can used to configure additional options.

opensearch.Domain(self, "Domain",
    version=opensearch.EngineVersion.OPENSEARCH_1_0,
    advanced_options={
        "rest.action.multi.allow_explicit_index": "false",
        "indices.fielddata.cache.size": "25",
        "indices.query.bool.max_clause_count": "2048"
    }
)
1.204.0 Jun 19, 2023
1.203.0 May 31, 2023
1.202.0 May 22, 2023
1.201.0 May 10, 2023
1.200.0 Apr 26, 2023
1.199.0 Apr 20, 2023
1.198.1 Mar 31, 2023
1.198.0 Mar 22, 2023
1.197.0 Mar 14, 2023
1.196.0 Mar 08, 2023
1.195.0 Mar 02, 2023
1.194.0 Feb 21, 2023
1.193.0 Feb 15, 2023
1.192.0 Feb 09, 2023
1.191.0 Jan 31, 2023
1.190.0 Jan 25, 2023
1.189.0 Jan 19, 2023
1.188.0 Jan 11, 2023
1.187.0 Jan 03, 2023
1.186.1 Dec 30, 2022
1.186.0 Dec 29, 2022
1.185.0 Dec 28, 2022
1.184.1 Dec 23, 2022
1.184.0 Dec 22, 2022
1.183.0 Dec 15, 2022
1.182.0 Dec 07, 2022
1.181.1 Nov 29, 2022
1.181.0 Nov 18, 2022
1.180.0 Nov 01, 2022
1.179.0 Oct 27, 2022
1.178.0 Oct 20, 2022
1.177.0 Oct 13, 2022
1.176.0 Oct 06, 2022
1.175.0 Sep 29, 2022
1.174.0 Sep 22, 2022
1.173.0 Sep 16, 2022
1.172.0 Sep 08, 2022
1.171.0 Aug 31, 2022
1.170.1 Aug 31, 2022
1.170.0 Aug 25, 2022
1.169.0 Aug 18, 2022
1.168.0 Aug 09, 2022
1.167.0 Aug 02, 2022
1.166.1 Jul 29, 2022
1.165.0 Jul 19, 2022
1.164.0 Jul 16, 2022
1.163.2 Jul 14, 2022
1.163.1 Jul 09, 2022
1.163.0 Jul 06, 2022
1.162.0 Jul 01, 2022
1.161.0 Jun 23, 2022
1.160.0 Jun 14, 2022
1.159.0 Jun 03, 2022
1.158.0 May 27, 2022
1.157.0 May 21, 2022
1.156.1 May 13, 2022
1.156.0 May 12, 2022
1.155.0 May 04, 2022
1.154.0 Apr 28, 2022
1.153.1 Apr 23, 2022
1.153.0 Apr 22, 2022
1.152.0 Apr 07, 2022
1.151.0 Apr 01, 2022
1.150.0 Mar 26, 2022
1.149.0 Mar 17, 2022
1.148.0 Mar 10, 2022
1.147.0 Mar 01, 2022
1.146.0 Feb 25, 2022
1.145.0 Feb 19, 2022
1.144.0 Feb 08, 2022
1.143.0 Feb 02, 2022
1.142.0 Jan 29, 2022
1.141.0 Jan 27, 2022
1.140.0 Jan 20, 2022
1.139.0 Jan 11, 2022
1.138.2 Jan 10, 2022
1.138.1 Jan 07, 2022
1.138.0 Jan 04, 2022
1.137.0 Dec 21, 2021
1.136.0 Dec 15, 2021
1.135.0 Dec 10, 2021
1.134.0 Nov 23, 2021
1.133.0 Nov 19, 2021
1.132.0 Nov 09, 2021
1.131.0 Nov 07, 2021
1.130.0 Oct 29, 2021
1.129.0 Oct 21, 2021
1.128.0 Oct 14, 2021
1.127.0 Oct 08, 2021
1.126.0 Oct 05, 2021
1.125.0 Sep 29, 2021
1.124.0 Sep 21, 2021
1.123.0 Sep 17, 2021
Extras: None
Dependencies:
aws-cdk.aws-certificatemanager (==1.204.0)
aws-cdk.aws-cloudwatch (==1.204.0)
aws-cdk.aws-ec2 (==1.204.0)
aws-cdk.aws-iam (==1.204.0)
aws-cdk.aws-kms (==1.204.0)
aws-cdk.aws-logs (==1.204.0)
aws-cdk.aws-route53 (==1.204.0)
aws-cdk.aws-secretsmanager (==1.204.0)
aws-cdk.core (==1.204.0)
aws-cdk.custom-resources (==1.204.0)
constructs (<4.0.0,>=3.3.69)
jsii (<2.0.0,>=1.84.0)
publication (>=0.0.3)
typeguard (~=2.13.3)