AWS CDK: Use Amazon Bedrock with AWS Lambda & API Gateway

October 30th, 2023 351 Words

After using Amazon Bedrock Generative AI with the AWS CLI and AWS JavaScript SDK, this guide will explain how to create an Amazon API Gateway and use the AWS Cloud Development Kit and AWS Lambda to invoke Amazon Bedrock.

Based on the example for AWS JavaScript SDK, you can modify the sendPromp function for the AWS Lambda use case:

import {
  BedrockRuntimeClient,
  InvokeModelCommand,
  InvokeModelCommandInput
} from "@aws-sdk/client-bedrock-runtime";

const client = new BedrockRuntimeClient({
  region: "us-east-1",
});

export const sendPrompt = async (prompt: string) => {
  const input: InvokeModelCommandInput = {
    body: JSON.stringify({
      prompt,
      "maxTokens": 300,
      "temperature": 0.5,
      "topP": 0.9
    }),
    contentType: 'application/json',
    modelId: "ai21.j2-mid-v1",
  };

  const command = new InvokeModelCommand(input);
  const response = await client.send(command);

  return JSON.parse(Buffer.from(response.body).toString()).completions[0]
}

Within your AWS Cloud Development Kit project, create a new TypeScript file for your AWS Lambda function:

import { APIGatewayProxyHandlerV2 } from 'aws-lambda'
import { sendPrompt } from '../scripts/invoke.ts'

export const handler: APIGatewayProxyHandlerV2 = async (event) => {
  const response = await sendPrompt("Please generate a funny Haiku about software engineering.")

  return response.data.text.trim()
}

Next, use the CDK to create additional AWS resources:

  • Amazon API Gateway
  • AWS Lambda function
  • Lambda Integration for API
  • CloudFormation Output

Altogether, this is the infrastructure:

import { HttpApi, HttpMethod } from '@aws-cdk/aws-apigatewayv2-alpha';
import { HttpLambdaIntegration } from '@aws-cdk/aws-apigatewayv2-integrations-alpha';
import { App, CfnOutput, Stack, StackProps } from 'aws-cdk-lib';
import { Effect, PolicyStatement } from 'aws-cdk-lib/aws-iam';
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs';

export class Components extends Stack {
  constructor(scope: App, id: string, props?: StackProps) {
    super(scope, id, props)

    const func = new NodejsFunction(this, 'invoke', {
      entry: 'functions/invoke.ts',
      handler: 'handler',
      initialPolicy: [
        new PolicyStatement({
          effect: Effect.ALLOW,
          actions: ['bedrock:InvokeModel'],
          resources: ['*']
        })
      ]
    })

    const api = new HttpApi(this, 'api');
    
    api.addRoutes({
      path: '/prompt',
      methods: [ HttpMethod.POST ],
      integration: new HttpLambdaIntegration('integration', func),
    });
    
    new CfnOutput(this, 'endpoint', {
      value: api.apiEndpoint
    })
  }
}

Using cURL, you can send a simple HTTP POST request to invoke the Amazon Bedrock model:

$ > curl -X POST https://a1b2c3.execute-api.us-east-1.amazonaws.com/prompt

Software engineering haiku
Debugging code all day
Still no solution in sight

Enjoy! 🎉 Soon, Bedrock Agents will enable you to chain and orchestrate individual prompts and automate complex tasks …