Accelerate Generative AI Inference with NVIDIA NIM Microservices on Amazon SageMaker

Accelerate Generative AI Inference with NVIDIA NIM Microservices on Amazon SageMaker

This post is co-written with Eliuth Triana, Abhishek Sawarkar, Jiahong Liu, Kshitiz Gupta, JR Morgan and Deepika Padmanabhan from NVIDIA. 

At the 2024 NVIDIA GTC conference, we announced support for NVIDIA NIM Inference Microservices in Amazon SageMaker Inference. This integration allows you to deploy industry-leading large language models (LLMs) on SageMaker and optimize their performance and cost. The optimized prebuilt containers enable the deployment of state-of-the-art LLMs in minutes instead of days, facilitating their seamless integration into enterprise-grade AI applications.

NIM is built on technologies like NVIDIA TensorRT, NVIDIA TensorRT-LLM, and vLLM. NIM is engineered to enable straightforward, secure, and performant AI inferencing on NVIDIA GPU-accelerated instances hosted by SageMaker. This allows developers to take advantage of the power of these advanced models using SageMaker APIs and just a few lines of code, accelerating the deployment of cutting-edge AI capabilities within their applications.

NIM, part of the NVIDIA AI Enterprise software platform listed on AWS Marketplace, is a set of inference microservices that bring the power of state-of-the-art LLMs to your applications, providing natural language processing (NLP) and understanding capabilities, whether you’re developing chatbots, summarizing documents, or implementing other NLP-powered applications. You can use pre-built NVIDIA containers to host popular LLMs that are optimized for specific NVIDIA GPUs for quick deployment. Companies like Amgen, A-Alpha Bio, Agilent, and Hippocratic AI are among those using NVIDIA AI on AWS to accelerate computational biology, genomics analysis, and conversational AI.

In this post, we provide a walkthrough of how customers can use generative artificial intelligence (AI) models and LLMs using NVIDIA NIM integration with SageMaker. We demonstrate how this integration works and how you can deploy these state-of-the-art models on SageMaker, optimizing their performance and cost.

You can use the optimized pre-built NIM containers to deploy LLMs and integrate them into your enterprise-grade AI applications built with SageMaker in minutes, rather than days. We also share a sample notebook that you can use to get started, showcasing the simple APIs and few lines of code required to harness the capabilities of these advanced models.

Solution overview

Getting started with NIM is straightforward. Within the NVIDIA API catalog, developers have access to a wide range of NIM optimized AI models that you can use to build and deploy your own AI applications. You can get started with prototyping directly in the catalog using the GUI (as shown in the following screenshot) or interact directly with the API for free.

To deploy NIM on SageMaker, you need to download NIM and subsequently deploy it. You can initiate this process by choosing Run Anywhere with NIM for the model of your choice, as shown in the following screenshot.

You can sign up for the free 90-day evaluation license on the API Catalog by signing up with your organization email address. This will grant you a personal NGC API key for pulling the assets from NGC and running on SageMaker. For pricing details on SageMaker, refer to Amazon SageMaker pricing.

Prerequisites

As a prerequisite, set up an Amazon SageMaker Studio environment:

  1. Make sure the existing SageMaker domain has Docker access enabled. If not, run the following command to update the domain:
# update domain
aws --region region 
    sagemaker update-domain --domain-id domain-id 
    --domain-settings-for-update '{"DockerSettings": {"EnableDockerAccess": "ENABLED"}}'
  1. After Docker access is enabled for the domain, create a user profile by running the following command:
aws --region region sagemaker create-user-profile 
    --domain-id domain-id 
    --user-profile-name user-profile-name
  1. Create a JupyterLab space for the user profile you created.
  2. After you create the JupyterLab space, run the following bash script to install the Docker CLI.

Set up your Jupyter notebook environment

For this series of steps, we use a SageMaker Studio JupyterLab notebook. You also need to attach an Amazon Elastic Block Store (Amazon EBS) volume of at least 300 MB in size, which you can do in the domain settings for SageMaker Studio. In this example, we use an ml.g5.4xlarge instance, powered by a NVIDIA A10G GPU.

We start by opening the example notebook provided on our JupyterLab instance, import the corresponding packages, and set up the SageMaker session, role, and account information:

import boto3, json, sagemaker, time
from sagemaker import get_execution_role
from pathlib import Path

sess = boto3.Session()
sm = sess.client("sagemaker")
client = boto3.client("sagemaker-runtime")
region = sess.region_name
sts_client = sess.client('sts')
account_id = sts_client.get_caller_identity()['Account']

Pull the NIM container from the public container to push it to your private container

The NIM container that comes with SageMaker integration built in is available in the Amazon ECR Public Gallery. To deploy it on your own SageMaker account securely, you can pull the Docker container from the public Amazon Elastic Container Registry (Amazon ECR) container maintained by NVIDIA and re-upload it to your own private container:

%%bash --out nim_image
public_nim_image="public.ecr.aws/nvidia/nim:llama3-8b-instruct-1.0.0"
nim_model="nim-llama3-8b-instruct"
docker pull ${public_nim_image} 
account=$(aws sts get-caller-identity --query Account --output text)
region=${region:-us-east-1}
nim_image="${account}.dkr.ecr.${region}.amazonaws.com/${nim_model}"
# If the repository doesn't exist in ECR, create it.
aws ecr describe-repositories --repository-names "${nim_image}" --region "${region}" > /dev/null 2>&1
if [ $? -ne 0 ]
then
    aws ecr create-repository --repository-name "${nim_image}" --region "${region}" > /dev/null
fi
# Get the login command from ECR and execute it directly
aws ecr get-login-password --region "${region}" | docker login --username AWS --password-stdin "${account}".dkr.ecr."${region}".amazonaws.com
docker tag ${public_nim_image} ${nim_image}
docker push ${nim_image}
echo -n ${nim_image}
gi

Set up the NVIDIA API key

NIMs can be accessed using the NVIDIA API catalog. You just need to register for an NVIDIA API key from the NGC catalog by choosing Generate Personal Key.

When creating an NGC API key, choose at least NGC Catalog on the Services Included dropdown menu. You can include more services if you plan to reuse this key for other purposes.

For the purposes of this post, we store it in an environment variable:

NGC_API_KEY = YOUR_KEY

This key is used to download pre-optimized model weights when running the NIM.

Create your SageMaker endpoint

We now have all the resources prepared to deploy to a SageMaker endpoint. Using your notebook after setting up your Boto3 environment, you first need to make sure you reference the container you pushed to Amazon ECR in an earlier step:

sm_model_name = "nim-llama3-8b-instruct"
container = {
    "Image": nim_image,
    "Environment": {"NGC_API_KEY": NGC_API_KEY}
}
create_model_response = sm.create_model(
    ModelName=sm_model_name, ExecutionRoleArn=role, PrimaryContainer=container
)

print("Model Arn: " + create_model_response["ModelArn"])

After the model definition is set up correctly, the next step is to define the endpoint configuration for deployment. In this example, we deploy the NIM on one ml.g5.4xlarge instance:

endpoint_config_name = sm_model_name

create_endpoint_config_response = sm.create_endpoint_config(
    EndpointConfigName=endpoint_config_name,
    ProductionVariants=[
        {
            "InstanceType": "ml.g5.4xlarge",
            "InitialVariantWeight": 1,
            "InitialInstanceCount": 1,
            "ModelName": sm_model_name,
            "VariantName": "AllTraffic",
            "ContainerStartupHealthCheckTimeoutInSeconds": 850
        }
    ],
)

print("Endpoint Config Arn: " + create_endpoint_config_response["EndpointConfigArn"])

Lastly, create the SageMaker endpoint:

endpoint_name = sm_model_name

create_endpoint_response = sm.create_endpoint(
    EndpointName=endpoint_name, EndpointConfigName=endpoint_config_name
)

print("Endpoint Arn: " + create_endpoint_response["EndpointArn"])

Run inference against the SageMaker endpoint with NIM

After the endpoint is deployed successfully, you can run requests against the NIM-powered SageMaker endpoint using the REST API to try out different questions and prompts to interact with the generative AI models:

messages = [
    {"role": "user", "content": "Hello! How are you?"},
    {"role": "assistant", "content": "Hi! I am quite well, how can I help you today?"},
    {"role": "user", "content": "Write a short limerick about the wonders of GPU Computing."}
]
payload = {
  "model": "meta/llama3-8b-instruct",
  "messages": messages,
  "max_tokens": 100
}


response = client.invoke_endpoint(
    EndpointName=endpoint_name, ContentType="application/json", Body=json.dumps(payload)
)

output = json.loads(response["Body"].read().decode("utf8"))
print(json.dumps(output, indent=2))

That’s it! You now have an endpoint in service using NIM on SageMaker.

NIM licensing

NIM is part of the NVIDIA Enterprise License. NIM comes with a 90-day evaluation license to start with. To use NIMs on SageMaker beyond the 90-day license, connect with NVIDIA for AWS Marketplace private pricing. NIM is also available as a paid offering as part of the NVIDIA AI Enterprise software subscription available on AWS Marketplace

Conclusion

In this post, we showed you how to get started with NIM on SageMaker for pre-built models. Feel free to try it out following the example notebook.

We encourage you to explore NIM to adopt it to benefit your own use cases and applications.


About the Authors

Saurabh Trikande is a Senior Product Manager for Amazon SageMaker Inference. He is passionate about working with customers and is motivated by the goal of democratizing machine learning. He focuses on core challenges related to deploying complex ML applications, multi-tenant ML models, cost optimizations, and making deployment of deep learning models more accessible. In his spare time, Saurabh enjoys hiking, learning about innovative technologies, following TechCrunch, and spending time with his family.

James Park is a Solutions Architect at Amazon Web Services. He works with Amazon.com to design, build, and deploy technology solutions on AWS, and has a particular interest in AI and machine learning. In his spare time, he enjoys seeking out new cultures, new experiences, and staying up to date with the latest technology trends. You can find him on LinkedIn.

Qing Lan is a Software Development Engineer in AWS. He has been working on several challenging products in Amazon, including high performance ML inference solutions and high-performance logging systems. Qing’s team successfully launched the first billion-parameter model in Amazon Advertising with very low latency required. Qing has in-depth knowledge on infrastructure optimization and deep learning acceleration.

Raghu Ramesha is a Senior GenAI/ML Solutions Architect on the Amazon SageMaker Service team. He focuses on helping customers build, deploy, and migrate ML production workloads to SageMaker at scale. He specializes in machine learning, AI, and computer vision domains, and holds a master’s degree in computer science from UT Dallas. In his free time, he enjoys traveling and photography.

Eliuth Triana is a Developer Relations Manager at NVIDIA empowering Amazon’s AI MLOps, DevOps, Scientists and AWS technical experts to master the NVIDIA computing stack for accelerating and optimizing Generative AI Foundation models spanning from data curation, GPU training, model inference and production deployment on AWS GPU instances. In addition, Eliuth is a passionate mountain biker, skier, tennis and poker player.

Abhishek Sawarkar is a product manager in the NVIDIA AI Enterprise team working on integrating NVIDIA AI Software in Cloud MLOps platforms. He focuses on integrating the NVIDIA AI end-to-end stack within Cloud platforms & enhancing user experience on accelerated computing.

Jiahong Liu is a Solutions Architect on the Cloud Service Provider team at NVIDIA. He assists clients in adopting machine learning and AI solutions that leverage NVIDIA-accelerated computing to address their training and inference challenges. In his leisure time, he enjoys origami, DIY projects, and playing basketball.

Kshitiz Gupta is a Solutions Architect at NVIDIA. He enjoys educating cloud customers about the GPU AI technologies NVIDIA has to offer and assisting them with accelerating their machine learning and deep learning applications. Outside of work, he enjoys running, hiking, and wildlife watching.

JR Morgan is a Principal Technical Product Manager in NVIDIA’s Enterprise Product Group, thriving at the intersection of partner services, APIs, and open source. After work, he can be found on a Gixxer, at the beach, or spending time with his amazing family.

Deepika Padmanabhan is a Solutions Architect at NVIDIA. She enjoys building and deploying NVIDIA’s software solutions in the cloud. Outside work, she enjoys solving puzzles and playing video games like Age of Empires.

Read More

Celebrating the final AWS DeepRacer  League championship and road ahead

Celebrating the final AWS DeepRacer League championship and road ahead

The AWS DeepRacer League is the world’s first autonomous racing league, open to everyone and powered by machine learning (ML). AWS DeepRacer brings builders together from around the world, creating a community where you learn ML hands-on through friendly autonomous racing competitions. As we celebrate the achievements of over 560,000 participants from more than 150 countries who sharpened their skills through the AWS DeepRacer League over the last 6 years, we also prepare to close this chapter with a final season that serves as both a victory lap and a launching point for what’s next in the world of AWS DeepRacer.

The legacy of AWS DeepRacer

The AWS DeepRacer community is the heartbeat of the league, where enthusiasts and league legends help foster learning for a global network of AWS DeepRacer participants at any stage of their ML journey. When we launched AWS DeepRacer in 2018, we set out to make ML model training concepts more accessible.

By removing common hurdles associated with the preparation of training and evaluating ML models, AWS DeepRacer gives builders a fun way to focus on fundamental training, evaluation, and model performance concepts, all without any prior experience.

The impact of racing in the league goes far beyond the podium and prizes, with many participants using their AWS DeepRacer experience and community support to advance their careers.

“Embracing the challenges of AWS DeepRacer has not only sharpened my technical skills but has also opened doors to new roles, where innovation and agility are key. Every lap on the track is a step closer to mastering the tools that drive modern solutions, making me ready for the future of technology.”

– AWS DeepRacer League veteran Daryl Jezierski, Lead Site Reliability Engineer at The Walt Disney Company.

Each year, hundreds of AWS customers such as JPMorgan and Chase, Vodafone, and Eviden host AWS DeepRacer events to upskill their employees in the fundamentals of ML through collaborative gamified education.

The transition to an AWS Solution

While the AWS DeepRacer League will no longer be a globally hosted competition by AWS in 2025, you can continue to access the AWS DeepRacer service for training, evaluation, and community racing on the AWS Management Console until December 2025.

Starting in early 2025, the AWS DeepRacer source code will also become available as an AWS Solution; an off-the-shelf deployment of the underlying AWS services, code, and configurations that make up the AWS DeepRacer service. In the short term, this provides you with the option to choose the AWS DeepRacer experience that works best for your organizational needs. The new solution retains all existing AWS DeepRacer console features to train reinforcement learning models using Amazon SageMaker, evaluate models in a simulated 3D environment, as well as race admin controls such as creating, hosting, and managing global races. The new AWS Solution now offers even more flexibility, enabling organizations to provide ML education to employees at scale while choosing the best optimizations for cost and convenience to meet your needs.

AWS DeepRacer continues to be the fastest way to get started with ML training fundamentals, with tens of thousands of builders using AWS DeepRacer programs within their organizations in 2024 alone. In addition to our customers using AWS DeepRacer to kickstart their ML transformation efforts, many of them have told us they are eager for their teams to apply their new skills to solve real business problems with artificial intelligence (AI).

To help them on the next step of their journey, we are launching four new AWS DeepRacer workshops focused on generative AI at AWS re:Invent 2024. These 200 and 300 level hands-on sessions bridge the fundamental concepts of ML using AWS DeepRacer with foundation model training and fine-tuning techniques using AWS services such as SageMaker and Amazon Bedrock for popular industry use cases. In addition, all four workshops will be made available off the shelf alongside the managed AWS DeepRacer solution beginning in 2025.

The road to re:Invent

As the final AWS DeepRacer League races towards a thrilling conclusion, all eyes are on the last heat of the season. In the 2024 League, a heat spans two monthly races, with top racers from each of the six global regions earning a trip to compete in the championships at re:Invent based on their cumulative performance over both races. September marks the launch of the fourth and final heat, the only remaining path for league hopefuls to earn the coveted expenses-paid trip to compete for this year’s record-breaking $50,000 championship prize purse. If you don’t earn a spot during the regular season, you’ll still have one opportunity to make it through by racing live in person during the last-chance qualifying round on December 2 in Las Vegas. For those skilled enough to make it into this year’s championship, the stakes have never been higher. Thirty-two racers will compete for the title of 2024 AWS DeepRacer Champion and a whopping $25,000 first place cash prize.

The destination may be glamorous, but the road to re:Invent is just as sweet—with loads of prizes still up for grabs in each of the six global competition regions. In both September and October, the top 50 and top 3 winners in each region will claim $99 and $250 amazon.com gift cards, respectively. In addition, the first 2,000 eligible racers to submit to the league globally each month will receive $30 in AWS credits.

Don’t miss your chance to be part of AWS DeepRacer history, build your ML skills, collaborate with a global community, and win big. Race in the 2024 AWS DeepRacer League today!


About the Author

Shashank Murthy is a Senior Product Marketing Manager with AWS Machine Learning. His goal is to make it machine learning more accessible to builders through hands-on educational experiences. For fun outside work, Shashank likes to hike the Pacific Northwest, play soccer, and run obstacle course races.

Read More

Provide a personalized experience for news readers using Amazon Personalize and Amazon Titan Text Embeddings on Amazon Bedrock

Provide a personalized experience for news readers using Amazon Personalize and Amazon Titan Text Embeddings on Amazon Bedrock

News publishers want to provide a personalized and informative experience to their readers, but the short shelf life of news articles can make this quite difficult. In news publishing, articles typically have peak readership within the same day of publication. Additionally, news publishers frequently publish new articles and want to show these articles to interested readers as quickly as possible. This poses challenges for interaction-based recommender system methodologies such as collaborative filtering and the deep learning-based approaches used in Amazon Personalize, a managed service that can learn user preferences from their past behavior and quickly adjust recommendations to account for changing user behavior in near real time.

News publishers typically don’t have the budget or the staff to experiment with in-house algorithms, and need a fully managed solution. In this post, we demonstrate how to provide high-quality recommendations for articles with short shelf lives by using text embeddings in Amazon Bedrock. Amazon Bedrock a fully managed service that offers a choice of high-performing foundation models (FMs) from leading artificial intelligence (AI) companies like AI21 Labs, Anthropic, Cohere, Meta, Mistral AI, Stability AI, and Amazon through a single API, along with a broad set of capabilities to build generative AI applications with security, privacy, and responsible AI.

Embeddings are a mathematical representation of a piece of information such as a text or an image. Specifically, they are a vector or ordered list of numbers. This representation helps capture the meaning of the image or text in such a way that you can use it to determine how similar images or text are to each other by taking their distance from each other in the embedding space. For our post, we use the Amazon Titan Text Embeddings model.

Solution overview

By combining the benefits of Amazon Titan Text Embeddings on Amazon Bedrock with the real-time nature of Amazon Personalize, we can recommend articles to interested users in an intelligent way within seconds of the article being published. Although Amazon Personalize can provide articles shortly after they’re published, it generally takes a few hours (and a filter to select items from the correct time frame) to surface items to the right users. For our use case, we want to recommend articles immediately after they’re published.

The following diagram shows the architecture of the solution and the high-level steps of the workflow. The architecture follows AWS best practices to use managed and serverless services where possible.

The workflow consists of the following steps:

  1. A trigger invokes an AWS Lambda function every time a new article is published, which runs Steps 2–5.
  2. A text embedding model hosted on Amazon Bedrock creates an embedding of the text of the article.
  3. An Amazon SageMaker hosted model assigns the article to a cluster of similar articles.
  4. An Amazon Bedrock hosted model can also generate headlines and summaries of the new article if needed.
  5. The new articles are added to Amazon DynamoDB with information on their type and when they were published, with a Time-To-Live (TTL) representing when the articles are no longer considered breaking news.
  6. When users arrive at the website, their requests are processed by Amazon API Gateway.
  7. API Gateway makes a request to Amazon Personalize to learn what individual articles and article types a reader is most interested in, which can be directly shown to the reader.
  8. To recommend breaking news articles, a call is made to DynamoDB to determine what articles have been recently published of each type. This allows newly published articles to be shown to interested readers in seconds.
  9. As users read articles, their interactions are streamed using Amazon Kinesis Data Streams to an Amazon Personalize event tracker.
  10. The Amazon Personalize event tracker updates the deployed personalization models within 1–2 seconds.

Prerequisites

To implement the proposed solution, you should have the following:

  • An AWS account and familiarity with Amazon Personalize, SageMaker, DynamoDB, and Amazon Bedrock.
  • The Amazon Titan Text Embeddings V2 model enabled on Amazon Bedrock. You can confirm it’s enabled on the Model access page of the Amazon Bedrock console. If Amazon Titan Text Embeddings is enabled, the access status will show as Access granted, as shown in the following screenshot. You can enable access to the model by choosing Manage model access, selecting Amazon Titan Text Embeddings V2, and then choosing Save Changes.

Create embeddings of the text of previously published articles

First, you need to load a set of historically published articles so you have a history of user interactions with those articles and then create embeddings for them using Amazon Titan Text Embeddings. AWS also has machine learning (ML) services that can perform tasks such as translation, summarization, and the identification of an article’s tags, title, or genre, if required. The following code snippet shows how to generate embeddings using Amazon Titan Text Embeddings:

def titan_embeddings(text, bedrock_client):
    prompt = f"{text}"
    body = json.dumps({
        "inputText": prompt,
    })
        
    model_id = 'amazon.titan-embed-text-v2:0'
    accept = 'application/json' 
    content_type = 'application/json'
        
    response = bedrock_client.invoke_model(
        body=body, 
        modelId=model_id, 
        accept=accept, 
        contentType=content_type
    )
        
    response_body = json.loads(response['body'].read())
    return response_body.get('embedding')

Train and deploy a clustering model

Next, you deploy a clustering model for the historical articles. A clustering model identifies clusters of article embeddings and assigns each cluster an ID. In this case, we use a k-means model hosted on SageMaker, but you can use a different clustering approach if you prefer.

The following code snippet is an example of how to create a list of the text embeddings using the Python function above and then train a k-means cluster for article embeddings. In this case, the choice of 100 clusters is arbitrary. You should experiment to find a number that is best for your use case. The instance type represents the Amazon Elastic Compute Cloud (Amazon EC2) compute instance that runs the SageMaker k-means training job. For detailed information on which instance types fit your use case and their performance capabilities, see Amazon EC2 Instance types. For information about pricing for these instance types, see Amazon EC2 Pricing. For information about available SageMaker notebook instance types, see CreateNotebookInstance. For most experimentation, you should use an ml.t3.medium instance. This is the default instance type for CPU-based SageMaker images, and is available as part of the AWS Free Tier.

text_embeddings_list = []
for text in text_list:
    text_embeddings_list.append(titan_embeddings(text, bedrock_client))

num_clusters = 100

kmeans = KMeans(
    role=role,
    instance_count=1,
    instance_type="ml.t3.medium",
    output_path="s3://your_unique_s3bucket_name/",
    k=num_clusters,
    num_trials=num_clusters,
    epochs=10
)

kmeans.fit(kmeans.record_set(np.asarray(text_embeddings_list, dtype=np.float32)))

After you finish training and deploying the clustering model, you can assign a cluster ID to each of the historical articles by passing their embeddings through the k-means (or other) clustering model. Also, importantly, you assign clusters to any articles you consider breaking news (article shelf life can vary from a couple of days to a couple of hours depending on the publication).

Set up a DynamoDB table

The next step of the process is to set up a DynamoDB table to contain the breaking news articles, their identifiers, and their clusters. This DynamoDB table will help you later when you try to query the mapping of the article item ID with the cluster ID.

The breaking news table has the following attributes:

  • Article cluster ID – An initial cluster ID
  • Article ID – The ID of the article (numeric for this example)
  • Article timestamp – The time when the article was created
  • Article genre – The genre of article, such as tech, design best practices, and so on
  • Article language – A two-letter language code of the article
  • Article text – The actual article text

The article cluster ID is the partition key and the article timestamp (in Unix Epoch Time) is the sort key for the breaking news table.

Update the article interactions dataset with article clusters

When you’re creating your Amazon Personalize user personalization campaign, the item interactions dataset represents the user interactions history with your items. For our use case, we train our recommender on the article clusters instead of the individual articles. This will give the model the opportunity to recommend based on the cluster-level interactions and understand user preferences to article types as opposed to individual articles. That way, when a new article is published, we simply have to identify what type of article it is, and we can immediately recommend it to interested users.

To do so, you need to update the interactions dataset, replacing the individual article ID with the cluster ID of the article and store the item interactions dataset in an Amazon Simple Storage Service (Amazon S3) bucket, at which point it can be brought into Amazon Personalize.

Create an Amazon Personalize user personalization campaign

The USER_PERSONALIZATION recipe generates a list of recommendations for a specific user subject to the constraints of filters added to it. This is useful for populating home pages of websites and subsections where specific article types, products, or other pieces of content are focused on. Refer to the following Amazon Personalize user personalization sample on GitHub for step-by-step instructions to create a user personalization model.

The steps in an Amazon Personalize workflow are as follows:

  1. Create a dataset group.
  2. Prepare and import data.
  3. Create recommenders or custom resources.
  4. Get recommendations.

To create and deploy a user personalization campaign, you first need to create a user personalization solution. A solution is a combination of a dataset group and a recipe, which is basically a set of instructions for Amazon Personalize for how to prepare a model to solve a specific type of business use case. After this, you train a solution version, then deploy it as a campaign.

This following code snippet shows how to create a user personalization solution resource:

create_solution_response = personalize.create_solution (
    name = "personalized-articles-solution”,
    datasetGroupArn = dataset_group_arn,
    recipeArn = "arn:aws:personalize:::recipe/aws-user-personalization-v2",
)
solution_arn = create_solution_response['solutionArn']

The following code snippet shows how to create a user personalization solution version resource:

create_solution_version_response = personalize.create_solution_version(
   solutionArn = solution_arn
)
solution_version_arn = create_solution_version_response['solutionVersionArn']

The following code snippet shows how to create a user personalization campaign resource:

create_campaign_response = personalize.create_campaign (
   name = "personalized-articles-campaign”,
   solutionVersionArn = solution_version_arn,
)
campaign_arn = create_campaign_response['campaignArn']

Deliver a curated and hyper-personalized breaking news experience

Articles for the breaking news section of the front page can be drawn from the Amazon Personalize campaign you trained on the article clusters in the previous section. This model identifies the types of articles aligned with each user’s preferences and interests.

The articles of this type can then be obtained by querying DynamoDB for all articles of that type, then selecting the most recent ones of each relevant type. This solution also allows the editorial team a degree of curation over the diversity of articles shown to individual users. This makes sure users can see the breadth of content available on the site and see a diverse array of perspectives while still having a hyper-personalized experience.

This is accomplished by setting a maximum number of articles that can be shown per type (a value that can be determined experimentally or by the editorial team). The most recently published articles, up to the maximum, can be selected from each cluster until the desired number of articles is obtained.

The following Python function obtains the most recently published articles (as measured by their timestamp) in the article cluster. In production, the individual articles should have a TTL representing the shelf life of the articles. The following code assumes the article IDs are numeric and increase over time. If you want to use string values for your article IDs and the article’s timestamp as the sort key for this table, you’ll need to adjust the code.

The following arguments are passed to the function:

  • cluster (str or int) – A string or integer representing the cluster in question for which we want to obtain the list of interested users
  • dynamo_client – A Boto3 DynamoDB client
  • table_name (str) – The table name of the DynamoDB table in which we store the information
  • index_name (str) – The name of the index
  • max_per_cluster (int) – The maximum number of items to pull per cluster
def query_dynamo_db_articles(
	cluster,
	index_name, 
	dynamo_client, 
	table_name, 
	max_per_cluster):

	arguments = {
		"TableName": table_name,
		"IndexName" : index_name,
		"ScanIndexForward": False,
		"KeyConditionExpression": "articleClusterId = :V1",
		"ExpressionAttributeValues": {
		":V1": {"S": str(cluster)}
	},
        "Limit": max_per_cluster
}

return dynamo_client.query(**arguments)

Using the preceding function, the following function selects the relevant articles in each cluster recommended by the Amazon Personalize user personalization model that we created earlier and continues iterating through each cluster until it obtains the maximum desired number of articles. Its arguments are as follows:

  • personalize_runtime – A Boto3 client representing Amazon Personalize Runtime
  • personalize_campaign – The campaign ARN generated when you deployed the user personalization campaign
  • user_id (str) – The user ID of the reader
  • dynamo_client – A Boto3 DynamoDB client
  • table_name (str) – The table name of the DynamoDB table storing the information
  • index_name (str) – The name of the index
  • max_per_cluster (str) – The maximum number of articles to pull per cluster
  • desired_items (int) – The total number of articles to return
def breaking_news_cluster_recommendation(personalize_runtime,
	personalize_campaign, 
	user_id,
	dynamo_client, 
	table_name,
	index_name,
	max_per_cluster,
	desired_items):


	recommendation = personalize_runtime.get_recommendations(
		campaignArn=personalize_campaign, 
		userId=user_id
	) # Returns recommended clusterId list

	item_count = 0
	item_list = []

	for cluster_number in recommendation['itemList']:
		cluster = cluster_number['itemId']
		dynamo_query_response = query_dynamo_db_articles(
			cluster,
			index_name,
			dynamo_client,
			table_name,
			max_per_cluster
		)

		for item in dynamo_query_response['Items']:
			item_list.append(item)
			item_count += 1
			if item_count == desired_items:
				break
			if item_count == desired_items:
				break
				
	return item_list

Keep recommendations up to date for users

When users interact with an article, the interactions are sent to an event tracker. However, unlike a typical Amazon Personalize deployment, in this case we send an interaction as if it occurred with the cluster the article is a member of. There are several ways to do this; one is to embed the article’s cluster in its metadata along with the article ID so they can be fed back to the event tracker. Another is to look up the article’s cluster using its ID in some form of lightweight cache (or key-value database).

Whichever way you choose, after you obtain the article’s cluster, you stream in an interaction with it using the event tracker.

The following code snippet sets up the event tracker:

create_event_tracker_response = personalize.create_event_tracker(
    name = event_tracker_name,
    datasetGroupArn=dataset_group_arn
)

The following code snippet feeds in new interactions to the event tracker:

event_tracker_id = create_event_tracker_response['trackingId']

response = personalize_events.put_events(
    trackingId=event_tracker_id,
    userId=sample_user,
    sessionId=session_id, # a unique id for this users session
    eventList=[]# contains a list of up to 10 item-interactions
)

These new interactions will cause Amazon Personalize to update its recommendations in real time. Let’s see what this looks like in practice.

With a sample dataset derived from the CI&T DeskDrop dataset, a user logging in to their homepage would see these articles. (The dataset is a mixture of Portuguese and English articles; the raw text has been translated but the titles have not. The solution described in this post works for multilingual audiences without requiring separate deployments.) All the articles shown are considered breaking news, meaning we haven’t tracked interactions with them in our dataset and they are being recommended using the clustering techniques described earlier.

However, we can interact with the more technical articles, as shown in the following screenshot.

When we refresh our recommendations, the page is updated.

Let’s change our behavior and interact with articles more about design best practices and career development.

We get the following recommendations.

If we limit the number of articles that we can draw per cluster, we can also enforce a bit more diversity in our recommendations.

As new articles are added as part of the news publishing process, the articles are saved to an S3 bucket first. A Lambda trigger on the bucket invokes a series of steps:

  1. Generate an embedding of the text of the article using the model on Amazon Bedrock.
  2. Determine the cluster ID of the article using the k-means clustering model on SageMaker that you trained earlier.
  3. Store the relevant information on the article in a DynamoDB table.

Clean up

To avoid incurring future charges, delete the resources you created while building this solution:

  1. Delete the SageMaker resources.
  2. Delete the Amazon Personalize resources.
  3. Delete the Amazon DynamoDB tables.

Conclusion

In this post, we described how you can recommend breaking news to a user using AWS AI/ML services. By taking advantage of the power of Amazon Personalize and Amazon Titan Text Embeddings on Amazon Bedrock, you can show articles to interested users within seconds of them being published.

As always, AWS welcomes your feedback. Leave your thoughts and questions in the comments section. To learn more about the services discussed in this blog, you can sign up for an AWS Skill Builder account, where you can find free digital courses on Amazon Personalize, Amazon Bedrock, Amazon SageMaker and other AWS services.


About the Authors

Eric Bolme is a Specialist Solution Architect with AWS based on the East Coast of the United States. He has 8 years of experience building out a variety of deep learning and other AI use cases and focuses on Personalization and Recommendation use cases with AWS.

Joydeep Dutta is a Principal Solutions Architect at AWS. Joydeep enjoys working with AWS customers to migrate their workloads to the cloud, optimize for cost, and help with architectural best practices. He is passionate about enterprise architecture to help reduce cost and complexity in the enterprise. He lives in New Jersey and enjoys listening to music and enjoying the outdoors in his spare time.

Read More

Implementing tenant isolation using Agents for Amazon Bedrock in a multi-tenant environment

Implementing tenant isolation using Agents for Amazon Bedrock in a multi-tenant environment

The number of generative artificial intelligence (AI) features is growing within software offerings, especially after market-leading foundational models (FMs) became consumable through an API using Amazon Bedrock. Amazon Bedrock is a fully managed service that offers a choice of high-performing foundation models from leading AI companies like AI21 Labs, Anthropic, Cohere, Meta, Stability AI, and Amazon through a single API, along with a broad set of capabilities you need to build generative AI applications with security, privacy, and responsible AI.

Agents for Amazon Bedrock enables software builders to complete actions and tasks based on user input and organization data. A common challenge in multi-tenant offerings, such as software as a service (SaaS) products, is tenant isolation. Tenant isolation makes sure each tenant can access only their own resources—even if all tenants run on shared infrastructure.

You can isolate tenants in an application using different multi-tenant architecture patterns. In some cases, isolation can be achieved by having entire stacks of resources dedicated to one tenant (silo model) with coarse-grained policies to prevent cross-tenant access. In other scenarios, you might have pooled resources (such as one database table containing rows from different tenants) that require fine-grained policies to control access. Oftentimes, Amazon Web Services (AWS) customers design their applications using a mix of both models to balance the models’ tradeoffs.

Isolating tenants in a pooled model is achieved by using tenant context information in different application components. The tenant context can be injected by an authoritative source, such as the identity provider (IdP) during the authentication of a user. Integrity of the tenant context must be preserved throughout the system to prevent malicious users from acting on behalf of a tenant that they shouldn’t have access to, resulting in potentially sensitive data being disclosed or modified.

FMs act on unstructured data and respond in a probabilistic fashion. These properties make FMs unfit to handle tenant context securely. For example, FMs are susceptible to prompt injection, which can be used by malicious actors to change the tenant context. Instead, tenant context should be securely passed between deterministic components of an application, which can in turn consume FM capabilities, giving the FM only information that is already scoped down to the specific tenant.

In this blog post, you will learn how to implement tenant isolation using Amazon Bedrock agents within a multi-tenant environment. We’ll demonstrate this using a sample multi-tenant e-commerce application that provides a service for various tenants to create online stores. This application uses Amazon Bedrock agents to develop an AI assistant or chatbot capable of providing tenant-specific information, such as return policies and user-specific information like order counts and status updates. This architecture showcases how you can use pooled Amazon Bedrock agents and enforce tenant isolation at both the tenant level for return policy information and the user level for user-related data, providing a secure and personalized experience for each tenant and their users.

Architecture overview

architecture digram

Figure 1: Architecture of the sample AI assistant application

Let’s explore the different components this solution is using.

  1. A tenant user signs in to an identity provider such as Amazon Cognito. They get a JSON Web Token (JWT), which they use for API requests. The JWT contains claims such as the user ID (or subject, sub), which identifies the tenant user, and the tenantId, which defines which tenant the user belongs to.
  2. The tenant user inputs their question into the client application. The client application sends the question to a GraphQL API endpoint provided by AWS AppSync, in the form of a GraphQL mutation. You can learn more about this pattern in the blog post Build a Real-time, WebSockets API for Amazon Bedrock. The client application authenticates to AWS AppSync using the JWT from Amazon Cognito. The user is authorized using the Cognito User Pools integration.
  3. The GraphQL mutation invokes using the EventBridge resolver. The event triggers an AWS Lambda function using an EventBridge rule.
  4. The Lambda function calls the Amazon Bedrock InvokeAgent API. This function uses a tenant isolation policy to scope the permissions and generates tenant specific scoped credentials. More about this can be read in the blog Building a Multi-Tenant SaaS Solution Using AWS Serverless Services. Then, it sends the tenant ID, user ID and tenant specific scoped credentials to this API using the sessionAttributes parameter from the agent’s sessionState.
  5. The Amazon Bedrock agent determines what it needs to do to satisfy the user request by using the reasoning capabilities of the associated large language model (LLM). A variety of LLMs can be used, and for this solution we used Anthropic Claude 3 Sonnet. It passes the sessionAttributes object to an action group determined to help with the request, thereby securely forwarding tenant and user ID for further processing steps.
  6. This Lambda function uses the provided tenant specific scoped credentials and tenant ID to fetch information from Amazon DynamoDB. Tenant configuration data is stored in a single, shared table, while user data is split in one table per tenant. After the correct data is fetched, it’s returned to the agent. The agent interacts with the LLM for the second time to formulate a natural-language answer to the user based on the provided data.
  7. The agent’s response is published as another GraphQL mutation through AWS AppSync.
  8. The client listens to the response using a GraphQL subscription. It renders the response to the user after it’s received from the server.

Note that each component in this sample architecture can be changed to fit into your pre-existing architecture and knowledge in the organization. For example, you might choose to use a WebSocket implementation through Amazon API Gateway instead of using GraphQL or implement a synchronous request and response pattern. Whichever technology stack you choose to use, verify that you securely pass tenant and user context between its different layers. Do not rely on probabilistic components of your stack, such as an LLM, to accurately transmit security information.

How tenant and user data is isolated

This section describes how user and tenant data is isolated when a request is processed throughout the system. Each step is discussed in more detail following the diagram. For each prompt in the UI, the frontend sends the prompt as a mutation request to the AWS AppSync API and listens for the response through a subscription, as explained in step 8 of Figure 1 shown above. The subscription is needed to receive the answer from the prompt, as the agent is invoked asynchronously. Both the request and response are authenticated using Amazon Cognito, and the request’s context, including user and tenant ID, is made available to downstream components.

tenant isolation architecture

Figure 2: User and tenant data isolation

  1. For each prompt created in the sample UI, a unique ID(answerId) is generated. The answerId is needed to correlate the input prompt with the answer from the agent. It uses the Cognito user ID (stored in the sub field in the JWT and accessible as userId in the AWS Amplify SDK) as a prefix to enable fine-grained permissions. This is explained in more depth in step 3. The answerId is generated in the page.tsx file:
const answerId = user?.userId + "." + uuidv4();
  1. The frontend uses the AWS Amplify SDK, which takes care of authenticating the GraqhQL request. This is done for the prompt request (a GraphQL mutation request) and for the response (a GraphQL subscription which listens to an answer to the prompt). The authentication mode is set in the tsx file. Amplify uses the Amazon Cognito user pool it has been configured with. Also, the previously generated answerId is used as a unique identifier for the request.
await client.graphql({
	authMode: "userPool",
    ...
    variables: {
      answerId,
      ...
    },
  });
  1. The frontend sends the GraphQL mutation request and the response is received by the subscription. To correlate the mutation request and response in the subscription, the answerId, generated in Step1, is used. By running the code below in a resolver attached to a subscription, user isolation is enforced. Users cannot subscribe to arbitrary mutations and receive their response. The code verifies that that the userId in the mutation request matches the userId in the response received by the subscription. The ctx variable is populated by AWS AppSync with the request’s payload and metadata such as the user identity.
if (!ctx.args.answerId.startsWith(ctx.identity.sub + ".")) {
  util.unauthorized()
}

Note that the authorization is checked against the cryptographically signed JWT from the Amazon Cognito user pool. Hence, even if a malicious user could tamper with the token locally to change the userId, the authorization check would still fail.

  1. The userId and tenantId (from the AWS AppSync context) is passed on to Amazon EventBridge and to AWS Lambda, which invokes the Agent. The Lambda function gets the user information from the event object in file invokeAgent/index.py:
tenant_id = event["detail"]["identity"]["claims"]["custom:tenantId"]
user_id = event["detail"]["identity"]["claims"]["sub"]

The Lambda function assumes the below IAM role that has permissions scoped down to a specific tenant and generates tenant specific scoped credentials. This role only grants access to DynamoDB items which has the given tenant ID as the leading key.

statements: [
	new PolicyStatement({
		actions: ["dynamodb:Query"],
		resources: [tenantConfigurationTable.tableArn],
		conditions: {
			"ForAllValues:StringEquals": {
				"dynamodb:LeadingKeys": [
					"${aws:PrincipalTag/TenantId}"
				]}}}),
        new PolicyStatement({
actions: ["dynamodb:Query"], resources: ["arn:aws:dynamodb:*:*:table/${aws:PrincipalTag/TenantId}-orders"] }) ]

By using this scoped IAM policy, we enforce tenant isolation. Read more about it the blog Building a Multi-Tenant SaaS Solution Using AWS Serverless Services.

  1. This identity information and tenant specific scoped credentials are passed to the agent through sessionAttributes in the Amazon Bedrock InvokeAgent API call as shown below.
response = client.invoke_agent(
    ...
sessionState={
"sessionAttributes": {
		"tenantId": tenant_id,
		"userId": user_id,
		"accessKeyId": credentials["accessKeyId"],
		"secretAccessKey":credentials["secretAccessKey"],
		"sessionToken": credentials["sessionToken"],
},)

Note that the sessionState object can also contain a promptSessionAttributes parameter. While sessionAttributes persist throughout the entire agent session, promptSessionAttributes only persist for only a single InvokeAgent call. promptSessionAttributes can also be used to dynamically update the agent’s prompt. For more information, see the Amazon Bedrock session context documentation. If you have more complex requirements, you might want to consider building an additional sessions management system.

  1. The sessionAttributes are used within the agent task to grant the agent access to only the database tables and rows for the specific tenant user. The task creates a DynamoDB client using the tenant-scoped credentials. Using the scoped client, it looks up the correct order table name in the tenant configuration and queries the order table for data:
tenant_id = event["sessionAttributes"]["tenantId"]
user_id = event["sessionAttributes"]["userId"]
access_key_id = event["sessionAttributes"]["accessKeyId"]
secret_access_key = event["sessionAttributes"]["secretAccessKey"]
session_token = event["sessionAttributes"]["sessionToken"]

dynamodb = boto3.resource(
        "dynamodb",
        aws_access_key_id=event["sessionAttributes"]["accessKeyId"],
        aws_secret_access_key=event["sessionAttributes"]["secretAccessKey"],
        aws_session_token=event["sessionAttributes"]["sessionToken"],
    )
tenant_config_table_name = os.getenv("TENANT_CONFIG_TABLE_NAME")
tenant_config_table = dynamodb.Table(tenant_config_table_name)

orders_table_name = tenant_config_table.query(
    KeyConditionExpression=Key("tenantId").eq(tenant_id)
)["Items"][0]["ordersTableName"]
...
orders_table.query(KeyConditionExpression=Key("userId").eq(user_id))[
    "Items"
]

When modifying / debugging this function, make sure that you don’t log any credentials or the whole event object.

Walkthrough

In this section, you will set up the sample AI assistant described in the previous sections in your own AWS account.

Prerequisites

For this walkthrough, you should have the following prerequisites:

Enable large language model

An agent needs a large language model (LLM) to reason about the best way to fulfil a user request and formulate natural-language answers. Follow the Amazon Bedrock model access documentation to enable Anthropic Claude 3 Sonnet model access in the us-east-1 (N. Virginia) Region. After enabling the LLM, you will see the following screen with a status of Access granted:

bedrock model access

Figure 3: You have now enabled Anthropic Claude 3 Sonnet in Amazon Bedrock for your AWS account.

Deploy sample application

We prepared most of the sample application’s infrastructure as an AWS Cloud Development Kit (AWS CDK) project.

If you have never used the CDK in the current account and Region (us-east-1), you must bootstrap the environment using the following command:

cdk bootstrap

Using your local command line interface, issue the following commands to clone the project repository and deploy the CDK project to your AWS account:

git clone https://github.com/aws-samples/multi-tenant-ai-assistant
cd multi-tenant-ai-assistant/cdk
npm install
cdk deploy 
cd ..

This takes about 3 minutes, after which you should see output similar to the following:

✅ MultiTenantAiAssistantStack

✨  Deployment time: 132.24s

Outputs:
MultiTenantAiAssistantStack.appClientId = ...
MultiTenantAiAssistantStack.graphqlEndpoint = https://...
MultiTenantAiAssistantStack.tenant1Password = Initial-...
MultiTenantAiAssistantStack.tenant2Password = Initial-...
MultiTenantAiAssistantStack.tenant3Password = Initial-...
MultiTenantAiAssistantStack.userPoolId = us-east-1_...
Stack ARN:
arn:aws:cloudformation:us-east-1:...:stack/MultiTenantAiAssistantStack/...

✨  Total time: 179.54s

In addition to the AWS resources shown in Figure1, this AWS CDK stack provisions three users, each for a separate tenant, into your AWS account. Note down the passwords for the three users from the CDK output, labelled MultiTenantAiAssistantStack.tenantXPassword. You will need them in the next section. If you come back to this walkthrough later, you can retrieve these values from the file cdk/cdk-output.json generated by the CDK. Note that these are only initial passwords and need to be changed on first sign-in of each user.

You have now successfully deployed the stack called MultiTenantAiAssistantStack.

Start the frontend and sign in

Now that the backend is deployed and configured, you can start the frontend on your local machine, which is built in JavaScript using React. The frontend automatically pulls information from the AWS CDK output, so you don’t need to configure it manually.

  1. Issue the following commands to install dependencies and start the local webserver:
    cd frontend
    npm install
    npm run dev

Open the frontend application by visiting localhost:3000 in your browser. You should see a sign-in page:
sign in screen
Figure 4: Sign-in screen

  1. For Username, enter tenant1-user. For Password, enter the password you have previously retrieved from CDK output.
  2. Set a new password for the user.
  3. On the page Account recovery requires verified contact information, choose Skip.

You’re now signed in and can start interacting with the agent.

Interact with the agent

You have completed the setup of the architecture shown in Figure 1 in your own environment. You can start exploring the web application by yourself or follow the steps suggested below.

  1. Under Enter your Prompt, enter the following question logged in as tenant1-user:
    What is your return policy?
    You should receive a response that you can return items for 10 days. Tenant 2 has a return policy of 20 days, tenant 3 of 30 days.
  2. Under Enter your Prompt, enter the following question:
    Which orders did I place?
    You should receive a response that you have not placed any orders yet.

agent interaction
Figure 5: Sample application screenshot

You have now verified the functionality of the application. You can also try to access data from another user, and you will not get an answer due to the scoped IAM policy. For example, you can modify the agent and hardcode a tenant ID (such as tenant2). In the UI, sign in as the tenant1 user and you will see that with the generated tenant1 scoped credentials you will not be able to access tenant2 resources and you will get an AccessDeniedException. You can also see the error in the CloudWatch Logs for the AgentTask Lambda function:

[ERROR] ClientError: An error occurred (AccessDeniedException) when calling the Query operation: User: *****/agentTaskLambda is not authorized to perform: dynamodb:Query on resource: TABLE  because no identity-based policy allows the dynamodb:Query action

Add test data

To simplify the process of adding orders to your database, we have written a bash script that inserts entries into the order tables.

  1. In your CLI, from the repository root folder, issue this command to add an order for tenant1-user:
    ./manage-orders.sh tenant1-user add
  2. Return to the web application and issue the following prompt:
    Which orders did I place?
    The agent should now respond with the order that you created.
  3. Issue the following command to delete the orders for tenant1-user:
    ./manage-orders.sh tenant1-user clear

Repeat steps 1 through 3 with multiple orders. You can create a new user in Amazon Cognito and sign in to see that no data from other users can be accessed. The implementation is detailed in Figure 2.

Clean up

To avoid incurring future charges, delete the resources created during this walkthrough. From the cdk folder of the repository, run the following command:

cdk destroy

Conclusion

Enabling secure multi-tenant capabilities in AI assistants is crucial for maintaining data privacy and preventing unauthorized access. By following the approach outlined in this blog post, you can create an AI assistant that isolates tenants while using the power of large language models.

The key points to remember are:

  1. When building multi-tenant SaaS applications, always enforce tenant isolation (leverage IAM where ever possible).
  2. Securely pass tenant and user context between deterministic components of your application, without relying on an AI model to handle this sensitive information.
  3. Use Agents for Amazon Bedrock to help build an AI assistant that can securely pass along tenant context.
  4. Implement isolation at different layers of your application to verify that users can only access data and resources associated with their respective tenant and user context.

By following these principles, you can build AI-powered applications that provide a personalized experience to users while maintaining strict isolation and security. As AI capabilities continue to advance, it’s essential to design architectures that use these technologies responsibly and securely.

Remember, the sample application demonstrated in this blog post is just one way to approach multi-tenant AI assistants. Depending on your specific requirements, you might need to adapt the architecture or use different AWS services.

To continue learning about generative AI patterns on AWS, visit the AWS Machine Learning Blog. To explore SaaS on AWS, start by visiting our SaaS landing page. If you have any questions, you can start a new thread on AWS re:Post or reach out to AWS Support.


About the authors

Ulrich Hinze is a Solutions Architect at AWS. He partners with software companies to architect and implement cloud-based solutions on AWS. Before joining AWS, he worked for AWS customers and partners in software engineering, consulting, and architecture roles for 8+ years.

Florian Mair is a Senior Solutions Architect and data streaming expert at AWS. He is a technologist that helps customers in Europe succeed and innovate by solving business challenges using AWS Cloud services. Besides working as a Solutions Architect, Florian is a passionate mountaineer and has climbed some of the highest mountains across Europe.

Read More

Connect the Amazon Q Business generative AI coding companion to your GitHub repositories with Amazon Q GitHub (Cloud) connector

Connect the Amazon Q Business generative AI coding companion to your GitHub repositories with Amazon Q GitHub (Cloud) connector

Incorporating generative artificial intelligence (AI) into your development lifecycle can offer several benefits. For example, using an AI-based coding companion such as Amazon Q Developer can boost development productivity by up to 30 percent. Additionally, reducing the developer context switching that stems from frequent interactions with many different development tools can also increase developer productivity. In this post, we show you how development teams can quickly obtain answers based on the knowledge distributed across your development environment using generative AI.

GitHub (Cloud) is a popular development platform that helps teams build, scale, and deliver software used by more than 100 million developers and over 4 million organizations worldwide. GitHub helps developers host and manage Git repositories, collaborate on code, track issues, and automate workflows through features such as pull requests, code reviews, and continuous integration and deployment (CI/CD) pipelines.

Amazon Q Business is a fully managed, generative AI–powered assistant designed to enhance enterprise operations. You can tailor it to specific business needs by connecting to company data, information, and systems using over 40 built-in connectors.

You can connect your GitHub (Cloud) instance to Amazon Q Business using an out-of-the-box connector to provide a natural language interface to help your team analyze the repositories, commits, issues, and pull requests contained in your GitHub (Cloud) organization. After establishing the connection and synchronizing data, your teams can use Amazon Q Business to perform natural language queries in the supported GitHub (Cloud) data entities, streamlining access to this information.

Overview of solution

To create an Amazon Q Business application to connect to your GitHub repositories using AWS IAM Identity Center and AWS Secrets Manager, follow these high-level steps:

  1. Create an Amazon Q Business application
  2. Perform sync
  3. Run sample queries to test the solution

The following screenshot shows the solution architecture.

Solution architecture, showing the integration of Amazon Q Business with a GitHub Cloud organisation and a sample repository structure

In this post, we show how developers and other relevant users can use the Amazon Q Business web experience to perform natural language–based Q&A over the indexed information reflective of the associated access control lists (ACLs). For this post, we set up a dedicated GitHub (Cloud) organization with four repositories and two teams—review and development. Two of the repositories are private and are only accessible to the members of the review team. The remaining two repositories are public and are accessible to all members and teams.

Prerequisites

To perform the solution, make sure you have the following prerequisites in place:

  1. Have an AWS account with privileges necessary to administer Amazon Q Business
  2. Have access to the AWS region in which Amazon Q Business is available (Supported regions)
  3. Enable the IAM Identity Center and add a user (Guide to enable IAM Identity CenterGuide to add user)
  4. Have a GitHub account with an organization and repositories (Guide to create organization)
  5. Have a GitHub access token classic (Guide to create access tokensPermissions needed for tokens)

Create, sync, and test an Amazon Q business application with IAM Identity Center

To create the Amazon Q Business application, you need to select the retriever, connect the data sources, and add groups and users.

Create application

  1. On the AWS Management Console, search for Amazon Q Business in the search bar, then select Amazon Q Business.

In the AWS Home Screen, type Amazon Q Business in the search bar to pull up the Q service, and select to open the service.

  1. On the Amazon Q Business landing page, choose Get started.

Amazon Q Business get started via AWS console

  1. On the Amazon Q Business Applications screen, at the bottom, choose Create application.

In the Q Home Screen, select "create application" to initiate the process

  1. Under Create application, provide the required values. For example, in Application name, enter anycompany-git-application. For Service access, select Create and use a new service-linked role (SLR). Under Application connected to IAM Identity Center, note the ARN for the associated IAM Identity Center instance. Choose Create.

Creation of a new Amazon Q Business application

Select retriever

Under Select retriever, in Retrievers, select Use native retriever. Under Index provisioning, enter “1.”

Amazon Q Business pricing is based on the chosen document index capacity. You can choose up to 50 capacity units as part of index provisioning. Each unit can contain up to 20,000 documents or 200 MB, whichever comes first. You can adjust this number as needed for your use case.

Choose Next at the bottom of the screen.

Select the "Use native retriever" and choose the "Number of units" based on the how many documents has to be indexed.

Connect data sources

  1. Under Connect data sources, in the search field under All, enter “GitHub” and select the plus sign to the right of the GitHub selection. Choose Next to configure the data source.

You can use the following examples to create a default configuration with file type exclusions to bypass crawling common image and stylesheet files.

Amazon Q Business already has connector for Github. Type Github in the search box, from the search results GitHub, click on the Plus icon.

  1. Enter anycompany-git-datasource in the Data source name and Description.

From the datasource profile, provide the Data source name, description, Github source as "Github Enterprise Cloud" and the Github Host URL.

  1. In the GitHub organization name field, enter your GitHub organization name. Under Authentication, provide a new access token or select an existing access token stored in AWS Secrets Manager.

ACLs and Identity Crawlers are by default enabled for Github connector. Provide the organization name, and the Token for Github authentication. VPC is optional, move to next step without selecting one.

  1. Under IAM role, select Create a new service role and enter the role name under Role name for the data source.

Create a new Service role for Amazon Q Business application

  1. Define Sync scope by selecting the desired repositories and content types to be synced.

Define sync scope

  1. Complete the Additional configuration and Sync mode.

This optional section can be used to specify the file names, types, or file path using regex patterns to define the sync scope. Also, the Sync Mode setting to define the types of content changes to sync when your data source content changes.

Optional configuration settings

  1. For the purposes of this post, under Sync run schedule, select Run on demand under Frequency so you can manually invoke the sync process. Other options for automated periodic sync runs are also supported. In the Field Mappings section, keep the default settings. After you complete the retriever creation, you can modify field mappings and add custom field attributes. You can access field mapping by editing the data source.

Configure sync scope

Add groups and users

There are two users we will use for testing: one with full permissions on all the repositories in the GitHub (Cloud) organization, and a second user with permission only on one specific repository.

  1. Choose Add groups and users.

Add groups and users

  1. Select Assign existing users and groups. This will show you the option to select the users from the IAM Identity Center and add them to this Amazon Q Business application. Choose Next.

  1. Search for the username or name and select the user from the listed options. Repeat for all of the users you wish to test with.

  1. Assign the desired subscrption to the added users.
  1. For Web experience service access, use the default value of Create and use a new service role. Choose Create Application and wait for the application creation process to complete.

Assign subscription and select service role

Perform sync

To sync your new Amazon Q Business application with your desired data sources, follow these steps:

  1. Select the newly created data source under Data sources and choose Sync now.

Depending on the number of supported data entities in the source GitHub (Cloud) organization, the sync process might take several minutes to complete.

Perform data sync

  1. Once the sync is complete, click on the data source name to show the sync history including number of objects scanned, added, deleted, modified, and failed. You can also access the associated Amazon CloudWatch logs to inspect the sync process and failed objects.

View sync history

  1. To access the Amazon Q Business application, select Web experience settings and choose Deployed URL. A new tab will open and ask you for sign-in details. Provide the details of the user you created earlier and choose Sign in.

Access Amazon Q Business Deployed URL

Run sample queries to test the solution

You should now see the home screen of Amazon Q Business, including the associated web experience. Now we can ask questions in natural language and Amazon Q Business will provide answers based on the information indexed from your GitHub (Cloud) organization.

  1. To begin, enter a natural language question in the Enter a prompt.

Access Amazon Q Business application

  1. You can ask questions about the information from the synced GitHub (Cloud) data entities. For example, you can enter, “Tell me how to start a new Serverless application from scratch?” and obtain a response based on the information from the associated repository README.md file.

Amazon Q Business response

  1. Because you are logged in as the first user and mapped to a GitHub (Cloud) user belonging to the review team, you should also be able to ask questions about the contents of private repositories accessible by the members of that team.

As shown in the following screenshot, you can ask questions about the private repository called aws-s3-object-management and obtain the response based on the README.md in that repository.

Amazon Q Business response

However, when you attempt to ask the same question when logged in as the second user, which has no access to the associated GitHub (Cloud) repository, Amazon Q Business will provide an ACL-filtered response.

Filtered Amazon Q Business response

Troubleshooting and frequently asked questions:

1. Why isn’t Amazon Q Business answering any of my questions?

If you are not getting answers to your questions from Amazon Q Business, verify the following:

  1. Permissions – document ACLs indexed by Amazon Q Business may not allow you to query certain data entities as demonstrated in our example. If this is the case, please reach out to your GitHub (Cloud) administrator to verify that your user has access to the restricted documents and repeat the sync process.
  2. Data connector sync – a failed data source sync may prevent the documents from being indexed, meaning that Amazon Q Business would be unable to answer questions about the documents that failed to sync. Please refer to the official documentation to troubleshoot data source connectors.

2. My connector is unable to sync.

Please refer to the official documentation to troubleshoot data source connectors. Please also verify that all of the required prerequisites for connecting Amazon Q Business to GitHub (Cloud) are in place.

3. I updated the contents of my data source but Amazon Q business answers using old data.

Verifying the sync status and sync schedule frequency for your GitHub (Cloud) data connector should reveal when the last sync ran successfully. It could be that your data connector sync run schedule is set to run on demand or has not yet been triggered for its next periodic run. If the sync is set to run on demand, it will need to be manually triggered.

4. How can I know if the reason I don’t see answers is due to ACLs?

If different users are getting different answers to the same questions, including differences in source attribution with citation, it is likely that the chat responses are being filtered based on user document access level represented via associated ACLs.

5. How can I sync documents without ACLs?

Access control list (ACL) crawling is on by default and can’t be turned off.

Cleanup

To avoid incurring future charges, clean up any resources you created as part of this solution, including the Amazon Q Business application:

  1. On the Amazon Q Business console, choose Applications in the navigation pane.
  2. Select the application you created.
  3. On the Actions menu, choose Delete.
  4. Delete the AWS Identity and Access Management (IAM) roles created for the application and data retriever. You can identify the IAM roles used by the created Amazon Q Business application and data retriever by inspecting the associated configuration using the AWS console or AWS Command Line Interface (AWS CLI).
  5. If you created an IAM Identity Center instance for this walkthrough, delete it.

Conclusion

In this post, we walked through the steps to connect your GitHub (Cloud) organization to Amazon Q Business using the out-of-the-box GitHub (Cloud) connector. We demonstrated how to create an Amazon Q Business application integrated with AWS IAM Identity Center as the identity provider. We then configured the GitHub (Cloud) connector to crawl and index supported data entities such as repositories, commits, issues, pull requests, and associated metadata from your GitHub (Cloud) organization. We showed how to perform natural language queries over the indexed GitHub (Cloud) data using the AI-powered chat interface provided by Amazon Q Business. Finally, we covered how Amazon Q Business applies ACLs associated with the indexed documents to provide permissions-filtered responses.

Beyond the web-based chat experience, Amazon Q Business offers a Chat API to create custom conversational interfaces tailored to your specific use cases. You can also use the associated API operations using the AWS CLI or AWS SDK to manage Amazon Q Business applications, retriever, sync, and user configurations.

By integrating Amazon Q Business with your GitHub (Cloud) organization, development teams can streamline access to information scattered across repositories, issues, and pull requests. The natural language interface powered by generative AI reduces context switching and can provide timely answers in a conversational manner.

To learn more about Amazon Q connector for GitHub (Cloud), refer to Connecting GitHub (Cloud) to Amazon Q Business, the Amazon Q User Guide, and the Amazon Q Developer Guide.


About the Authors

Maxim Chernyshev

Maxim Chernyshev is a Senior Solutions Architect working with mining, energy, and industrial customers at AWS. Based in Perth, Western Australia, Maxim helps customers devise solutions to complex and novel problems using a broad range of applicable AWS services and features. Maxim is passionate about industrial Internet of Things (IoT), scalable IT/OT convergence, and cyber security.

Manjunath Arakere

Manjunath Arakere is a Senior Solutions Architect on the Worldwide Public Sector team at AWS, based in Atlanta, Georgia. He works with public sector partners to design and scale well-architected solutions and supports their cloud migrations and modernization initiatives. Manjunath specializes in migration, modernization, and serverless technology.

Mira Andhale

Mira Andhale is a Software Development Engineer on the Amazon Q and Amazon Kendra engineering team. She works on the Amazon Q connector design, development, integration and test operations.

Read More

Elevate customer experience through an intelligent email automation solution using Amazon Bedrock

Elevate customer experience through an intelligent email automation solution using Amazon Bedrock

Organizations spend a lot of resources, effort, and money on running their customer care operations to answer customer questions and provide solutions. Your customers may ask questions through various channels, such as email, chat, or phone, and deploying a workforce to answer those queries can be resource intensive, time-consuming, and unproductive if the answers to those questions are repetitive.

Although your organization might have the data assets for customer queries and answers, you may still struggle to implement an automated process to reply to your customers. Challenges might include unstructured data, different languages, and a lack of expertise in artificial intelligence (AI) and machine learning (ML) technologies.

In this post, we show you how to overcome such challenges by using Amazon Bedrock to automate email responses to customer queries. With our solution, you can identify the intent of customer emails and send an automated response if the intent matches your existing knowledge base or data sources. If the intent doesn’t have a match, the email goes to the support team for a manual response.

Amazon Bedrock is a fully managed service that makes foundation models (FMs) from leading AI startups and Amazon available through an API, so you can choose from a wide range of FMs to find the model that is best suited for your use case. Amazon Bedrock offers a serverless experience so you can get started quickly, privately customize FMs with your own data, and integrate and deploy them into your applications using AWS tools without having to manage infrastructure.

The following are some common customer intents when contacting customer care:

  • Transaction status (for example, status of a money transfer)
  • Password reset
  • Promo code or discount
  • Hours of operation
  • Find an agent location
  • Report fraud
  • Unlock account
  • Close account

Agents for Amazon Bedrock can help you perform classification and entity detection on emails for these intents. For this solution, we show how to classify customer emails for the first three intents. You can also use Agents for Amazon Bedrock to detect key information from emails, so you can automate your business processes with some actions. For example, you can use Agents for Amazon Bedrock to automate the reply to a customer request with specific information related to that query.

Moreover, Agents for Amazon Bedrock can serve as an intelligent conversational interface, facilitating seamless interactions with both internal team members and external clients, efficiently addressing inquiries and implementing desired actions. Currently, Agents for Amazon Bedrock supports Anthropic Claude models and the Amazon Titan Text G1 – Premier model on Amazon Bedrock.

Solution overview

To build our customer email response flow, we use the following services:

Although we illustrate this use case using WorkMail, you can use another email tool that allows integration with serverless functions or webhooks to accomplish similar email automation workflows. Agents for Amazon Bedrock enables you to build and configure autonomous agents in your application. An agent helps your end-users complete actions based on organization data and user input. Agents orchestrate interactions between FMs, data sources, software applications, and user conversations. In addition, agents automatically call APIs to take actions and invoke knowledge bases to supplement information for these actions. Developers can save weeks of development effort by integrating agents to accelerate the delivery of generative AI applications. For this use case, we use the Anthropic Claude 3 Sonnet model.

When you create your agent, you enter details to tell the agent what it should do and how it should interact with users. The instructions replace the $instructions$ placeholder in the orchestration prompt template.

The following is an example of instructions we used for our use cases:

“You are a classification and entity recognition agent. 

Task 1: Classify the given text into one of the following categories: "Transfer Status", "Password Reset", or "Promo Code". Return only the category without additional text.

Task 2: If the classified category is "Transfer Status", find the 10-digit entity "money_transfer_id" (example: "MTN1234567") in the text. Call the "GetTransferStatus" action, passing the money_transfer_id as an argument, to retrieve the transfer status.

Task 3: Write an email reply for the customer based on the received text, the classified category, and the transfer status (if applicable). Include the money_transfer_id in the reply if the category is "Transfer Status".

Task 4: Use the email signature "Best regards, Intelligent Corp" at the end of the email reply.”

An action group defines actions that the agent can help the user perform. For example, you could define an action group called GetTransferStatus with an OpenAPI schema and Lambda function attached to it. Agents for Amazon Bedrock takes care of constructing the API based on the OpenAPI schema and fulfills actions using the Lambda function to get the status from the DynamoDB money_transfer_status table.

The following architecture diagram highlights the end-to-end solution.

The solution workflow includes the following steps:

  1. A customer initiates the process by sending an email to the dedicated customer support email address created within WorkMail.
  2. Upon receiving the email, WorkMail invokes a Lambda function, setting the subsequent workflow in motion.
  3. The Lambda function seamlessly relays the email content to Agents for Amazon Bedrock for further processing.
  4. The agent employs the natural language processing capabilities of Anthropic Claude 3 Sonnet to understand the email’s content classification based on the predefined agent instruction configuration. If relevant entities are detected within the email, such as a money transfer ID, the agent invokes a Lambda function to retrieve the corresponding payment status.
  5. If the email classification doesn’t pertain to a money transfer inquiry, the agent generates an appropriate email response (for example, password reset instructions) and calls a Lambda function to facilitate the response delivery.
  6. For inquiries related to money transfer status, the agent action group Lambda function queries the DynamoDB table to fetch the relevant status information based on the provided transfer ID and relays the response back to the agent.
  7. With the retrieved information, the agent crafts a tailored email response for the customer and invokes a Lambda function to initiate the delivery process.
  8. The Lambda function uses Amazon SES to send the email response, providing the email body, subject, and customer’s email address.
  9. Amazon SES delivers the email message to the customer’s inbox, providing seamless communication.
  10. In scenarios where the agent can’t discern the customer’s intent accurately, it escalates the issue by pushing the message to an SNS topic. This mechanism allows subscribed ticketing system to receive the notification and create a support ticket for further investigation and resolution.

Prerequisites

Refer to the README.md file in the GitHub repo to make sure you meet the prerequisites to deploy this solution.

Deploy the solution

The solution is comprised of three AWS Cloud Deployment Kit (AWS CDK) stacks:

  • WorkmailOrgUserStack – Creates the WorkMail account with domain, user, and inbox access
  • BedrockAgentCreation – Creates the Amazon Bedrock agent, agent action group, OpenAPI schema, S3 bucket, DynamoDB table, and agent group Lambda function for getting the transfer status from DynamoDB
  • EmailAutomationWorkflowStack – Creates the classification Lambda function that interacts with the agent and integration Lambda function, which is integrated with WorkMail

To deploy the solution, you also perform some manual configurations using the AWS Management Console.

For full instructions, refer to the README.md file in the GitHub repo.

Test the solution

To test the solution, send an email from your personal email to the support email created as part of the AWS CDK deployment (for this post, we use support@vgs-workmail-org.awsapps.com). We use the following three intents in our sample data for custom classification training:

  • MONEYTRANSFER – The customer wants to know the status of a money transfer
  • PASSRESET – The customer has a login, account locked, or password request
  • PROMOCODE – The customer wants to know about a discount or promo code available for a money transfer

The following screenshot shows a sample customer email requesting the status of a money transfer.

The following screenshot shows the email received in a WorkMail inbox.

The following screenshot shows a response from the agent regarding the customer query.

If the customer email isn’t classified, the content of the email is forwarded to an SNS topic. The following screenshot shows an example customer email.

The following screenshot shows the agent response.

Whoever is subscribed to the topic receives the email content as a message. We subscribed to this SNS topic with the email that we passed with the human_workflow_email parameter during the deployment.

Clean up

To avoid incurring ongoing costs, delete the resources you created as part of this solution when you’re done. For instructions, refer to the README.md file.

Conclusion

In this post, you learned how to configure an intelligent email automation solution using Agents for Amazon Bedrock, WorkMail, Lambda, DynamoDB, Amazon SNS, and Amazon SES. This solution can provide the following benefits:

  • Improved email response time
  • Improved customer satisfaction
  • Cost savings regarding time and resources
  • Ability to focus on key customer issue

You can expand this solution to other areas in your business and to other industries. Also, you can use this solution to build a self-service chatbot by deploying the BedrockAgentCreation stack to answer customer or internal user queries using Agents for Amazon Bedrock.

As next steps, check out Agents for Amazon Bedrock to start using its features. Follow Amazon Bedrock on the AWS Machine Learning Blog to keep up to date with new capabilities and use cases for Amazon Bedrock.


About the Author

Godwin Sahayaraj Vincent is an Enterprise Solutions Architect at AWS who is passionate about Machine Learning and providing guidance to customers to design, deploy and manage their AWS workloads and architectures. In his spare time, he loves to play cricket with his friends and tennis with his three kids.

Ramesh Kumar Venkatraman is a Senior Solutions Architect at AWS who is passionate about Generative AI, Containers and Databases. He works with AWS customers to design, deploy and manage their AWS workloads and architectures. In his spare time, he loves to play with his two kids and follows cricket.

Read More

Build an end-to-end RAG solution using Knowledge Bases for Amazon Bedrock and the AWS CDK

Build an end-to-end RAG solution using Knowledge Bases for Amazon Bedrock and the AWS CDK

Retrieval Augmented Generation (RAG) is a state-of-the-art approach to building question answering systems that combines the strengths of retrieval and generative language models. RAG models retrieve relevant information from a large corpus of text and then use a generative language model to synthesize an answer based on the retrieved information.

The complexity of developing and deploying an end-to-end RAG solution involves several components, including a knowledge base, retrieval system, and generative language model. Building and deploying these components can be complex and error-prone, especially when dealing with large-scale data and models.

This post demonstrates how to seamlessly automate the deployment of an end-to-end RAG solution using Knowledge Bases for Amazon Bedrock and the AWS Cloud Development Kit (AWS CDK), enabling organizations to quickly set up a powerful question answering system.

Solution overview

The solution provides an automated end-to-end deployment of a RAG workflow using Knowledge Bases for Amazon Bedrock. By using the AWS CDK, the solution sets up the necessary resources, including an AWS Identity and Access Management (IAM) role, Amazon OpenSearch Serverless collection and index, and knowledge base with its associated data source.

The RAG workflow enables you to use your document data stored in an Amazon Simple Storage Service (Amazon S3) bucket and integrate it with the powerful natural language processing (NLP) capabilities of foundation models (FMs) provided by Amazon Bedrock. The solution simplifies the setup process by allowing you to programmatically modify the infrastructure, deploy the model, and start querying your data using the selected FM.

Prerequisites

To implement the solution provided in this post, you should have the following:

  • An active AWS account and familiarity with FMs, Amazon Bedrock, and Amazon OpenSearch Service.
  • Model access enabled for the required models that you intend to experiment with.
  • The AWS CDK already set up. For installation instructions, refer to the AWS CDK workshop.
  • An S3 bucket set up with your documents in a supported format (.txt, .md, .html, .doc/docx, .csv, .xls/.xlsx, .pdf).
  • The Amazon Titan Embeddings V2 model enabled in Amazon Bedrock. You can confirm it’s enabled on the Model Access page of the Amazon Bedrock console. If the Amazon Titan Embeddings V2 model is enabled, the access status will show as Access granted, as shown in the following screenshot.

Set up the solution

When the prerequisite steps are complete, you’re ready to set up the solution:

  1. Clone the GitHub repository containing the solution files:
    git clone https://github.com/aws-samples/amazon-bedrock-samples.git
    

  2. Navigate to the solution directory:
    cd knowledge-bases/ features-examples/04-infrastructure/e2e_rag_using_bedrock_kb_cdk
    

  3. Create and activate the virtual environment:
    $ python3 -m venv .venv
    $ source .venv/bin/activate

The activation of the virtual environment differs based on the operating system; refer to the AWS CDK workshop for activating in other environments.

  1. After the virtual environment is activated, you can install the required dependencies:
    $ pip install -r requirements.txt

You can now prepare the code .zip file and synthesize the AWS CloudFormation template for this code.

  1. In your terminal, export your AWS credentials for a role or user in ACCOUNT_ID. The role needs to have all necessary permissions for CDK deployment:
    export AWS_REGION=”<region>” # Same region as ACCOUNT_REGION above
    export AWS_ACCESS_KEY_ID=”<access-key>” # Set to the access key of your role/user
    export AWS_SECRET_ACCESS_KEY=”<secret-key>” # Set to the secret key of your role/user
  2. Create the dependency:
    ./prepare.sh

  3. If you’re deploying the AWS CDK for the first time, run the following command:
    cdk bootstrap

  4. To synthesize the CloudFormation template, run the following command:
    $ cdk synth

  5. Because this deployment contains multiple stacks, you have to deploy them in a specific sequence. Deploy the stacks in the following order:
    $ cdk deploy KbRoleStack
    $ cdk deploy OpenSearchServerlessInfraStack
    $ cdk deploy KbInfraStack

  6. Once deployment is finished, you can see these deployed stacks by visiting AWS CloudFormation console as shown below. Also you can note knowledge base details (i.e. name, id) under resources tab.

Test the solution

Now that you have deployed the solution using the AWS CDK, you can test it with the following steps:

  1. On the Amazon Bedrock console, choose Knowledge bases in the navigation page.
  2. Select the knowledge base you created.
  3. Choose Sync to initiate the data ingestion job.
  4. After the data ingestion job is complete, choose the desired FM to use for retrieval and generation. (This requires model access to be granted to this FM in Amazon Bedrock before using.)
  5. Start querying your data using natural language queries.

That’s it! You can now interact with your documents using the RAG workflow powered by Amazon Bedrock.

Clean up

To avoid incurring future charges on the AWS account, complete the following steps:

  1. Delete all files within the provisioned S3 bucket.
  2. Run the following command in the terminal to delete the CloudFormation stack provisioned using the AWS CDK:
    $ cdk destroy --all

Conclusion

In this post, we demonstrated how to quickly deploy an end-to-end RAG solution using Knowledge Bases for Amazon Bedrock and the AWS CDK.

This solution streamlines the process of setting up the necessary infrastructure, including an IAM role, OpenSearch Serverless collection and index, and knowledge base with an associated data source. The automated deployment process enabled by the AWS CDK minimizes the complexities and potential errors associated with manually configuring and deploying the various components required for a RAG solution. By taking advantage of the power of FMs provided by Amazon Bedrock, you can seamlessly integrate your document data with advanced NLP capabilities, enabling you to efficiently retrieve relevant information and generate high-quality answers to natural language queries.

This solution not only simplifies the deployment process, but also provides a scalable and efficient way to use the capabilities of RAG for question-answering systems. With the ability to programmatically modify the infrastructure, you can quickly adapt the solution to help meet your organization’s specific needs, making it a valuable tool for a wide range of applications that require accurate and contextual information retrieval and generation.


About the Authors

Sandeep Singh is a Senior Generative AI Data Scientist at Amazon Web Services, helping businesses innovate with generative AI. He specializes in generative AI, machine learning, and system design. He has successfully delivered state-of-the-art AI/ML-powered solutions to solve complex business problems for diverse industries, optimizing efficiency and scalability.

Manoj Krishna Mohan is a Machine Learning Engineering at Amazon. He specializes in building AI/ML solutions using Amazon SageMaker. He is passionate about developing ready-to-use solutions for the customers. Manoj holds a master’s degree in Computer Science specialized in Data Science from the University of North Carolina, Charlotte.

Mani Khanuja is a Tech Lead – Generative AI Specialists, author of the book Applied Machine Learning and High-Performance Computing on AWS, and a member of the Board of Directors for Women in Manufacturing Education Foundation Board. She leads machine learning projects in various domains such as computer vision, natural language processing, and generative AI. She speaks at internal and external conferences such AWS re:Invent, Women in Manufacturing West, YouTube webinars, and GHC 23. In her free time, she likes to go for long runs along the beach.

Read More

Index website contents using the Amazon Q Web Crawler connector for Amazon Q Business

Index website contents using the Amazon Q Web Crawler connector for Amazon Q Business

Amazon Q Business is a fully managed service that lets you build interactive chat applications using your enterprise data. These applications can generate answers based on your data or a large language model (LLM) knowledge. Your data is not used for training purposes, and the answers provided by Amazon Q Business are based solely on the data users have access to.

Enterprise data is often distributed across different sources, such as documents in Amazon Simple Storage Service (Amazon S3) buckets, database engines, websites, and more. In this post, we demonstrate how to create an Amazon Q Business application and index website contents using the Amazon Q Web Crawler connector for Amazon Q Business.

For this example, we use two data sources (websites). The first data source is an employee onboarding guide from a fictitious company, which requires basic authentication. We demonstrate how to set up authentication for the Web Crawler. The second data source is the official documentation for Amazon Q Business. For this data source, we demonstrate how to apply advanced settings, such as regular expressions, to instruct the Web Crawler to crawl only pages and links related to Amazon Q Business, ignoring pages related to other AWS services.

Overview of the Amazon Q Web Crawler connector

The Amazon Q Web Crawler connector makes it possible to crawl websites that use HTTPS and index their contents so you can build a generative artificial intelligence (AI) experience for your users based on the indexed data. This connector relies on the Selenium Web Crawler Package and a Chromium driver. The connector is fully managed and updates to these components are applied automatically without your intervention.

This connector crawls and indexes the contents of webpages and attachments. Amazon Q Business supports multiple connectors, and each connector has its own properties and entities that it considers documents. In the context of the Web Crawler connector, a document refers to a single page or attachment contents. Separately, an index is commonly referred to as a corpus of documents; think of it as the place where you add and sync your documents for Amazon Q Business to use for generating answers to user requests.

Each document has its own attributes, also known as metadata. Metadata can be mapped to fields in your Amazon Q Business index. By creating index fields, you can boost results based on document attributes. For example, there might be use cases where you want to give more relevance to results from a specific category, department, or creation date.

Amazon Q Business data source connectors are designed to crawl the default attributes in your data source automatically. You can also add custom document attributes and map them to custom fields in your index. To learn more, see Mapping document attributes in Amazon Q Business.

For a better understanding of what is indexed by the Web Crawler connector, we present a list of metadata indexed from webpages and attachments.

The following table lists webpage metadata indexed by the Amazon Q Web Crawler connector.

Field Data Source Field Amazon Q Business Index Field (reserved) Field Type
Category category _category String
URL sourceUrl _source_uri String
Title title _document_title String
Meta Tags metaTags wc_meta_tags String List
File Size htmlSize wc_html_size Long (numeric)

The following table lists attachments metadata indexed by the Amazon Q Web Crawler connector.

Field Data Source Field Amazon Q Business Index Field (reserved) Field Type
Category category _category String
URL sourceUrl _source_uri String
File Name fileName wc_file_name String
File Type fileType wc_file_type String
File Size fileSize wc_file_size Long (numeric)

When configuring the data source for your website, you can use URLs or sitemaps, which can be defined either manually or using a text file stored in Amazon S3.

To enforce secure access to protected websites, the Amazon Q Web Crawler supports the following authentication types and standards:

  • Basic authentication
  • NTLM/Kerberos authentication
  • Form-based authentication
  • SAML authentication

Unlike other data source connectors, the Amazon Q Web Crawler connector doesn’t support access control list (ACL) crawling or identity crawling.

Lastly, you have a range of options for configuring how and what data is synchronized. For example, you can choose to synchronize website domains only, website domains with subdomains only, or website domains with subdomains and the webpages included in links. Additionally, you can use regular expressions to filter which URLS to include or exclude in the crawling process.

Overview of solution

On a high level, this solution consists of an Amazon Q Business application that utilizes two data sources: a website hosting documents related to an employee onboarding guide, and the Amazon Q Business official documentation website. This solution demonstrates how to configure both websites as data sources for the Amazon Q Business application. The following steps will be performed:

  1. Deploy an AWS CloudFormation template containing a static website secured with basic authentication.
  2. Create an Amazon Q Business application.
  3. Create a Web Crawler data source for the Amazon Q Business documentation.
  4. Create a Web Crawler data source for the employee onboarding guide.
  5. Add groups and users to the Amazon Q Business application.
  6. Run sample queries to test the solution.

You can follow along using one or both data sources provided in this post or try your own URLs.

Prerequisites

To follow along with this demo, you should have the following prerequisites:

  • An AWS account with privileges to create Amazon Q Business applications and AWS Identity and Access Management (IAM) roles and policies
  • An IAM Identity Center instance with at least one user (and optionally, one or more groups)
  • If you decide to use a public website, make sure you have permission to crawl the website
  • Optionally, privileges to deploy CloudFormation templates

Deploy a CloudFormation template for the employee onboarding website secured with basic authentication

Deploying this CloudFormation template is optional, but we recommend using it so you can learn more about how the Web Crawler connector works with websites that require authentication.

We start by deploying a CloudFormation template. This template will create a simple static website secured with basic authentication.

  1. On the AWS CloudFormation console, choose Create stack and choose With new resources (standard).
  2. Select Choose an existing template.
  3. For Specify template, select Amazon S3 URL.
  4. For Amazon S3 URL enter the URL https://aws-blogs-artifacts-public.s3.amazonaws.com/artifacts/ML-16532/template-website.yml
  5. Choose Next.
  6. For Stack name, enter a name. For example, onboarding-website-for-q-business-sample.
  7. Choose Next.
  8. Leave all options in Configure stack options as default and choose Next.
  9. On the Review and create page, select I acknowledge that AWS CloudFormation might create IAM resources, then choose Submit.

The deployment process will take a few minutes to complete. You can move to the next section of this post while it’s in process. Keep this tab open—you’ll need to refer to the Outputs tab later.

Create an Amazon Q Business application

Before you start creating Amazon Q Business applications, you are required to enable and configure an IAM Identity Center instance. This step is mandatory because Amazon Q Business integrates with IAM Identity Center to manage user access to your Amazon Q Business applications. If you don’t have an IAM Identity Center instance set up when trying to create your first application, you will see the option to create one, as shown in the following screenshot.

Create IAM Identity Center

If you already have an IAM Identity Center instance set up, you’re ready to start creating your first application by following these steps:

  1. On a new tab in your browser, open the Amazon Q Business console.
  2. Choose Get started or Create application (options will vary based on whether it’s your first time trying the service).
  3. For Application name¸ enter a name for your application, for example, my-q-business-app.
  4. For Service access, select Create and use a new service-linked role (SLR).
  5. Choose Create.
  6. For Retrievers, select Use native retriever.
  7. For Index provisioning, enter 1 for Number of units. One unit can index 20,000 documents (a document in this context is either a single page of content or a single attachment).
  8. Choose Next.

Create a Web Crawler data source for the Amazon Q Business documentation

After you complete the steps in the previous section, you should see the Connect data sources page, as shown in the following screenshot.

Connect data sources

If you closed the tab by accident, you can get to this page by navigating to the Amazon Q Business console, choosing your application name, and then choosing Add data source.

Let’s create the data source for the Amazon Q Business documentation website:

  1. On the Connect data sources page, choose Web crawler.
  2. For Data source name, enter a name, for example, q-business-documentation
  3. For Description, enter a description.
  4. For Source, you have the option to provide either URLs or sitemaps. For this example, select Source URLs and enter the URL of the official documentation of Amazon Q: https://docs.aws.amazon.com/amazonq/

Starting point URLs can be added directly in this UI (up to 10), or you could use a file hosted in Amazon S3 to list up to 100 starting point URLs. Likewise, sitemap URLs can be added in this UI (up to three), or you could add up to three sitemap XML files hosted in Amazon S3.

We refer to source URLs as starting point URLs; later in this post, you’ll have the opportunity to define what gets crawled, for example, domains and subdomains that the webpages might link to. It’s worth mentioning that the Web Crawler connector can only work with HTTPS.

  1. Select No authentication in the Authentication section because this is a public website.
  2. The Web proxy section is optional, so we leave it empty.
  3. For Configure VPC and security group, select No VPC.
  4. In the IAM role section, choose Create a new service role.
  5. In the Sync scope section, for Sync domain range, select Sync domains with subdomains only.
  6. For Maximum file size, you can keep the default value of 50 MB.
  7. Under Additional configuration, expand Scope settings.
  8. Leave Crawl depth set to 2, Maximum links per page set to 999, and Maximum throttling set to 300.

If you open the Amazon Q official documentation, you’ll see that there are links to Amazon Q Developer documentation and other AWS services. Because we’re only interested in crawling Amazon Q Business, we need to instruct the crawler to focus only on relevant links and pages related to Amazon Q Business. To achieve this, we use regular expressions to define exactly what URLs the crawler should crawl.

  1. Under Crawl URL Patterns, enter the following expressions one by one, and choose Add:
    1. ^https://docs.aws.amazon.com/amazonq/$
    2. ^https://docs.aws.amazon.com/amazonq/latest/qbusiness-ug/.*.html$
    3. ^https://docs.aws.amazon.com/amazonq/latest/business-use-dg/.*.html$

List of URLs to crawl

  1. In the Sync mode section, select Full sync. This option makes it possible to sync all contents regardless of their previous status.
  2. In the Sync run schedule section, you define how often Amazon Q Business should sync this data source. For Frequency, select Run on demand.

Choosing this option means you must manually run the sync operation; this option is suitable given the simplicity of this example. For production workloads, you’ll want to define a schedule tailored to your needs, for example, hourly, daily, or weekly, or you could define your own schedule using a cron expression.

  1. The Tags section is optional, so we leave it empty.

The default values in the Field mappings section can’t be changed at this point. This can only be modified after the application and retriever have been created.

  1. Choose Add data source and wait a couple of seconds while changes are applied.

After the data source is created, you will be shown the same interface you saw at the beginning of this section, with the note that one Web Crawler data source has been added. Keep this tab open, because you’ll create a second data source for the employee onboarding guide in the next section.

Web crawler added

Create a Web Crawler data source for the employee onboarding guide

Complete the following steps to create your second data source:

  1. On the Connect data sources page, choose Web crawler.
  2. Keep this tab open and navigate back to the AWS CloudFormation console tab and verify the stack’s status is CREATE_COMPLETE.
  3. If the status of the stack is CREATE_COMPLETE, choose the Outputs tab of the stack you deployed.
  4. Note the URL, user name, and password (the following screenshot shows sample values).

Website settings

  1. Choose the link for WebsiteURL.

Although unlikely, if the URL isn’t working, it might be because Amazon CloudFront hasn’t finished replicating the website. In that case, you should wait a couple of minutes and try again.

  1. Sign in with your user name and password.

Basic auth login form

You should now be able to browse the employee onboarding guide. Take a few minutes to get familiar with the contents of the website, because you’ll be asking your Amazon Q Business application questions about this content in a later step.

  1. Return to the browser tab where you’re creating the new data source.
  2. For Data source name, enter a name, for example, onboarding-guide.
  3. For Source, select Source URLs and enter the website URL you saved earlier.
  4. For Authentication, select Basic authentication.
  5. Under Authentication credentials, for AWS Secrets Manager secret, choose Create and add new secret.

Create and add secret

  1. For Secret name, enter a secret name of your preference.
  2. For User name and Password, use the values you saved earlier and make sure there are no extra whitespaces.
  3. Choose Save.

These credentials will be stored as a secret in AWS Secrets Manager.

Depending on the type of authentication you use, you’ll need certain fields present in your secret, as shown in the following table.

Authentication Type Fields present in secret
Form based username, password, userNameFieldXpath, passwordFieldXpath, passwordButtonXpath, loginPageUrl
NTLM username, password
Basic auth username, password
No Authentication NA
  1. Leave the Web proxy section empty.
  2. Select No VPC in the Configure VPC and security group
  3. For IAM role, choose Create a new service role.
  4. Select Sync domains with subdomains only in the Sync scope
  5. Select Full sync in the Sync mode
  6. For Sync run schedule, choose Run on demand.
  7. Leave the sections Tags and Field mappings with their default values.
  8. Choose Add data source and wait a couple of seconds while changes are applied.

After changes are applied, the Connect data sources page shows two Web Crawler data sources have been added.

Two web crawlers have been added

  1. Scroll down to the end of the page and choose Next.

We have added our two data sources. In the next section, we add groups and users to our Amazon Q Business application.

Add groups and users to the Amazon Q Business application

Complete the following steps to add groups and users:

  1. On the Add groups and users page, choose Add groups and users.
  2. Select Assign existing users and groups and choose Next.

If you’ve completed the prerequisite of setting up IAM Identity Center, you’ve likely added at least one user. Although it’s not mandatory, we recommend creating multiple users and groups. This will enable you to fully explore and understand all the features of Amazon Q Business beyond what’s covered in this post.

If you haven’t added any users to your Identity Center directory, you can create them here by choosing Add new users. However, you’ll need to complete additional steps, such as setting up their passwords on the IAM Identity Center console. To fully benefit from this tutorial, we recommend having active users and groups by the time you reach this step.

  1. In the search bar, enter either the display name or group name you want to add to the application.

Start typing name

  1. Choose the user (or group) and choose Assign.

If you added a group, you’ll see it on the Groups tab. If you added a user, you’ll see it on the Users tab.

The next step is choosing a subscription for your groups or users.

  1. Select the user (or group) you just added, and on the Current subscription dropdown menu, choose your subscription tier. For this example, we choose Q Business Pro.

Assign Q Business license

This is a good time to get familiar with the Amazon Q Business subscription tiers and pricing. For this example, we use Q Business Pro, but you could also use a Q Business Lite subscription.

  1. In the Web experience service access section, select Create and use a new service role.

A web experience is the chat interface that your users will utilize to ask questions and perform tasks.

  1. Choose Create application.

After the application is created successfully, you’ll be redirected to the Amazon Q Business console, where you can see your new application. Your application is ready, but the data sources haven’t synced any data yet. We’ll do that in the next steps.

  1. Choose the name of your new application to open the Application Details.

Q Business Application

  1. In the Data sources section, select each data source and choose Sync now.

You will see the Current sync state for both data sources as Syncing. This process might take several minutes.

After the data sources are synced, you will see their Last sync status as Completed.

Sync completed

You’re now ready to test your application! Keep this page open because you’ll need it for next steps.

Run sample queries to test the solution

At this point, you have created an Amazon Q Business application, added two data sources using the Amazon Q Web Crawler connector, added users to the application, and synchronized all data sources.

The next step is going through the full user experience of logging in to the application and running a few test queries to test our application.

  1. On the Application Details page, navigate to the Web experience settings
  2. Choose the link under Deployed URL.

Web experience settings tab

You’ll be redirected to the AWS access portal URL, which is set up by IAM Identity Center.

  1. Enter the user name of a user previously added to your Amazon Q Business application and choose Next.

You’re now on your Amazon Q Business app and ready to start asking questions!

  1. Enter your question (prompt) in the Enter a prompt text field and press Enter.

For this example, we start by asking questions related to the employee onboarding website.

Amazon Q Business Conversation

Amazon Q Business uses the onboarding guide data source you created earlier. If you choose Sources, you’ll see a list of in-text source citations in the form of a numbered list.

Now we ask questions related to the Amazon Q Business documentation.

Amazon Q Business conversation

Try it out with your own prompts!

Troubleshooting

In this section, we discuss several common issues and how to troubleshoot:

  • Amazon Q Business isn’t answering your questions – If Amazon Q Business isn’t answering your questions, it’s likely due to your data not being indexed correctly. To make sure your data has synced correctly, make sure your data sources have synced correctly.
  • The Web Crawler is unable to sync – If you used a starting point URL different from this post and the Web Crawler can’t sync, it might be due to permissions. If the website requires authentication, refer to the section where we create a data source for more information. Another common scenario is when settings on the web server or firewalls prevent the Web Crawler from accessing the data. Lastly, it’s recommended to check if a txt file on your web server is explicitly denying access to the Web Crawler. For more details on how to configure a robots.txt file, refer to Configuring a robots.txt file for Amazon Q Business Web Crawler.
  • Amazon Q Business answers questions using old data – When you create a data source, you have the option to tell Amazon Q Business how often it should sync your data source with your index. During the creation of our data sources, we chose to sync the data sources manually (Run on demand), which means the sync process will occur only when we choose Sync now on our data source. For more information, refer to Sync run schedule.
  • Amazon Q Business provides an inaccurate answer or no answer at all – In situations where Amazon Q Business is providing an inaccurate answer, incomplete answers, or no answer at all, we recommend looking at the format of the data. Is the data part of an image? Is the data in a tabular format? Amazon Q Business works best with unstructured, plain text data.

Document enrichment

Although not covered in this post, we recommend exploring document enrichment. This functionality allows you to manipulate and enrich document attributes prior to being added to an index. The following are a couple of ideas for advanced applications of document enrichment:

  • Run an AWS Lambda function that sends your document to Amazon Textract. This service uses optical character recognition (OCR) to extract text from images containing handwriting, forms, tables, and more.
  • Use Amazon Transcribe to convert videos or audio files in your documents into text.
  • Use Amazon Comprehend to detect and redact personal identifiable information (PII).

Clean up

After you finish testing the solution and to avoid incurring in extra costs, clean up the resources you created as part of this solution.

Let’s start by deleting the Amazon Q Business application.

  1. On the Amazon Q Business console, select your application from the application list and on the Actions menu, choose Delete.

Delete Q Business application

  1. Confirm its deletion by entering Delete, then choose Delete.

You might be asked to complete an optional survey on your reasons for application deletion. You are can select multiple reasons (or none), then choose Submit.

The next step is to delete the CloudFormation stack responsible for deploying the employee onboarding website we used as a data source.

  1. On the CloudFormation console, select the stack you created at the beginning of this walkthrough and choose Delete.

Delete Cloudformation stack

  1. Choose Delete to confirm the stack deletion.

The stack deletion might take a few minutes. When the deletion is complete, you’ll see the stack has been removed from your list of stacks.

Optionally, if you enabled IAM Identity Center only for this tutorial and want to delete your IAM Identity Center instance, follow these steps:

  1. On IAM Identity Center console, choose Settings in the navigation pane.

IAM identity center settings

  1. Choose the Management tab

IAM IDC management

  1. Choose Delete.
  1. Select the acknowledgement check boxes, enter your instance, and choose Confirm.

Conclusion

The Amazon Q Business Web Crawler allows you to connect websites to your Amazon Q Business applications. This connector supports multiple forms of authentication (if required by your website) and can run sync jobs on a defined schedule.

To learn more about Amazon Q Business and its features, refer to the Amazon Q Business Developer Guide. For a comprehensive list of what can be done with this connector, refer to Connecting Web Crawler to Amazon Q Business.


About the Author

Guillermo MansillaGuillermo Mansilla is a Senior Solutions Architect based in Orlando, Florida. He has had the opportunity to collaborate with startups and enterprise customers in the USA and Canada, assisting them in building and architecting their applications on AWS. Guillermo has developed a keen interest in serverless architectures and generative AI applications. Prior to his current role, he gained over a decade of experience working as a software developer. Away from work, Guillermo enjoys participating in chess tournaments at his local chess club, a pursuit that allows him to exercise his analytical skills in a different context.

Read More

Getting started with cross-region inference in Amazon Bedrock

Getting started with cross-region inference in Amazon Bedrock

With the advent of generative AI solutions, a paradigm shift is underway across industries, driven by organizations embracing foundation models to unlock unprecedented opportunities. Amazon Bedrock has emerged as the preferred choice for numerous customers seeking to innovate and launch generative AI applications, leading to an exponential surge in demand for model inference capabilities. Bedrock customers aim to scale their worldwide applications to accommodate growth, and require additional burst capacity to handle unexpected surges in traffic. Currently, users might have to engineer their applications to handle scenarios involving traffic spikes that can use service quotas from multiple regions by implementing complex techniques such as client-side load balancing between AWS regions, where Amazon Bedrock service is supported. However, this dynamic nature of demand is difficult to predict, increases operational overhead, introduces potential points of failure, and might hinder businesses from achieving true global resilience and continuous service availability.

Today, we are happy to announce the general availability of cross-region inference, a powerful feature allowing automatic cross-region inference routing for requests coming to Amazon Bedrock. This offers developers using on-demand inference mode, a seamless solution for managing optimal availability, performance, and resiliency while managing incoming traffic spikes of applications powered by Amazon Bedrock. By opting in, developers no longer have to spend time and effort predicting demand fluctuations. Instead, cross-region inference dynamically routes traffic across multiple regions, ensuring optimal availability for each request and smoother performance during high-usage periods. Moreover, this capability prioritizes the connected Amazon Bedrock API source/primary region when possible, helping to minimize latency and improve responsiveness. As a result, customers can enhance their applications’ reliability, performance, and efficiency.

Let us dig deeper into this feature where we will cover:

  • Key features and benefits of cross-region inference
  • Getting started with cross-region inference
  • Code samples for defining and leveraging this feature
  • How to think about migrating to cross-region inference
  • Key considerations
  • Best Practices to follow for this feature
  • Conclusion

Let’s dig in!

Key features and benefits.

One of the critical requirements from our customers is the ability to manage bursts and spiky traffic patterns across a variety of generative AI workloads and disparate request shapes. Some of the key features of cross-region inference include:

  • Utilize capacity from multiple AWS regions allowing generative AI workloads to scale with demand.
  • Compatibility with existing Amazon Bedrock API
  • No additional routing or data transfer cost and you pay the same price per token for models as in your source/primary region.
  • Become more resilient to any traffic bursts. This means, users can focus on their core workloads and writing logic for their applications powered by Amazon Bedrock.
  • Ability to choose from a range of pre-configured AWS region sets tailored to your needs.

The below image would help to understand how this feature works. Amazon Bedrock makes real-time decisions for every request made via cross-region inference at any point of time. When a request arrives to Amazon Bedrock, a capacity check is performed in the same region where the request originated from, if there is enough capacity the request is fulfilled else a second check determines a secondary region which has capacity to take the request, it is then re-routed to that decided region and results are retrieved for customer request. This ability to perform capacity checks was not available to customers so they had to implement manual checks of every region of choice after receiving an error and then re-route. Further the typical custom implementation of re-routing might be based on round robin mechanism with no insights into the available capacity of a region. With this new capability, Amazon Bedrock takes into account all the aspects of traffic and capacity in real-time, to make the decision on behalf of customers in a fully-managed manner without any extra costs.

 Few points to be aware of:

  1. AWS network backbone is used for data transfer between regions instead of internet or VPC peering, resulting in secure and reliable execution.
  2. The feature will try to serve the request from your primary region first. It will route to other regions in case of heavy traffic, bottlenecks and load balance the requests.
  3. You can access a select list of models via cross-region inference, which are essentially region agnostic models made available across the entire region-set. You will be able to use a subset of models available in Amazon Bedrock from anywhere inside the region-set even if the model is not available in your primary region.
  4. You can use this feature in the Amazon Bedrock model invocation APIs (InvokeModel and Converse API).
  5. You can choose whether to use Foundation Models directly via their respective model identifier or use the model via the cross-region inference mechanism. Any inferences performed via this feature will consider on-demand capacity from all of its pre-configured regions to maximize availability.
  6. There will be additional latency incurred when re-routing happens and, in our testing, it has been a double-digit milliseconds latency add.
  7. All terms applicable to the use of a particular model, including any end user license agreement, still apply when using cross-region inference.
  8. When using this feature, your throughput can reach up to double the allocated quotas in the region that the inference profile is in. The increase in throughput only applies to invocation performed via inference profiles, the regular quota still applies if you opt for in-region model invocation request. To see quotas for on-demand throughput, refer to the Runtime quotas section in Quotas for Amazon Bedrock or use the Service Quotas console

Definition of a secondary region

Let us dive deep into a few important aspects:

  1. What is a secondary region? As part of this launch, you can select either a US Model or EU Model, each of which will include 2-3 preset regions from these geographical locations.
  2. Which models are included? As part of this launch, we will have Claude 3 family of models (Haiku, Sonnet, Opus) and Claude 3.5 Sonnet made available.
  3. Can we use PrivateLink? Yes, you will be able to leverage your private links and ensure traffic flows via your VPC with this feature.
  4. Can we use Provisioned Throughput with this feature as well? Currently, this feature will not apply to Provisioned Throughput and can be used for on-demand inference only.
  5. When does the workload traffic get re-routed? Cross-region inference will first try to service your request via the primary region (region of the connected Amazon Bedrock endpoint). As the traffic patterns spike up and Amazon Bedrock detects potential delays, the traffic will shift pro-actively to the secondary region and get serviced from those regions.
  6. Where would the logs be for cross-region inference? The logs and invocations will still be in the primary region and account where the request originates from. Amazon Bedrock will output indicators on the logs which will show which region actually serviced the request.
  7. Here is an example of the traffic patterns can be from below (map not to scale).

A customer with a workload in eu-west-1 (Ireland) may choose both eu-west-3 (Paris) and eu-central-1 (Frankfurt) as a pair of secondary regions, or a workload in us-east-1 (Northern Virginia) may choose us-west-2 (Oregon) as a single secondary region, or vice versa. This would keep all inference traffic within the United States of America or European Union.

Security and Architecture of how cross-region inference looks like

The following diagram shows the high-level architecture for a cross-region inference request:

The operational flow starts with an Inference request coming to a primary region for an on-demand baseline model. Capacity evaluations are made on the primary region and the secondary region list, creating a region capacity list in capacity order. The region with the most available capacity, in this case eu-central-1 (Frankfurt), is selected as the next target. The request is re-routed to Frankfurt using the AWS Backbone network, ensuring that all traffic remains within the AWS network. The request bypasses the standard API entry-point for the Amazon Bedrock service in the secondary region and goes directly to the Runtime inference service, where the response is returned back to the primary region over the AWS Backbone and then returned to the caller as per a normal inference request. If processing in the chosen region fails for any reason, then the next region in the region capacity list highest available capacity is tried, eu-west-1 (Ireland) in this example, followed by eu-west-3 (Paris), until all configured regions have been attempted. If no region in the secondary region list can handle the inference request, then the API will return the standard “throttled” response.

Networking and data logging

The AWS-to-AWS traffic flows, such as Region-to-Region (inclusive of Edge Locations and Direct Connect paths), will always traverse AWS-owned and operated backbone paths. This not only reduces threats, such as common exploits and DDoS attacks, but also ensures that all internal AWS-to-AWS traffic uses only trusted network paths. This is combined with inter-Region and intra-Region path encryption and routing policy enforcement mechanisms, all of which use AWS secure facilities. This combination of enforcement mechanisms helps ensure that AWS-to-AWS traffic will never use non-encrypted or untrusted paths, such as the internet, and hence as a result all cross-region inference requests will remain on the AWS backbone at all times.

Log entries will continue to be made in the original source region for both Amazon CloudWatch and AWS CloudTrail, and there will be no additional logs in the re-routed region. In order to indicate that re-routing happened the related entry in AWS CloudTrail will also include the following additional data – it is only added if the request was processed in a re-routed region.

<requestRoutedToRegion>
    us-east-1
</requestRoutedToRegion>

During an inference request, Amazon Bedrock does not log or otherwise store any of a customer’s prompts or model responses. This is still true if cross-region inference re-routes a query from a primary region to a secondary region for processing – that secondary region does not store any data related to the inference request, and no Amazon CloudWatch or AWS CloudTrail logs are stored in that secondary region.

Identity and Access Management

AWS Identity and Access Management (IAM) is key to securely managing your identities and access to AWS services and resources. With the introduction of cross-region inference there is a new context key aws:RequestedRegion. The caller must have this enabled for each of the regions in the inference region list. This is evaluated in the source region before any model inference request is made, and if the caller does not have permission for every region in the inference region list, then the request is denied without any inference taking place.

An example policy, which allows the caller to use the cross-region inference with the InvokeModel* APIs for any model in the us-east-1 and us-west-2 region is as follows:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": ["bedrock:InvokeModel*"],
      "Resource": ["arn:aws:bedrock:us-east-1:<account_id>:inference-profile/*"],
      "Condition": {
        "StringEquals": {
          "aws:RequestedRegion": ["us-east-1", "us-west-2"]
        }
      }
    }
  ]
}

Getting started with Cross-region inference

To get started with cross-region inference, you make use of Inference Profiles in Amazon Bedrock. An inference profile for a model, configures different model ARNs from respective AWS regions and abstracts them behind a unified model identifier (both id and ARN). Just by simply using this new inference profile identifier with the InvokeModel or Converse API, you can use the cross-region inference feature.

Here are the steps to start using cross-region inference with the help of inference profiles:

  1. List Inference Profiles
    You can list the inference profiles available in your region by either signing in to Amazon Bedrock AWS console or API.

    • Console
      1. From the left-hand pane, select “Cross-region Inference”
      2. You can explore different inference profiles available for your region(s).
      3. Copy the inference profile ID and use it in your application, as described in the section below
    • API
      It is also possible to list the inference profiles available in your region via boto3 SDK or AWS CLI.

      aws bedrock list-inference-profiles

You can observe how different inference profiles have been configured for various geo locations comprising of multiple AWS regions. For example, the models with the prefix us. are configured for AWS regions in USA, whereas models with eu. are configured with the regions in European Union (EU).

  1. Modify Your Application
    1. Update your application to use the inference profile ID/ARN from console or from the API response as modelId in your requests via InvokeModel or Converse
    2. This new inference profile will automatically manage inference throttling and re-route your request(s) across multiple AWS Regions (as per configuration) during peak utilization bursts.
  2. Monitor and Adjust
    1. Use Amazon CloudWatch to monitor your inference traffic and latency across regions.
    2. Adjust the use of inference profile vs FMs directly based on your observed traffic patterns and performance requirements.

Code example to leverage Inference Profiles

Use of inference profiles is similar to that of foundation models in Amazon Bedrock using the InvokeModel or Converse API, the only difference between the modelId is addition of a prefix such as us. or eu.

Foundation Model

modelId = 'anthropic.claude-3-5-sonnet-20240620-v1:0'
bedrock_runtime.converse(
  modelId=modelId,
  system=[{
    "text": "You are an AI assistant."
  }],
  messages=[{
    "role": "user",
    "content": [{"text": "Tell me about Amazon Bedrock."}]
  }]
)

Inference Profile

modelId = 'eu.anthropic.claude-3-5-sonnet-20240620-v1:0'
bedrock_runtime.converse(
  modelId=modelId,
  system=[{
    "text": "You are an AI assistant."
  }],
  messages=[{
    "role": "user",
    "content": [{"text": "Tell me about Amazon Bedrock."}]
  }]
)

Deep Dive

While it is straight forward to start using inference profiles, you first need to know which inference profiles are available as part of your region. Start with the list of inference profiles and observe models available for this feature. This is done through the AWS CLI or SDK.

import boto3
bedrock_client = boto3.client("bedrock", region_name="us-east-1")
bedrock_client.list_inference_profiles()

You can expect an output similar to the one below:

{
  "inferenceProfileSummaries": [
    {
     "inferenceProfileName": "us. Anthropic Claude 3.5 Sonnet",
        "models": [
           {
             "modelArn": "arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-3-5-sonnet-20240620-v1:0"
           },
           {
             "modelArn": "arn:aws:bedrock:us-west-2::foundation-model/anthropic.claude-3-5-sonnet-20240620-v1:0"
           }
        ],
        "description": "Routes requests to Anthropic Claude 3.5 Sonnet in us-east-1 and us-west-2",
        "createdAt": "2024-XX-XXT00:00:00Z",
        "updatedAt": "2024-XX-XXT00:00:00Z",
        "inferenceProfileArn": "arn:aws:bedrock:us-east-1:<account_id>:inference-profile/us.anthropic.claude-3-5-sonnet-20240620-v1:0",
        "inferenceProfileId": "us.anthropic.claude-3-5-sonnet-20240620-v1:0",
        "status": "ACTIVE",
        "type": "SYSTEM_DEFINED"
    },
    ...
  ]
}

The difference between ARN for a foundation model available via Amazon Bedrock and the inference profile can be observed as:

Foundation Model: arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-3-5-sonnet-20240620-v1:0

Inference Profile: arn:aws:bedrock:us-east-1:<account_id>:inference-profile/us.anthropic.claude-3-5-sonnet-20240620-v1:0

Choose the configured inference profile, and start sending inference requests to your model’s endpoint as usual. Amazon Bedrock will automatically route and scale the requests across the configured regions as needed. You can choose to use both ARN as well as ID with the Converse API whereas just the inference profile ID with the InvokeModel API. It is important to note which models are supported by Converse API.

import boto3

primary_region ="<primary-region-name>" #us-east-1, eu-central-1
bedrock_runtime = boto3.client("bedrock-runtime", region_name= primary_region)
inferenceProfileId = '<regional-prefix>.anthropic.claude-3-5-sonnet-20240620-v1:0' 

# Example with Converse API
system_prompt = "You are an expert on AWS AI services."
input_message = "Tell me about AI service for Foundation Models"
response = bedrock_runtime.converse(
    modelId = inferenceProfileId,
    system = [{"text": system_prompt}],
    messages=[{
        "role": "user",
        "content": [{"text": input_message}]
    }]
)

print(response['output']['message']['content'])
us-east-1 or eu-central-1

In the code sample above you must specify <your-primary-region-name> such as US regions including us-east-1, us-west-2 or EU regions including eu-central-1, eu-west-1, eu-west-3. The <regional-prefix> will then be relative, either us or eu.

Adapting your applications to use Inference Profiles for your Amazon Bedrock FMs is quick and easy with steps above. No significant code changes are required on the client side. Amazon Bedrock handles the cross-region inference transparently. Monitor CloudTrail logs to check if your request is automatically re-routed to another region as described in the section above.

How to think about adopting to the new cross-region inference feature?

When considering the adoption of this new capability, it’s essential to carefully evaluate your application requirements, traffic patterns, and existing infrastructure. Here’s a step-by-step approach to help you plan and adopt cross-region inference:

  1. Assess your current workload and traffic patterns. Analyze your existing generative AI workloads and identify those that experience significant traffic bursts or have high availability requirements including current traffic patterns, including peak loads, geographical distribution, and any seasonal or cyclical variations
  2. Evaluate the potential benefits of cross-region inference. Consider the potential advantages of leveraging cross-region inference, such as increased burst capacity, improved availability, and better performance for global users. Estimate the potential cost savings by not having to implement a custom logic of your own and pay for data transfer (as well as different token pricing for models) or efficiency gains by off-loading multiple regional deployments into a single, fully-managed distributed solution.
  3. Plan and execute the migration. Update your application code to use the inference profile ID/ARN instead of individual foundation model IDs, following the provided code sample above. Test your application thoroughly in a non-production environment, simulating various traffic patterns and failure scenarios. Monitor your application’s performance, latency, and cost during the migration process, and make adjustments as needed.
  4. Develop new applications with cross-region inference in mind. For new application development, consider designing with cross-region inference as the foundation, leveraging inference profiles from the start. Incorporate best practices for high availability, resilience, and global performance into your application architecture.

Key Considerations

Impact on Current Generative AI Workloads

Inference profiles are designed to be compatible with existing Amazon Bedrock APIs, such as InvokeModel and Converse. Also, any third-party/opensource tool which uses these APIs such as LangChain can be used with inference profiles. This means that you can seamlessly integrate inference profiles into your existing workloads without the need for significant code changes. Simply update your application to use the inference profiles ARN instead of individual model IDs, and Amazon Bedrock will handle the cross-region routing transparently.

Impact on Pricing

The feature comes with no additional cost to you. You pay the same price per token of individual models in your primary/source region. There is no additional cost associated with cross-region inference including the failover capabilities provided by this feature. This includes management, data-transfer, encryption, network usage and potential differences in price per million token per model.

Regulations, Compliance, and Data Residency

Although none of the customer data is stored in either the primary or secondary region(s) when using cross-region inference, it’s important to consider that your inference data will be processed and transmitted beyond your primary region. If you have stric7t data residency or compliance requirements, you should carefully evaluate whether cross-region inference aligns with your policies and regulations.

Conclusion

In this blog we introduced the latest feature from Amazon Bedrock, cross-region inference via inference profiles, and a peek into how it operates and also dived into some of the how-to’s and points for considerations. This feature empowers developers to enhance the reliability, performance, and efficiency of their applications, without the need to spend time and effort building complex resiliency structures. This feature is now generally available in US and EU for supported models.


About the authors

Talha Chattha is a Generative AI Specialist Solutions Architect at Amazon Web Services, based in Stockholm. Talha helps establish practices to ease the path to production for Gen AI workloads. Talha is an expert in Amazon Bedrock and supports customers across entire EMEA. He holds passion about meta-agents, scalable on-demand inference, advanced RAG solutions and cost optimized prompt engineering with LLMs. When not shaping the future of AI, he explores the scenic European landscapes and delicious cuisines. Connect with Talha at LinkedIn using /in/talha-chattha/.

Rupinder Grewal is a Senior AI/ML Specialist Solutions Architect with AWS. He currently focuses on the serving of models and MLOps on Amazon SageMaker. Prior to this role, he worked as a Machine Learning Engineer building and hosting models. Outside of work, he enjoys playing tennis and biking on mountain trails.

Sumit Kumar is a Principal Product Manager, Technical at AWS Bedrock team, based in Seattle. He has 12+ years of product management experience across a variety of domains and is passionate about AI/ML. Outside of work, Sumit loves to travel and enjoys playing cricket and Lawn-Tennis.

Dr. Andrew Kane is an AWS Principal WW Tech Lead (AI Language Services) based out of London. He focuses on the AWS Language and Vision AI services, helping our customers architect multiple AI services into a single use-case driven solution. Before joining AWS at the beginning of 2015, Andrew spent two decades working in the fields of signal processing, financial payments systems, weapons tracking, and editorial and publishing systems. He is a keen karate enthusiast (just one belt away from Black Belt) and is also an avid home-brewer, using automated brewing hardware and other IoT sensors.

Read More

Building automations to accelerate remediation of AWS Security Hub control findings using Amazon Bedrock and AWS Systems Manager

Building automations to accelerate remediation of AWS Security Hub control findings using Amazon Bedrock and AWS Systems Manager

Several factors can make remediating security findings challenging. First, the sheer volume and complexity of findings can overwhelm security teams, leading to delays in addressing critical issues. Findings often require a deep understanding of AWS services and configurations and require many cycles for validation, making it more difficult for less experienced teams to remediate issues effectively. Some findings might require coordination across multiple teams or departments, leading to communication challenges and delays in implementing fixes. Finally, the dynamic nature of cloud environments means that new security findings can appear rapidly and constantly, requiring a more effective and scalable solution to remediate findings.

In this post, we will harness the power of generative artificial intelligence (AI) and Amazon Bedrock to help organizations simplify and effectively manage remediations of AWS Security Hub control findings. By using Agents for Amazon Bedrock with action groups and Knowledge Bases for Amazon Bedrock, you can now create automations with AWS Systems Manager Automation (for services that support automations with AWS Systems Manager) and deploy them into AWS accounts. Thus, by following a programmatic continuous integration and development (CI/CD) approach, you can scale better and remediate security findings promptly.

Solution overview

This solution follows prescriptive guidance for automating remediation for AWS Security Hub standard findings. Before delving into the deployment, let’s review the key steps of the solution architecture, as shown in the following figure.

Figure 1 : AWS Security Hub control remediations using Amazon Bedrock and AWS Systems Manager

Figure 1 : AWS Security Hub control remediation using Amazon Bedrock and AWS Systems Manager

  1. A SecOps user uses the Agents for Amazon Bedrock chat console to enter their responses. For instance, they might specify “Generate an automation for remediating the finding, database migration service replication instances should not be public.” Optionally, if you’re already aggregating findings in Security Hub, you can export them to an Amazon Simple Storage Service (Amazon S3) bucket and still use our solution for remediation.
  2. On receiving the request, the agent invokes the large language model (LLM) with the provided context from a knowledge base. The knowledge base contains an Amazon S3 data source with AWS documentation. The data is converted into embeddings using the Amazon Titan Embeddings G1 model and stored in an Amazon OpenSearch vector database.
  3. Next, the agent passes the information to an action group that invokes an AWS Lambda function. The Lambda function is used to generate the Systems Manager automation document.
  4. The output from the Lambda function is published to a AWS CodeCommit repository.
  5. Next, the user validates the template file that is generated as an automation for a particular service. In this case, the user will navigate to the document management system (DMS) folder and validate the template file. Once the file has been validated, the user places the template file into a new deploy folder in the repo.
  6. This launches AWS CodePipeline to invoke a build job using AWS CodeBuild. Validation actions are run on the template.
  7. Amazon Simple Notification Service (Amazon SNS) notification is sent to the SecOps user to approve changes for deployment.
  8. Once changes are approved, the CloudFormation template is generated that creates an SSM automation document
    • If an execution role is provided, via AWS CloudFormation stack set, SSM automation document is executed across specified workload accounts.
    • If an execution role is not provided, SSM automation document is deployed only to the current account.
  9. SSM automation document is executed to remediate the finding.
  10. The user navigates to AWS Security Hub service via AWS management console and validates the compliance status of the control (For example, DMS.1).

In this post, we focus on remediation of two example security findings:

The example findings demonstrate the two potential paths the actions group can take for remediation. It also showcases the capabilities of action groups with Retrieval Augmented Generation (RAG) and how you can use Knowledge Bases for Amazon Bedrock to automate security remediation.

For the first finding, AWS has an existing Systems Manager runbook to remediate the S3.5 finding. The solution uses the existing runbook (through a knowledge base) and renders an AWS CloudFormation template as automation.

The second finding has no AWS provided runbook or playbook. The solution will generate a CloudFormation template that creates an AWS Systems Manager document to remediate the finding.

Prerequisites

Below are the prerequisites that are needed before you can deploy the solution.

  1. An AWS account with the necessary permissions to access and configure the required services in a specific AWS Region (AWS Security Hub, Amazon S3, AWS CodeCommit, AWS CodePipeline, AWS CodeBuild, AWS Systems Manager, AWS Lambda, Amazon OpenSearch service).
  2. Access to Anthropic Claude 3 Sonnet LLM model granted in the AWS account.
  3. AWS Config is enabled in the account. Ensure that the configuration recorder is configured to record all resources in your AWS account.
  4. Security Hub is enabled in the account. Integrate other AWS security services, such as AWS Config to aggregate their findings in Security Hub.
  5. Understanding of general key terms:

Deployment steps

There are five main steps in order to deploy the solution.

Step 1: Configure a knowledge base

Configuring a knowledge base enables your Amazon Bedrock agents to access a repository of information for AWS account provisioning. Follow these steps to set up your knowledge base.

Prepare the data sources:

  1. Create an S3 bucket that will store the knowledge base data sources. Such as, KnowledgeBaseDataSource-<AccountId>.
  2. Define the data source. For this solution, we’re using three AWS documentation guides in PDF that covers all AWS provided automations through runbooks or playbooks. Upload files from the data-source folder in the Git repository to the newly created S3 bucket from previous step.

Create the knowledge base:

  1. Access the Amazon Bedrock console. Sign in and go directly to the Knowledge Base section.
  2. Name your knowledge base. Choose a clear and descriptive name that reflects the purpose of your knowledge base, such as AWSAutomationRunbooksPlaybooks.
  3. Select an AWS Identity and Access Management (IAM) role. Assign a preconfigured IAM role with the necessary permissions. It’s typically best to let Amazon Bedrock create this role for you to ensure it has the correct permissions.
  4. Choose the default embeddings model. The Amazon Titan Embeddings G1 is a text model that is preconfigured and ready to use, simplifying the process.
  5. Choose the Quick create a new vector store. Allow Amazon Bedrock to create and manage the vector store for you in OpenSearch Service.
  6. Review and finalize. Double-check all entered information for accuracy. Pay special attention to the S3 bucket URI and IAM role details.

Note: After successful creation, copy the knowledge base ID because you will need to reference it in the next step.

Sync the data source:

  1. Select the newly created knowledge base.
  2. In the Data source section, choose Sync to begin data ingestion.
  3. When data ingestion completes, a green success banner appears if it is successful.

Step 2: Configure the Amazon Bedrock agent

  1. Open the Amazon Bedrock console, select Agents in the left navigation panel, then choose Create Agent.
  2. Enter agent details including an agent name and description (optional).
  3. Under Agent resource role section, select Create and use a new service role. This IAM service role gives your agent access to required services, such as Lambda.
  4. In the Select model section, choose Anthropic and Claude 3 Sonnet.
  5. To automate remediation of Security Hub findings using Amazon Bedrock agents, attach the following instruction to the agent:
    “You are an AWS security expert, tasked to help customer remediate security related findings.Inform the customer what your objective is. Gather relevant information such as finding ID or finding title so that you can perform your task. With the information given, you will attempt to find an automated remediation of the finding and provide it to the customer as IaC.”
  6.  Select the newly created agent and take note of the Agent ARN in the Agent Overview section. You will be required to input this as a parameter in the next step.

Step 3: Deploy the CDK project

  1. Download the CDK project repository containing the solution’s infrastructure code. You can find the code from GitHub repository.
  2. To work with a new project, create and activate a virtual environment. This allows the project’s dependencies to be installed locally in the project folder, instead of globally. Create a new virtual environment: python -m venv .venv. Activate the environment: source .venv/bin/activate
  3. Install dependencies from requirements.txt: pip install -r requirements.txt
  4. Before deploying the solution, you need to bootstrap your AWS environment for CDK. Run the following command to bootstrap your environment: cdk bootstrap aws://<your-aws-account-id>/<your-aws-region>
  5. Navigate to the downloaded CDK project directory and open the cdk.json file. Update the following parameters in the file:
    • KB_ID: Provide the ID of the Amazon Bedrock knowledge base you set up manually in the prerequisites.
    • BEDROCK_AGENT_ARN: The Amazon Bedrock agent Amazon Resource Name (ARN) that was created in Step 2.
    • NOTIFICATION_EMAILS: Enter an email address for pipeline approval notifications.
    • CFN_EXEC_ROLE_NAME: (Optional) IAM role that will be used by CloudFormation to deploy templates into the workload accounts.
    • WORKLOAD_ACCOUNTS: (Optional) Specify a space-separated list of AWS account IDs where the CloudFormation templates will be deployed. “<account-id-1> <account-id-2>”.
  6. Run the following command to synthesize the CDK app and generate the CloudFormation template: cdk synth
  7. Finally, deploy the solution to your AWS environment using the following command: cdk deploy --all. This command will deploy all the necessary resources, including the Lambda function, the CodeCommit repository, the CodePipeline, and the Amazon SNS notification.
  8. After the deployment is complete, verify that all the resources were created successfully. You can check the outputs of the CDK deployment to find the necessary information, such as the CodeCommit repository URL, Lambda function name, and the Amazon SNS topic ARN.

Step 4: Configure the agent action groups

Create an action group linked to the Lambda function that was created in the CDK app. This action group is launched by the agent after the user inputs the Security Hub finding ID or finding title, and outputs a CloudFormation template in the Code Commit repository.

Step 5: Add the action groups to the agent

  1. Enter securityhubremediation as the Action group name and Security Hub Remediations as the Description.
  2. Under Action group type, select Define with API schemas.
  3. For Action group invocation, choose Select an existing Lambda function.
  4. From the dropdown, select the Lambda function that was created in Step 3.
  5. In Action group schema, choose Select an existing API schema. Provide a link to the Amazon S3 URI of the schema with the API description, structure, and parameters for the action group. APIs manage the logic for receiving user inputs and launching the Lambda functions for account creation and customization. For more information, see Action group OpenAPI schemas.

Note: For this solution, openapischema.json is provided to you in the Git repository. Upload the JSON into the S3 bucket created in Step 1 and reference the S3 URI when selecting the API schema in this step.

Testing

In order to validate the solution, follow the below steps :

Step 1: Sign in to AWS Security Hub console.

  1. Select a Security Hub Finding.
  2.  For testing the solution, look for a finding that has a status of FAILED.
  3. Copy the finding title – ” Database Migration Service replication instance should not be public”. This is shown in Figure 2.

    Figure 2 : AWS Security Hub Finding title

    Figure 2 : AWS Security Hub finding title

Step 2: Sign in to the Amazon Bedrock console.

  1. Select the agent.
    • As you begin to interact with the agent, it will ask you for a Security Hub finding title to remediate.
    • Enter a Security Hub finding title. For example, “Database migration service replication instances should not be public”.
  2. Review the resulting CloudFormation template published to the CodeCommit repository provisioned as part of the deployment.

If a finding already has an AWS remediation runbook available, the agent will output its details. That is, it will not create a new runbook. When automation through a Systems Manager runbook isn’t possible, the agent will output a message similar to “Unable to automate remediation for this finding.” An example Bedrock Agent interaction is shown in Figure 3.

Figure 3 : An example Bedrock Agent Interaction

Figure 3 : An example Bedrock Agent Interaction

Step 3: For the new runbooks, validate the template file and parameters

  1. Check if the template requires any parameters to be passed.
  2. If required, create a new file parameter file with the following naming convention:
    • <Bedrock_Generated_Template_Name>-params.json
    • For example: DatabaseMigrationServicereplicationinstanceshouldnotbepublic-params.json

Step 4: Stage files for deployment

  1. Create new folder named deploy in the CodeCommit repository.
  2. Create a new folder path deploy/parameters/ in the CodeCommit repository.
  3. Upload the YAML template file to the newly created deploy folder.
  4. Upload the params JSON file to deploy/parameters.
  5. The structure of the deploy folder should be as follows:
    ├ deploy
    
      ├ < Bedrock_Generated_Template_Name >.yaml
    
      ├ parameters
    
        ├ < Bedrock_Generated_Template_Name >-params.json

Note: Bedrock_Generated_Template_Name refers to the name of the YAML file that has been output by Amazon Bedrock. Commit of the file will invoke the pipeline. An example Bedrock generated YAML file is shown in Figure 4.

Figure 4 : An example Bedrock generated YAML file

Figure 4 : An example Bedrock generated YAML file

Step 5: Approve the pipeline

  1. Email will be sent through Amazon SNS during the manual approval stage. Approve the pipeline to continue the build.
  2. Systems Manager automation will be built using CloudFormation in the workload account.

Step 6: Validate compliance status

  1. Sign in to the Security Hub console and validate the compliance status of the finding ID or title.
  2. Verify that the compliance status has been updated to reflect the successful remediation of the security issue. This is shown in Figure 5.
Figure 5 : Validation of successful remediation of AWS Security Hub Control Finding

Figure 5 : Validation of successful remediation of AWS Security Hub control finding

Cleanup

To avoid unnecessary charges, delete the resources created during testing. To delete the resources, perform the following steps:

  1. Delete the knowledge base
    • Open the Amazon Bedrock console.
    • From the left navigation pane, choose Knowledge base.
    • To delete a source, either choose the radio button next to the source and select Delete or choose the Name of the source and then select Delete in the top right corner of the details page.
    • Review the warnings for deleting a knowledge base. If you accept these conditions, enter “delete” in the input box and choose Delete to confirm.
    • Empty and delete the S3 bucket data source for the knowledge base.
  2. Delete the agent
    • In the Amazon Bedrock console, choose Agents from the navigation pane.
    • Select the radio button next to the agent to delete.
    • A modal window will pop up warning you about the consequences of deletion. Enter delete in the input box and choose Delete to confirm.
    • A blue banner will inform you that the agent is being deleted. When deletion is complete, a green success banner will appear.
  3. Delete all the other resources
    • Use cdk destroy -all to delete the app and all stacks associated with it.

Conclusion

The integration of generative AI for remediating security findings is an effective approach, allowing SecOps teams to scale better and remediate findings in a timely manner. Using the generative AI capabilities of Amazon Bedrock alongside AWS services such as AWS Security Hub and automation, a capability of AWS Systems Manager, allows organizations to quickly remediate security findings by building automations that align with best practices while minimizing development effort. This approach not only streamlines security operations but also embeds a CI/CD approach for remediating security findings.

The solution in this post equips you with a plausible pattern of AWS Security Hub and AWS Systems Manager integrated with Amazon Bedrock, deployment code, and instructions to help remediate security findings efficiently and securely according to AWS best practices.

Ready to start your cloud migration process with generative AI in Amazon Bedrock? Begin by exploring the Amazon Bedrock User Guide to understand how you can use Amazon Bedrock to streamline your organization’s cloud journey. For further assistance and expertise, consider using AWS Professional Services to help you accelerate remediating AWS Security Hub findings and maximize the benefits of Amazon Bedrock.


About the Authors

Shiva Vaidyanathan is a Principal Cloud Architect at AWS. He provides technical guidance for customers ensuring their success on AWS. His primary expertise include Migrations, Security, GenAI and works towards making AWS cloud adoption simpler for everyone. Prior to joining AWS, he has worked on several NSF funded research initiatives on performing secure computing in public cloud infrastructures. He holds a MS in Computer Science from Rutgers University and a MS in Electrical Engineering from New York University.

Huzaifa Zainuddin is a Senior Cloud Infrastructure Architect at AWS, specializing in designing, deploying, and scaling cloud solutions for a diverse range of clients. With a deep expertise in cloud infrastructure and a passion for leveraging the latest AWS technologies, he is eager to help customers embrace generative AI by building innovative automations that drive operational efficiency. Outside of work, Huzaifa enjoys traveling, cycling, and exploring the evolving landscape of AI.

Read More