In the critical moments of a production incident, engineering teams face a formidable challenge: navigating a deluge of log data to find the needle in the haystack. Traditional log analysis demands that engineers formulate precise, often complex, queries using specialized languages. This is effective when you know what to look for, but the real difficulty often lies in diagnosing the "unknown unknowns" - unexpected failures not captured by simple keyword searches.
What if you could ask questions in plain English, like, "What were the most common errors for the checkout service in the last 15 minutes?" This article demonstrates how to build a powerful, serverless AIOps pipeline on AWS to create a natural language interface for your application logs, transforming log analysis from a rigid, query-based task into an intuitive, conversational experience.
This solution leverages a powerful pattern in generative AI known as Retrieval-Augmented Generation (RAG). RAG enhances the capabilities of Large Language Models (LLMs) by connecting them to external knowledge sources - in this case, your real-time application logs. This approach is highly cost-effective as it avoids expensive model retraining, instead providing the LLM with relevant, live context to answer questions accurately.
The system is composed of a series of integrated, serverless AWS services that form a complete AIOps pipeline, from ingestion to a conversational response.
The data flows as follows:
The core of the data processing is a seamless, serverless flow between the Amazon OpenSearch Ingestion pipeline and the embedding_lambda
function. This is how raw logs are enriched with semantic meaning before they are ever stored.
Here’s a step-by-step breakdown of their interaction:
processor
stage that points to our embedding_lambda
function. When the pipeline receives log data, it automatically invokes this Lambda, passing the batch of log records to it.embedding_lambda
function executes its logic: it iterates through each log, extracts the text, and makes an API call to Amazon Bedrock's Titan Text Embeddings model. Bedrock returns a numerical vector (the embedding) that captures the log's meaning.log_embedding
) to the original log record.sink
- the OpenSearch Serverless vector collection - where it is indexed and becomes available for semantic search.The embedding_lambda
is a small but critical piece of the pipeline. Its sole job is to enrich the log data with semantic meaning. Triggered by the OpenSearch Ingestion pipeline for every new batch of logs, it performs three key steps:
log_embedding
, and returns the modified batch to the ingestion pipeline, which then stores it in OpenSearch.This function acts as a serverless, on-demand transformation engine, making our logs "smart" before they are even indexed.
def generate_embedding(text): body = json.dumps({"inputText": text}) model_id = 'amazon.titan-embed-text-v2:0' try: response = bedrock_runtime.invoke_model( body=body, modelId=model_id, accept='application/json', contentType='application/json' ) response_body = json.loads(response.get('body').read()) return response_body.get('embedding') except Exception as e: print(f"Error generating embedding: {e}") return None def lambda_handler(event, context): for record in event: log_data = record.get('data', {}) log_message = log_data.get('message', '') if log_message: embedding = generate_embedding(log_message) if embedding: # Add the new embedding vector to the log data log_data['log_embedding'] = embedding ...
We use an Amazon OpenSearch Serverless collection as our vector database. Its Vector search
collection type is optimized for the high-performance similarity searches (k-NN) we need.
For this to work, we must configure the index mapping to treat our log_embedding
field as a vector. This tells OpenSearch how to index the vector for efficient searching.
Here is a sample index mapping, which you would typically define in your Terraform configuration:
"log_embedding": { "type": "knn_vector", "dimension": 1024, "method": { "name": "hnsw", "engine": "faiss", "space_type": "l2", "parameters": { "ef_construction": 512, "m": 16 } } }
The Git repository is structured using a modular approach, which is a best practice that promotes reusability and maintainability.
├── README.md ├── envs/ │ ├── dev/ │ │ ├── main.tf │ │ └── terraform.tfvars ├── modules/ │ ├── iam/ │ ├── ingestion_pipeline/ │ ├── embedding_lambda/ │ └── opensearch/ └── src/ ├── embedding_lambda/ └── streamlit_app/
A simple web application built with Streamlit serves as the user-facing component. The quality of the final answer is heavily dependent on the quality of the prompt sent to the Claude model. A simple "Answer the question" prompt is insufficient. Instead, a robust prompt template is used to guide the model's behavior.
File: src/streamlit_app/app.py
(logic for generating the answer)
def get_llm_response(question, logs): log_context = "\n".join(logs) prompt = f""" You are an expert AIOps assistant. Your task is to answer questions about application behavior based *only* on the provided log entries. Do not use any prior knowledge. If the answer cannot be found in the logs, you must state 'I cannot answer the question based on the provided logs.' Here are the relevant log entries retrieved: <logs> {log_context} </logs> Based on the logs above, please answer the following question: <question> {question} </question> """ body = json.dumps({ "anthropic_version": "bedrock-2023-05-31", "max_tokens": 4096, "messages": [{"role": "user", "content": prompt}] }) response = bedrock_runtime.invoke_model(body=body, modelId=BEDROCK_MODEL_ID_CLAUDE) response_body = json.loads(response.get('body').read()) return response_body['content']['text']
This serverless RAG solution represents a new approach to log analysis, with different strategic considerations compared to traditional tools.
The AIOps RAG architecture shifts the cost model. The cost of ingesting and creating embeddings for logs is relatively low. The primary cost driver is the LLM inference at query time. Each user question triggers an API call to the Claude model with a context of retrieved logs. This means the system's operational cost is driven not by log volume, but by query volume and complexity. This makes the system ideal for high-value, deep-investigation queries during incidents, rather than high-frequency, dashboard-style monitoring.
The vector embeddings generated during ingestion are a valuable data asset that can be leveraged for capabilities far beyond simple question-answering.
The serverless RAG architecture presented here offers a transformative approach to log analysis on AWS. By combining the scalable vector search of Amazon OpenSearch Serverless with the advanced reasoning of foundation models on Amazon Bedrock, organizations can build powerful, conversational interfaces for their observability data. This approach lowers the barrier to deep log analysis, empowers a wider range of team members to participate in incident investigation, and opens the door to a new class of intelligent AIOps tools.
\