AWS-Lambda
view release on metacpan or search on metacpan
[](https://github.com/shogo82148/p5-aws-lambda/actions?workflow=Test)
# NAME
AWS::Lambda - Perl support for AWS Lambda Custom Runtime.
# SYNOPSIS
Save the following Perl script as `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 [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;
};
}
# DESCRIPTION
This package makes it easy to run AWS Lambda Functions written in Perl.
## Use Pre-built Public Lambda Layers
1. Login to your AWS Account and go to the Lambda Console.
2. Create a new function and give it a name and an IAM Role.
3. For the "Runtime" selection, select **Provide your own bootstrap on Amazon Linux 2**.
4. In the "Designer" section of your function dashboard, select the **Layers** box.
5. Scroll down to the "Layers" section and click **Add a layer**.
6. Select the **Provide a layer version ARN** option, then copy/paste the Layer ARN for your region.
7. Click the **Add** button.
8. Click **Save** in the upper right.
9. Upload your code and start using Perl in AWS Lambda!
You can get the layer ARN in your script by using `get_layer_info`.
use AWS::Lambda;
my $info = AWS::Lambda::get_layer_info_al2023(
"5.38", # Perl Version
"us-east-1", # Region
"x86_64", # Architecture ("x86_64" or "arm64", optional, the default is "x86_64")
);
say $info->{runtime_arn}; # arn:aws:lambda:us-east-1:445285296882:layer:perl-5-38-runtime-al2023-x86_64:1
say $info->{runtime_version}; # 1
say $info->{paws_arn} # arn:aws:lambda:us-east-1:445285296882:layer:perl-5-38-paws-al2023-x86_64:1
say $info->{paws_version} # 1,
Or, you can use following one-liner.
perl -MAWS::Lambda -e 'AWS::Lambda::print_runtime_arn_al2023("5.38", "us-east-1")'
perl -MAWS::Lambda -e 'AWS::Lambda::print_paws_arn_al2023("5.38", "us-east-1")'
The list of all layer ARNs is available on [AWS::Lambda::AL2023](https://metacpan.org/pod/AWS%3A%3ALambda%3A%3AAL2023).
## Use Pre-built Zip Archives
1. Login to your AWS Account and go to the Lambda Console.
2. Create a new layer and give it a name.
3. For the "Code entry type" selection, select **Upload a file from Amazon S3**.
4. In the "License" section, input [https://github.com/shogo82148/p5-aws-lambda/blob/main/LICENSE](https://github.com/shogo82148/p5-aws-lambda/blob/main/LICENSE).
5. Click **Create** button.
6. Use the layer created. For detail, see Use Prebuilt Public Lambda Layer section.
URLs for Zip archives are here.
`https://shogo82148-lambda-perl-runtime-$REGION.s3.amazonaws.com/perl-$VERSION-runtime-al2023-$ARCHITECTURE.zip`
## Use Pre-built Docker Images
Prebuilt Docker Images based on [https://gallery.ecr.aws/lambda/provided](https://gallery.ecr.aws/lambda/provided) are available.
You can pull from [https://gallery.ecr.aws/shogo82148/p5-aws-lambda](https://gallery.ecr.aws/shogo82148/p5-aws-lambda) or [https://hub.docker.com/r/shogo82148/p5-aws-lambda](https://hub.docker.com/r/shogo82148/p5-aws-lambda),
build your custom images and deploy them to AWS Lambda.
Here is an example of Dockerfile.
FROM shogo82148/p5-aws-lambda:base-5.38.al2023
# or if you want to use ECR Public.
# FROM public.ecr.aws/shogo82148/p5-aws-lambda:base-5.38.al2023
COPY handler.pl /var/task/
CMD [ "handler.handle" ]
Build the hello-perl container image locally:
$ docker build -t hello-perl .
To check if this is working, start the container image locally using the Lambda Runtime Interface Emulator:
$ docker run -p 9000:8080 hello-perl:latest
Now, you can test a function invocation with cURL.
$ curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'
To upload the container image, you need to create a new ECR repository in your account and tag the local image to push it to ECR.
$ aws ecr create-repository --repository-name hello-perl --image-scanning-configuration scanOnPush=true
$ docker tag hello-perl:latest 123412341234.dkr.ecr.sa-east-1.amazonaws.com/hello-perl:latest
$ aws ecr get-login-password | docker login --username AWS --password-stdin 123412341234.dkr.ecr.sa-east-1.amazonaws.com
$ docker push 123412341234.dkr.ecr.sa-east-1.amazonaws.com/hello-perl:latest
Finally, create new function using awscli.
$ 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
## Run in Local using Docker
Prebuilt Docker Images based on [https://hub.docker.com/r/lambci/lambda/](https://hub.docker.com/r/lambci/lambda/) are available.
You can pull from [https://gallery.ecr.aws/shogo82148/p5-aws-lambda](https://gallery.ecr.aws/shogo82148/p5-aws-lambda) or [https://hub.docker.com/r/shogo82148/p5-aws-lambda](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 in 1.379 second using v1.01-cache-2.11-cpan-39bf76dae61 )