首页 > 解决方案 > Versioning using base path mappings in AWS API Gateway

问题描述

I have a pretty straightforward stack: API Gateway sitting in front of a lambda. Currently my paths looks something like: /dogs, /dogs/{id}, etc.

All I want to do is add a version to the base path (i.e. api.dogs.com/v1/dogs). I tried doing this by creating a custom domain name with a base path mapping of v1 pointing to my stage in API Gateway.

This routes just fine through API Gateway but has issues once it hits the routing logic in my lambda. My lambda is expecting /dogs but with the base path mapping the path is actually v1/dogs.

What's a good way to approach this? I want to get away from having to deal with versions directly in my code (lambda) if possible.

标签: amazon-web-servicesaws-lambdaaws-api-gateway

解决方案


event您的 lambda 函数接收的对象中,您实际上应该找到所有需要的信息,无论是否有版本控制:

event = {
  "resource": "/hi",
  "path": "/v1/hi",
  "requestContext": {   
    "resourcePath": "/hi",
    "path": "/v1/hi",
    ....
  },
  ....
}

只需调整路由器逻辑中的代码以访问所需的属性即可解决您的问题,并且无需再次关注代码中的版本控制。


推荐阅读