You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 7, 2024. It is now read-only.
This example demonstrates how you can create a bunch of resized copies right after uploading an image to S3. The solution requires no servers, is scalable and can be automatically deployed within minutes. The solution makes use of two S3 buckets: One that contains the original files and one that contains the resized images.
3
+
This repository demonstrates how you can create a bunch of resized images right after uploading an image to S3. The solution requires no servers, is scalable and can be automatically deployed within minutes. The following figure demonstrate the image resizing process.
* The bucket that contains the original images. Users upload images to this bucket. This bucket is suffixed with `-original` in the following example.
10
+
* The bucket that contains the resized images. The bucket is suffixed with `-resized` in the following example.
11
+
12
+
When you upload an image to the `-original` bucket a Lambda function is executed. The Lambda function downloads the image from S3, creates multiple resized versions and uploads them to the `-resized` S3 bucket. As of now three resized copies are created for every upload to `-original`:
13
+
14
+
*`150x` fixed width, height is scaled as needed
15
+
*`50x50` scale image best into box
16
+
*`x150` fixed height, width is scaled as needed
17
+
18
+
You can find out how [ImageMagick resize](http://www.imagemagick.org/Usage/resize/) works or read on to deploy the solution.
19
+
20
+
## Deploying the solution
21
+
22
+
==WARNING: This example assumes that you have installed and configured the [AWS Command Line Interface](https://aws.amazon.com/cli/)==
23
+
24
+
Clone this repository ...
8
25
9
26
```
10
27
$ git clone git@github.com:AWSinAction/lambda.git
11
28
$ cd lambda/
12
29
```
13
30
14
-
create an S3 bucket for your Lambda code in the US East (N. Virginia, `us-east-1`) region and upload the `lambda.zip` file (replace `$LambdaS3Bucket` with a S3 bucket name e. g. `lambda-michael`)
Create an S3 bucket for your Lambda code in the US East (N. Virginia, `us-east-1`) region and upload the `lambda.zip` file (replace `$LambdaS3Bucket` with a S3 bucket name e.g. `lambda-michael`).
40
+
41
+
==WARNING: This bucket has nothing to do with the resizing of images. It contains the zipped source code of the Lambda function.==
create cloudformation stack (replace `$ImageS3Bucket` with a name for your image bucket e. g. `image-michael`, replace `$LambdaS3Bucket` with your Lambda code S3 bucket name)
49
+
Create a CloudFormation stack (replace `$ImageS3Bucket` with a name for your image bucket e. g. `image-michael`, replace `$LambdaS3Bucket` with your Lambda code S3 bucket name).
you can now upload your first image to the original S3 bucket (you can also use the web based Management Console if you prefere)
64
+
You can now upload your first image to the `-original` S3 bucket (you can also use the web based [Management Console](https://console.aws.amazon.com/s3) if you prefer).
you will see the resized images in the resized bucket (you can also use the web based Management Console if you prefere)
70
+
You will see the resized images in the `-resized` bucket (you can also use the web based Management Console if you prefer).
44
71
45
72
```
46
73
$ aws s3 ls s3://$ImageS3Bucket-resized
47
-
PRE 150x/
48
-
PRE 50x50/
49
-
PRE x150/
74
+
PRE 150x/
75
+
PRE 50x50/
76
+
PRE x150/
50
77
51
78
$ aws s3 ls s3://$ImageS3Bucket-resized/150x/
52
79
```
53
80
54
-
as you can see, for every size configuration a new "directory" is created.
81
+
As you can see, for every size configuration a new "directory" was created.
82
+
83
+
## What's next?
84
+
85
+
From this point you can think about how to enable your users to upload files into the `-original` S3 bucket if your use cases requires user generated content. To allow uploads from your users you should think about a way to protect your `-original` bucket with IAM permissions. You could use the [Security Token Service](http://docs.aws.amazon.com/STS/latest/APIReference/Welcome.html) to achieve this.
86
+
87
+
You should use CloudFront in combination with the `-resized` bucket to serve the resized images to your end users. This will decrease the latency by pushing your content to one of the edge locations of the CloudFront CDN network.
55
88
56
89
## Teardown
57
90
58
-
remove all files in the original and resized bucket
91
+
Remove all files in the `-original` and `-resized` bucket.
If you want to make changes to the code you need to create a new lambda code file (`lambda.zip`) and upload the new zip to S3. Adjust the `config.json` file to adjust the size configuration.
112
+
If you want to make changes to the code you need to create a new lambda code file (`lambda.zip`) and upload the new zip to S3. You can adjust the `config.json` file to adjust the size configurations.
AWS Lambda can respond to S3 events like a new file was uploaded. The Lambda function will download the original image from S3 to create new resized images. The resized images are then upload to S3 again. The Lambda solution in scalable and does not require any operational work.
0 commit comments