Serverless functions using Netlify API error

Having issues deploying serverless functions using Netlify API. I have followed this guide and can successfully deploy a site with the file digest method. However Netlify API always returns Error 422 Unprocessable Entity if I include a zipped serverless function.

# Lambda function
dev: zipinfo netlify/functions/hello-world.zip

-rw-r--r--  3.0 unx      114 tx defN 27-Dec-21 16:40 hello-world.js
1 file, 114 bytes uncompressed, 102 bytes compressed:  10.5%


dev: unzip -l netlify/functions/hello-world.zip

  Length     Date   Time    Name
 --------    ----   ----    ----
      114  12-27-21 16:40   hello-world.js
 --------                   -------
      114                   1 file

// hello-world.js
exports.handler = async function(event, context) {
  return {
    statusCode: 200,
    body: 'Hello world'
  };
};
# submit digest
curl --request POST "https://api.netlify.com/api/v1/sites/$site_id/deploys" \
--header "Authorization: Bearer $NETLIFY_TOKEN" \
--header 'Content-Type: application/json; charset=utf-8' \
--data-raw '{
  "files": {
    "/about.html": "907d14fb3af2b0d4f18c2d46abe8aedce17367bd",
    "/index.html": "f18c2d7367bd9046abe8aedce17d14fb3af2b0d4"
  },
  "functions": {
    "hello-world": "a9a39b1bd4b72e09205bbf2e5255cab900c56102"
  }
}'

# upload zip
curl --request PUT "https://api.netlify.com/api/v1/deploys/$deploy_id/functions/hello-world?runtime=js" \
--header "Authorization: Bearer $NETLIFY_TOKEN" \
--header 'Content-Type: application/octet-stream' \
--data-binary '@/Users/dev/test-fn/netlify/functions/hello-world.zip'
# response
{
  "errors":"Uploaded file hello-world did not match deploy"
}

Server status: 422 Unprocessable Entity

Hey there, @noproblem :wave:

Thanks for reaching out, and welcome to the Netlify Forums. Sorry to hear you have encountered an obstacle. Can you please share a link to your site as well as the name of your function?

Thanks!

Hi @noproblem,

Could you share the repo with your code (and the files) that you’re trying to upload?

Hey @noproblem,

I tried this and yes, something weird is happening.

I sent a request like:

{
    "files": {
        "/index.html": "907d70b236f94c60556666d82c262336afd71151"
    },
    "functions": {
        "hello-word": "cdbb5cba56d8a61eb49dee1f52a33ba64fb6b080"
    }
}

(for some reason, I am seeing a different checksum than what you’ve shown)

But when I see the response from this API, I can see the required array to be empty:

Are you seeing that too? That array should have /index.html at the least.

Hello @hrishikesh

Yes I get that. From what I’ve understood (but please correct me if I’m wrong) the required field is only populated with files not cached by Netlify’s CDN. Currently my site has received those html files but any further deploys bail if I submit a digest that includes a function. So far I have tried sending:

  • hello-world.js hash a9a39b1bd4b72e09205bbf2e5255cab900c56102
  • zip hash cdbb5cba56d8a61eb49dee1f52a33ba64fb6b080
  • just a function in the digest
  • new html files and a function in the digest

But I still get the same 422 error. Are you able to deploy the zipped function using Netlify API to a new site?

Thanks

In the past, there was an issue (and it’s not yet fixed) that if you send only a function (without an other static files), it would fail. This is not the case here as you’re sending files too. Your understanding is correct, but the reason why I asked if you are seeing the required array as empty because I tried creating a new site and uploading those files via the API and I’m getting the error as you describe and the required array is turning out to be empty which should not happen.

Yes, I get the same response. If I poll the last deploy it stays stuck on uploading.

Also new files submitted are missing from the required field and do not get deployed.

{
  state: 'uploading',
  error_message: null,
  required: [],
  required_functions: [ 'cdbb5cba56d8a61eb49dee1f52a33ba64fb6b080' ],
  summary: { status: 'building', messages: [] }
}

hey there, @noproblem :wave:

Apologies for the delay. Can you confirm if you are still experiencing issues? If you are, can you please share additional debugging steps you have taken in the past week? Thanks!

Hi @hillary

No, unfortunately the same error persists. This week I have tried submitting:

  • a new function 98f694523e13e3b2e5a16829b815b9ce6f53c95a to respond with ‘Hello lambda’

  • a digest with "async": true

  • hello-world.js or hello.world.zip hash alone and with a new file

  • hello-world.zip first, then a new file and vice versa

  • using different tools: curl, node and fetch js

  • zipping a function inside a directory with a new package.json

  • zipping a function inside an empty directory 78201697fd2e68f5ebe709d856166be52abfde2c

Archive:  hello-world.zip   458 bytes   2 files
drwxr-xr-x  3.0 unx        0 bx stor  8-Jan-22 13:29 hello-world/
-rw-r--r--  3.0 unx      116 tx defN  8-Jan-22 13:29 hello-world/hello-world.js
2 files, 116 bytes uncompressed, 104 bytes compressed:  10.3%

dev:functions unzip -l hello-world.zip
Archive:  hello-world.zip
  Length     Date   Time    Name
 --------    ----   ----    ----
        0  01-08-22 13:29   hello-world/
      116  01-08-22 13:29   hello-world/hello-world.js
 --------                   -------
      116                   2 files

But with each submission e.g:

{
  files: {
    "/about.html": "92031022e31de45bc1d13c87985ce7c36b3bb7f4"
  },
  functions: { "hello-world": "78201697fd2e68f5ebe709d856166be52abfde2c" }
}

I get the same response:

{
  required: [],
  required_functions: [ "78201697fd2e68f5ebe709d856166be52abfde2c" ],
}
{
  "errors": "Uploaded file hello-world did not match deploy"
}

422 Unprocessable Entity

Hey @noproblem,

I can see that this problem does exist and would file an escalation for the developers to check. We don’t know if/when this will be fixed, so till then you might have to use some other way.

Hey @noproblem,

We heard back from the devs. Could you try to use SHA256 for the functions instead of SHA1? Note, the normal static files should still use SHA1.

Hi @hrishikesh

Working perfectly. Thank you :grin: