Creating resources using Terraform

Navigate to the folder containing the main.tf file and download the zip file containing the Lambda function that will be used by Bedrock

cd challenge-day2/backend/src/lambda
cp list_products.zip ../../../../terraform-project/
cd ../../../../terraform-project

Add the following lines at the end of the main.tf file

# IAM Role for Lambda function
resource "aws_iam_role" "lambda_role" {
  name = "cloudmart_lambda_role"

  assume_role_policy = jsonencode({
    Version = "2012-10-17"
    Statement = [
      {
        Action = "sts:AssumeRole"
        Effect = "Allow"
        Principal = {
          Service = "lambda.amazonaws.com"
        }
      }
    ]
  })
}

# IAM Policy for Lambda function
resource "aws_iam_role_policy" "lambda_policy" {
  name = "cloudmart_lambda_policy"
  role = aws_iam_role.lambda_role.id

  policy = jsonencode({
    Version = "2012-10-17"
    Statement = [
      {
        Effect = "Allow"
        Action = [
          "dynamodb:Scan",
          "logs:CreateLogGroup",
          "logs:CreateLogStream",
          "logs:PutLogEvents"
        ]
        Resource = [
          aws_dynamodb_table.cloudmart_products.arn,
          aws_dynamodb_table.cloudmart_orders.arn,
          aws_dynamodb_table.cloudmart_tickets.arn,
          "arn:aws:logs:*:*:*"
        ]
      }
    ]
  })
}

# Lambda function for listing products
resource "aws_lambda_function" "list_products" {
  filename         = "list_products.zip"
  function_name    = "cloudmart-list-products"
  role             = aws_iam_role.lambda_role.arn
  handler          = "index.handler"
  runtime          = "nodejs20.x"
  source_code_hash = filebase64sha256("list_products.zip")

  environment {
    variables = {
      PRODUCTS_TABLE = aws_dynamodb_table.cloudmart_products.name
    }
  }
}

# Lambda permission for Bedrock
resource "aws_lambda_permission" "allow_bedrock" {
  statement_id  = "AllowBedrockInvoke"
  action        = "lambda:InvokeFunction"
  function_name = aws_lambda_function.list_products.function_name
  principal     = "bedrock.amazonaws.com"
}

# Output the ARN of the Lambda function
output "list_products_function_arn" {
  value = aws_lambda_function.list_products.arn
}

Configuring the Amazon Bedrock Agent

Follow these steps to manually create the Bedrock Agent for CloudMart:

Model Access:

<aside> 🔇

Note: The video below has no audio and serves only as a reference for the step-by-step process.

</aside>

Screenshare - 2024-09-19 4_29_23 PM.mp4

  1. In the Amazon Bedrock console, go to "Model access" in the navigation panel.
  2. Choose "Enable specific models".
  3. Select the Claude 3 Sonnet model.
  4. Wait until the model access status changes to "Access granted".

Create the Agent:

  1. In the Amazon Bedrock console, choose "Agents" under "Builder tools" in the navigation panel.

  2. Click on "Create agent".

  3. Name the agent "cloudmart-product-recommendation-agent".

  4. Select "Claude 3 Sonnet" as the base model.

  5. Paste the agent instructions below in the "Instructions for the Agent" section.

    You are a product recommendations agent for CloudMart, an online e-commerce store. Your role is to assist customers in finding products that best suit their needs. Follow these instructions carefully:
    
    1. Begin each interaction by retrieving the full list of products from the API. This will inform you of the available products and their details.
    
    2. Your goal is to help users find suitable products based on their requirements. Ask questions to understand their needs and preferences if they're not clear from the user's initial input.
    
    3. Use the 'name' parameter to filter products when appropriate. Do not use or mention any other filter parameters that are not part of the API.
    
    4. Always base your product suggestions solely on the information returned by the API. Never recommend or mention products that are not in the API response.
    
    5. When suggesting products, provide the name, description, and price as returned by the API. Do not invent or modify any product details.
    
    6. If the user's request doesn't match any available products, politely inform them that we don't currently have such products and offer alternatives from the available list.
    
    7. Be conversational and friendly, but focus on helping the user find suitable products efficiently.
    
    8. Do not mention the API, database, or any technical aspects of how you retrieve the information. Present yourself as a knowledgeable sales assistant.
    
    9. If you're unsure about a product's availability or details, always check with the API rather than making assumptions.
    
    10. If the user asks about product features or comparisons, use only the information provided in the product descriptions from the API.
    
    11. Be prepared to assist with a wide range of product inquiries, as our e-commerce store may carry various types of items.
    
    12. If a user is looking for a specific type of product, use the 'name' parameter to search for relevant items, but be aware that this may not capture all categories or types of products.
    
    Remember, your primary goal is to help users find the best products for their needs from what's available in our store. Be helpful, informative, and always base your recommendations on the actual product data provided by the API.