Lesson 1 of 4 FM API Integration Patterns

Invoking Foundation Models Synchronously

This lesson covers synchronous invocation of Amazon Bedrock from different compute environments, the simplest integration, for interactive use where generation completes within the caller's timeout.

Learning outcomes

After completing this lesson, you will be able to:

  • Choose between Converse and InvokeModel.
  • Call Bedrock correctly from Lambda, containers and EC2.
  • Validate requests with API Gateway before they cost tokens.

Bedrock Runtime APIs

Applications invoke models through the bedrock-runtime endpoint, which offers two synchronous APIs. Converse uses a provider-neutral messages format, so the same code works with every model that supports it. InvokeModel takes the request body in each provider's native format, which gives access to features a provider exposes only in its own format. Both return the complete response once generation has finished, together with token usage.

Calls are signed with AWS Signature Version 4 using credentials from the environment, typically an IAM role, which the AWS SDKs handle automatically. Amazon Bedrock API keys offer an alternative bearer-token authentication for development and for some third-party tools, but IAM roles remain the recommended method for applications running on AWS, because they provide temporary, automatically rotated credentials.

Calling from different compute environments

Lambda functions call Bedrock with the function's execution role and suit event-driven and API-backed use. Create the SDK client outside the handler so connections are reused across invocations, and set the function timeout above the expected generation time. Containers on ECS, EKS or Fargate use task or pod roles (for example EKS Pod Identity) and suit long-running services and streaming servers. EC2 instances use instance profiles. On-premises applications use temporary credentials from IAM Roles Anywhere or federation, never long-term access keys.

Language-specific AWS SDKs (Boto3 for Python, the AWS SDK for JavaScript, Java, .NET, Go and others) expose the same operations, with retry and timeout configuration. The following table summarises the options.

EnvironmentCredentialsNotes
LambdaExecution roleReuse clients; timeout above generation time
ECS / FargateTask roleLong-running services; streaming servers
EKSPod Identity or IRSA roleKubernetes-based platforms
EC2Instance profileCustom servers
On premisesIAM Roles Anywhere or federationNo long-term access keys

The following diagram compares the configuration of a summarisation function that timed out with the corrected configuration.

Fixing summarisation timeouts in Lambda. The function used the default 3-second timeout, far below generation time, and created a new SDK client on every invocation. Raising the function and SDK read timeouts and creating the client outside the handler fix it; generation longer than the API's integration timeout needs streaming or an asynchronous design.
Lambda's default timeout is 3 seconds.
Fix the problem

Synchronous calls that fail

Exam skills 2.4.1 · 5.2.2

Find the fix for each problem with synchronous model calls.

Log in to try this activity Interactive activities are free with an account.

Request validation with API Gateway

When clients call through API Gateway, request validation rejects malformed requests before they reach the backend and cost tokens. A REST API can validate the body against a JSON Schema model, requiring fields and limiting string lengths (a crude but effective input size limit), and validate required headers and query parameters. API Gateway can also generate client SDKs from the API definition, giving custom clients a typed interface.

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "required": ["use_case", "text"],
  "properties": {
    "use_case": {"type": "string", "enum": ["summarize", "classify", "draft_reply"]},
    "text": {"type": "string", "minLength": 1, "maxLength": 20000}
  },
  "additionalProperties": false
}
True or false

Validating requests early

Exam skills 2.4.1 · 2.5.1

Check three claims about validating FM requests in API Gateway.

Log in to try this activity Interactive activities are free with an account.
Worked example

Timeouts in Lambda

A Lambda function behind API Gateway summarises documents with Converse. Long documents fail with a Lambda timeout after 3 seconds, and a new SDK client is created on every invocation.

The function's timeout is the default of 3 seconds, far below generation time; raising it (and the SDK read timeout) fixes the failures, and creating the client outside the handler reuses connections. If generation can exceed the API's integration timeout, the design should switch to streaming or an asynchronous pattern.

Check yourself

Where should a Lambda function create its Bedrock client?

Show answer

Outside the handler, so it is reused across invocations of the same execution environment.

Exam Tips

Remember for the exam

  • Converse for provider-neutral calls; InvokeModel for native bodies and embeddings.
  • Use IAM roles for credentials in every compute environment.
  • Create SDK clients outside the Lambda handler.
  • Validate requests in API Gateway before they cost tokens.