I am exploring the AWS lambda - http4k connection ...
# http4k
n
I am exploring the AWS lambda - http4k connection .. I see that there is a AWS tool (sam cli) which you can use to test your function locally But it doesn't seem to support jvm runtimes .. am I wrong? Did someone successfully use it to test an http4k application locally? thanks
d
what type of function is it? Apigateway or an event?
n
I have started with Apigateway but it should be an event (probably) At the end it will be probably invoked directly If I understand well (I am new to AWS lambda) you can invoke directly the function in both cases .. But I am not sure...
d
are you looking test the functionality or the configuration? if it's functionality, - if it's an API gateway the you can just start the handler without the wrapper and call it directly. For other functions we would normally do that in a test
n
I know I can test the handler directly (I've read this in the http4k docs) but I would like to test the functionality in the AWS lambda environment, just to be sure that it works within and with the aws stack
so perhaps this means that I would like to test both, functionality + configuration ...
d
I've not really used SAM, so can't help there unfortunately, maybe @Andrew O'Hara can help as I know he does a bunch of lambda things 🙂 ?
n
Thanks David. It seemed an interesting tool to me, but there is also localstack or maybe others .. Anything that could be used to test locally seems interesting/promising to me 😄
a
I only use the SAM CLI to package and deploy my application. All other testing (automated and manual) is performed with http4k normally. In this example application, I actually have 3 entry points into my application: 1. Main Function - to run manual tests locally 2. LambdaHandler - to attach to Lambda runtime environment 3. testApp - for automated testing (local and CI) That being said, the SAM CLI can do what you're looking for: • sam local start-lambda - starts a local lambda emulator; provides an endpoint you can invoke with the aws lambda cli • sam local start-api - Starts an emulated lambda and API gateway you can send HTTP requests to (requires docker) • sam local invoke - send a single test event to an emulated lambda function Personally, I prefer to get 90% of my testing done without thinking about the deployment environment (ie lambda) at all. I argue that Lambda should just be a layer on top of your application that requires only minimal testing; something that can easily be done manually in a real lambda environment.
n
Ok thank you very much Andrew
I am testing my function locally with localstack. I created the function with aws cli:
Copy code
aws --endpoint-url=<http://localhost:4566> \
lambda create-function --function-name it.multidialogo.multiprinter.MyAwsLambdaExposedFunction \
--zip-file <fileb://build/distributions/Multiprinter.zip> \
--environment Variables={NB_PORT="9001",NB_OUTPUT_PATH="/output}" \
--handler index.handler --runtime java11 \
--role arn:aws:iam::000000000000:role/lambda-role
when trying invoke:
Copy code
aws --endpoint-url=<http://localhost:4566> \
lambda invoke \
--function-name it.multidialogo.multiprinter.MyAwsLambdaExposedFunction \
--payload <file://payload.json> \
response.json
I get this error: string argument should contain only ASCII characters Searching for this error I found only a SO post which mentions accepted media types. I didn't configured explicitly accepted media in my function (it should be application/json btw). Do you think this could be the cause? thanks
(i ask because, in http4k examples, i don't see examples which explicitly sets accepted media types)
Never mind I found a post with solution using fileb instead of file thanks anyway sorry for spamming