AWS-Lambda
view release on metacpan or search on metacpan
lib/AWS/Lambda.pm view on Meta::CPAN
our $context;
our $LAYERS = $AWS::Lambda::AL::LAYERS;
our $LAYERS_AL2 = $AWS::Lambda::AL2::LAYERS;
our $LAYERS_AL2023 = $AWS::Lambda::AL2023::LAYERS;
sub get_layer_info {
my ($version, $region) = @_;
return $LAYERS->{$version}{$region};
}
sub print_runtime_arn {
my ($version, $region) = @_;
print $LAYERS->{$version}{$region}{runtime_arn};
}
sub print_paws_arn {
my ($version, $region) = @_;
print $LAYERS->{$version}{$region}{paws_arn};
}
sub get_layer_info_al2 {
my ($version, $region, $arch) = @_;
$arch //= 'x86_64';
return $LAYERS_AL2->{$version}{$arch}{$region};
}
sub print_runtime_arn_al2 {
my ($version, $region, $arch) = @_;
$arch //= 'x86_64';
print $LAYERS_AL2->{$version}{$arch}{$region}{runtime_arn};
}
sub print_paws_arn_al2 {
my ($version, $region, $arch) = @_;
$arch //= 'x86_64';
print $LAYERS_AL2->{$version}{$arch}{$region}{paws_arn};
}
sub get_layer_info_al2023 {
my ($version, $region, $arch) = @_;
$arch //= 'x86_64';
return $LAYERS_AL2023->{$version}{$arch}{$region};
}
sub print_runtime_arn_al2023 {
my ($version, $region, $arch) = @_;
$arch //= 'x86_64';
print $LAYERS_AL2023->{$version}{$arch}{$region}{runtime_arn};
}
sub print_paws_arn_al2023 {
my ($version, $region, $arch) = @_;
$arch //= 'x86_64';
print $LAYERS_AL2023->{$version}{$arch}{$region}{paws_arn};
}
1;
__END__
=encoding utf-8
=head1 NAME
AWS::Lambda - Perl support for AWS Lambda Custom Runtime.
=head1 SYNOPSIS
Save the following Perl script as C<handler.pl>.
sub handle {
my ($payload, $context) = @_;
return $payload;
}
1;
and then, zip the script.
$ zip handler.zip handler.pl
Finally, create new function using awscli.
$ aws --region "$REGION" --profile "$PROFILE" lambda create-function \
--function-name "hello-perl" \
--zip-file "fileb://handler.zip" \
--handler "handler.handle" \
--runtime provided.al2023 \
--role arn:aws:iam::xxxxxxxxxxxx:role/service-role/lambda-custom-runtime-perl-role \
--layers "arn:aws:lambda:$REGION:445285296882:layer:perl-5-38-runtime-al2023-x86_64:1"
It also supports L<response streaming|https://docs.aws.amazon.com/lambda/latest/dg/configuration-response-streaming.html>.
sub handle {
my ($payload, $context) = @_;
return sub {
my $responder = shift;
my $writer = $responder->('application/json');
$writer->write('{"foo": "bar"}');
$writer->close;
};
}
=head1 DESCRIPTION
This package makes it easy to run AWS Lambda Functions written in Perl.
=head2 Use Pre-built Public Lambda Layers
=over
=item 1
Login to your AWS Account and go to the Lambda Console.
=item 2
Create a new function and give it a name and an IAM Role.
=item 3
For the "Runtime" selection, select B<Provide your own bootstrap on Amazon Linux 2>.
lib/AWS/Lambda.pm view on Meta::CPAN
$ aws --region "$REGION" --profile "$PROFILE" lambda create-function \
--function-name "hello-perl" \
--code ImageUri=123412341234.dkr.ecr.sa-east-1.amazonaws.com/hello-perl:latest \
--handler "handler.handle" \
--runtime provided.al2023 \
--role arn:aws:iam::xxxxxxxxxxxx:role/service-role/lambda-custom-runtime-perl-role
=head2 Run in Local using Docker
Prebuilt Docker Images based on L<https://hub.docker.com/r/lambci/lambda/> are available.
You can pull from L<https://gallery.ecr.aws/shogo82148/p5-aws-lambda> or L<https://hub.docker.com/r/shogo82148/p5-aws-lambda>,
and build zip archives to deploy.
# Install the dependency.
docker run --rm -v $(PWD):/var/task shogo82148/p5-aws-lambda:build-5.38.al2023 \
cpanm --notest --local-lib extlocal --no-man-pages --installdeps .
# run an event.
docker run --rm -v $(PWD):/var/task shogo82148/p5-aws-lambda:5.38.al2023 \
handler.handle '{"some":"event"}'
=head2 Pre-installed modules
The following modules are pre-installed for convenience.
=over
=item L<AWS::Lambda>
=item L<AWS::XRay>
=item L<JSON>
=item L<Cpanel::JSON::XS>
=item L<JSON::MaybeXS>
=item L<YAML>
=item L<YAML::Tiny>
=item L<YAML::XS>
=item L<Net::SSLeay>
=item L<IO::Socket::SSL>
=item L<Mozilla::CA>
=item L<local::lib>
=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 ...
};
};
capture "another" => sub {
# do something ...
};
return;
}
1;
=head1 Paws SUPPORT
If you want to call AWS API from your Lambda function,
you can use a pre-built Lambda Layer for L<Paws> - A Perl SDK for AWS (Amazon Web Services) APIs.
=head2 Use Prebuilt Public Lambda Layers
Add the perl-runtime layer and the perl-paws layer into your lambda function.
aws --region "$REGION" --profile "$PROFILE" lambda create-function \
--function-name "hello-perl" \
--zip-file "fileb://handler.zip" \
--handler "handler.handle" \
--runtime provided.al2023 \
--role arn:aws:iam::xxxxxxxxxxxx:role/service-role/lambda-custom-runtime-perl-role \
--layers \
"arn:aws:lambda:$REGION:445285296882:layer:perl-5-38-runtime-al2023-x86_64:5" \
"arn:aws:lambda:$REGION:445285296882:layer:perl-5-38-paws-al2023-x86_64:4"
Now, you can use L<Paws> to call AWS API from your Lambda function.
use Paws;
my $obj = Paws->service('...');
my $res = $obj->MethodCall(Arg1 => $val1, Arg2 => $val2);
print $res->AttributeFromResult;
The list of all layer ARNs is available on L<AWS::Lambda::AL2023>.
URLs for Zip archive are:
C<https://shogo82148-lambda-perl-runtime-$REGION.s3.amazonaws.com/perl-$VERSION-paws-al2-$ARCHITECTURE.zip>
=head2 Use Prebuilt Docker Images for Paws
use the C<base-$VERSION-paws.al2023> tag on L<https://gallery.ecr.aws/shogo82148/p5-aws-lambda> or L<https://hub.docker.com/r/shogo82148/p5-aws-lambda>.
FROM shogo82148/p5-aws-lambda:base-5.38-paws.al2023
# or if you want to use ECR Public.
# FROM public.ecr.aws/shogo82148/p5-aws-lambda:base-5.38-paws.al2023
COPY handler.pl /var/task/
( run in 0.815 second using v1.01-cache-2.11-cpan-f56aa216473 )