Skip to content

Instantly share code, notes, and snippets.

@disruptek disruptek/lambda.nim Secret
Created May 15, 2019

Embed
What would you like to do?
example of processing events at aws lambda (500ms in async, <1ms in sync)
#? replace(sub = "\t", by = " ")
import os
import strutils
import httpclient
import asyncdispatch
import logging
const WITH_ASYNC = false
let
RUNTIME_API = os.get_env("AWS_LAMBDA_RUNTIME_API")
HANDLER = os.get_env("_HANDLER")
TASK_ROOT = os.get_env("LAMBDA_TASK_ROOT")
FETCH_URL = "http://" & RUNTIME_API & "/2018-06-01/runtime/invocation/next"
while true:
when WITH_ASYNC:
var client = new_async_http_client()
debug "made a client; query", FETCH_URL
var response = waitfor client.get(FETCH_URL)
var requestid = response.headers["Lambda-Runtime-Aws-Request-Id"].replace(" ", "")
debug "got a response", requestid
var replyto = "http://" & RUNTIME_API & "/2018-06-01/runtime/invocation/" & requestid & "/response"
var body = waitfor response.body
debug "read body"
body = "your request id was [" & requestid & "] and you said:" & body
debug "initiate post", requestid
var complete = waitfor client.post(replyto, body=body)
echo $complete.code
else:
var client = new_http_client()
debug "made a client; query", FETCH_URL
var response = client.get(FETCH_URL)
var requestid = response.headers["Lambda-Runtime-Aws-Request-Id"].replace(" ", "")
debug "got a response", requestid
var replyto = "http://" & RUNTIME_API & "/2018-06-01/runtime/invocation/" & requestid & "/response"
var body = response.body
body = "your request id was [" & requestid & "] and you said:" & body
debug "initiate post", requestid
var complete = client.post(replyto, body=body)
echo $complete.code
@disruptek

This comment has been minimized.

Copy link
Owner Author

commented May 15, 2019

#bash i guess

nim c -d:release bootstrap
zip runtime.zip bootstrap
export SOME_REGION=us-west-2
# new function
aws lambda create-function --region $SOME_REGION --function-name SomeName --zip-file fileb://runtime.zip --handler function.handler --runtime provided --role arn:aws:iam::$AWS_ACCOUNT_ID:role/AnyLambdaRole
# or
aws lambda update-function-code --region $SOME_REGION --function-name SomeName --zip-file fileb://runtime.zip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.