15.5 C
London
Friday, September 20, 2024

Unlock AWS Cost and Usage Insights with Amazon Bedrock’s Generative AI

Introduction

Managing cloud costs and understanding resource usage can be a daunting task, especially for organizations with complex AWS deployments. AWS Cost and Usage Reports (AWS CUR) provides valuable data insights, but interpreting and querying the raw data can be challenging. In this post, we explore a solution that uses generative artificial intelligence (AI) to generate a SQL query from a user’s question in natural language.

Managing Cloud Costs with Generative AI

The solution uses Amazon Bedrock, a fully managed service that offers a choice of high-performing foundation models (FMs) from leading 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.

Challenges Addressed

The following challenges can hinder organizations from effectively analyzing their CUR data, leading to potential inefficiencies, overspending, and missed opportunities for cost-optimization. We aim to target and simplify them using generative AI with Amazon Bedrock.

  • Complexity of SQL queries – Writing SQL queries to extract insights from CUR data can be complex, especially for non-technical users or those unfamiliar with the CUR data structure (unless you’re a seasoned database administrator)
  • Data accessibility – To gain insights from structured data in databases, users need to get access to databases, which can be a potential threat to overall data protection
  • User-friendliness – Traditional methods of analyzing CUR data often lack a user-friendly interface, making it challenging for non-technical users to take advantage of the valuable insights hidden within the data

Solution Overview

The solution that we discuss is a web application (chatbot) that allows you to ask questions related to your AWS costs and usage in natural language. The application generates SQL queries based on the user’s input, runs them against an Athena database containing CUR data, and presents the results in a user-friendly format. The solution combines the power of generative AI, SQL generation, database querying, and an intuitive web interface to provide a seamless experience for analyzing CUR data.

Solution Architecture

The data flow consists of the following steps:

  1. The CUR data is stored in Amazon S3.
  2. Athena is configured to access and query the CUR data stored in Amazon S3.
  3. The user interacts with the Streamlit web application and submits a natural language question related to AWS costs and usage.

Figure 2. Shows the Chatbot Dashboard to ask question

  1. The Streamlit application sends the user’s input to Amazon Bedrock, and the LangChain application facilitates the overall orchestration.
  2. The LangChain code uses the BedrockChat class from LangChain to invoke the FM and interact with Amazon Bedrock to generate a SQL query based on the user’s input.
Figure 3. Shows initialization of SQL chain

Figure 3. Shows initialization of SQL chain

  1. The generated SQL query is run against the Athena database using the FM on Amazon Bedrock, which queries the CUR data stored in Amazon S3.
  2. The query results are returned to the LangChain application.
Figure 4. Shows generated Query in the application output logs

Figure 4. Shows generated Query in the application output logs

  1. LangChain sends the SQL query and query results back to the Streamlit application.
  2. The Streamlit application displays the SQL query and query results to the user in a formatted and user-friendly manner.
Figure 5. Shows final output presented on the chat bot webapp including SQL Query and the Query results

Figure 5. Shows final output presented on the chat bot webapp including SQL Query and the Query results

Prerequisites

To set up this solution, you should have the following prerequisites:

Configure the Solution

Complete the following steps to set up the solution:

  1. Create an Athena database and table to store your CUR data. Make sure the necessary permissions and configurations are in place for Athena to access the CUR data stored in Amazon S3.
  2. Set up your compute environment to call Amazon Bedrock APIs. Make sure you associate an IAM role with this environment that has IAM policies that grant access to Amazon Bedrock.
  3. When your instance is up and running, install the following libraries:
import streamlit as st
from cur_lib import get_response
import os

# Create a session state variable to store the chat history
if "chat_history" not in st.session_state:
    st.session_state.chat_history = []

user_input = st.text_input("You:", key="user_input")

if user_input:
    try:
        result = get_response(user_input)
        sql_query, sql_result = format_result(result)
        st.code(sql_query, language="sql")
        if sql_result:
            st.write("SQLResult:")
            st.table(sql_result)
        else:
            st.write(result)
        st.session_state.chat_history.append({"user": user_input, "bot": result})
        st.text_area("Conversation:", value="n".join([f"You: {chat['user']}nBot: {chat['bot']}" for chat in st.session_state.chat_history]), height=300)
    except Exception as e:
        st.error(str(e))

if __name__ == "__main__":
    main()

Clean Up

Unless you invoke Amazon Bedrock with this solution, you won’t incur charges for it. To avoid ongoing charges for Amazon S3 storage for saving the CUR reports, you can remove the CUR data and S3 bucket. If you set up the solution using Amazon EC2, make sure you stop or delete the instance when you’re done.

Benefits

This solution offers the following benefits:

  • Simplified data analysis – You can analyze CUR data using natural language using generative AI, eliminating the need for advanced SQL knowledge
  • Increased accessibility – The web-based interface makes it efficient for non-technical users to access and gain insights from CUR data without needing credentials for the database
  • Time-saving – You can quickly get answers to your cost and usage questions without manually writing complex SQL queries
  • Enhanced visibility – The solution provides visibility into AWS costs and usage, enabling better cost-optimization and resource management decisions

Conclusion

The AWS CUR chatbot solution uses Anthropic Claude on Amazon Bedrock to generate SQL queries, database querying, and a user-friendly web interface to simplify the analysis of CUR data. By allowing you to ask natural language questions, the solution removes barriers and empowers both technical and non-technical users to gain valuable insights into AWS costs and resource usage.

Frequently Asked Questions

Q1: What is the purpose of this solution?

This solution is designed to simplify the process of analyzing AWS Cost and Usage Reports (CUR) data using natural language and generative AI.

Q2: What are the benefits of this solution?

This solution offers several benefits, including simplified data analysis, increased accessibility, time-saving, and enhanced visibility into AWS costs and usage.

Q3: What is the prerequisite for setting up this solution?

You should have an Athena database and table to store your CUR data, and a compute environment to call Amazon Bedrock APIs.

Q4: How does the solution work?

The solution uses a web application (chatbot) that generates SQL queries based on the user’s input, runs them against an Athena database containing CUR data, and presents the results in a user-friendly format.

Q5: Can I customize this solution?

Yes, you can customize this solution by modifying the code and adapting it to your specific use case.

Latest news
Related news
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x