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
@@ -6,20 +6,24 @@ What you will learn at the end of this tutorial is how to execute a Lambda Funct
6
6
7
7
If you want to learn why you should care about image optimizations I encourage you to visit [web.dev](web.dev), especially [Fast load times](https://web.dev/fast).
8
8
9
+
## Notice
10
+
11
+
This Lambda function uses a pure Javascript image optimizaction ([Jimp](https://www.npmjs.com/package/jimp)). For a better performant solution consider using Imagemin or GraphicsMagic.
@@ -29,6 +33,10 @@ S3 Bucket structure will be the following:
29
33
4. Lambda Function resize and compress image to generate 4 new images.
30
34
5. Lambda Function upload optimized images to `/thumbs` folder.
31
35
36
+
## prerequisites
37
+
38
+
This tutorial asumes you have an AWS account and you've configured AWS credentials for CLI, if you haven't [please do so](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html#post-install-configure).
39
+
32
40
## S3
33
41
34
42
Let's create an S3 bucket. You can do this though the console or using the AWS CLI. named it `{prefix}-gallery`. Replace prefix with a unique identifier so that S3 name is [globally unique](https://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html). I'll use `rtalexk-gallery`, as my username.
@@ -37,14 +45,16 @@ Let's create an S3 bucket. You can do this though the console or using the AWS C
37
45
38
46
```bash
39
47
(bash)$ aws s3 mb s3://rtalexk-gallery
40
-
output: make_bucket: rtalexk-gallery
48
+
----------------
49
+
make_bucket: rtalexk-gallery
41
50
```
42
51
43
52
You can list your buckets to make sure it was created successfuly:
44
53
45
54
```bash
46
55
(bash)$ aws s3 ls
47
-
output: 2019-08-13 10:53:34 rtalexk-gallery
56
+
----------------
57
+
2019-08-13 10:53:34 rtalexk-gallery
48
58
```
49
59
50
60
## Execution role
@@ -83,25 +93,25 @@ And now we can create the role:
@@ -194,6 +205,177 @@ This step is easier within the Console. From the Lambda console, select `Add tri
194
205
195
206
To achieve this, we'll require to perform two actions:
196
207
197
-
1) Add permissions to the Lambda Function to be executed by S3,
208
+
1) Add permissions to the Lambda Function to be executed by S3, and
198
209
2) Add notification configuration to the S3 bucket so that when an object is created, it performs an action, which is the function execution.
199
210
211
+
**To add permissions to the function policy**
212
+
213
+
1. Run the following Lambda CLI add-permission command to grant Amazon S3 service principal (s3.amazonaws.com) permissions to perform the lambda:InvokeFunction action. Note that permission is granted to Amazon S3 to invoke the function only if the following conditions are met:
214
+
215
+
* An object-created event is detected on a specific bucket.
216
+
* The bucket is owned by a specific AWS account. If a bucket owner deletes a bucket, some other AWS account can create a bucket with the same name. This condition ensures that only a specific AWS account can invoke your Lambda function.
> Note: Notice that in the S3 section, where whe created the bucket, we used the `s3` command, and here we used `s3api` command. These commands are different: `s3` is used to manipulate buckets and its content, whilst `s3api` is used to command the S3 service to perform certain actions using its API. Refer to [Leveraging the s3 and s3api Commands](https://aws.amazon.com/blogs/developer/leveraging-the-s3-and-s3api-commands/) for a clearer explanation.
352
+
353
+
## Testing
354
+
355
+
Upload either a `png` or a `jpg` image to S3 with the prefix of `original/`, you can use the following command (make sure you have an image in your current directory):
356
+
357
+
```
358
+
(bash) $ ls -lh cat.jpg
359
+
----------------
360
+
-rw-r--r--@ 1 alejandro rivera 1780047346 244K Aug 8 16:54 cat.jpg
0 commit comments