Skip to content

Commit 5039a39

Browse files
committed
add aws-node-image-resizer example
1 parent 4d491b0 commit 5039a39

File tree

17 files changed

+8501
-0
lines changed

17 files changed

+8501
-0
lines changed

aws-node-image-resizer/Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM amazonlinux
2+
WORKDIR /deploy
3+
RUN yum -y install make gcc*
4+
RUN curl --silent --location https://rpm.nodesource.com/setup_8.x | bash -
5+
RUN yum -y install nodejs
6+
RUN npm install -g serverless
7+
COPY . .
8+
RUN npm i --production
9+
10+
RUN ["chmod", "+x", "deploy.sh"]
11+
CMD ./deploy.sh ; sleep 2m

aws-node-image-resizer/README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<!--
2+
title: 'Dynamic Image Resizing API'
3+
description: 'This example shows you how to setup a dynamic image resizer API'
4+
layout: Doc
5+
framework: v1
6+
platform: AWS
7+
language: nodeJS
8+
authorLink: 'https://github.com/sebito91'
9+
authorName: 'Sebastian Borza'
10+
authorAvatar: 'https://avatars0.githubusercontent.com/u/3159454?v=4&s=140'
11+
-->
12+
13+
# Dynamic image resizing with Node.js and Serverless framework
14+
15+
In this example, we set up a dynamic image resizing solution with AWS S3 and a Serverless framework function written in Node.js. We use [the `sharp` package](https://www.npmjs.com/package/sharp) for image resizing.
16+
17+
`sharp` includes native dependencies, so in this example we are building and deploying the Serverless function from a Docker container that’s based on Amazon Linux.
18+
19+
## Pre-requisites
20+
21+
In order to deploy the function, you will need the following:
22+
23+
- API credentials for AWS, with Administrator permissions (for simplicity, not recommended in production).
24+
- An S3 bucket in your AWS account.
25+
- Serverless framework installed locally via `yarn global add serverless`.
26+
- Node.js 8 and `yarn` installed locally.
27+
- Docker and docker-compose installed locally.
28+
29+
## Deploying the Serverless project
30+
31+
1. Clone the repository and install the dependencies:\
32+
33+
```
34+
yarn
35+
```
36+
37+
2. Add your AWS credentials into the `secrets/secrets.env` file.
38+
3. Deploy the Serverless project:\
39+
40+
```
41+
docker-compose up --build
42+
```
43+
44+
## Setting up the S3 bucket
45+
46+
Make sure that your S3 bucket is public. Then follow these additional setup steps:
47+
48+
1. Configure the S3 bucket for website hosting as shown [in the S3 documentation](https://docs.aws.amazon.com/AmazonS3/latest/dev/HowDoIWebsiteConfiguration.html).
49+
2. In the [Advanced Conditional Redirects section](https://docs.aws.amazon.com/AmazonS3/latest/dev/how-to-page-redirect.html#advanced-conditional-redirects) of the Website Hosting settings for the S3 bucket, set up the following redirect rule:
50+
51+
```
52+
<RoutingRules>
53+
<RoutingRule>
54+
<Condition>
55+
<HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals>
56+
</Condition>
57+
<Redirect>
58+
<Protocol>https</Protocol>
59+
<HostName>your_api_endpoint.execute-api.us-east-1.amazonaws.com</HostName>
60+
<ReplaceKeyPrefixWith>dev-1/</ReplaceKeyPrefixWith>
61+
<HttpRedirectCode>307</HttpRedirectCode>
62+
</Redirect>
63+
</RoutingRule>
64+
</RoutingRules>
65+
```
66+
67+
You will need to replace `your_api_endpoint` part with the URL of your Serverless endpoint. You can find out what’s the endpoint URL by running:
68+
69+
```
70+
serverless info
71+
```
72+
73+
or observing the output of the deployment step.
74+
75+
## Any questions or suggestions?
76+
77+
Please feel free to open an issue on this repository if something doesn’t work is doesn’t behave as described here. Thanks for giving this project a try!
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
STAGE=dev-1
2+
PROFILE=
3+
REGION=us-east-1
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
echo "🔍 $(tput bold)Pull secret$(tput sgr0)"
2+
3+
if [ "$#" -lt 1 ]; then
4+
printf "Secret key: "
5+
read key
6+
else
7+
key=$1
8+
fi;
9+
10+
aws ssm get-parameter --name $key --region us-east-1 --with-decryption
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
echo "💾 $(tput bold)Push secret$(tput sgr0)"
2+
3+
if [ "$#" -lt 2 ]; then
4+
printf "Secret key: "
5+
read key
6+
printf "Secret value: "
7+
read value
8+
else
9+
value=$1
10+
key=$2
11+
fi;
12+
13+
aws ssm put-parameter --name $key --value $value --type SecureString --region us-east-1 --overwrite

aws-node-image-resizer/deploy.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
stage=${STAGE}
2+
region=${REGION}
3+
bucket=${BUCKET}
4+
secrets='/deploy/secrets/secrets.json'
5+
6+
sls config credentials \
7+
--provider aws \
8+
--key ${SLS_KEY} \
9+
--secret ${SLS_SECRET} \
10+
--profile ${PROFILE}
11+
12+
echo 'Deploying'
13+
sls deploy -s ${STAGE}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: "3"
2+
services:
3+
image-resize:
4+
build: .
5+
volumes:
6+
- ./secrets:/deploy/secrets
7+
env_file:
8+
- ./secrets/secrets.env

aws-node-image-resizer/package.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"name": "fii-sls",
3+
"version": "1.0.0",
4+
"license": "MIT",
5+
"author": "Artur Figiel",
6+
"scripts": {
7+
"eslint": "eslint ./src",
8+
"deploy": "SLS_DEBUG=* sls deploy --stage $STAGE --region $REGION --verbose",
9+
"deploy:local": "export $(cat ./config/.env | xargs) && serverless deploy --stage $STAGE --region $REGION --verbose",
10+
"remove:stack": "export $(cat ./config/.env | xargs) && sls remove -s $STAGE",
11+
"offline": "export $(cat ./config/.env | xargs) && serverless offline start --stage $STAGE",
12+
"add:env": "./config/push-secret.sh"
13+
},
14+
"dependencies": {
15+
"@babel/runtime": "^7.3.1",
16+
"babel-runtime": "^7.0.0-beta.3",
17+
"lodash": "^4.17.11",
18+
"serverless-offline": "^4.7.0",
19+
"serverless-webpack": "^5.2.0",
20+
"sharp": "^0.21.3",
21+
"source-map-support": "^0.5.10",
22+
"stream": "^0.0.2"
23+
},
24+
"devDependencies": {
25+
"@babel/cli": "^7.2.3",
26+
"@babel/core": "^7.3.4",
27+
"@babel/plugin-transform-runtime": "^7.2.0",
28+
"@babel/preset-env": "^7.3.1",
29+
"aws-sdk": "^2.409.0",
30+
"babel-core": "^7.0.0-bridge.0",
31+
"babel-jest": "^24.1.0",
32+
"babel-loader": "^8.0.5",
33+
"babel-plugin-source-map-support": "^2.0.1",
34+
"babel-plugin-transform-runtime": "^6.23.0",
35+
"babel-preset-env": "^1.7.0",
36+
"babel-preset-stage-3": "^6.24.1",
37+
"jest": "^23.6.0",
38+
"serverless": "^1.35.1",
39+
"webpack": "^4.28.3",
40+
"webpack-node-externals": "^1.7.2"
41+
},
42+
"resolutions": {
43+
"babel-core": "7.0.0-bridge.0"
44+
}
45+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
SLS_KEY=
2+
SLS_SECRET=
3+
PROFILE=
4+
STAGE=dev-1
5+
REGION=us-east-1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"DOMAIN":""}

0 commit comments

Comments
 (0)