AWS-Lambda

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
    - Europe (Stockholm) eu-north-1 is available
 
0.0.30 2021-10-06T00:13:52Z
    - add amd64 compatible layers
    - pre-install some modules
 
0.0.29 2021-07-16T11:25:47Z
    - bump Paws 0.44
 
0.0.28 2021-06-13T15:46:13Z
    - Fix mojibake when the input and the output are utf8 encoded
    - Update WwwCounter v4.0.0 in the examples
 
0.0.27 2021-05-21T03:58:38Z
    - Perl 5.34.0 is released
 
0.0.26 2021-05-09T02:54:24Z
    - bump Paws 0.43
 
0.0.25 2021-03-02T05:32:05Z
    - new region Asia Pacific (Osaka) ap-northeast-3 is available

README.md  view on Meta::CPAN

161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
- [local::lib](https://metacpan.org/pod/local%3A%3Alib)
 
[Paws](https://metacpan.org/pod/Paws) is optional. See the "Paws SUPPORT" section.
 
## AWS X-Ray SUPPORT
 
[AWS X-Ray](https://aws.amazon.com/xray/) is a service that collects data about requests that your application serves.
You can trace AWS Lambda requests and sends segment data with pre-install module [AWS::XRay](https://metacpan.org/pod/AWS%3A%3AXRay).
 
    use utf8;
    use warnings;
    use strict;
    use AWS::XRay qw/ capture /;
 
    sub handle {
        my ($payload, $context) = @_;
        capture "myApp" => sub {
            capture "nested" => sub {
                # do something ...
            };

author/perl-stripper/perl-stripper/handler.pl  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
use v5.36;
use utf8;
use lib "$ENV{'LAMBDA_TASK_ROOT'}/local/lib/perl5";
use lib "$ENV{'LAMBDA_TASK_ROOT'}/local/lib/perl5/aarch64-linux";
 
 
mkdir '/tmp/.perl-strip';
my $stripper = Perl::Strip->new(
    cache => '/tmp/.perl-strip',

examples/cgi/WwwCounter/app.psgi  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
use utf8;
use strict;
 
my $count_dir = $ENV{WWWCOUNT_DIR} // ".";
for my $ext("cnt", "acc", "dat") {
    open my $fh, ">>", "$count_dir/wwwcount.$ext" or die "cannot touch wwwcount.$ext";
    close $fh;

examples/cgi/WwwCounter/handler.pl  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
use utf8;
use strict;
use lib "$ENV{'LAMBDA_TASK_ROOT'}/local/lib/perl5";
 
$ENV{WWWCOUNT_DIR} ||= "/tmp";
chdir($ENV{'LAMBDA_TASK_ROOT'});
my $app = require "$ENV{'LAMBDA_TASK_ROOT'}/app.psgi";
my $func = AWS::Lambda::PSGI->wrap($app);

examples/custom-docker/handler.pl  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
use utf8;
use strict;
 
sub handle {
    my $payload = shift;
    return +{"hello" => "lambda"};
}
 
1;

examples/docker/handler.pl  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
use utf8;
use strict;
 
sub handle {
    my $payload = shift;
    return +{"hello" => "lambda"};
}
 
1;

examples/hello/handler.pl  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
use utf8;
use strict;
 
sub handle {
    my $payload = shift;
    return +{"hello" => "lambda"};
}
 
1;

examples/psgi/app.psgi  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
13
use strict;
use utf8;
 
return builder {
    mount '/foo' => sub {
        my $env = shift;
        my $req = Plack::Request->new($env);
 
        my $meth = $req->method;

examples/psgi/handler.pl  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
use utf8;
use strict;
 
my $app = require "$ENV{'LAMBDA_TASK_ROOT'}/app.psgi";
my $func = AWS::Lambda::PSGI->wrap($app);
 
sub handle {
    my $payload = shift;
    return $func->($payload);

examples/s3-get-object/handler.pl  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
use utf8;
use strict;
use 5.30.0;
use lib "$ENV{'LAMBDA_TASK_ROOT'}/extlocal/lib/perl5";
use Paws;
 
my $obj = Paws->service('S3', region => 'ap-northeast-1');

lib/AWS/Lambda.pm  view on Meta::CPAN

300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
=back
 
L<Paws> is optional. See the "Paws SUPPORT" section.
 
=head2 AWS X-Ray SUPPORT
 
L<AWS X-Ray|https://aws.amazon.com/xray/> is a service that collects data about requests that your application serves.
You can trace AWS Lambda requests and sends segment data with pre-install module L<AWS::XRay>.
 
    use utf8;
    use warnings;
    use strict;
    use AWS::XRay qw/ capture /;
 
    sub handle {
        my ($payload, $context) = @_;
        capture "myApp" => sub {
            capture "nested" => sub {
                # do something ...
            };

lib/AWS/Lambda/Bootstrap.pm  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
13
use 5.026000;
use utf8;
use strict;
use JSON::XS qw/decode_json encode_json/;
use Scalar::Util qw(blessed);
use Exporter 'import';

lib/AWS/Lambda/Bootstrap.pm  view on Meta::CPAN

221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
AWS::Lambda::Bootstrap - the bootrap script for AWS Lambda Custom Runtime.
 
=head1 SYNOPSIS
 
Save the following script as C<bootstrap>, and then zip it with your perl script.
Now, you can start using Perl in AWS Lambda!
 
    #!perl
    use strict;
    use warnings;
    use utf8;
    use AWS::Lambda::Bootstrap;
 
    bootstrap(@ARGV);
 
Prebuild Perl Runtime Layer includes the C<bootstrap> script.
So, if you use the Layer, no need to include the C<bootstrap> script into your zip.
See L<AWS::Lambda> for more details.
 
=head1 DESCRIPTION

lib/AWS/Lambda/PSGI.pm  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
13

lib/AWS/Lambda/PSGI.pm  view on Meta::CPAN

150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
$env->{'psgi.nonblocking'}  = Plack::Util::FALSE;
$env->{'psgix.harakiri'}    = Plack::Util::TRUE;
$env->{'psgix.input.buffered'} = Plack::Util::TRUE;
 
# inject the request id that compatible with Plack::Middleware::RequestId
if ($ctx) {
    $env->{'psgix.request_id'} = $ctx->aws_request_id;
    $env->{'HTTP_X_REQUEST_ID'} = $ctx->aws_request_id;
}
 
my $body = encode_utf8($payload->{body} // '');
if ($payload->{isBase64Encoded}) {
    $body = decode_base64 $body;
}
open my $input, "<", \$body;
$env->{REQUEST_METHOD} = $payload->{httpMethod};
$env->{'psgi.input'} = $input;
$env->{CONTENT_LENGTH} //= bytes::length($body);
$env->{REQUEST_URI} = $payload->{path};
if ($env->{QUERY_STRING}) {
    $env->{REQUEST_URI} .= '?' . $env->{QUERY_STRING};

lib/AWS/Lambda/PSGI.pm  view on Meta::CPAN

209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
$env->{'psgi.nonblocking'}  = Plack::Util::FALSE;
$env->{'psgix.harakiri'}    = Plack::Util::TRUE;
$env->{'psgix.input.buffered'} = Plack::Util::TRUE;
 
# inject the request id that compatible with Plack::Middleware::RequestId
if ($ctx) {
    $env->{'psgix.request_id'} = $ctx->aws_request_id;
    $env->{'HTTP_X_REQUEST_ID'} = $ctx->aws_request_id;
}
 
my $body = encode_utf8($payload->{body} // '');
if ($payload->{isBase64Encoded}) {
    $body = decode_base64 $body;
}
open my $input, "<", \$body;
$env->{'psgi.input'} = $input;
$env->{CONTENT_LENGTH} //= bytes::length($body);
my $requestContext = $payload->{requestContext};
$env->{REQUEST_METHOD} = $requestContext->{http}{method};
$env->{REQUEST_URI} = $payload->{rawPath};
if ($env->{QUERY_STRING}) {

lib/AWS/Lambda/PSGI.pm  view on Meta::CPAN

257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
    $body->close;
}
 
my $type = $singleValueHeaders->{'content-type'} // 'application/octet-stream';
my $isBase64Encoded = $type !~ m(^text/.*|application/(:?json|javascript|xml))i;
if ($isBase64Encoded) {
    $content = encode_base64 $content, '';
} else {
    $content = try {
        # is valid utf-8 string? try to decode as utf-8.
        decode_utf8($content, Encode::FB_CROAK | Encode::LEAVE_SRC);
    } catch {
        # it looks not utf-8 encoding. fallback to base64 encoding.
        $isBase64Encoded = 1;
        encode_base64 $content, '';
    };
}
 
return +{
    isBase64Encoded => bool $isBase64Encoded,
    headers => $singleValueHeaders,

lib/AWS/Lambda/PSGI.pm  view on Meta::CPAN

358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
=head1 NAME
 
AWS::Lambda::PSGI - It translates enevt of Lambda Proxy Integrations in API Gateway and
Application Load Balancer into L<PSGI>.
 
=head1 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->(@_);
    }

lib/AWS/Lambda/ResponseWriter.pm  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
13
use 5.026000;
use utf8;
use strict;
use Carp qw(croak);
use Scalar::Util qw(blessed);
use MIME::Base64 qw(encode_base64);
 
my %DefaultPort = (
    http => 80,
    https => 443,

script/bootstrap  view on Meta::CPAN

1
2
3
4
5
6
7
8
#!perl
 
use strict;
use utf8;
use AWS::Lambda::Bootstrap ('bootstrap');
 
bootstrap(@ARGV);

t/01_echo.t  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
13
use strict;
use utf8;
 
 
use FindBin;
use lib "$FindBin::Bin/lib";
 
my $payload = +{

t/02_error.t  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
13
use strict;
use utf8;
 
use FindBin;
use lib "$FindBin::Bin/lib";
 
my $error;
my $bootstrap = BootstrapMock->new(
    handler     => "error.handle",
    runtime_api => "example.com",

t/03_init_error.t  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
13
use strict;
use utf8;
 
use FindBin;
use lib "$FindBin::Bin/lib";
 
my $error;
my $bootstrap = BootstrapMock->new(

t/04_handler_not_found.t  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
13
use strict;
use utf8;
 
use FindBin;
use lib "$FindBin::Bin/lib";
 
my $error;
my $bootstrap = BootstrapMock->new(

t/10_lambda_next.t  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
13
use strict;
use utf8;
 
use FindBin;
 
my $app_server = Test::TCP->new(
    listen => 1,

t/11_lambda_response.t  view on Meta::CPAN

t/12_lambda_error.t  view on Meta::CPAN

t/13_lambda_init_error.t  view on Meta::CPAN

t/14_streaming.t  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
13
use strict;
use utf8;
 
 
use FindBin;
use lib "$FindBin::Bin/lib";
 
my $payload = +{

t/15_lambda_response_streaming.t  view on Meta::CPAN

t/20_psgi.t  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
13
use strict;
use utf8;
 
use FindBin;
use JSON::XS qw/decode_json encode_json/;
use File::Slurp qw(slurp);
use Test::Deep qw(cmp_deeply);
use Encode;

t/20_psgi.t  view on Meta::CPAN

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    my $name = $_[0];
    return decode_json(slurp("$FindBin::Bin/$name"));
}
 
sub slurp_fh {
    my $fh = shift;
    my $data = "";
    while ($fh->read(my $buf, 4096)) {
        $data .= $buf;
    }
    return decode_utf8($data);
}
 
my $app = AWS::Lambda::PSGI->new;
 
subtest "API Gateway GET Request" => sub {
    my $input = slurp_json("testdata/apigateway-get-request.json");
    my $output = $app->format_input($input);
    my $req = Plack::Request->new($output);
    is $req->method, 'GET', 'method';
    my $content;

t/20_psgi.t  view on Meta::CPAN

204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
my $response = [
    200,
    [
        'Content-Type' => 'text/plain',
        'Header-Name' => 'value1',
        'header-name' => 'value2',
    ],
    [
        "Hello",
        "World",
        encode_utf8("こんにちは世界"),
    ]
];
my $res = $app->format_output($response);
cmp_deeply $res, {
    isBase64Encoded => bool 0,
    headers => {
        'content-type' => 'text/plain',
        'header-name' => 'value2',
    },
    multiValueHeaders => {

t/21_psgi_response_streaming.t  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
13
use strict;
use utf8;
 
use FindBin;
use JSON::XS qw/decode_json encode_json/;
use File::Slurp qw(slurp);
use Test::Deep qw(cmp_deeply);
use Encode;

t/21_psgi_response_streaming.t  view on Meta::CPAN

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
sub slurp_json {
    my $name = $_[0];
    return decode_json(slurp("$FindBin::Bin/$name"));
}
 
sub slurp_fh {
    my $fh = $_[0];
    local $/;
    my $v = <$fh>;
    defined $v ? decode_utf8($v) : '';
}
 
my $app = AWS::Lambda::PSGI->new;
 
subtest "array reference" => sub {
    my $content_type = undef;
    my $buf = "";
    my $res = $app->_handle_response_stream([200, ["Content-Type" => "text/plain"], ["hello"]]);
    my $responder = sub {
        $content_type = shift;

t/test_handlers/echo.pl  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
use utf8;
use strict;
 
# an example of echo
# it is called from 01_echo.t
sub handle {
    my ($payload, $context) = @_;
    die "payload is empty" unless $payload;
    die "context is empty" unless $context;

t/test_handlers/error.pl  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
use utf8;
use strict;
 
sub handle {
    die "some error";
}
 
1;



( run in 1.438 second using v1.01-cache-2.11-cpan-49f99fa48dc )