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:
- User sends a prompt (e.g., “Write a blog intro”)
- API Gateway receives the request
- Lambda processes it
- Lambda calls AWS Bedrock
- Bedrock returns AI-generated text
Simple, scalable, and fully serverless.
Step 1: Enable AWS Bedrock
- Go to AWS Console
- Search for AWS Bedrock
- Request access to available foundation models
- Wait for approval (usually quick)
Step 2: Create a Lambda Function
Go to AWS Lambda
Click Create Function
Choose:
- Runtime: Python 3.11
- Architecture: x86
- 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")
})
}
👉 This code sends a prompt to Bedrock and returns AI-generated text.
🌐 Step 4: Create API Gateway
- Go to API Gateway
- Create a HTTP API
- Connect it to your Lambda function
- 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"}'
🎉 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)