We use machine learning technology to do auto-translation. Click "English" on top navigation bar to check Chinese version.
Deploy Amazon OpenSearch Serverless with Terraform
This post demonstrates how to use Terraform to create, deploy, and clean up OpenSearch Serverless infrastructure.
Solution overview
To create and deploy an OpenSearch Serverless collection with security and access policies using Terraform, you need to follow these steps:
- Initialize the Terraform configuration.
- Create an encryption policy.
- Create an OpenSearch Serverless collection.
- Create a network policy.
- Create a virtual private cloud (VPC) endpoint.
- Create a data access policy.
- Deploy using Terraform.
Prerequisites
This post assumes that you’re familiar with
For this walkthrough, you need the following:
- An Amazon Web Services account. If you don’t have an account, you can
sign up for one . - Access to an
Amazon Web Services Identity and Access Management (IAM) user or role that has theminimum required permissions for setting up a collection. - Terraform 0.12 or greater installed on your workstation. See
Install Terraform .
Initialize the Terraform configuration
The sample code is available in the
terraform-provider-aws/examples/opensearchserverless
directory. This configuration will get you started with OpenSearch Serverless. First, clone the repository to your workstation and navigate to the directory:
$ git clone https://github.com/hashicorp/terraform-provider-aws.git && \
cd ./terraform-provider-aws/examples/opensearchserverless
Initialize the configuration to install the aws
provider by running the following command:
$ terraform init
The Terraform configuration first defines the version of Terraform required and configures the
aws_region
variable:
The variables used in this Terraform configuration are defined in the variables.tf
file. This post assumes the default values are used:
Create an encryption policy
Now that the provider is installed and configured, the Terraform configuration moves on to defining OpenSearch Serverless policies for security. OpenSearch Serverless uses
resource, which has a name
parameter, a type
of encryption
, a JSON string that defines the policy, and an optional description
:
This encryption policy is named example-encryption-policy
, applies to a collection named example-collection
, and uses an Amazon Web Services owned key to encrypt the data.
Create an OpenSearch Serverless collection
You can organize your
resource, which has a name
parameter, and optionally, description
, tags
, and type
:
This collection is named example-collection
. If type
is not specified, a
resource. OpenSearch Serverless requires encryption at rest, so an applicable encryption policy is required before a collection can be created. The Terraform configuration explicitly defines this dependency using the
meta-argument. Errors can arise if this dependency is not defined.
Now that a collection has been created with an Amazon Web Services owned KMS key, the Terraform configuration goes on to define the network and data access policy to configure access to the collection.
Create a network policy
A
resource, which has a name
parameter, a type
of network
, a JSON string that defines the policy, and an optional description
:
This network policy is named example-network-policy
and applies to the collection named example-collection
. This policy only allows access to the collection’s OpenSearch endpoint through a VPC endpoint, but allows public access to the OpenSearch Dashboards endpoint.
You’ll notice the VPC endpoint has not been defined yet, but it is referenced in the network policy. Terraform determines this dependency automatically and will not create the network policy until the VPC endpoint has been created.
Create a VPC endpoint
A VPC endpoint enables you to privately access your OpenSearch Serverless collection using
resource, where you define name
, vpc_id
, subnet_ids
, and optionally, security_group_ids
:
Creating a VPC and all the required networking resources is out of scope for this post, but the minimum required VPC resources are created here in a separate file to demonstrate the VPC endpoint functionality. Refer to
Create a data access policy
The configuration defines a
.
A
resource, which has a name
parameter, a type
parameter set to data
, a JSON string that defines the policy, and an optional description
:
This data access policy allows the current Amazon Web Services role or user to perform
example-collection
and
Deploy using Terraform
Now that you have configured the necessary resources, apply the configuration using terraform apply
. Before creating the resources, Terraform will describe all the resources that will be created so you can verify your configuration:
$ terraform apply
...
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
...
Plan: 13 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ collection_enpdoint = (known after apply)
+ dashboard_endpoint = (known after apply)
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value:
If this is the first OpenSearch Serverless collection in your account, applying the configuration may take over 10 minutes because Terraform waits for the collection to become active.
Apply complete! Resources: 13 added, 0 changed, 0 destroyed.
Outputs:
collection_enpdoint = "..."
dashboard_endpoint = "..."
You have now deployed an OpenSearch Serverless time series collection with policies to configure encryption and access to the collection!
Clean up
The resources will incur costs as long as they are running, so clean up the resources when you are done using them. Use the terraform destroy command to do this:
$ terraform destroy
...
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
- destroy
Terraform will perform the following actions:
...
Plan: 0 to add, 0 to change, 13 to destroy.
Changes to Outputs:
...
Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value:
Answer yes
to run this plan and destroy the infrastructure.
Destroy complete! Resources: 13 destroyed.
All resources created during this walkthrough have now been deleted.
Conclusion
In this post, you created an OpenSearch Serverless collection. Using IaC software like Terraform can make it simple to manage your resources like OpenSearch Serverless collections, encryption, network, and data access policies, and VPC endpoints.
Thank you to all the open-source contributors who help maintain
Try using OpenSearch Serverless with Terraform to simplify your resource management. Check out the
About the authors
Joshua Luo is a Software Development Engineer for Amazon OpenSearch Serverless. He works on the systems that enable customers to manage and monitor their OpenSearch Serverless resources. He enjoys bouldering, photography, and videography in his free time.
Satish Nandi is a Senior Technical Product Manager for Amazon OpenSearch Service.
The mentioned AWS GenAI Services service names relating to generative AI are only available or previewed in the Global Regions. Amazon Web Services China promotes AWS GenAI Services relating to generative AI solely for China-to-global business purposes and/or advanced technology introduction.