# Building a search application with Amazon OpenSearch Service We will create a search application with Amazon OpenSearch Service. Instead of using a server to host the frontend application and then authorize the server to call the OpenSearch APIs directly and have the server send requests to Amazon ES, we're going to use Amazon API Gateway to restrict users to a subset of the OpenSearch APIs and AWS Lambda to sign requests from API Gateway to Amazon ES. ## Requirements * **AWS Account:** If you don’t have an account nor an account provided to you, [click here](https://aws.amazon.com/es/free/). ## Architecture ![arch](images/OS-Arch.png) What you have here is a VPC in which the Amazon EC2 instance will reside. This instance is going to add the sample movies data into the movies index in the Amazon OpenSearch service cluster. Once the data is ready, users can access and query the data through a web form hosted on Amazon S3. The queries will go through Amazon API Gateway and pass the request to AWS Lambda function which is the only authorized entity to interact with the Amazon OpenSearch cluster and retrieve the requested data. ## Launch the template 1. Go to Amazon S3 service on the AWS Console and make sure that you have the **Block Public Access for the Account** turned off. This is only for Demo purposes and you should **NOT** do this in your production account. ![S3_setting](images/s3.png) This is needed beacuse we're going to create a public bucket to host the static website. 2. Deploy the AWS Cloud Formation template clicking on the button below: [![Launch CFN stack](https://s3.amazonaws.com/cloudformation-examples/cloudformation-launch-stack.png)](https://eu-west-1.console.aws.amazon.com/cloudformation/home?region=eu-west-1#/stacks/quickcreate?templateUrl=https://elastic-search-movies-search-app.s3-eu-west-1.amazonaws.com/Templates/main_es_diy.yaml&stackName=search-app) **NOTE**: The template takes 30 min to deploy approx. * If you don’t have the AWS CLI installed, follow [these](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) steps. And to configure the AWS CLI, follow [these](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html#cli-configure-quickstart-config). * Clone the repository. * Export the following parameters in your CLI: ```bash export AWSREGION= export AWSPROFILE= export STACKNAME= ``` * Go back to your terminal and create the CloudFormation stack: ```bash aws cloudformation create-stack --stack-name $STACKNAME --template-url https://elastic-search-movies-search-app.s3-eu-west-1.amazonaws.com/Templates/main_es_diy.yaml --tags Key=project,Value=opensearch-app --profile $AWSPROFILE --region=$AWSREGION --capabilities CAPABILITY_IAM ``` Once completed, create the AWS Lambda Function following these instructions: 3. Create the Lambda function In this solution, API Gateway passes requests to the following Python 3.8 Lambda function, which queries Amazon ES and returns results. Because this sample function uses external libraries, you need to create a deployment package and upload it to Lambda for the code to work. ``` git clone https://github.com/aws-samples/future-reponame.git cd future-reponame/search-es-lambda pip3 install requests -t . pip3 install requests_aws4auth -t . zip -r lambda.zip * aws lambda update-function-code --function-name search-es-lambda --zip-file fileb://lambda.zip --region=$AWSREGION ``` 4. Test your movies search app Look for the Amazon S3 static website endpoint. Go to CloudFormation, select the recently launched template, click on the nested one called **YourStackName-Lambda-RandomString** and click on the Outputs tab. You'll see WebsiteURL with a link like **http://search-app-accountID-.s3-website-eu-west-1.amazonaws.com/**. Once opened, a static website like this will appear: ![sample-site](images/sample-site.png) Now, type the item to search for! ### Clean up After completing your demo, delete AWS CloudFormation Stack using AWS Console or AWS CLI: ``` aws cloudformation delete-stack --stack-name $STACKNAME --region $AWSREGION ``` Revert back **Block Public Access for the Account** configuration. ## Security See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more information. ## License This library is licensed under the MIT-0 License. See the LICENSE file. ## Authors * Daniel Neri * Serhat Gülbetekin ## Security See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more information. ## License This library is licensed under the MIT-0 License. See the LICENSE file.