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.
| Environment | Credentials | Notes |
|---|---|---|
| Lambda | Execution role | Reuse clients; timeout above generation time |
| ECS / Fargate | Task role | Long-running services; streaming servers |
| EKS | Pod Identity or IRSA role | Kubernetes-based platforms |
| EC2 | Instance profile | Custom servers |
| On premises | IAM Roles Anywhere or federation | No long-term access keys |
The following diagram compares the configuration of a summarisation function that timed out with the corrected configuration.

Synchronous calls that fail
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
}Validating requests early
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.
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.