The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

AWS::Lambda::PSGI - It translates enevt of Lambda Proxy Integrations in API Gateway and Application Load Balancer into PSGI.

SYNOPSIS

Add the following script into your Lambda code archive.

    use utf8;
    use warnings;
    use strict;
    use AWS::Lambda::PSGI;

    my $app = require "$ENV{'LAMBDA_TASK_ROOT'}/app.psgi";
    my $func = AWS::Lambda::PSGI->wrap($app);

    sub handle {
        return $func->(@_);
    }

    1;

And then, Set up Lambda Proxy Integrations in API Gateway or Lambda Functions as ALB Targets

DESCRIPTION

Streaming Response

AWS::Lambda::PSGI supports response streaming. The function urls's invoke mode is configured as "RESPONSE_STREAM", and Lambda environment variable "PERL5_LAMBDA_PSGI_INVOKE_MODE" is set to "RESPONSE_STREAM".

    ExampleApi:
        Type: AWS::Serverless::Function
        Properties:
            FunctionUrlConfig:
                AuthType: NONE
                InvokeMode: RESPONSE_STREAM
            Environment:
                Variables:
                PERL5_LAMBDA_PSGI_INVOKE_MODE: RESPONSE_STREAM
            # (snip)

In this mode, the PSGI server accespts Delayed Response and Streaming Body.

    my $app = sub {
        my $env = shift;
    
        return sub {
            my $responder = shift;
            $responder->([ 200, ['Content-Type' => 'text/plain'], [ "Hello World" ] ]);
        };
    };

An application MAY omit the third element (the body) when calling the responder.

    my $app = sub {
        my $env = shift;
    
        return sub {
            my $responder = shift;
            my $writer = $responder->([ 200, ['Content-Type' => 'text/plain'] ]);
            $writer->write("Hello World");
            $writer->close;
        };
    };

Request ID

AWS::Lambda::PSGI injects the request id that compatible with Plack::Middleware::RequestId.

    env->{'psgix.request_id'} # It is same value with $context->aws_request_id

LICENSE

The MIT License (MIT)

Copyright (C) ICHINOSE Shogo.

AUTHOR

ICHINOSE Shogo <shogo82148@gmail.com>