xp-forge / lambda

AWS Lambda for the XP Framework
3 stars 0 forks source link

Implement `xp lambda run [Handler]` to run lambdas locally #21

Closed thekid closed 1 year ago

thekid commented 1 year ago

This pull request adds a subcommand xp lambda run which executes lambdas directly on the system. While this does not provide a complete lambda environment, and does not have any execution limits imposed on it, it greatly speeds up the development process.

Declaration

Inside a file called Greet.class.php:

use com\amazon\aws\lambda\Handler;

class Greet extends Handler {

  /** @return com.amazon.aws.lambda.Lambda|callable */
  public function target() {
    return fn($event, $context) => sprintf(
      'Hello %s from PHP %s via %s @ %s',
      $event['name'] ?? 'world',
      PHP_VERSION,
      $context->functionName,
      $context->region
    );
  }
}

Invocation

In a shell window:

$ xp lambda run Greet
Hello world from PHP 8.2.7 via Greet @ test-local-1

# Will take environment variables into account
$ AWS_REGION=eu-central-1 xp lambda run Greet
Hello world from PHP 8.2.7 via Greet @ eu-central-1

# Specify payload as JSON
$ xp lambda run Greet '{"name":"Timm"}'
Hello Timm from PHP 8.2.7 via Greet @ test-local-1

# Can be invoked with multiple payloads, initializing only once
$ xp lambda run Greet '{"name":"Timm"}' '{"name":"Alex"}'
Hello Timm from PHP 8.2.7 via Greet @ test-local-1
Hello Alex from PHP 8.2.7 via Greet @ test-local-1

Note: The output will not contain the typical telemetry information displayed in CloudWatch / Lambda console (START RequestId / END RequestId / REPORT ...). To run your lambda in something closer to the actual execution environment use xp lambda test.

thekid commented 1 year ago

Released in https://github.com/xp-forge/lambda/releases/tag/v4.4.0