Theano Docker image to be run on AWS Lambda via SCAR
595
SCAR is a framework to transparently execute Docker containers in AWS Lambda, in order to run applications (see examples for ImageMagickā , FFmpegā and AWS CLIā ) and code in virtually any programming language (see examples for Erlangā and Elixirā ) on AWS Lambda.
SCAR provides the benefits of AWS Lambda with the execution environment you decide, provided as a Docker image available in Docker Hub. It is probably the easiest, most convenient approach to run generic applications on AWS Lambda, as well as code in your favourite programming language, not only in those languages supported by AWS Lambda.
SCAR also supports a High Throughput Computing Programming Modelā to create highly-parallel event-driven file-processing serverless applications that execute on customized runtime environments provided by Docker containers run on AWS Lambda.
SCAR provides a command-line interface to create a Lambda function to execute a container out of a Docker image stored in Docker Hubā . Each invocation of the Lambda function will result in the execution of such a container (optionally executing a shell-script inside the container for further versatility).
The following underlying technologies are employed:
SCAR can optionally define a trigger so that the Lambda function is executed whenever a file is uploaded to an Amazon S3 bucket. This file is automatically made available to the underlying Docker container run on AWS Lambda so that an user-provided shell-script can process the file. See the Programming Modelā for more details.
yum, apt-get, etc.) is currently not possible.git clone https://github.com/grycap/scar.git
You can automatically install the dependencies by issuing the following command:
sudo pip install -r requirements.txt
cd scar
alias scar=`pwd`/scar.py
You need:
Valid AWS IAMā user credentials (Access Key and Secret Key ID) with permissions to deploy Lambda functions.
An IAM Role for the Lambda function to be authorized to access other AWS services during its execution.
The credentials have to be configured in your $HOME/.aws/credentials file (as when using AWS CLIā ). Check the AWS CLI documentation, specially section "Configuration and Credential Files"ā .
The Lambda functions require an IAM Roleā in order to acquire the required permissions to access the different AWS services during its execution.
There is a sample policy in the lambda-execute-role.jsonā file. This IAM Role should be created beforehand. There is further documentation on this topic in the "Creating IAM roles"ā section of the AWS documentation.
Create the file ~/.scar/scar.cfg with the following structure (sample values are included, please customize it to your environment):
[scar]
lambda_description = SCAR Lambda function
lambda_memory = 256
lambda_time = 200
lambda_region = us-east-1
lambda_role = arn:aws:iam::974349055189:role/lambda-s3-execution-role
The values represent:
-d parameter in scar init)-m parameter in scar init)-t parameter in scar init).In these examples the grycap/cowsayā Docker image in Docker Hub will be employed:
scar init -n lambda-docker-cowsay -m 128 -t 300 grycap/cowsay
Notice that the memory and time limits for the Lambda function can be specified in the command-line. Upon first execution, the file $HOME/.scar/scar.cfg is created with default values for the memory and timeout, among other features. The command-line values always take precedence over the values in the configuration file. The default values are 128 MB for the memory (minimize memory) and 300 seconds for the timeout (maximize runtime).
Further information about the command-line arguments is available in the help:
scar --help
scar run lambda-docker-cowsay
The first invocation to the Lambda function will pull the Docker image from Docker Hub so it will take considerably longer than the subsequent invocations, which will most certainly reuse the existing Docker image, stored in /tmp.
The logs are stored in CloudWatch with a default retention policy of 30 days. The following command retrieves all the logs related to the Lambda function:
scar log lambda-docker-cowsay
If you only want the logs related to a log-stream-name you can use:
scar log lambda-docker-cowsay -ls 'log-stream-name'
And finally if you know the request id generated by your invocation, you can specify it if to get the logs related:
scar log lambda-docker-cowsay -ri request-id
You can also specify the log stream name to retrieve the values related with the request id, usually this will be faster if the function has generated a lot of log output:
scar log lambda-docker-cowsay -ls 'log-stream-name' -ri request-id
All values are shown in the output when executing scar log. Do not forget to use the single quotes, as indicated in the example, to avoid unwanted shell expansions.
You can remove the Lambda function together with the logs generated in CloudWatch by:
scar rm -n lambda-docker-cowsay
You can execute the Lambda function and specify a shell-script locally available in your machine to be executed within the container.
scar run -s test/test-cowsay.sh lambda-docker-cowsay
The shell-script can be changed in each different execution of the Lambda function.
A shell-script can be specified when initializing the Lambda function to trigger its execution inside the container on each invocation of the Lambda function. For example:
scar init -s test/test-env.sh -n lambda-test-init-script ubuntu:16.04
Now whenever this Lambda function is executed, the script will be run in the container:
scar run lambda-test-init-script
This can be overridden by speciying a different shell-script when running the Lambda function.
You can specify environment variables to the run command which will be in turn passed to the executed Docker container and made available to your shell-script:
scar run -e TEST1=45 -e TEST2=69 -s test/test-global-vars.sh lambda-docker-cowsay
In particular, the following environment variables are automatically made available to the underlying Docker container:
AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYAWS_SESSION_TOKENAWS_SECURITY_TOKENThis allows a script running in the Docker container to access other AWS services. As an example, see how the AWS CLI is run on AWS Lambda in the examples/aws-cliā folder.
Applications available in the Docker image can be directly executed:
scar run lambda-docker-cowsay /usr/games/fortune
You can also supply arguments which will be passed to the command executed in the Docker container:
scar run lambda-docker-cowsay /usr/bin/perl /usr/games/cowsay Hello World
Note that since cowsay is a Perl script you will have to prepend it with the location of the Perl interpreter (in the Docker container).
For easier scripting, a JSON output can be obtained by including the --json or the -v (even more verbose output) flags.
scar run --json lambda-docker-cowsay
SCAR supports an event-driven programming model suitable for the execution of highly-parallel file-processing applications that require a customized runtime environment.
The following command:
scar init -s user-defined-script.sh -n lambda-function-name -es bucket-name repo/image:latest
Creates a Lambda function to execute the shell-script user-defined-script.sh inside a Docker container created out of the repo/image:latest Docker image stored in Docker Hub.
The following workflow summarises the programming model, which heavily uses the convention over configurationā pattern:
bucket-name will be created if it does not exist, and the input and output folders inside.input folder of the bucket-name bucket./tmp/$REQUEST_ID/input folder. The $SCAR_INPUT_FILE environment variable will point to the location of the input file./tmp/$REQUEST_ID/output.output folder of bucket-name.Many instances of the Lambda function may run concurrently and independently, depending on the files to be processed in the S3 bucket. Initial executions of the Lambda may require retrieving the Docker image from Docker Hub but this will be cached for subsequent invocations, thus speeding up the execution process.
For further information, examples of such application are included in the examples/ffmpegā folder, in order to run the FFmpegā video codification tool, and in the examples/imagemagickā , in order to run the ImageMagickā image manipulation tool, both on AWS Lambda.
You can test locally if the Docker image will be able to run in AWS Lambda by means of udocker (available in the lambda directory) and taking into account the following limitations:
Procedure for testing:
alias udocker=`pwd`/lambda/udocker
udocker pull grycap/cowsay
udocker create --name=my-container grycap/cowsay
udocker setup --execmode=F1 my-container
udocker run my-container
udocker run my-container /bin/sh
Further information is available in the udocker documentation:
udocker help
For easier debugging of the Lambda functions, emulambdaā can be employed to locally execute them.
Install emulambda
Execute a sample local test
sh test/emulambda/run-local-test.sh
This test locally executes the ubuntu:16.04 image in DockerHub via udocker executing a simple shell-script.
SCAR is licensed under the Apache License, Version 2.0. See LICENSEā for the full license text.
Content type
Image
Digest
Size
104.6 MB
Last updated
over 9 years ago
docker pull grycap/theano