- Newest
- Most votes
- Most comments
According to the current AWS::OpenSearchServerless::Index PropertyMapping documentation, Type property is limited only to text or knn_vector values. You can create a custom Lambda function that will use one of the OpenSearch clients to create a new index and register that Lambda as a custom resource in your CDK stack. This approach will allow you to use any type definition that is supported by the OpenSearch Create Index API.
answered a year ago
The issue you're encountering is related to how the CDK expects the mapping structure to be defined for OpenSearch Serverless indexes. The error messages suggest that the CDK is not recognizing the field types you've specified as valid enum values. To resolve this, you need to adjust your mapping structure to match the CDK's expectations for the CfnIndex construct.
Here's how you can modify your code to create a valid mapping:
-
Instead of using an object literal for the mapping, use the
MappingsPropertyinterface provided by the CDK. -
For each field in your mapping, use the
PropertyMappingPropertyinterface to define its properties. -
The
typeproperty for each field should be a string that matches the OpenSearch field types.
Here's an example of how you can rewrite your mapping:
import * as aoss from 'aws-cdk-lib/aws-opensearchserverless'; const indexMapping: aoss.CfnIndex.MappingsProperty = { properties: { account_id: { type: "keyword" }, address: { type: "text" }, city: { type: "text", properties: { keyword: { type: "keyword" } } }, created_at: { type: "date" }, created_at_timestamp: { type: "long" }, // ... other fields ... jane_store: { type: "object", properties: { id: { type: "keyword" }, name: { type: "text" } } }, // ... remaining fields ... } }; this.opensearchIndex = new aoss.CfnIndex(this, "OpenSearchIndex", { collectionEndpoint: this.environmentConfig.aoss.CollectionEndpoint, indexName: prefix, mappings: indexMapping });
In this revised version:
- We're using the
MappingsPropertystructure expected by the CDK. - Each field's
typeis specified as a string. - For fields with multiple types (like
citywith bothtextandkeyword), we use nestedproperties. - The
objecttype fields (likejane_store) are defined with their ownproperties.
This structure should align with what the CDK expects for OpenSearch Serverless index mappings. Make sure to import the necessary types from the aws-cdk-lib/aws-opensearchserverless module.
Remember that some advanced mapping features might not be directly supported through the CDK and may require using escape hatches or custom resources if needed.
Sources
interface PropertyMappingProperty · AWS CDK
interface MappingsProperty · AWS CDK
interface CfnIndexProps · AWS CDK
answered 2 years ago
Relevant content
- AWS OFFICIALUpdated 3 months ago

Did you end up finding a solution / work-around? I posted a related question: https://repost.aws/questions/QUJQDqGe2BTiud_coGNirLwg/how-to-use-date-float-values-in-opensearchserverless-propertymapping-cdk