Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit 26713e0

Browse files
committed
improved docs
1 parent 62181f6 commit 26713e0

File tree

2 files changed

+53
-16
lines changed

2 files changed

+53
-16
lines changed

README.md

+53-16
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,58 @@
11
# AWS in Action: Lambda
22

3-
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.
44

5-
## Setup
5+
![Image resizing process](./lambda_resize.png?raw=true "Image resizing process")
66

7-
clone this repository
7+
The solution makes use of two S3 buckets:
8+
9+
* 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 ...
825

926
```
1027
$ git clone git@github.com:AWSinAction/lambda.git
1128
$ cd lambda/
1229
```
1330

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`)
31+
or download and extract the zipped repository.
32+
33+
```
34+
$ wget https://github.com/AWSinAction/lambda/archive/master.zip
35+
$ unzip master.zip
36+
$ cd lambda-master/
37+
```
38+
39+
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.==
1542

1643
```
1744
export AWS_DEFAULT_REGION=us-east-1
1845
$ aws s3 mb s3://$LambdaS3Bucket
1946
$ aws s3 cp lambda.zip s3://$LambdaS3Bucket/lambda.zip
2047
```
2148

22-
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).
2350

2451
```
2552
$ aws cloudformation create-stack --stack-name lambda-resize --template-body file://template.json --capabilities CAPABILITY_IAM --parameters ParameterKey=ImageS3Bucket,ParameterValue=$ImageS3Bucket ParameterKey=LambdaS3Bucket,ParameterValue=$LambdaS3Bucket
2653
```
2754

28-
wait until the stack is created (retry if you get `CREATE_IN_PROGRESS` this can take a few minutes)
55+
Wait until the stack is created (retry if you get `CREATE_IN_PROGRESS` this can take a few minutes).
2956

3057
```
3158
$ aws cloudformation describe-stacks --stack-name lambda-resize --query Stacks[].StackStatus
@@ -34,52 +61,62 @@ $ aws cloudformation describe-stacks --stack-name lambda-resize --query Stacks[]
3461
]
3562
```
3663

37-
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).
3865

3966
```
4067
$ aws s3 cp path/to/image.png s3://$ImageS3Bucket-original/image.png
4168
```
4269

43-
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).
4471

4572
```
4673
$ aws s3 ls s3://$ImageS3Bucket-resized
47-
PRE 150x/
48-
PRE 50x50/
49-
PRE x150/
74+
PRE 150x/
75+
PRE 50x50/
76+
PRE x150/
5077
5178
$ aws s3 ls s3://$ImageS3Bucket-resized/150x/
5279
```
5380

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.
5588

5689
## Teardown
5790

58-
remove all files in the original and resized bucket
91+
Remove all files in the `-original` and `-resized` bucket.
5992

6093
```
6194
$ aws s3 rm --recursive s3://$ImageS3Bucket-original
6295
$ aws s3 rm --recursive s3://$ImageS3Bucket-resized
6396
```
6497

65-
delete CloudFormation stack
98+
Delete the CloudFormation stack.
6699

67100
```
68101
$ aws cloudformation delete-stack --stack-name lambda-resize
69102
```
70103

71-
delete Lambda code S3 bucket (replace `$LambdaS3Bucket`)
104+
Delete Lambda code S3 bucket (replace `$LambdaS3Bucket`).
72105

73106
```
74107
$ aws s3 rb --force s3://$LambdaS3Bucket
75108
```
76109

77110
## Customize the code
78111

79-
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.
80113

81114
```
82115
$ npm install
83116
$ ./bundle.sh
84117
$ aws s3 cp lambda.zip s3://$LambdaS3Bucket/lambda.zip
85118
```
119+
120+
## Summary
121+
122+
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.

lambda_resize.png

64.2 KB
Loading

0 commit comments

Comments
 (0)