AWS-Lambda-Quick

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN

            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

# DESCRIPTION

README.md  view on Meta::CPAN

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.

## 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

### Create A New Role For Use With AWS::Lambda::Quick

Execution creates a new role called `perl-aws-lambda-quick` that can

README.md  view on Meta::CPAN


    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 `role` parameter described below.

### Create a new Lambda Function

Execution uploads the script (and any extra files specified, see
the `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.

### 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".

README.md  view on Meta::CPAN

    This will become part of the URL that can be used to call this
    function so you are strongly encouraged not to use any non-url
    safe characters (including `/`, `?`, etc) in the name.

- description

    The description of the Lambda function (shown in the AWS console, etc.)

- extra\_files

    An array of extra files and directories that you wish to upload in
    addition to the script itself.

    For example, to upload the `lib` directory, you would write:

        use AWS::Lambda::Quick (
            name => 'some-script-that-needs-modules',
            extra_files => [ 'lib' ],
        );

    These filenames must be relative and will be expected to be in the
    same directory as the script itself.  Passing a directory name will
    cause the directory contents to be recursively uploaded.  If a named
    file/directory is not present at the passed location it will be
    silently ignored.

- region

    The region you wish to deploy the lamda function to.  By default
    this will be `us-east-1`.

- memory\_size

README.md  view on Meta::CPAN

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 `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.

## Enabling debugging output

To gain a little more insight into what is going on you can set
the `AWS_LAMBDA_QUICK_DEBUG` environment variable to enabled
debugging to STDERR:

    shell$ AWS_LAMBDA_QUICK_DEBUG=1 perl lambda-function.pl
    updating function code

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

    # process the whole thing
    my $proc = AWS::Lambda::Quick::Processor->new(
        src_filename => $file,
        @_,
    );
    my $url = $proc->process;
    print "$url\n" or die "problem with fh: $!";

    # and exit before we run the remainder of the script
    # (since that's meant to be run on AWS Lambda, and we're just
    # uploading at this time!)
    exit;
}

1;

__END__

=head1 NAME

AWS::Lambda::Quick - quickly create a REST accesible AWS Lambda function

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

            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

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

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

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


    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".

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

This will become part of the URL that can be used to call this
function so you are strongly encouraged not to use any non-url
safe characters (including C</>, C<?>, etc) in the name.

=item description

The description of the Lambda function (shown in the AWS console, etc.)

=item extra_files

An array of extra files and directories that you wish to upload in
addition to the script itself.

For example, to upload the C<lib> directory, you would write:

    use AWS::Lambda::Quick (
        name => 'some-script-that-needs-modules',
        extra_files => [ 'lib' ],
    );

These filenames must be relative and will be expected to be in the
same directory as the script itself.  Passing a directory name will
cause the directory contents to be recursively uploaded.  If a named
file/directory is not present at the passed location it will be
silently ignored.

=item region

The region you wish to deploy the lamda function to.  By default
this will be C<us-east-1>.

=item memory_size

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

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

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

    AWS::Lambda::Quick::CreateZip->new(
        $self->selfkv(
            qw(
                extra_files
                src_filename
                zip_filename
                )
        ),
    )->create_zip;

    my $uploader = AWS::Lambda::Quick::Upload->new(
        $self->selfkv(
            qw(
                description
                extra_layers
                memory_size
                name
                region
                stage_name
                timeout
                zip_filename
                )
        ),
    );

    if ( $ENV{AWS_LAMBDA_QUICK_UPDATE_CODE_ONLY} ) {
        $uploader->just_update_function_code;
        return q{};
    }

    $uploader->upload;
    return $uploader->api_url;
}

1;

__END__

=head1 NAME

AWS::Lambda::Quick::Processor - main object class for AWS::Lambda::Quick

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

            'parent-id'   => $self->resource_id,
            'path-part'   => '{proxy+}',
        },
    );
    $self->debug('created new greedy resource');
    return $result->{id};
};

### methods

sub upload {
    my $self = shift;

    my $function_arn = $self->_upload_function;

    for my $resource_id ( $self->resource_id, $self->greedy_resource_id ) {
        $self->_create_method($resource_id);
        $self->_create_method_response($resource_id);
        $self->_create_integration( $function_arn, $resource_id );
        $self->_create_integration_response($resource_id);
    }
    $self->_stage;

    return ();

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

        {
            %{$identifiers},
            'selection-pattern' => q{},
        }
    );
    $self->debug('new integration put');

    return ();
}

sub _upload_function {
    my $self = shift;

    my $update_type = $self->update_type;
    my $region      = $self->region;

    # compute the arn based on the list in the AWS::Lambda 0.0.11
    # documentation
    my $v      = $region eq 'me-south-1' ? 3 : 5;
    my $layers = [
        "arn:aws:lambda:$region:445285296882:layer:perl-5-30-runtime:$v",

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


    return ();
}

1;

__END__

=head1 NAME

AWS::Lambda::Quick::Upload - upload for AWS::Lambda::Quick

=head1 DESCRIPTION

No user servicable parts.  See L<AWS::Lambda::Quick> for usage.

=head1 AUTHOR

Written by Mark Fowler B<mark@twoshortplanks.com>

Copyright Mark Fowler 2019.

t/03process.t  view on Meta::CPAN


use AWS::Lambda::Quick::Processor ();
use TestHelper::CreateTestFiles qw(populated_tempdir);

ok( 1, 'processor compiled' );

if ( $ENV{DO_AWS_TESTS} ) {
    my $tempdir      = populated_tempdir();
    my $zip_filename = path( $tempdir, 'handler.zip' );

    # upload;

    my $url = AWS::Lambda::Quick::Processor->new(
        src_filename => path( $tempdir, 'src', 'handler.pl' ),
        name         => 'aws-uploader-quick-test-suite-function',
        extra_files  => ['lib'],
    )->process;

    ## try to use

    my $response = HTTP::Tiny->new->get( $url . '?who=Everyone' );
    is( $response->{content}, 'Hello, Everyone', 'api works' );
}

done_testing;



( run in 3.008 seconds using v1.01-cache-2.11-cpan-b16cb0d3907 )