TLS issue - WORKER to AWS IoT Core

EDIT: THIS WORKS NOW! Just follow the steps bellow...

EDIT: Make sure the compatibility_date = "2024-09-30" in your wrangler.toml is at least september 2024.

I am trying to communicate to the AWS IoT core HTTPS endpoint through a cloudflare worker (using hono).

I created a certificate and I tested successfully sending a message with curl and the downloaded certificates - https://docs.aws.amazon.com/iot/latest/developerguide/http.html

curl --tlsv1.2 \
    --cacert Amazon-root-CA-1.pem \
    --cert device.pem.crt \
    --key private.pem.key \
    --request POST \
    --data "{ \"message\": \"Hello, world\" }" \
    "https://IoT_data_endpoint:8443/topics/topic?qos=1"


Then I configured the binding with wrangler

pnpx wrangler mtls-certificate upload --cert device.pem.crt --key private.pem.key --name AWS_IOT


And then I configured an endpoint in hono

iotPublish.post("/", async (c) => {
  try {
    let topic = "test";
    let message = { message: "hello" };

    return await c.env.AWS_IOT.fetch(
      `https://xxxxxx.iot.eu-central-1.amazonaws.com:8443/topics/topic?qos=1`,
      {
        method: "POST",

        body: JSON.stringify(message),
      }
    );
  } catch (e) {
    console.log(e);
    return Response.json({ error: JSON.stringify(e) }, { status: 500 });
  }
});


The result is always
{
  "message": "Missing authentication",
  "traceId": "8c6b18f4-b3d5-42c7-8edb-e2b0bbc09ad3"
}


Edit: you should get
{"message":"OK","traceId":"f6353148-67b1-707f-c50c-40e723b4c0d4"}


Enjoy
Was this page helpful?