AWS-Lambda-Quick

 view release on metacpan or  search on metacpan

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


=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

This will create a REST API API Gateway that we use for accessing
any quick functions created on that account.  If you're not familiar
with AWS, this can we considered somewhat like a top level domain where
all the APIs we create will be "mounted".

If you want you can pass in an alternative existing rest API to
be used instead, either with the C<rest_api_id> parameter to specify
by id, or by passing in the name via the C<rest_api> parameter.

=head3 Create a new resource

Executing will create a new resource for each Lambda function (If you're
not familiar with AWS this is somewhat like specifying a path for the
API to be callable on.) This will be created directly off the top level
resource (i.e. off of "/") and will be named after the name of the
Lambda function (i.e. calling C<use AWS::Lambda::Quick (name => "foo")>
will create a resource C</foo>)

=head3 Create a new method

Each Lambda function we create gets its own method, which is where
AWS specifies what HTTP method it accepts (C<GET>,C<POST>,C<PUT>,
etc.) and how it decides who can access it.

This module always sets the type of method to C<ANY> (i.e. we always
call the lambda function and let it figure out what it wants to accept
or not.)

We setup the C<NONE> authentication, meaning anyone can call the API
over the internet - i.e. it's configured as a public API.

=head3 Create a new integration

Integrations are how AWS decides both where a request is routed to
and what extracted from that HTTP request is passed on and how.

We configure an AWS_PROXY integration routing to our new Lambda
function.  This essentially means everything is passed "as is"
through to our handler as the first argument.

Upload and GET the following to see what is being passed in
your environment:



( run in 1.121 second using v1.01-cache-2.11-cpan-13bb782fe5a )