├── .gitignore ├── cpanfile ├── scripts ├── build.sh ├── build-all.pl └── publish-layer.sh ├── Dockerfile ├── LICENSE ├── Makefile ├── bootstrap └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | cpanfile.snapshot 2 | *.tar.gz 3 | *.zip 4 | local/ 5 | -------------------------------------------------------------------------------- /cpanfile: -------------------------------------------------------------------------------- 1 | requires 'HTTP::Tiny', '0.076'; 2 | requires 'JSON::XS', '4.0'; 3 | requires 'Carton', 'v1.0.34'; 4 | 5 | -------------------------------------------------------------------------------- /scripts/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | BASE_DIR="/tmp/layer" 4 | 5 | cd "/opt" || exit 1 6 | 7 | cp "${BASE_DIR}/bootstrap" . 8 | chmod 755 'bootstrap' 9 | 10 | # XXX copy some libs for workaround: https://github.com/moznion/aws-lambda-perl5-layer/issues/6 11 | cp -R /lib64/libcrypt* ./lib/ 12 | cp -R /usr/lib64/libcurl* ./lib/ 13 | 14 | zip -r "${BASE_DIR}/lambda-layer-perl-${TAG}.zip" . 15 | 16 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM lambci/lambda:build-provided 2 | 3 | ARG PERL_VERSION 4 | RUN yum install -y zip curl 5 | RUN curl -L https://raw.githubusercontent.com/tokuhirom/Perl-Build/master/perl-build > /tmp/perl-build 6 | RUN perl /tmp/perl-build ${PERL_VERSION} /opt/ 7 | RUN curl -L https://cpanmin.us | /opt/bin/perl - App::cpanminus 8 | COPY cpanfile /tmp/cpanfile 9 | RUN /opt/bin/cpanm -n --installdeps /tmp/ 10 | 11 | -------------------------------------------------------------------------------- /scripts/build-all.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl 2 | 3 | use strict; 4 | use warnings; 5 | use utf8; 6 | 7 | my @perl_versions = qw/5.26.3 5.28.2 5.30.1/; 8 | 9 | for my $perl_version (@perl_versions) { 10 | $perl_version =~ /([0-9]+[.][0-9]+)[.][0-9]+/; 11 | my $container_version = $1; 12 | system("make build-docker-image PERL_VERSION=$perl_version CONTAINER_TAG=$container_version"); 13 | system("make build CONTAINER_TAG=$container_version"); 14 | } 15 | 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright © 2018 moznion, http://moznion.net/ 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 9 | 10 | -------------------------------------------------------------------------------- /scripts/publish-layer.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eu 4 | 5 | AWS_PROFILE=$1 6 | BIN_PATH="$(cd "$(dirname "$0")" || exit; pwd)" 7 | 8 | REGIONS=( 9 | 'ap-northeast-1' 10 | 'ap-northeast-2' 11 | 'ap-south-1' 12 | 'ap-southeast-1' 13 | 'ap-southeast-2' 14 | 'ca-central-1' 15 | 'eu-central-1' 16 | 'eu-west-1' 17 | 'eu-west-2' 18 | 'eu-west-3' 19 | 'sa-east-1' 20 | 'us-east-1' 21 | 'us-east-2' 22 | 'us-west-1' 23 | 'us-west-2' 24 | ) 25 | 26 | PERL_VERSIONS=( 27 | '5.26' 28 | '5.28' 29 | '5.30' 30 | ) 31 | 32 | for PERL_VERSION in "${PERL_VERSIONS[@]}"; do 33 | ZIP="${BIN_PATH}/../lambda-layer-perl-${PERL_VERSION}.zip" 34 | LAYER_NAME="perl-$(echo "$PERL_VERSION" | sed -e's/[.]/_/')-layer" 35 | 36 | for REGION in "${REGIONS[@]}"; do 37 | echo "Publishing perl ${PERL_VERSION} Lambda Layer in ${REGION}..." 38 | 39 | VERSION=$(aws --region "$REGION" --profile "$AWS_PROFILE" lambda publish-layer-version \ 40 | --layer-name "$LAYER_NAME" \ 41 | --description "A lambda runtime layer of perl-${PERL_VERSION}" \ 42 | --license-info "MIT" \ 43 | --zip-file "fileb://${ZIP}" | jq -r .Version) 44 | echo "Published perl ${PERL_VERSION} Lambda Layer in ${REGION}: version ${VERSION}" 45 | 46 | echo "Setting public permissions on perl ${PERL_VERSION} Lambda Layer version ${VERSION} in ${REGION}..." 47 | aws --region "$REGION" --profile "$AWS_PROFILE" lambda add-layer-version-permission \ 48 | --layer-name "$LAYER_NAME" \ 49 | --version-number "$VERSION" \ 50 | --statement-id=public \ 51 | --action lambda:GetLayerVersion \ 52 | --principal '*' > /dev/null 53 | echo "Public permissions set on perl ${PERL_VERSION} Lambda Layer version ${VERSION} in ${REGION}" 54 | done 55 | done 56 | 57 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: build build-docker-image publish-docker-image 2 | 3 | ROOT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) 4 | CONTAINER_BASE_DIR := /tmp/layer 5 | 6 | build: 7 | ifndef CONTAINER_TAG 8 | @echo '[ERROR] $$CONTAINER_TAG must be specified' 9 | @echo 'usage: make build CONTAINER_TAG=x.x' 10 | exit 255 11 | endif 12 | docker run --rm \ 13 | -v $(ROOT_DIR):$(CONTAINER_BASE_DIR) \ 14 | -e TAG=$(CONTAINER_TAG) \ 15 | -e BASE_DIR=$(CONTAINER_BASE_DIR) \ 16 | $(DOCKER_HUB_ACCOUNT)lambda-perl-layer-foundation:$(CONTAINER_TAG) \ 17 | $(CONTAINER_BASE_DIR)/scripts/build.sh 18 | 19 | build-docker-image: 20 | ifndef PERL_VERSION 21 | @echo '[ERROR] $$PERL_VERSION must be specified' 22 | @echo 'usage: make build-docker-image PERL_VERSION=x.x.x CONTAINER_TAG=x.x' 23 | exit 255 24 | endif 25 | ifndef CONTAINER_TAG 26 | @echo '[ERROR] $$CONTAINER_TAG must be specified' 27 | @echo 'usage: make build-docker-image PERL_VERSION=x.x.x CONTAINER_TAG=x.x' 28 | exit 255 29 | endif 30 | docker build $(OPT) --rm --build-arg PERL_VERSION=$(PERL_VERSION) -t lambda-perl-layer-foundation:$(CONTAINER_TAG) . 31 | 32 | publish-docker-image: 33 | ifndef CONTAINER_TAG 34 | @echo '[ERROR] $$CONTAINER_TAG must be specified' 35 | @echo 'usage: make publish-docker-image CONTAINER_TAG=x.x DOCKER_ID_USER=xxx' 36 | exit 255 37 | endif 38 | ifndef DOCKER_ID_USER 39 | @echo '[ERROR] $$CONTAINER_TAG must be specified' 40 | @echo 'usage: make publish-docker-image CONTAINER_TAG=x.x DOCKER_ID_USER=xxx' 41 | exit 255 42 | endif 43 | docker login --username $(DOCKER_ID_USER) 44 | docker tag lambda-perl-layer-foundation:$(CONTAINER_TAG) $(DOCKER_ID_USER)/lambda-perl-layer-foundation:$(CONTAINER_TAG) 45 | docker push $(DOCKER_ID_USER)/lambda-perl-layer-foundation:$(CONTAINER_TAG) 46 | 47 | publish-lambda-layer: 48 | ifndef AWS_PROFILE 49 | @echo '[ERROR] $$AWS_PROFILE must be specified' 50 | @echo 'usage: make publish-lambda-layer AWS_PROFILE=xxx' 51 | exit 255 52 | endif 53 | ./scripts/publish-layer.sh $(AWS_PROFILE) 54 | 55 | -------------------------------------------------------------------------------- /bootstrap: -------------------------------------------------------------------------------- 1 | #!/opt/bin/perl 2 | 3 | use strict; 4 | use warnings; 5 | use utf8; 6 | use HTTP::Tiny; 7 | use JSON::XS qw/decode_json encode_json/; 8 | 9 | my $task_root; 10 | BEGIN { 11 | $task_root = $ENV{'LAMBDA_TASK_ROOT'}; 12 | } 13 | use lib "${task_root}/local/lib/perl5"; # for vendoring 14 | 15 | my $http = HTTP::Tiny->new; 16 | my $aws_lambda_runtime_api = $ENV{'AWS_LAMBDA_RUNTIME_API'} // die '$AWS_LAMBDA_RUNTIME_API is not found'; 17 | my $next_event_url = "http://${aws_lambda_runtime_api}/2018-06-01/runtime/invocation/next"; 18 | 19 | my ($handler, $func) = split(/[.]/, $ENV{'_HANDLER'}, 2); 20 | eval { 21 | require "${task_root}/${handler}.pl"; 22 | }; 23 | if ($@) { 24 | # something went wrong on initiation 25 | # ref: https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html#runtimes-api-initerror 26 | my $err_message = $@; 27 | my $error_resp_url = "http://${aws_lambda_runtime_api}/2018-06-01/runtime/init/error"; 28 | my $resp = $http->post($error_resp_url, { 29 | headers => { 30 | 'Content-Type' => 'application/json', 31 | 'Lambda-Runtime-Function-Error-Type' => 'Unhandled', 32 | }, 33 | content => encode_json({ 34 | errorMessage => $err_message, 35 | errorType => "FunctionInitiationError", 36 | }), 37 | }); 38 | unless ($resp->{success}) { 39 | die "failed to response of failure initiation"; 40 | } 41 | die $err_message; 42 | } 43 | my $f = \&$func; 44 | 45 | while (1) { 46 | my $next_event_resp = $http->get($next_event_url); 47 | unless ($next_event_resp->{success}) { 48 | die "failed to retrieve the next event: $next_event_resp->{status} $next_event_resp->{reason}"; 49 | } 50 | 51 | my $request_id = $next_event_resp->{headers}->{'lambda-runtime-aws-request-id'}; 52 | unless ($request_id) { 53 | die 'cannot take the Lambda request ID'; 54 | } 55 | 56 | my $payload = decode_json($next_event_resp->{content}); 57 | 58 | my $result; 59 | eval { 60 | $result = $f->($payload); 61 | }; 62 | if ($@) { 63 | # something went wrong on called function 64 | # ref: https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html#runtimes-api-invokeerror 65 | my $err_message = $@; 66 | my $error_resp_url = "http://${aws_lambda_runtime_api}/2018-06-01/runtime/invocation/$request_id/error"; 67 | my $resp = $http->post($error_resp_url, { 68 | headers => { 69 | 'Content-Type' => 'application/json', 70 | 'Lambda-Runtime-Function-Error-Type' => 'Unhandled', 71 | }, 72 | content => encode_json({ 73 | errorMessage => $err_message, 74 | errorType => "FunctionInvocationError", 75 | }), 76 | }); 77 | unless ($resp->{success}) { 78 | die "failed to response of failure execution: $request_id"; 79 | } 80 | next; 81 | } 82 | 83 | my $exec_resp_url = "http://${aws_lambda_runtime_api}/2018-06-01/runtime/invocation/$request_id/response"; 84 | my $resp = $http->post($exec_resp_url, { 85 | headers => { 86 | 'Content-Type' => 'application/json', 87 | }, 88 | content => encode_json($result), 89 | }); 90 | 91 | unless ($resp->{success}) { 92 | die "failed to response of execution: $resp->{status} $resp->{reason}"; 93 | } 94 | } 95 | 96 | __END__ 97 | 98 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | aws-lambda-perl5-layer 2 | == 3 | 4 | This repository provides the Perl5 layer for AWS Lambda with runtime API. 5 | 6 | How to use 7 | -- 8 | 9 | ### 1. Using provided layers 10 | 11 | #### ARN 12 | 13 | - `arn:aws:lambda:${REGION}:652718333417:layer:perl-5_26-layer:1` 14 | - `arn:aws:lambda:${REGION}:652718333417:layer:perl-5_28-layer:1` 15 | 16 | ##### Supported regions 17 | 18 | - ap-northeast-1 19 | - ap-northeast-2 20 | - ap-south-1 21 | - ap-southeast-1 22 | - ap-southeast-2 23 | - ca-central-1 24 | - eu-central-1 25 | - eu-west-1 26 | - eu-west-2 27 | - eu-west-3 28 | - sa-east-1 29 | - us-east-1 30 | - us-east-2 31 | - us-west-1 32 | - us-west-2 33 | 34 | ### 1'. Building a layer yourself 35 | 36 | If you want to build a layer and use that instead of provided layer, please follow the following sequence. 37 | 38 | #### How to build a layer 39 | 40 | ``` 41 | $ make build-docker-image PERL_VERSION=x.x.x CONTAINER_TAG=x.x 42 | $ make build CONTAINER_TAG=x.x 43 | ``` 44 | 45 | NOTE: `x.x.x` and `x.x` means perl runtime version (e.g. `5.28.1` and `5.28`). You can specify as you like. 46 | 47 | Then this command stats to create a zip archive as `lambda-layer-perl-x.x.zip`. 48 | 49 | -- 50 | 51 | Or you can use pre-built docker container (RECOMMENDED!): 52 | 53 | ``` 54 | $ make build CONTAINER_TAG=x.x DOCKER_HUB_ACCOUNT='moznion/' 55 | ``` 56 | 57 | Please refer to the following so that getting available containers: https://hub.docker.com/r/moznion/lambda-perl-layer-foundation/ 58 | 59 | #### How to publish the layer 60 | 61 | ```sh 62 | aws --region "$REGION" --profile "$PROFILE" lambda publish-layer-version \ 63 | --layer-name "perl-x.x" \ 64 | --zip-file "fileb://lambda-layer-perl-x.x.zip" 65 | ``` 66 | 67 | ### 2. Create a Lambda function and publish it 68 | 69 | Please refer: [moznion/aws-lambda-perl5-layer-example](https://github.com/moznion/aws-lambda-perl5-layer-example) 70 | 71 | If you register the handler as `handler.handle`, you have to write code into `handler.pl` file and it must have `handle` subroutine. 72 | 73 | An simple example is below (`handler.pl`): 74 | 75 | ```perl 76 | #!perl 77 | 78 | use strict; 79 | use warnings; 80 | use utf8; 81 | 82 | sub handle { 83 | my ($payload) = @_; 84 | 85 | # do something 86 | ... 87 | 88 | # if you want to finish the execution as failed, please die 89 | 90 | # NOTE: must return hashref or arrayref 91 | return +{ 92 | 'msg' => 'OK', 93 | 'this is' => 'example', 94 | }; 95 | } 96 | 97 | 1; # must return TRUE value here! 98 | ``` 99 | 100 | And there is necessary to publish the function with layer information. Please refer to the following example: [How to publish a function](https://github.com/moznion/aws-lambda-perl5-layer-example/tree/master/simple#how-to-publish-a-function) 101 | 102 | FAQ 103 | -- 104 | 105 | ### How to build a Lambda function with package vendoring? 106 | 107 | You have to build a function zip with the perl runtime that is the same version as Lambda's one. 108 | 109 | Please refer: [https://github.com/moznion/aws-lambda-perl5-layer-example/tree/master/simple](https://github.com/moznion/aws-lambda-perl5-layer-example/tree/master/simple) 110 | 111 | ### How should it do when the runtime complains "error while loading shared libraries: libXXX.so"? 112 | 113 | Probably this because of the Lambda runtime environment (i.e. base Linux). 114 | 115 | This lambda applies workaround that includes those missing shared libraries. 116 | 117 | Ref: 118 | 119 | - https://github.com/moznion/aws-lambda-perl5-layer/issues/6 120 | - https://github.com/moznion/aws-lambda-perl5-layer/commit/833ded09dd460ccba08bd4e176a9bc99ff4ec7e1 121 | 122 | However, I think this way is a little bit fragile... is there any good method? 123 | 124 | For Developers 125 | -- 126 | 127 | ### How to publish foundation docker container 128 | 129 | ``` 130 | $ make build-docker-image PERL_VERSION=x.x.x CONTAINER_TAG=x.x 131 | $ make publish-docker-image CONTAINER_TAG=x.x DOCKER_ID_USER=xxx 132 | ``` 133 | 134 | ### How to build foundation docker container without caching 135 | 136 | ``` 137 | $ make build-docker-image PERL_VERSION=x.x.x CONTAINER_TAG=x.x OPT='--no-cache' 138 | ``` 139 | 140 | See Also 141 | -- 142 | 143 | - https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html 144 | 145 | Author 146 | -- 147 | 148 | moznion () 149 | 150 | License 151 | -- 152 | 153 | ``` 154 | The MIT License (MIT) 155 | Copyright © 2018 moznion, http://moznion.net/ 156 | 157 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 158 | 159 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 160 | 161 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 162 | ``` 163 | --------------------------------------------------------------------------------