Skip to content

Commit e2d60ef

Browse files
authored
Update README.md
The full description of creating lambda
1 parent 29f221c commit e2d60ef

File tree

1 file changed

+144
-33
lines changed

1 file changed

+144
-33
lines changed

README.md

Lines changed: 144 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,56 @@
1-
Static website hosting
2-
```xml
3-
<RoutingRules>
4-
<RoutingRule>
5-
<Condition>
6-
<KeyPrefixEquals/>
7-
<HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals>
8-
</Condition>
9-
<Redirect>
10-
<Protocol>https</Protocol>
11-
<HostName>__DOMAIN__</HostName>
12-
<ReplaceKeyPrefixWith>__PATH_TO_LAMBDA__?path=</ReplaceKeyPrefixWith>
13-
<HttpRedirectCode>307</HttpRedirectCode>
14-
</Redirect>
15-
</RoutingRule>
16-
</RoutingRules>
17-
```
1+
## What is it?
182

3+
It's AWS Lambda, which is a compute service that lets you run code without provisioning or managing servers.
4+
> _[Read more about AWS Lambda.](http://docs.aws.amazon.com/lambda/latest/dg/welcome.html)_
195
20-
Bucket policy
21-
```json
22-
{
23-
"Version": "2012-10-17",
24-
"Statement": [
25-
{
26-
"Sid": "AddPerm",
27-
"Effect": "Allow",
28-
"Principal": "*",
29-
"Action": "s3:GetObject",
30-
"Resource": "arn:aws:s3:::__BUCKET_NAME__/*"
31-
}
32-
]
33-
}
34-
```
356

7+
## What this lambda provides
8+
Let's say we have some _shared image_ in **S3**, for example:
9+
`https://example.com/images/pretty_photo.jpg`
10+
11+
to resize this image to _150x150_, for instance, on fly we can make a request like this:
12+
`https://example.com/images/150x150/pretty_photo.jpg`
13+
14+
So, if there's not image in this path, it's redirected to lambda and, after a moment, lambda creates the suitable image and then redirects back. We'll obviously have a new image next time.
3615

37-
Role policy document
16+
***
17+
Instead of `WxH` there're some extra available _magic paths_:
18+
`.../AUTOx150/...`
19+
`.../150xAUTO/...`
20+
or
21+
`.../150x150_max/...`
22+
`.../150x150_min/...`
23+
24+
> Note that **s3-resizer** **don't enlarge an image** if the original image width or height are already less than required dimensions. You can read about **[#withoutEnlargement](http://sharp.dimens.io/en/stable/api-resize/#withoutenlargement)** method.
25+
26+
27+
## Setting up
28+
29+
#### To resize images we need a storage, which is _S3_, and _Lambda_ function. Then we should set up redirection rules.
30+
31+
* Create a **Bucket**
32+
* * Go to [Services -> Storage -> S3](https://s3.console.aws.amazon.com/s3/home)
33+
* * Click on the blue button **Create bucket**
34+
* * Enter the name and click on **Create**
35+
36+
* Create a **Lambda**
37+
* * Go to [Services -> Compute -> Lambda](https://console.aws.amazon.com/lambda/home)
38+
* * Click on the orange button **Create a function**
39+
* * In the next page, click on the similar button **Author from scratch**
40+
* * Add a trigger, which would listen to http requests (you also would be able to do it later)
41+
* * * On the dotted square choose **API Gateway**
42+
* * * You can use default **API name** or create new one
43+
* * * In **Security** select **Open**, then click **Next**
44+
* * In **Configure function** page
45+
* * * Name a new lambda
46+
* * * In **Runtime** select **Node.js 6.10**
47+
* * * Upload a _.zip_ file (download it from [releases](https://github.com/kofon95/s3-resizer/releases))
48+
* * * > You'll also need to set up two **Environment variables**, with _BUCKET_ and _URL_ as keys. But in this time, you don't know about that _URL_. It is **endpoint** which you'll see below.
49+
* * * Choose role which has permission to put any object or create a new one. To do that
50+
* * * * choose **Create a custom role** in role's list. It should open a new page in your browser. On that page
51+
* * * * choose **Create a new IAM Role**
52+
* * * * name you role, for example: *"access_to_putObject"*
53+
* * * * Expand **View Policy Document**, click **Edit**, and write this content:
3854
```json
3955
{
4056
"Version": "2012-10-17",
@@ -56,3 +72,98 @@ Role policy document
5672
]
5773
}
5874
```
75+
> Pay attention to `__BUCKET_NAME__`
76+
77+
* * * That page should closes after that action. So go on creating a lambda. And take a look at **Advanced settings**
78+
* * * * Allocate a memory 512
79+
* * * * Timeout could be 5 seconds
80+
> It's mooore than enough. And you shouldn't care of limits because images caches, which means lambda calls only for the first time.
81+
* * * Click **Next**, **Create function**. And wait for 20-30 seconds. Lambda is created.
82+
83+
***
84+
85+
* Public access to files in your bucket and relationships between lambda and bucket.
86+
* * Firstly, you need Lambda's _url_
87+
* * * Click the link of **API name** (in case you didn't change it in creating lambda, it should name like **LambdaMicroservice**)
88+
* * * On the new page, look for **Actions** button, select **Deploy API** and choose **prod** in **Deployment stage**. Then click **Deploy**
89+
* * * Expand **prod** in **Stages**, click on **GET** and copy URL that you see
90+
* * Open your created bucket -> Permissions -> Bucket Policy
91+
* * Paste this pease of code there and click **Save**
92+
```json
93+
{
94+
"Version": "2012-10-17",
95+
"Statement": [
96+
{
97+
"Sid": "AddPerm",
98+
"Effect": "Allow",
99+
"Principal": "*",
100+
"Action": "s3:GetObject",
101+
"Resource": "arn:aws:s3:::__BUCKET_NAME__/*"
102+
}
103+
]
104+
}
105+
```
106+
> Pay attention to `__BUCKET_NAME__`. By the way, you're able to open access not to whole bucket but to specific directory specifying it instead of __*__.
107+
* * Go to Properties (near to Permissions) -> Static website hosting -> Select **"Use this bucket to host a website"**
108+
* * In **Index document** paste any file, it'd be logical to name it _"index.html"_
109+
* * Paste this **Redirection rules**
110+
```xml
111+
<RoutingRules>
112+
<RoutingRule>
113+
<Condition>
114+
<KeyPrefixEquals/>
115+
<HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals>
116+
</Condition>
117+
<Redirect>
118+
<Protocol>https</Protocol>
119+
<HostName>__DOMAIN__</HostName>
120+
<ReplaceKeyPrefixWith>__PATH_TO_LAMBDA__?path=</ReplaceKeyPrefixWith>
121+
<HttpRedirectCode>307</HttpRedirectCode>
122+
</Redirect>
123+
</RoutingRule>
124+
</RoutingRules>
125+
```
126+
> Pay attention to `__DOMAIN__` and `__PATH_TO_LAMBDA__` (protocol is always _https_)
127+
> For example, lambda's url is `https://some-id.execute-api.us-east-1.amazonaws.com/prod/your-lambdas-name`, the correct xml nodes must looks like
128+
```xml
129+
<HostName>some-id.execute-api.us-east-1.amazonaws.com</HostName>
130+
<ReplaceKeyPrefixWith>prod/your-lambdas-name?path=</ReplaceKeyPrefixWith>
131+
```
132+
* * At this state, copy your **Endpoint** and click save
133+
* * Go to your lambda -> **Code** and set up these two **Environment variables** _(format: key=value)_
134+
**BUCKET**=_your bucket's name_
135+
**URL**=**Endpoint** you copied before
136+
* * **Save** it. You've done!
137+
138+
***
139+
140+
* Test your lambda (optional)
141+
* * Upload an image to your bucket and copy link to it. Check if the image shows in your browser
142+
> Attention. That link must be of your **Endpoint** (website hosting). It make by concatinating **"$endpoint_url/$path_to_image/$image_name"**
143+
* * Go to lambda, click on **Test**, and paste this json:
144+
```json
145+
{
146+
"queryStringParameters": {"path": __YOUR_IMAGE_PATH_WITH_SIZE_PREFIX__}
147+
}
148+
```
149+
> `__YOUR_IMAGE_PATH_WITH_SIZE_PREFIX__` - for example: `150x150/pretty_image.jpg`
150+
151+
* * Go back to the bucket, a new directory _150x150_ must be created
152+
153+
154+
## Some patterns
155+
This is some pattern which you can use in the models:
156+
```ruby
157+
IMAGE_PATH = "#{YOUR_ENDPOINT_URL}/uploads/images/models/"
158+
159+
def images()
160+
img = self.image_name
161+
162+
return {
163+
original: "#{IMAGE_PATH}#{img}",
164+
big: "#{IMAGE_PATH}1000x1000_max/#{img}",
165+
small: "#{IMAGE_PATH}450x450_max/#{img}",
166+
thumb: "#{IMAGE_PATH}128x128_max/#{img}"
167+
}
168+
end
169+
```

0 commit comments

Comments
 (0)