问题
Is there any possibility of running a Perl program from lambda functions in Amazon Web Services?
回答1:
You can run essentially anything in Lambda as long as it is binary-compatible with the Lambda runtime environment and can fit into a deployment package. But you have to wrap it in a function written in one of the supported languages. This is surprisingly more effective and efficient than you might expect, particularly if you can write your code in the other language so that it can be spawned once per container and exchange serialized messages with the Lambda function. Or, you can just run the external program once per invocation.
There is a "blueprint" in the Lambda console called node-exec
that illustrates a simple example. Its source is this:
'use strict';
const exec = require('child_process').exec;
exports.handler = (event, context, callback) => {
if (!event.cmd) {
return callback('Please specify a command to run as event.cmd');
}
const child = exec(event.cmd, (error) => {
// Resolve with result of process
callback(error, 'Process complete!');
});
// Log process stdout and stderr
child.stdout.on('data', console.log);
child.stderr.on('data', console.error);
};
It's just enough to give you the idea of how it can be done.
The system Perl on the Lambda/Node 6 runtime environment is perl5 (revision 5 version 16 subversion 3).
Where the task can be accomplished equally effectively with one of the natively supported languages, using one of the supported languages is almost certainly the way to go... and if you know Perl and particularly if you have also worked with asynchronous frameworks like Mojolicious, you're potentially not far off from being able to be productive with Node in a relatively short time.
But one example where running Perl in Lambda was an appropriate choice for me was decoding legacy data serialized with Storable::freeze() and then encoded in base64. Storable is a serialization format that is specific to Perl. My Node Lambda function passes the event data to the Perl script (spawned with child_process.exec
) as JSON on STDIN, which writes the decoded result (also in JSON) to STDOUT, where the Node code captures it and invokes the callback. The Perl script continues running between invocations. Runtime for the entire invocation on a warmed up container is < 4 ms.
回答2:
Now that Lambda supports custom runtimes a selection of Perl runtimes exist. And, of course, a CPAN module: https://metacpan.org/pod/AWS::Lambda
来源:https://stackoverflow.com/questions/50396562/perl-script-in-amazon-web-services-lambda-functions