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
Copy file name to clipboardExpand all lines: lessons/08-worker-lambda/README.md
+26-35Lines changed: 26 additions & 35 deletions
Original file line number
Diff line number
Diff line change
@@ -24,16 +24,13 @@ In this lesson we will learn how to consume messages from an SQS queue, how to w
24
24
25
25
## 08.01 - Consuming messages from an SQS queue
26
26
27
-
You can consume a message from an SQS queue in a Lambda function by using the AWS
28
-
SDK.
27
+
You can consume a message from an SQS queue in a Lambda function by using the AWS SDK.
29
28
30
29
The idea is that you start by pulling the queue for one or more messages, then you process the received payload,
31
-
finally when the processing is done, you remove the message(s) from the queue to mark the job as
32
-
completed.
30
+
finally when the processing is done, you remove the message(s) from the queue to mark the job as completed.
33
31
34
32
SQS, in fact, by default will put messages back in the queue if they are not deleted by the worker.
35
-
This happens because the worker might crash before completing the processing of the message and the
36
-
queue tries to protect you from losing messages.
33
+
This happens because the worker might crash before completing the processing of the message and the queue system tries to protect you from losing messages.
37
34
38
35
Using the AWS SDK, you can pull for a message as follows:
39
36
@@ -79,8 +76,7 @@ SQS will look like the following:
79
76
```
80
77
([View this JSON in the browser](https://gist.githubusercontent.com/lmammino/ae072002e64b1e1e8372aac9b0158ea4/raw/85ffc54239e1134e4395955269311160cc01660d/example.json))
81
78
82
-
So in order to get the real content of the message you will need to JSON-parse-it™
83
-
twice as follows:
79
+
So in order to get the real content of the message you will need to JSON-parse-it™ twice as follows:
84
80
85
81
86
82
```javascript
@@ -117,11 +113,9 @@ As part of this lesson we will need to send an email from a Lambda function.
117
113
AWS offers a complete service for sending transactional emails called
118
114
[Simple Email Service (SES)](https://aws.amazon.com/ses/).
119
115
120
-
Although this is the right service to use in production for sending emails in the
121
-
AWS cloud, it takes sometime to be configured and you will need a custom domain registered.
116
+
Although this is the right service to use in production for sending emails in the AWS cloud, it takes sometime to be configured and you will need a custom domain registered.
122
117
123
-
Also, for the sake of this tutorial, we don't really need to send real email to real people,
124
-
so a simple SMTP test server is more than enough for our purposes.
118
+
Also, for the sake of this tutorial, we don't really need to send real email to real people, so a simple SMTP test server is more than enough for our purposes.
125
119
126
120
A very good (and mostly free) cloud based SMTP test server is [Mailtrap](https://mailtrap.io).
127
121
@@ -135,7 +129,11 @@ later:
135
129
-`Password`: yyy
136
130
137
131
In order to send emails from Node.js we can use the [nodemailer](http://npm.im/nodemailer) module
138
-
in combination with the [nodemailer-smtp-transport](http://npm.im/nodemailer-smtp-transport) companion module.
132
+
in combination with the [nodemailer-smtp-transport](http://npm.im/nodemailer-smtp-transport) companion module, so be sure to install them in your `src` folder:
133
+
134
+
```bash
135
+
npm i --save nodemailer nodemailer-smtp-transport
136
+
```
139
137
140
138
Here's a quick 'n dirty example on how to send a quick email with `nodemailer` using
@@ -176,9 +174,9 @@ It should be pretty much obvious at this point that we need a way to manage conf
176
174
coming from the outside.
177
175
178
176
Ideally we don't want to embed these parameters directly in our `template.yaml`, but
179
-
have some facility to inject them from the outside at deploy time, maybe from environment variables.
177
+
have some facility to inject them from the outside at deploy time, maybe from local environment variables.
180
178
181
-
Turns out that in SAM (and in Cloudformation), there is the concept of generic parameters.
179
+
Turns out that in SAM (and in Cloudformation), there is a concept of "generic parameters".
182
180
183
181
That's how we can add all the parameters we need for this lesson in our `template.yaml`:
184
182
@@ -207,12 +205,9 @@ Resources:
207
205
# ...
208
206
```
209
207
210
-
From now on, every time we deploy we have to specify values for these parameters,
211
-
which is a bit annoying.
208
+
From now on, every time we deploy we have to specify values for these parameters, which is a bit annoying.
212
209
213
-
A strategy I often use in those cases is to create a file called `.env` (which I immediately add
214
-
to my `.gitignore` to make sure I don't commit it by mistake! 😅) that contains
215
-
the values for all the variables:
210
+
A strategy I often use in those cases is to create a file called `.env` (which I immediately add to my `.gitignore` to make sure I don't commit it by mistake! 😅) that contains the values for all the variables:
216
211
217
212
```bash
218
213
# .env
@@ -227,6 +222,8 @@ export SMTP_PASSWORD="yyy"
227
222
228
223
> 💡 **TIP**: If you do this, be sure to replace the `DEPLOYMENT_BUCKET`, `SMTP_USERNAME` and `SMTP_PASSWORD` with your actual value.
229
224
225
+
> ⚠️ **CAUTION**: This new `.env` file is not the same you might be already using if you are using the [helper container](https://github.com/lucpod/serverless-workshop-helper-container). In order to differentiate between them, I would suggest you to store this new one inside the `lambda` folder.
226
+
230
227
Now when you start your development session you can run:
231
228
232
229
```bash
@@ -239,7 +236,6 @@ So now, for the entire duration of your shell session you can use the following
239
236
240
237
```bash
241
238
sam deploy \
242
-
--region eu-west-1 \
243
239
--template-file packaged.yaml \
244
240
--stack-name $STACK_NAME \
245
241
--capabilities CAPABILITY_IAM \
@@ -251,12 +247,10 @@ sam deploy \
251
247
"SmtpPassword=$SMTP_PASSWORD"
252
248
```
253
249
254
-
> 💡 **TIP**: You can update your deploy script (if you created one previously) to
255
-
include these changes.
250
+
> 💡 **TIP**: You can update your deploy script (if you created one previously) to include these changes.
256
251
257
252
> 💡 **TIP**: In real production apps it can be a good idea to store these parameters
258
-
in AWS by using dedicated services like [Systems Manager Parameter Store](http://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-paramstore.html) or [Config](https://aws.amazon.com/config). With those services you can also store these values as encrypted strings
259
-
and fine tune the level of access to different users or systems.
253
+
in AWS by using dedicated services like [Systems Manager Parameter Store](http://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-paramstore.html) or [Config](https://aws.amazon.com/config) or even the new [Secret Manager](https://aws.amazon.com/blogs/aws/aws-secrets-manager-store-distribute-and-rotate-credentials-securely/). With most of these services you can also store these values as encrypted strings and fine tune the level of access to different users or systems.
260
254
261
255
262
256
## 08.04 - Defining the worker Lambda
@@ -336,20 +330,17 @@ Resources:
336
330
# ...
337
331
```
338
332
339
-
The new things to notice here is how we are defining environment variables by
340
-
referencing the SAM parameters we defined in the previous section.
333
+
The new things to notice here is how we are defining environment variables by referencing the SAM parameters we defined in the previous section (using the `!Ref` operator).
341
334
342
-
With this approach all the parameters values will be available in the Lambda code
343
-
as environment variables.
335
+
With this approach all the parameters values will be available in the Lambda code as environment variables.
344
336
345
337
For example you can access the SMTP password with:
346
338
347
339
```javascript
348
340
process.env.SMTP_PASSWORD
349
341
```
350
342
351
-
Another new thing here is the `Schedule` event. The schedule event allows us to
352
-
execute a Lambda at periodic intervals (in this case every minute).
343
+
Another new thing here is the `Schedule` event. The schedule event allows us to execute a Lambda at periodic intervals (in this case every minute).
353
344
354
345
The syntax is very simple in this case, for more elaborate schedule rules you can even use
If you feel lost or if you need some inspiration you can consult my implementation
394
385
in [`resources/lambda/worker-lambda`](/resources/lambda/worker-lambda/src/index.js).
395
386
396
-
When you feel comfortable enough with the code you can deploy (this time make
397
-
sure you have all the environment variables in your shell session):
387
+
When you feel comfortable enough with the code you can deploy (this time make sure you have all the environment variables in your shell session):
398
388
399
389
```bash
400
390
sam package \
@@ -403,7 +393,6 @@ sam package \
403
393
--output-template-file packaged.yaml
404
394
405
395
sam deploy \
406
-
--region eu-west-1 \
407
396
--template-file packaged.yaml \
408
397
--stack-name $STACK_NAME \
409
398
--capabilities CAPABILITY_IAM \
@@ -421,6 +410,8 @@ sam deploy \
421
410
If you did everything correctly, every time you purchase a new ticket, after one or more
422
411
minutes you should see an email appearing in your *Mailtrap* account.
423
412
413
+
> ⚠️ **CAUTION**: Polling SQS every minute is not for free. If you keep this running for few hours you will still be inside the free tier period, but if you leave it running forever you might start to reach a paid level. So be sure to disable the schedule for the worker lambda (or to remove the lambda entirely) when you are finished with this tutorial 🙄
Copy file name to clipboardExpand all lines: lessons/extra/README.md
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -44,7 +44,7 @@ I hope you had fun along the way and that's just the beginning of your Serverles
44
44
45
45
SAM is the official solution from AWS to manage Serverless applications, but there are a lot of alternatives out there, be sure to check them out to wide your horizons:
46
46
47
-
-[Serverless framework](https://serverless.com)
47
+
-[Serverless framework](https://serverless.com) (the most famous, works also for other cloud providers!)
0 commit comments