AWS Lambda HTTP POST request

Hi,

I am trying to make HTTP POST requests to AWS Lambda with BearSSLClient on Arduino Nano 33 IoT, but the endpoint always disconnects after 60 seconds. I verified it with curl requests from the host and it responds just fine.

I generated the certificates, created the AWS Thing and created a HTTP Endpoint. Maybe I should not be using SSL/TLS?

The code looks like this:

WiFiSSLClient    wifiClient;            // Used for the TCP socket connection
BearSSLClient client(wifiClient); // Used for SSL/TLS connection, integrates with ECC508

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }
   if (!ECCX08.begin()) {
    Serial.println("No ECCX08 present!");
    while (1);
  }

  // Set a callback to get the current time
  // used to validate the servers certificate
  ArduinoBearSSL.onGetTime(getTime);

  // Note how the certificate is formed
  String(certificate) = String(client_cert) + String(root_ca);
  Serial.println(certificate);
  
  // Set the ECCX08 slot to use for the private key
  // and the accompanying public certificate for it
  client.setEccSlot(0, certificate.c_str());


  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(5000);
  }
  Serial.println("Connected to WiFi");
  printWiFiStatus();

  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 443)) {
    Serial.println("connected to server");
    // Make a HTTP request:
    client.println("GET / HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    //client.print("content-length: ");
    //client.println("3");
    //lambda.println("connection: close");
    client.println();
    //client.println("{}");

  }
}

void loop() {
  // if there are incoming bytes available
  // from the server, read them and print them:
  while (client.available()) {
    char c = client.read();
    Serial.write(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting from server.");
    client.stop();

    // do nothing forevermore:
    while (true);
  }
}

Rather than just spinning after the client.stop() have you tried to reconnect using client.connect?

Have you tried to set the timeout using setConnectionTimeout(milis)? I believe the default is 1000 so about 1 second, this might affect how long the client waits before deciding it is disconnect - I'm not entirely sure :roll_eyes: but worth trying as an experiment as it may help.

1 Like

I am really baffled by what AWS expects from me in this case. On the host I understand that the TLS handshake lasts very briefly and I have no idea how long it takes in the case of BearSSL implementation of it.

Currently, I decided to communicate through MQTT and it works like a charm (well almost - my payload is getting truncated somehow - 1600 bytes), and I relay my publish requests to AWS Lambda.

I will try applying what you said nevertheless.