AWS Logo
Menu

Mastering API Gateway: OpenAPI 3.0, VTL mapping and custom validations (Part 1/2)

Learn how to optimize AWS API Gateway using VTL mapping. Transform complex architectures, reduce costs, and boost performance with real-world examples and code snippets.

Alejandro
Amazon Employee
Published Mar 11, 2025

The Cost of Over-engineering

It was a typical Monday morning when Sarah, a lead developer at a growing e-commerce platform, reached out to me about their API performance issues. Their platform was handling thousands of product queries per minute, with each request following what seemed to be a "safe" architectural pattern:
Common architecture for retrieving DynamoDB data
Image 1: Common architecture for retrieving DynamoDB data
"It works," Sarah said, "but our costs are skyrocketing, and the latency is becoming noticeable."
Looking at their architecture, I spotted a common anti-pattern: using Lambda functions for operations that API Gateway could handle natively. They were spending approximately $2,500 monthly on Lambda invocations, with most functions doing simple data transformations and validations.

The Revelation: Less is More

What if I told you that you could eliminate those Lambda functions and reduce latency and costs significantly - all while maintaining the same functionality? This is where Velocity Template Language (VTL) mapping comes into play.

Understanding the power of VTL in API Gateway

When Sarah's team first approached their API performance challenges, they were following a common but inefficient pattern: using Lambda functions as intermediaries for nearly every API operation. This architecture, while functional, created unnecessary complexity and costs. To understand how VTL could help, I first needed to understand what operations their Lambda functions were actually performing:
  1. Request validation and sanitization
  2. Data transformation for DynamoDB queries
  3. Response formatting and filtering
  4. Error handling and response standardization
These operations, while crucial, don't necessarily require the full power (and cost) of Lambda functions. This realization led me to explore API Gateway's native transformation capabilities through VTL.

What is VTL and why should you care?

Velocity Template Language (VTL) is a powerful template engine integrated directly into Amazon API Gateway. While many developers view it as just a simple text processor, it's actually a robust transformation layer that can:
  1. Process Requests and Responses: VTL acts as both a preprocessor and postprocessor, allowing you to:
    • Transform incoming JSON/XML payloads
    • Modify request parameters and headers
    • Restructure response data
    • Implement conditional logic
  2. Interface with AWS Services: VTL provides native integration with AWS services through mapping templates:{
    "TableName": "Users",
    "Key": {
    "userId": {
    "S": "$input.params('id')"
    }
    }
    }
  3. Handle Complex Transformations: Beyond simple mappings, VTL supports:
    • Loops and conditionals
    • Variable assignments
    • JSON/XML parsing
    • String manipulation
    • Mathematical operations
  4. Provide Built-in Utilities: API Gateway exposes useful utilities through VTL:
    $util.escapeJavaScript() // For input sanitization
    $util.parseJson() // For JSON parsing
    $util.urlEncode() // For URL encoding
    $util.toJson() // For JSON serialization

The technical advantages

VTL's integration with API Gateway delivers a range of technical benefits that strongly influenced Sarah's team. From a performance standpoint, eliminating Lambda cold starts and reducing network hops makes a noticeable difference. Because VTL runs natively within API Gateway, requests can be transformed and processed directly without extra network calls, leading to a significant reduction in latency (also without extra costs).
Key performance benefits:
  • Complete removal of Lambda cold starts
  • Fewer network hops and lower latency
Cost efficiency was another critical factor. By handling transformations at the API Gateway level, the team was able to avoid numerous Lambda invocations, resulting in considerable cost savings on both compute and data transfer.
Security also saw marked improvements with the adoption of VTL. Centralizing request and response transformations minimizes potential vulnerability points. Moreover, VTL's built-in escaping and sanitization functions provide robust protection against common injection attacks, ensuring a consistent security approach across all endpoints.
  • Native execution within API Gateway
Critical security improvements:
  • Centralized request/response handling
  • Built-in input sanitization and escaping functions
  • A reduced attack surface
Consider the architectural evolution:
Before:
Previous architecture
Image 2: Previous architecture
After:
Modernized and optimized architecture using Data Models and VTL Mapping
Image 3: Modernized and optimized architecture using Data Models and VTL Mapping
This streamlined architecture not only enhances performance and security but also simplifies system maintenance and auditing. With transformation logic consolidated in one place, security reviews and updates become significantly easier to manage.

Understanding VTL Context Objects

VTL provides several context objects that give you access to request data:
$input // Access to the request payload and parameters
$context // Request context (requestId, accountId, etc.)
$util // Utility functions
$stageVariables // Access to API Gateway stage variables
Example of context usage:

The learning curve consideration

While VTL is powerful, it does require an initial investment in learning and understanding. Sarah's team found that the learning curve followed this pattern:
  1. Basic Transformations (1-2 days)
    • Simple request/response mapping
    • Basic parameter handling
  2. Intermediate Usage (3-5 days)
    • Conditional logic
    • Loop structures
    • Error handling
  3. Advanced Implementation (1-2 weeks)
    • Complex transformations
    • Dynamic queries
    • Performance optimization
However, the time invested in learning VTL was quickly recovered through reduced development and maintenance time for new API endpoints.
This understanding of VTL's capabilities and context laid the foundation for Sarah's team to begin their optimization journey. In the next section, we'll look at how they implemented these concepts in their actual API infrastructure.

Implementing VTL: A practical guide

After understanding VTL's capabilities, Sarah's team began their implementation journey. Let me walk you through how I helped transform their architecture, starting with the basics and progressing to more complex scenarios.

Basic request and response transformations

The team's first challenge was a simple product lookup endpoint. Originally, they had a Lambda function that received requests, validated them, transformed them for DynamoDB, and then formatted the response. Here's how we reimagined it using VTL.
For the request transformation, we created a mapping template that handles the basic DynamoDB query structure:
This simple template demonstrates several key concepts. The $input.params() function extracts parameters from the request, while the structure matches DynamoDB's expected format. The transformation happens directly in API Gateway, eliminating the need for a Lambda function.
For the response, we created a template that transforms DynamoDB's verbose JSON structure into a client-friendly format:

Advanced VTL techniques

As the team gained confidence, we tackled more complex scenarios. One particularly challenging endpoint needed to handle product searches with multiple optional filters. This required combining several advanced VTL features.
Here's how I guided them to implement dynamic query building:
This template demonstrates several advanced concepts: array manipulation, conditional logic, and dynamic query construction. The $util.qr() function is used for quiet references, preventing unwanted output during array operations.

Real-World examples

Let's look at three real scenarios from Sarah's application that showcase VTL's power.

Product catalog search

The team needed an endpoint that could search products with pagination and filtering. Here's the complete implementation:
Request template:
Response template:

Order creation with validation

For order creation, we needed extensive validation before saving to DynamoDB:
Request Template:

Error response standardization

To ensure consistent error handling across all endpoints, we created a standard error response template:
These real-world examples demonstrate how VTL can handle complex business requirements while maintaining clean, maintainable code. The team found that once they understood these patterns, implementing new endpoints became significantly faster and more consistent.

Known limitations and when not to use VTL

While VTL proved transformative for Sarah's team, it's crucial to understand its limitations. Through our implementation journey, we identified several scenarios where VTL might not be the best choice.
Complex business logic often requires more sophisticated processing than VTL can provide. For instance, when Sarah's team needed to implement a recommendation engine that analyzed purchase history and user behavior, they kept this logic in Lambda functions. VTL's template-based approach simply wasn't designed for such computational complexity.
Key scenarios where Lambda functions are preferred:
  • Multi-step transaction processing requiring ACID compliance
  • Machine learning model inference or complex calculations
  • Orchestration of multiple AWS services in a single request
  • Stateful operations requiring session management
External service integration presents another significant limitation. When the team needed to validate payments through a third-party payment processor, they couldn't do this directly through VTL. These scenarios require Lambda functions to handle the HTTP calls, manage retries, and process responses from external services.
Debugging VTL can be challenging, especially when dealing with complex transformations. Unlike Lambda functions where you can use standard debugging tools and log statements, VTL debugging often relies on CloudWatch logs that can be less intuitive. Sarah's team found that complex VTL templates were harder to troubleshoot, particularly when dealing with nested conditionals or complex data transformations.
Resource constraints also play a role. API Gateway imposes limits on mapping template size (300KB) and execution time (29 seconds for REST APIs). When the team encountered a scenario requiring processing of large JSON payloads with multiple transformations, they had to split the logic between VTL and Lambda to stay within these limits.

Conclusion and Next Steps

Sarah's journey from skepticism to successful VTL adoption demonstrates a crucial lesson in API development: sometimes the best solution isn't adding more components, but better utilizing existing tools. The transformation not only improved technical metrics but also changed how the team approaches API design.
Looking forward, the team has identified several exciting opportunities. They're exploring how to combine VTL with OpenAPI 3.0 specifications for even better API documentation and client generation. They're also investigating how to use VTL in combination with API Gateway's request validators to create more robust input validation patterns.
For teams considering a similar journey, start small. Begin with simple transformations and gradually move to more complex patterns as your team builds confidence. Document your patterns, share your learnings, and remember that VTL is just one tool in your API development toolkit.
As APIs continue to be the backbone of modern applications, understanding and effectively utilizing tools like VTL becomes increasingly important. The key is not to use VTL for everything, but to use it where it makes the most sense, creating efficient, maintainable, and cost-effective APIs.
Remember Sarah's initial skepticism? She recently commented, "What seemed like a complex template language at first has become one of our most valuable tools for API optimization. The key was understanding not just how to use it, but when to use it."
 

Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.

Comments