通過Restful API創建Azure Lambda Function - 不太穩定,時而遇到400Bad Request

Our team is trying to create functions via Restful API as documented here:

https://docs.microsoft.com/en-us/rest/api/appservice/webapps/createfunction

This API can work, sometimes.

We are testing it using postman with url:

https://management.azure.com/subscriptions//resourceGroups/ep-lambda-functions/providers/Microsoft.Web/sites/beforeCreateUser/functions/strange?api-version=2019-08-01

In HTTP request body, maintain the following JSON source code:

{
 "properties": {
 "files": {
 "index.js": "console.log(\"Hello\");"
},
 "test_data": "{\r\n    \"name\": \"Azure\"\r\n}",
 "config": {
	"bindings": [
	{
		"authLevel": "function",
		"type": "httpTrigger",
		"direction": "in",
		"name": "req",
		"methods": ["get", "post"]
	},
	{
		"type": "http",
		"direction": "out",
		"name": "res"
	}
	],
	"disabled": false
}
}
}

Execute the request in postman, sometimes the API returns 201 Created as expected. Unfortunately sometimes, we get HTTP 400 bad request instead:

Error detail:

{
    "Code": "BadRequest",
    "Message": "Encountered an error (InternalServerError) from host runtime.",
    "Target": null,
    "Details": [
        {
            "Message": "Encountered an error (InternalServerError) from host runtime."
        },
        {
            "Code": "BadRequest"
        },
        {
            "ErrorEntity": {
                "Code": "BadRequest",
                "Message": "Encountered an error (InternalServerError) from host runtime."
            }
        }
    ],
    "Innererror": null
}

We can ensure the correctness of our HTTP request payload.

When I make some deep research, I found a fact which makes me even more surprised.

I setup a jMeter project to fire 10 requests to Azure simultaneously:

For the name of functions to be created, I use prefix “jerryfuncv3_” plus {uuid}, which is a randiom integrate between 1 and 100:

only 2 among these 10 requests are successful:

Let’s check detail of one failed request, take this one for example, function name: jerryfuncv3_70

To my surprise(!!!), although the 8 requests return 400 bad request, still I could see all 10 functions are created in Azure, with status “Enabled” marked.

And for some times, the function creation would fail with 400 Bad request as well, even when I create it manually in Azure portal, as below:


{"properties":{"files":{"index.js":"module.exports = async function (context, req) {\r\n    context.log('JavaScript HTTP trigger function processed a request.');\r\n\r\n    if (req.query.name || (req.body && req.body.name)) {\r\n        context.res = {\r\n            // status: 200, /* Defaults to 200 */\r\n            body: \"Hello \" + (req.query.name || req.body.name)\r\n        };\r\n    }\r\n    else {\r\n        context.res = {\r\n            status: 400,\r\n            body: \"Please pass a name on the query string or in the request body\"\r\n        };\r\n    }\r\n};"},"test_data":"{\r\n    \"name\": \"Azure\"\r\n}","config":{"bindings":[{"authLevel":"anonymous","type":"httpTrigger","direction":"in","name":"req","methods":["get","post"]},{"type":"http","direction":"out","name":"res"}],"disabled":false}}}

400 Bad request would appear sometimes in Azure API console as well.


{
  "Code": "BadRequest",
  "Message": "Encountered an error (InternalServerError) from host runtime.",
  "Target": null,
  "Details": [
    {
      "Message": "Encountered an error (InternalServerError) from host runtime."
    },
    {
      "Code": "BadRequest"
    },
    {
      "ErrorEntity": {
        "Code": "BadRequest",
        "Message": "Encountered an error (InternalServerError) from host runtime."
      }
    }
  ],
  "Innererror": null
}

I searched related github issue and cannot find solution.

This issue below might be related, unfortunately no solution are provided.
https://github.com/microsoft/vscode-azurefunctions/issues/1838


In a nutshell, we need the API mentioned in the document below to be STABLE, which means, if the function creation is successfully, we expected 201 created status code. The current behavior is quite confusing indeed!

https://docs.microsoft.com/en-us/rest/api/appservice/webapps/createfunction

要獲取更多Jerry的原創文章,請關注公衆號"汪子熙":

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章