Load, resize, cache, and generate images via a serverless architecture
Load, resize, cache, and generate (simple, single-letter) images.
/bucket_directory/filename.ext?width=300&height=300
/any_directory/letter?width=300&height=300&primaryColor=%23ff0000ff&secondaryColor=%23ffd700ff
width
and height
are standard pixel valuesprimaryColor
and secondaryColor
should be hex values with a hash (#) prefix#
= %23
; see the examples below)#00cc33
or #00cc33ff
)letter
parameter instead of in the path) then the system will fall back to returning the requested letterhttps://some.endpoint.com/my_s3_bucket/0123abcd.png?width=300&height=300
https://some.endpoint.com/my_s3_bucket/w?width=300&height=300&primaryColor=%23ff0000ff&secondaryColor=%23ffd700ff
https://some.endpoint.com/my_s3_bucket/0123abcd.png?width=300&height=300&letter=t&primaryColor=%23ff0000ff&secondaryColor=%23ffd700ff
Built with the Serverless framework for use with AWS lambda functions and S3 storage.
To deploy to an AWS account, setup the AWS Credential store or environment variables as usual.
.editorconfig
filestslint
warningsInstall ImageMagick (download or use choco install imagemagick.tool
) and make sure it is in your PATH
.
Tip: On Windows, some of the tools are named the same as Windows commands (like convert
) so make sure it is very early in your PATH.
serverless-local-example.yml
to server-local.yml
and configure it according to your environmentnpm install -g yarn
npm install -g serverless
yarn
to install and upgrade necessary development librariesaws-sdk
library should be set to, since it is fixed on their side; the package.json
aws-sdk
version should be updated to match the current “AWS SDK for JavaScript” version listed there.packages.json
, make sure to check and update the serverless.yml
frameworkVersion
accordinglyRun npm test
to run unit tests
Note: Configure the slow (via packages.json
call to mocha) and timeout (via calls to describe...it(...).timeout(ms)
on each test) settings as necessary when testing image processing functions that call out to the external process, which can take longer than average.
Run a serverless command as necessary; see the Serverless Quick Start documentation
Example deployment to AWS Lambda development (staging):
serverless deploy -v --stage dev
Example deployment of a single function (based on the base function name given in serverless.yml
)
serverless deploy -f image
Example deployment to production:
serverless deploy -v --stage production
Example cleanup:
serverless remove
Some tips:
ImageMagick
libraries so it only needs referenced as a developer dependencyserverless
tool can also be run with the symlink/cmd sls
serverless print
to check your serverless configuration (including variable substitutions)serverless invoke local -f image --path test/test.json
(or any of the other testX.json
files); if returns without much of a message beyond some asset names and does not also show a lot of base64-encoded data, then check your provider configurationserverless package
, then check ~/.serverless/cloudformation-template...json
to check your provider configuration and what is going to end up as your AWS CloudFormation stack
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::your.bucket.name.here"
},
{
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject",
"s3:DeleteObject"
],
"Resource": [
"arn:aws:s3:::your.bucket.name.here/directory-one",
"arn:aws:s3:::your.bucket.name.here/directory-one/*",
"arn:aws:s3:::your.bucket.name.here/directory-two",
"arn:aws:s3:::your.bucket.name.here/directory-two/*",
"arn:aws:s3:::your.bucket.name.here"
]
},
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:DescribeLogGroups",
"logs:DescribeLogStreams",
"logs:PutLogEvents",
"logs:PutRetentionPolicy"
],
"Resource": [
"*"
]
}
]
}
*.ts
files in the test
directory) are setup to not be run with the full serverless framework and provider system. Instead, they are meant to test your service layer. (You abstracted the service layer from the provider/serverless handler layer, correct?) For full serverless level but still local (non-live) tests, write JSON files with the necessary data and call sls invoke local --path test/some.test.data.file.name.here.json
. (See the test/*.json
files for examples.)