DEV Community

KomalLM
KomalLM

Posted on

Build a Generative AI App with AWS Bedrock (Step-by-Step Guide)

Generative AI is changing how we build applications and now, you don’t need deep ML expertise to get started.

With AWS Bedrock, part of Amazon Web Services (AWS), you can integrate powerful foundation models into your apps using simple APIs.

In this hands-on tutorial, we’ll build a serverless AI text generator using:

  • AWS Bedrock
  • AWS Lambda
  • Amazon API Gateway

By the end, you’ll have a working AI-powered API 🚀


Prerequisites

Before we begin, make sure you have:

  • An AWS account
  • Access to AWS Bedrock (request access if needed)
  • Basic knowledge of Python or JavaScript
  • AWS CLI configured

Architecture Overview

Here’s what we’re building:

  1. User sends a prompt (e.g., “Write a blog intro”)
  2. API Gateway receives the request
  3. Lambda processes it
  4. Lambda calls AWS Bedrock
  5. Bedrock returns AI-generated text

Simple, scalable, and fully serverless.


Step 1: Enable AWS Bedrock

  1. Go to AWS Console
  2. Search for AWS Bedrock
  3. Request access to available foundation models
  4. Wait for approval (usually quick)

Step 2: Create a Lambda Function

  1. Go to AWS Lambda

  2. Click Create Function

  3. Choose:

  • Runtime: Python 3.11
  • Architecture: x86
  1. Add permissions:
  • Attach a role with Bedrock access (bedrock:InvokeModel)

🧠 Step 3: Add Bedrock Code (Python Example)

Paste this into your Lambda function:

import json
import boto3

bedrock = boto3.client("bedrock-runtime")

def lambda_handler(event, context):
    prompt = event.get("prompt", "Write something creative about AWS.")

    response = bedrock.invoke_model(
        modelId="anthropic.claude-v2",
        body=json.dumps({
            "prompt": f"\n\nHuman: {prompt}\n\nAssistant:",
            "max_tokens_to_sample": 200
        }),
        contentType="application/json",
        accept="application/json"
    )

    result = json.loads(response["body"].read())

    return {
        "statusCode": 200,
        "body": json.dumps({
            "response": result.get("completion")
        })
    }
Enter fullscreen mode Exit fullscreen mode

👉 This code sends a prompt to Bedrock and returns AI-generated text.


🌐 Step 4: Create API Gateway

  1. Go to API Gateway
  2. Create a HTTP API
  3. Connect it to your Lambda function
  4. Add a POST route (e.g., /generate)

Deploy the API and copy the endpoint URL.


🧪 Step 5: Test Your API

Use Postman or curl:

curl -X POST https://your-api-url/generate \
-H "Content-Type: application/json" \
-d '{"prompt": "Write a motivational quote"}'
Enter fullscreen mode Exit fullscreen mode

🎉 You should receive an AI-generated response!


💡 Step 6: Improve Your Prompts

Prompt engineering is key. Try:

  • “Write a professional LinkedIn post about AWS”
  • “Explain cloud computing to a 10-year-old”
  • “Generate 3 startup ideas using AI”

Better prompts = better results.


🔮 What You Just Built

You created a serverless AI application using:

  • API Gateway (entry point)
  • Lambda (logic)
  • Bedrock (AI brain)

This pattern can be reused for:

  • Chatbots
  • Content generators
  • AI assistants
  • Learning tools

🌟 Final Thoughts

AWS Bedrock makes it incredibly easy to bring generative AI into real applications.

In just a few steps, you went from idea → working AI API.

That’s the power of combining serverless + AI on AWS.

Happy Learning.

Top comments (0)