'Requests to AWS Lambda HTTPS endpoint from R

I am trying to use the recently released AWS Lambda HTTPS endpoints feature: https://aws.amazon.com/blogs/aws/announcing-aws-lambda-function-urls-built-in-https-endpoints-for-single-function-microservices

I have created an AWS Lambda function called multiply that expects an x and a y in the request body and returns the product. I created the HTTPS endpoint and set it to IAM authentication. Now, I'd like to call it from R with help of the aws.signature package.

The expected result is an HTTP response containing the Lambda function's output.

But I run into error "400 Bad Request". I suspect that the action parameter for aws.signature::signature_v4_auth must be set, but I can't figure out what it should be.

library(httr)
library(jsonlite)

function_url <- "https://<url-id>.lambda-url.eu-central-1.on.aws"
region <- "eu-central-1"
request_body <- toJSON(list(x = 2, y = 4), auto_unbox = TRUE)

headers <- list()
headers[["x-amz-date"]] <- format(Sys.time(), "%Y%m%dT%H%M%SZ", tz = "UTC")
headers[["host"]] <- function_url
headers[["content-type"]] <- "application/json"

signature <- aws.signature::signature_v4_auth(
    datetime = headers[["x-amz-date"]],
    region = region,
    action = "",
    service = "lambda",
    verb = "POST",
    canonical_headers = headers,
    request_body = request_body,
    key = "***",
    secret = "***"
 )

headers[["Authorization"]] <- signature[["SignatureHeader"]]

POST(
  url = function_url, 
  body = request_body, 
  do.call(add_headers, headers)
)



Solution 1:[1]

There are two errors in the request:

  1. The host should not include "https://". So the correct host is <url-id>.lambda-url.eu-central-1.on.aws

  2. An empty action is not permitted. Use "/" instead.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 psimm