Recent

Author Topic: Running code in AWS Lambda  (Read 2979 times)

macmike

  • Jr. Member
  • **
  • Posts: 85
    • Soft-Practice
Running code in AWS Lambda
« on: December 03, 2018, 07:29:10 pm »
If you don't know AWS Lambda is a "serverless" code execution environment in which code runs in response to events. You only pay for runtime and the first 1,000,000 a month are free so for small code it's practically free.

Anyway, last week AWS announced that you could now bring any language to Lambda with custom runtimes, so I've made a prototype for FPC code. Check it out, if you're interested:

github: https://github.com/macmike/fpc-lambda
blog: https://mikemacd.wordpress.com/2018/12/03/building-a-custom-lambda-runtime-for-anything-even-pascal-yes-lambda-reinvent-aws/
twitter announcement: https://twitter.com/mikemacd/status/1069655731773812741

Basically this means you can write code to respond to JSON events like so (function.pas):
Code: [Select]
uses
  fpjson, jsonparser, sysutils;

var
  lambdaEventData: TJSONData;
  lambdaEvent: TJSONObject;
  nameParameter, outputJSON: string;
begin
  //get incoming JSON and parse
  lambdaEventData := GetJSON(ParamStr(1));

  // cast as TJSONObject to make access easier
  lambdaEvent := TJSONObject(lambdaEventData);

  nameParameter := lambdaEvent.Get('name');
  outputJSON := format('{"status":"200", "message":"hello %s from fpc lambda"}',[nameParameter]);
  WriteLn(outputJSON);

end.


and then create it as a lambda function by doing the following:
Code: [Select]
  zip function.zip ./function.pas
  aws lambda create-function --function-name fpc-lambda-event --zip-file fileb://function.zip --handler function.handler --runtime provided --layers arn:aws:lambda:eu-west-1:743697633610:layer:fpc-runtime:20 --role arn:aws:iam::<your-id>:role/lambda-role --timeout 10

You can then invoke from the command line, trigger from a https endpoint (api gateway), an AWS service, or the web console:
Code: [Select]
  aws lambda invoke --function-name fpc-lambda-event --payload '{"name":"Mike"}' response.txt
  cat response.txt
  // result:
  {"status":"200", "message":"hello Mike from fpc lambda"}


sash

  • Sr. Member
  • ****
  • Posts: 366
Re: Running code in AWS Lambda
« Reply #1 on: December 03, 2018, 10:15:04 pm »
you could now bring any language to Lambda with custom runtimes...

In addition to custom runtimes real applications needs database, disk, memory resources. Is it also free?
Lazarus 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

macmike

  • Jr. Member
  • **
  • Posts: 85
    • Soft-Practice
Re: Running code in AWS Lambda
« Reply #2 on: December 03, 2018, 11:08:45 pm »
Obviously not everything is free all of the time, you're kind of missing the point here, but thanks for commenting.

sash

  • Sr. Member
  • ****
  • Posts: 366
Re: Running code in AWS Lambda
« Reply #3 on: December 03, 2018, 11:27:14 pm »
you're kind of missing the point here, but thanks for commenting.
Exactly. Since you mentioned aspects of economic feasibility, I found some details were missed, so I asked.
Lazarus 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

Trenatos

  • Hero Member
  • *****
  • Posts: 535
    • MarcusFernstrom.com
Re: Running code in AWS Lambda
« Reply #4 on: December 04, 2018, 02:40:05 am »
You get lots for free and for cheap.

AWS is very "nickel and dime" if you just add stuff, but if you use only what you need it's very cheap for ordinary users.

There's also some dynamic magic you can do to auto provision things as you need them, which can save a lot of money.

 

TinyPortal © 2005-2018