method/verb 使 AWS 无服务器功能独一无二
Make AWS serverless function unique by method/verb
我为我的用户进行了一些 CRUD 操作。我希望我的 URI 是 /user/{id} 并让 VERB 决定使用什么方法,例如。 post 然后放。
在我的 cloudformation 模板文件中,我的资源如下所示:
"UpdateUser" : {
"Type" : "AWS::Serverless::Function",
"Properties": {
"Handler": "AWSServerless::AWSServerless.UserFunctions::UpdateUserAsync",
"Runtime": "dotnetcore1.0",
"CodeUri": "",
"Description": "Function to update a user",
"MemorySize": 128,
"Timeout": 30,
"Role": null,
"Policies": [ "AWSLambdaFullAccess" ],
"Events": {
"PutResource": {
"Type": "Api",
"Properties": {
"Path": "/user/{id}",
"Method": "PUT"
}
}
}
}
},
"GetUser" : {
"Type" : "AWS::Serverless::Function",
"Properties": {
"Handler": "AWSServerless::AWSServerless.UserFunctions::GetUserAsync",
"Runtime": "dotnetcore1.0",
"CodeUri": "",
"Description": "Function to get a single user",
"MemorySize": 128,
"Timeout": 30,
"Role": null,
"Policies": [ "AWSLambdaFullAccess" ],
"Events": {
"PutResource": {
"Type": "Api",
"Properties": {
"Path": "/user/{Id}",
"Method": "GET"
}
}
}
}
}
那会给我这个错误:"Unable to create resource at path '/user/{id}': A sibling ({Id}) of this resource already has a variable path part -- only one is allowed"。
是否可以通过 Method/Verb 使它们独一无二?或者什么解决方案是最好的..?要将 /user/ 作为我的路径并在对象中发送我的 id,请使用查询字符串或将路径设为类似“/user/update/{id}”“/user/get/{id}”的路径?
查看 this example,您的模板中似乎存在一些问题:
- 确保您的路径参数相同。您正在使用
{id}
和 {Id}
。看起来路径参数区分大小写,因此您可能只想在两个函数中使用 {id}
。
- 对于您的
GetUser
函数,使用 GetResource
事件而不是 PutResource
希望对您有所帮助!
我为我的用户进行了一些 CRUD 操作。我希望我的 URI 是 /user/{id} 并让 VERB 决定使用什么方法,例如。 post 然后放。 在我的 cloudformation 模板文件中,我的资源如下所示:
"UpdateUser" : {
"Type" : "AWS::Serverless::Function",
"Properties": {
"Handler": "AWSServerless::AWSServerless.UserFunctions::UpdateUserAsync",
"Runtime": "dotnetcore1.0",
"CodeUri": "",
"Description": "Function to update a user",
"MemorySize": 128,
"Timeout": 30,
"Role": null,
"Policies": [ "AWSLambdaFullAccess" ],
"Events": {
"PutResource": {
"Type": "Api",
"Properties": {
"Path": "/user/{id}",
"Method": "PUT"
}
}
}
}
},
"GetUser" : {
"Type" : "AWS::Serverless::Function",
"Properties": {
"Handler": "AWSServerless::AWSServerless.UserFunctions::GetUserAsync",
"Runtime": "dotnetcore1.0",
"CodeUri": "",
"Description": "Function to get a single user",
"MemorySize": 128,
"Timeout": 30,
"Role": null,
"Policies": [ "AWSLambdaFullAccess" ],
"Events": {
"PutResource": {
"Type": "Api",
"Properties": {
"Path": "/user/{Id}",
"Method": "GET"
}
}
}
}
}
那会给我这个错误:"Unable to create resource at path '/user/{id}': A sibling ({Id}) of this resource already has a variable path part -- only one is allowed"。
是否可以通过 Method/Verb 使它们独一无二?或者什么解决方案是最好的..?要将 /user/ 作为我的路径并在对象中发送我的 id,请使用查询字符串或将路径设为类似“/user/update/{id}”“/user/get/{id}”的路径?
查看 this example,您的模板中似乎存在一些问题:
- 确保您的路径参数相同。您正在使用
{id}
和{Id}
。看起来路径参数区分大小写,因此您可能只想在两个函数中使用{id}
。 - 对于您的
GetUser
函数,使用GetResource
事件而不是PutResource
希望对您有所帮助!