Amazon Bedrock allows you to build and scale generative AI applications with foundation models.
https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/aws-bedrock
When making requests to Amazon Bedrock, replace https://bedrock-runtime.us-east-1.amazonaws.com/
in the URL you’re currently using with https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/aws-bedrock/bedrock-runtime/us-east-1/
, then add the model you want to run at the end of the URL.
With Bedrock, you will need to sign the URL before you make requests to AI Gateway. You can try using the aws4fetch
SDK.
import { AwsClient } from 'aws4fetch'
async fetch ( request : Request , env : Env , ctx : ExecutionContext ) : Promise < Response > {
// replace with your configuration
const cfAccountId = "{account_id}" ;
const gatewayName = "{gateway_id}" ;
const region = 'us-east-1' ;
// added as secrets (https://developers.cloudflare.com/workers/configuration/secrets/)
const accessKey = env . accessKey ;
const secretKey = env . secretAccessKey ;
inputText : "What does ethereal mean?"
'Content-Type' : 'application/json'
// sign the original request
const stockUrl = new URL ( "https://bedrock-runtime.us-east-1.amazonaws.com/model/amazon.titan-embed-text-v1/invoke" )
const awsClient = new AwsClient ( {
secretAccessKey : secretKey ,
const presignedRequest = await awsClient . sign ( stockUrl . toString () , {
// change the signed request's host to AI Gateway
const stockUrlSigned = new URL ( presignedRequest . url ) ;
stockUrlSigned . host = "gateway.ai.cloudflare.com"
stockUrlSigned . pathname = `/v1/ ${ cfAccountId } / ${ gatewayName } /aws-bedrock/bedrock-runtime/ ${ region } /model/amazon.titan-embed-text-v1/invoke`
const response = await fetch ( stockUrlSigned , {
headers : presignedRequest . headers ,
body : JSON . stringify ( requestData )
if ( response . ok && response . headers . get ( 'content-type' ) ?. includes ( 'application/json' )) {
const data = await response . json () ;
return new Response ( JSON . stringify ( response )) ;
return new Response ( "Invalid response" , { status : 500 } ) ;