Building a GenAI Blog Generator with AWS Bedrock, Amazon S3, Lambda, API Gateway, and Postman

Creating a blog generator using AWS Bedrock, Amazon S3, AWS Lambda, API Gateway, and Postman combines the power of generative AI with serverless architecture. Follow this step-by-step guide to set up the system.
Step 1: Create the Lambda Function
Lambda Function Code
Below is the Python code for the Lambda function:
import boto3
import botocore.config
import json
from datetime import datetime
def blog_generate_using_bedrock(blogtopic: str) -> str:
prompt = f"""<s>[INST]Human: Write a 200 words blog on the topic {blogtopic}
Assistant:[/INST]
"""
body = {
"prompt": prompt,
"max_gen_len": 512,
"temperature": 0.5,
"top_p": 0.9
}
try:
bedrock = boto3.client("bedrock-runtime", region_name="us-east-1",
config=botocore.config.Config(read_timeout=300, retries={'max_attempts': 3}))
response = bedrock.invoke_model(body=json.dumps(body), modelId="meta.llama2-13b-chat-v1")
response_content = response.get('body').read()
response_data = json.loads(response_content)
print(response_data)
blog_details = response_data['generation']
return blog_details
except Exception as e:
print(f"Error generating the blog:{e}")
return ""
def save_blog_details_s3(s3_key, s3_bucket, generate_blog):
s3 = boto3.client('s3')
try:
s3.put_object(Bucket=s3_bucket, Key=s3_key, Body=generate_blog)
print("Blog saved to S3")
except Exception as e:
print(f"Error saving blog to S3: {e}")
def lambda_handler(event, context):
event = json.loads(event['body'])
blogtopic = event['blog_topic']
generate_blog = blog_generate_using_bedrock(blogtopic=blogtopic)
if generate_blog:
current_time = datetime.now().strftime('%H%M%S')
s3_key = f"blog-output/{current_time}.txt"
s3_bucket = 'aws_bedrock_course1'
save_blog_details_s3(s3_key, s3_bucket, generate_blog)
else:
print("No blog was generated")
return {
'statusCode': 200,
'body': json.dumps('Blog generation completed')
}
Step 2: Build the Code in VS Code
Set up a Conda Environment:
conda create -p venv python==3.12 conda activate ./venv
Create Project Files:
Install Dependencies:
Add
boto3
andbotocore
torequirements.txt
:boto3 botocore
Install dependencies using:
pip install -r requirements.txt
Deploy the Code to Lambda:
Paste the code into the AWS Lambda console.
Add a layer to the Lambda function with an updated
boto3
library if needed.
Step 3: Set Up API Gateway
Create an HTTP API:
Open the API Gateway console.
Create a new HTTP API.
Add a POST Route:
- Define a POST route (e.g.,
/generate-blog
).
- Define a POST route (e.g.,
Attach Integration:
- Connect the POST route to the Lambda function.
Step 4: Create an S3 Bucket
Open the S3 console and create a bucket (e.g.,
aws_bedrock_course1
).Ensure the bucket has appropriate permissions for the Lambda function to write to it.
Step 5: Test Using Postman
Open Postman and create a new POST request.
Enter the API Gateway URL in the request URL field.
Go to the Body tab and select raw with JSON format.
Add the following payload:
{ "blog_topic": "The future of AI in education" }
Send the request. Check the S3 bucket for the generated blog.
Common Errors and Solutions
Error: Insufficient Lambda Permissions:
- Ensure the Lambda execution role has AdministratorAccess or at least permissions for
S3
andBedrock
actions.
- Ensure the Lambda execution role has AdministratorAccess or at least permissions for
Error: Unable to Invoke Model:
- Check if Bedrock is enabled in your AWS account and ensure the region matches (
us-east-1
in this example).
- Check if Bedrock is enabled in your AWS account and ensure the region matches (
Error Saving to S3:
- Verify the S3 bucket exists and the Lambda function has
s3:PutObject
permissions.
- Verify the S3 bucket exists and the Lambda function has
With this setup, you can generate blogs using AWS Bedrock's AI capabilities, save them in S3, and test the workflow seamlessly through Postman!
Subscribe to my newsletter
Read articles from Shivam Soni directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Shivam Soni
Shivam Soni
๐I enjoy sharing my insights and experiences with the community through blog posts and speaking engagements.๐๐ฌ My goal is to empower teams to achieve operational excellence through DevOps best practices and a culture of continuous improvement.๐ช๐