AWS-Lambda-Quick
view release on metacpan or search on metacpan
lib/AWS/Lambda/Quick.pm view on Meta::CPAN
use strict;
use warnings;
use AWS::Lambda::Quick (
name => 'hello-world',
);
sub handler {
my $data = shift;
my $name = $data->{queryStringParameters}{who} // "World";
return {
statusCode => 200,
headers => {
'Content-Type' => 'text/plain',
},
body => "Hello, $name",
};
}
1;
To upload to and configure AWS, just run the script locally:
shell$ perl myscriptname.pl
https://52p3rf890b.execute-api.us-east-1.amazonaws.com/quick/hello-world
Then you can access it from anywhere:
shell$ curl https://52p3rf890b.execute-api.us-east-1.amazonaws.com/quick/hello-world?who=Mark'
Hello, Mark
=head1 DESCRIPTION
This module allows you to very quickly create a Perl based AWS
Lambda function which can be accessed via HTTP.
Coding Lambda functions in Perl is straight forward: You need only
implement a script with the one C<handler> function that returns the
expected data structure as described in the L<AWS::Lambda>
documentation.
The hard part is configuring AWS to execute the code. Traditionally
you have to complete the following steps.
=over
=item Create a zip file containing your code
=item Create (or update) an AWS Lambda function with this zip file
=item Create a REST API with AWS Gateway API
=item Configure a resource for that REST API for this script
=item Set up a method and put method response for that resource
=item Manage an integration and integration response for that resource
=back
And then debug all the above things, a lot, and google weird error
messages it generates when you inevitably make a mistake.
This module provides a way to do all of this completely transparently
just by executing your script, without having to either interact with
the AWS Management Console nor directly use the awscli utility.
Simply include this module at the top of your script containing the
handler function:
use AWS::Lambda::Quick (
name => 'random-lottery-numbers',
);
And then execute it locally. Rather than running as normal your script
will instead upload itself to AWS as a Lambda function (modifying
itself so that it no longer has a dependency on AWS::Lambda::Quick) and
handle all the other steps needed to make itself web accessible.
Running the script locally subsequent times will update the code and
AWS settings.
=head2 What This Actually Does
You probably don't care about this, but this is actually what's
going on when the script uploads itself. This is subject to change
in later versions of this utility as better ways to do things
become available (for example AWS has a HTTP API that is currently in
beta that could make some of this easier!).
By default, unless you specify extra parameters when you import
AWS::Lambda::Quick, AWS will be configured as described below
=head3 Create A New Role For Use With AWS::Lambda::Quick
Execution creates a new role called C<perl-aws-lambda-quick> that can
be assumed by both the API Gateway (C<apigateway.amazonaws.com>) and
Lambda (C<lambda.amazonaws.com>) services. The role will have
C<AWSLambdaRole> and C<CloudWatchLogsFullAccess> policies permissioned
(so it execute the lambda function and write logs.)
You can modify this role as you see fit. For example, to give your
lambda functions the ability to access S3:
shell$ aws iam attach-role-policy \
--policy-arn arn:aws:iam::aws:policy/AmazonS3FullAccess \
--role-name perl-aws-lambda-quick
If you really want your lambda functions to use a different role then
you can control this with the C<role> parameter described below.
=head3 Create a new Lambda Function
Execution uploads the script (and any extra files specified, see
the C<extra_files> parameter below) as a new Lambda function with the
passed name. Subsequent uploads will replace and update that Lambda
function
The function will be assigned the previously created role.
=head3 Create a API Gateway For All Quick Functions
lib/AWS/Lambda/Quick.pm view on Meta::CPAN
Currently AWS Lamda supports up to four extra layers (five in total
including the prebuilt public layer for Perl.) All layers, when
decompressed, must be less that 250MB in size.
You may either identify a layer by its ARN, or by using a identifying
name that is known to this module. At this time the only known
identifying name is C<paws> which indicates that the Lambda function
should use the prebuilt Paws layer in the same region as the Lambda
function.
use AWS::Lambda::Quick (
name => 'email sender',
extra_layers => [ 'paws' ],
);
=back
=head2 Installing the CLI tools
This module requires you to have the version 1 AWS CLI tools installed
on your system and configured with your authentication credentials.
Installing the tools are covered in many AWS guides, but can be
quickly summarized as:
shell$ curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"
shell$ unzip awscli-bundle.zip
shell$ sudo ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws
You'll need to configure awscli with your own personal AWS Access
Key ID and AWS Secret Access Key. You can create these from the AWS
Management console by following the guide on
L<How to quickly find and update your access keys, password, and MFA setting using the AWS Management Console|https://aws.amazon.com/blogs/security/how-to-find-update-access-keys-password-mfa-aws-management-console/>
Once you have your keys you can then use the C<configure> command
to update the aws command line utility.
shell$ aws configure
AWS Access Key ID [********************]:
AWS Secret Access Key [********************]:
Default region name [us-east-1]:
Default output format [None]:
=head2 Speeding up Code Updates
By default this module will check that everything is configured
correctly in AWS and will make changes as needed. This requires several
API calls (and several executions of the AWS python command line
tool.)
If you've only changed the source code and want to deploy a new version
you can just do that by setting the C<AWS_LAMBDA_QUICK_UPDATE_CODE_ONLY>
enviroment variable:
shell$ AWS_LAMBDA_QUICK_UPDATE_CODE_ONLY=1 perl lambda-function.pl
In the interest of being as quick as possible, when this is environment
variable is enabled the URL for the upload is not computed and printed
out.
=head2 Enabling debugging output
To gain a little more insight into what is going on you can set
the C<AWS_LAMBDA_QUICK_DEBUG> environment variable to enabled
debugging to STDERR:
shell$ AWS_LAMBDA_QUICK_DEBUG=1 perl lambda-function.pl
updating function code
function code updated
updating function configuration
searching for existing role
found existing role
...
=head1 AUTHOR
Written by Mark Fowler B<mark@twoshortplanks.com>
Copyright Mark Fowler 2019.
This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 SEE ALSO
L<AWS::Lambda>, L<AWS::CLIWrapper>
=cut
( run in 1.786 second using v1.01-cache-2.11-cpan-13bb782fe5a )