|
| 1 | + |
| 2 | +# Ingesting data into a collection using Amazon OpenSearch Ingestion |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | +This is an Amazon OpenSearch ingestion project for CDK development with Python. |
| 7 | + |
| 8 | +This project builds on the following tutorial: [Ingesting data into a collection using Amazon OpenSearch Ingestion](https://docs.aws.amazon.com/opensearch-service/latest/developerguide/osis-serverless-get-started.html). |
| 9 | + |
| 10 | +This project shows you how to use Amazon OpenSearch Ingestion to configure a simple pipeline and ingest data into an Amazon OpenSearch Serverless collection. |
| 11 | + |
| 12 | +The `cdk.json` file tells the CDK Toolkit how to execute your app. |
| 13 | + |
| 14 | +This project is set up like a standard Python project. The initialization |
| 15 | +process also creates a virtualenv within this project, stored under the `.venv` |
| 16 | +directory. To create the virtualenv it assumes that there is a `python3` |
| 17 | +(or `python` for Windows) executable in your path with access to the `venv` |
| 18 | +package. If for any reason the automatic creation of the virtualenv fails, |
| 19 | +you can create the virtualenv manually. |
| 20 | + |
| 21 | +To manually create a virtualenv on MacOS and Linux: |
| 22 | + |
| 23 | +``` |
| 24 | +$ python3 -m venv .venv |
| 25 | +``` |
| 26 | + |
| 27 | +After the init process completes and the virtualenv is created, you can use the following |
| 28 | +step to activate your virtualenv. |
| 29 | + |
| 30 | +``` |
| 31 | +$ source .venv/bin/activate |
| 32 | +``` |
| 33 | + |
| 34 | +If you are a Windows platform, you would activate the virtualenv like this: |
| 35 | + |
| 36 | +``` |
| 37 | +% .venv\Scripts\activate.bat |
| 38 | +``` |
| 39 | + |
| 40 | +Once the virtualenv is activated, you can install the required dependencies. |
| 41 | + |
| 42 | +``` |
| 43 | +(.venv) $ pip install -r requirements.txt |
| 44 | +``` |
| 45 | + |
| 46 | +At this point you can now synthesize the CloudFormation template for this code. |
| 47 | + |
| 48 | +<pre> |
| 49 | +(.venv) $ export CDK_DEFAULT_ACCOUNT=$(aws sts get-caller-identity --query Account --output text) |
| 50 | +(.venv) $ export CDK_DEFAULT_REGION=$(curl -s 169.254.169.254/latest/dynamic/instance-identity/document | jq -r .region) |
| 51 | +(.venv) $ cdk synth -c iam_user_name=<i>your-iam-user-name</i> --all |
| 52 | +</pre> |
| 53 | + |
| 54 | +:warning: Amazon OpenSearch Serverless requires mandatory IAM permission for access to resources. |
| 55 | +You are required to add these two IAM permissions for your OpenSearch Serverless **"aoss:APIAccessAll"** for Data Plane API access, and **"aoss:DashboardsAccessAll"** for Dashboards access. Failure to add the two new IAM permissions will result in 403 errors starting on May 10th, 2023 |
| 56 | + |
| 57 | +For a sample data-plane policy [here](https://docs.aws.amazon.com/opensearch-service/latest/developerguide/security-iam-serverless.html#security_iam_id-based-policy-examples-data-plane.html): |
| 58 | + |
| 59 | + - [Using OpenSearch Serverless in the console |
| 60 | +](https://docs.aws.amazon.com/opensearch-service/latest/developerguide/security-iam-serverless.html#security_iam_serverless_id-based-policy-examples-console) |
| 61 | + - [Administering OpenSearch Serverless collections](https://docs.aws.amazon.com/opensearch-service/latest/developerguide/security-iam-serverless.html#security_iam_id-based-policy-examples-collection-admin) |
| 62 | + - [Viewing OpenSearch Serverless collections](https://docs.aws.amazon.com/opensearch-service/latest/developerguide/security-iam-serverless.html#security_iam_id-based-policy-examples-view-collections) |
| 63 | + - [Using data-plane policies](https://docs.aws.amazon.com/opensearch-service/latest/developerguide/security-iam-serverless.html#security_iam_id-based-policy-examples-data-plane) |
| 64 | + |
| 65 | +Use `cdk deploy` command to create the stack shown above. |
| 66 | + |
| 67 | +<pre> |
| 68 | +(.venv) $ cdk deploy -c iam_user_name=<i>your-iam-user-name</i> --all |
| 69 | +</pre> |
| 70 | + |
| 71 | +To add additional dependencies, for example other CDK libraries, just add |
| 72 | +them to your `setup.py` file and rerun the `pip install -r requirements.txt` |
| 73 | +command. |
| 74 | + |
| 75 | +## Clean Up |
| 76 | + |
| 77 | +Delete the CloudFormation stack by running the below command. |
| 78 | + |
| 79 | +<pre> |
| 80 | +(.venv) $ cdk destroy -c iam_user_name=<i>your-iam-user-name</i> --force --all |
| 81 | +</pre> |
| 82 | + |
| 83 | +## Useful commands |
| 84 | + |
| 85 | + * `cdk ls` list all stacks in the app |
| 86 | + * `cdk synth` emits the synthesized CloudFormation template |
| 87 | + * `cdk deploy` deploy this stack to your default AWS account/region |
| 88 | + * `cdk diff` compare deployed stack with current state |
| 89 | + * `cdk docs` open CDK documentation |
| 90 | + |
| 91 | +Enjoy! |
| 92 | + |
| 93 | +## Run Tests |
| 94 | + |
| 95 | +#### Step 1: Ingest some sample data |
| 96 | + |
| 97 | +First, get the ingestion URL from the **Pipeline settings** page: |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | +Then, ingest some sample data. The following sample request uses [awscurl](https://github.com/okigan/awscurl) to send a single log file to the `my_logs` index: |
| 102 | + |
| 103 | +<pre> |
| 104 | +$ awscurl --service osis --region <i>us-east-1</i> \ |
| 105 | + -X POST \ |
| 106 | + -H "Content-Type: application/json" \ |
| 107 | + -d '[{"time":"2014-08-11T11:40:13+00:00","remote_addr":"122.226.223.69","status":"404","req |
| 108 | +uest":"GET http://www.k2proxy.com//hello.html HTTP/1.1","http_user_agent":"Mozilla/4.0 (compatible; WOW64; SLCC2;)"}]' \ |
| 109 | +https://<i>{pipeline-endpoint}.us-east-1</i>.osis.amazonaws.com/log-pipeline/test_ingestion_path |
| 110 | +</pre> |
| 111 | + |
| 112 | +You should see a `200 OK` response. |
| 113 | + |
| 114 | +#### Step 2: Query the sample data |
| 115 | + |
| 116 | +Now, query the `my_logs` index to ensure that the log entry was successfully ingested: |
| 117 | + |
| 118 | +<pre> |
| 119 | +$ awscurl --service aoss --region <i>us-east-1</i> \ |
| 120 | + -X GET \ |
| 121 | + https://<i>{collection-id}.us-east-1</i>.aoss.amazonaws.com/my_logs/_search | jq -r '.' |
| 122 | +</pre> |
| 123 | + |
| 124 | +**Sample response:** |
| 125 | + |
| 126 | +<pre> |
| 127 | +{ |
| 128 | + "took": 367, |
| 129 | + "timed_out": false, |
| 130 | + "_shards": { |
| 131 | + "total": 0, |
| 132 | + "successful": 0, |
| 133 | + "skipped": 0, |
| 134 | + "failed": 0 |
| 135 | + }, |
| 136 | + "hits": { |
| 137 | + "total": { |
| 138 | + "value": 1, |
| 139 | + "relation": "eq" |
| 140 | + }, |
| 141 | + "max_score": 1, |
| 142 | + "hits": [ |
| 143 | + { |
| 144 | + "_index": "my_logs", |
| 145 | + "_id": "1%3A0%3ALkidTIgBbiu_ytx_zXnH", |
| 146 | + "_score": 1, |
| 147 | + "_source": { |
| 148 | + "time": "2014-08-11T11:40:13+00:00", |
| 149 | + "remote_addr": "122.226.223.69", |
| 150 | + "status": "404", |
| 151 | + "request": "GET http://www.k2proxy.com//hello.html HTTP/1.1", |
| 152 | + "http_user_agent": "Mozilla/4.0 (compatible; WOW64; SLCC2;)", |
| 153 | + "@timestamp": "2023-05-24T07:16:29.708Z" |
| 154 | + } |
| 155 | + } |
| 156 | + ] |
| 157 | + } |
| 158 | +} |
| 159 | +</pre> |
| 160 | + |
| 161 | +## References |
| 162 | + |
| 163 | + * [Tutorial: Ingesting data into a collection using Amazon OpenSearch Ingestion](https://docs.aws.amazon.com/opensearch-service/latest/developerguide/osis-serverless-get-started.html) |
| 164 | + * [Amazon OpenSearch Ingestion Developer Guide](https://docs.aws.amazon.com/opensearch-service/latest/developerguide/ingestion.html) |
| 165 | + * [Data Prepper](https://opensearch.org/docs/latest/data-prepper/index/) - a server-side data collector capable of filtering, enriching, transforming, normalizing, and aggregating data for downstream analytics and visualization. |
| 166 | + * [Top strategies for high volume tracing with Amazon OpenSearch Ingestion (2023-04-27)](https://aws.amazon.com/blogs/big-data/top-strategies-for-high-volume-tracing-with-amazon-opensearch-ingestion/) |
| 167 | + * [Use cases for Amazon OpenSearch Ingestion |
| 168 | +](https://docs.aws.amazon.com/opensearch-service/latest/developerguide/use-cases-overview.html) - some common use cases for Amazon OpenSearch Ingestion. |
| 169 | + * [Best practices for Amazon OpenSearch Ingestion](https://docs.aws.amazon.com/opensearch-service/latest/developerguide/osis-best-practices.html) |
| 170 | + * [Identity and Access Management for Amazon OpenSearch Serverless](https://docs.aws.amazon.com/opensearch-service/latest/developerguide/security-iam-serverless.html#security_iam_id-based-policy-examples-data-plane.html) |
| 171 | + * [Setting up roles and users in Amazon OpenSearch Ingestion](https://docs.aws.amazon.com/opensearch-service/latest/developerguide/pipeline-security-overview.html) |
| 172 | + * [AWS Signature Version 4 Signing Examples](https://github.com/aws-samples/sigv4a-signing-examples) |
| 173 | + * [awscurl](https://github.com/okigan/awscurl) - curl-like tool with AWS Signature Version 4 request signing. |
| 174 | + |
0 commit comments