Last active
November 27, 2025 14:27
-
-
Save DDuarte/6bd7e3851e6519c988037f883f80eeb1 to your computer and use it in GitHub Desktop.
Cloudflare AI Gateway with Boto3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import boto3 | |
| import botocore | |
| from botocore import UNSIGNED | |
| from botocore.config import Config | |
| CF_AIG_TOKEN = "XYZ" # https://developers.cloudflare.com/ai-gateway/configuration/authentication/ | |
| CF_ACCOUNT_ID = "xyz" | |
| CF_AIG_NAME = "xyz" | |
| AWS_REGION = "us-east-1" | |
| # requires BYOK for Amazon Bedrock (https://developers.cloudflare.com/ai-gateway/configuration/bring-your-own-keys/) since we do not do signatures here | |
| def _add_header(request, **kwargs): | |
| """https://stackoverflow.com/questions/58828800/adding-custom-headers-to-all-boto3-requests""" | |
| request.headers.add_header("cf-aig-authorization", f"Bearer {CF_AIG_TOKEN}") | |
| bedrock_runtime = boto3.client( | |
| "bedrock-runtime", | |
| region_name=AWS_REGION, | |
| endpoint_url=f"https://gateway.ai.cloudflare.com/v1/{CF_ACCOUNT_ID}/{CF_AIG_NAME}/aws-bedrock/bedrock-runtime/{AWS_REGION}", | |
| config=Config(signature_version=UNSIGNED), | |
| ) | |
| bedrock_runtime.meta.events.register_first("before-sign.*.*", _add_header) | |
| print( | |
| bedrock_runtime.converse( | |
| modelId="amazon.nova-pro-v1:0", | |
| ... | |
| ) | |
| ) | |
| print( | |
| bedrock_runtime.invoke_model( | |
| modelId="amazon.nova-pro-v1:0", | |
| contentType="application/json", # mandatory due to AIG "bug" | |
| body=..., | |
| ) | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment