Skip to content

Latest commit

 

History

History

webcrawler-datasource

RAG Application CDK Python project!

amazon_kendra_arch

This is Amazon Kendra example for CDK development with Python. In this project, we will crawl web pages from amazon developer guides and make them searchable.

The cdk.json file tells the CDK Toolkit how to execute your app.

This project is set up like a standard Python project. The initialization process also creates a virtualenv within this project, stored under the .venv directory. To create the virtualenv it assumes that there is a python3 (or python for Windows) executable in your path with access to the venv package. If for any reason the automatic creation of the virtualenv fails, you can create the virtualenv manually.

To manually create a virtualenv on MacOS and Linux:

$ python3 -m venv .venv

After the init process completes and the virtualenv is created, you can use the following step to activate your virtualenv.

$ source .venv/bin/activate

If you are a Windows platform, you would activate the virtualenv like this:

% .venv\Scripts\activate.bat

Once the virtualenv is activated, you can install the required dependencies.

(.venv) $ pip install -r requirements.txt

Upload Lambda Layer code

Before deployment, you should uplad zipped code files to s3 like this example:

⚠️ Important: Replace lambda-layer-resources with your s3 bucket name for lambda layer zipped code. :warning: To create a bucket outside of the us-east-1 region, aws s3api create-bucket command requires the appropriate LocationConstraint to be specified in order to create the bucket in the desired region. For more information, see these examples.

⚠️ Make sure you have Docker installed.

(.venv) $ aws s3api create-bucket --bucket lambda-layer-resources --region us-east-1
(.venv) $ cat <requirements-lambda_layer.txt
 > cfnresponse==1.1.2
 > urllib3<2
 > EOF
(.venv) $ docker run -v "$PWD":/var/task "public.ecr.aws/sam/build-python3.10" /bin/sh -c "pip install -r requirements-lambda_layer.txt -t python/lib/python3.10/site-packages/; exit"
(.venv) $ zip -r cfnresponse-lib.zip python > /dev/null
(.venv) $ aws s3 cp cfnresponse-lib.zip s3://lambda-layer-resources/pylambda-layer/

For more information about how to create a package for Amazon Lambda Layer, see here.

Deploy

Before to synthesize the CloudFormation template for this code, you should update cdk.context.json file.
In particular, you need to fill the s3 location of the previously created lambda lay codes.

For example,

{
  "lambda_layer_lib_s3_path": "s3://lambda-layer-resources/pylambda-layer/cfnresponse-lib.zip",
  "kendra_index": {
    "name": "webcrawl-index",
    "edition": "ENTERPRISE_EDITION"
  },
  "kendra_data_source": {
    "name": "webcrawl-ds",
    "web_crawler_configuration": {
      "site_maps": [
        "https://docs.aws.amazon.com/lex/latest/dg/sitemap.xml",
        "https://docs.aws.amazon.com/kendra/latest/dg/sitemap.xml",
        "https://docs.aws.amazon.com/sagemaker/latest/dg/sitemap.xml"
      ],
      "url_inclusion_patterns": [
        ".*https://docs.aws.amazon.com/lex/.*",
        ".*https://docs.aws.amazon.com/kendra/.*",
        ".*https://docs.aws.amazon.com/sagemaker/.*"
      ]
    }
  }
}

Now you are ready to synthesize the CloudFormation template for this code.

At this point you can now synthesize the CloudFormation template for this code.

(.venv) $ export CDK_DEFAULT_ACCOUNT=$(aws sts get-caller-identity --query Account --output text)
(.venv) $ export CDK_DEFAULT_REGION=$(aws configure get region)
(.venv) $ cdk synth --all

Use cdk deploy command to create the stack shown above.

(.venv) $ cdk deploy --require-approval never --all

Or, we can provision each CDK stack one at a time like this:

Step 1: List all CDK Stacks

(.venv) $ cdk list
KendraIndexStack
KendraDataSourceStack
KendraDSSyncLambdaStack
KendraDSSyncStack

Step 2: Create Amazon Kendra

(.venv) $ cdk deploy --require-approval never RAGwithKendraIndexStack RAGwithKendraDataSourceStack

Step 3: Ingest documents into Amazon Kendra

(.venv) $ cdk deploy --require-approval never RAGwithKendraDSSyncLambdaStack RAGwithKendraDSSyncStack

⚠️ Launching the Amazon Kendra stacks (i.e., Step 2, 3) requires about 30 minutes followed by about 15 minutes to synchronize it and ingest the data in the index. Therefore, wait for about 45 minutes after launching the stacks. Note the index ID on the KendraIndexStack’s Outputs tab.

ℹ️ To add additional dependencies, for example other CDK libraries, just add them to your setup.py file and rerun the pip install -r requirements.txt command.

Once all CDK stacks have been successfully created, we can search documents with Amazon Kendra like this:

amazon-kendra-search-demo

Clean Up

Delete the CloudFormation stack by running the below command.

(.venv) $ cdk destroy --force --all

Useful commands

  • cdk ls list all stacks in the app
  • cdk synth emits the synthesized CloudFormation template
  • cdk deploy deploy this stack to your default AWS account/region
  • cdk diff compare deployed stack with current state
  • cdk docs open CDK documentation

Enjoy!

References

Troubleshooting