Skip to content

Commit 622cdc3

Browse files
committed
Updated lesson 8
1 parent 4b26271 commit 622cdc3

File tree

2 files changed

+28
-37
lines changed

2 files changed

+28
-37
lines changed

lessons/08-worker-lambda/README.md

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,13 @@ In this lesson we will learn how to consume messages from an SQS queue, how to w
2424

2525
## 08.01 - Consuming messages from an SQS queue
2626

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

3029
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.
3331

3432
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.
3734

3835
Using the AWS SDK, you can pull for a message as follows:
3936

@@ -79,8 +76,7 @@ SQS will look like the following:
7976
```
8077
([View this JSON in the browser](https://gist.githubusercontent.com/lmammino/ae072002e64b1e1e8372aac9b0158ea4/raw/85ffc54239e1134e4395955269311160cc01660d/example.json))
8178

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:
8480

8581

8682
```javascript
@@ -117,11 +113,9 @@ As part of this lesson we will need to send an email from a Lambda function.
117113
AWS offers a complete service for sending transactional emails called
118114
[Simple Email Service (SES)](https://aws.amazon.com/ses/).
119115

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

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

126120
A very good (and mostly free) cloud based SMTP test server is [Mailtrap](https://mailtrap.io).
127121

@@ -135,7 +129,11 @@ later:
135129
- `Password`: yyy
136130

137131
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+
```
139137

140138
Here's a quick 'n dirty example on how to send a quick email with `nodemailer` using
141139
SMTP:
@@ -157,7 +155,7 @@ const mailOptions = {
157155
158156
159157
subject: 'Just catching up...',
160-
text: 'Hey how are you today?'
158+
text: 'Hey buddy, how are you today?'
161159
}
162160

163161
transporter.sendMail(mailOptions, (err, info) => {
@@ -176,9 +174,9 @@ It should be pretty much obvious at this point that we need a way to manage conf
176174
coming from the outside.
177175

178176
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.
180178

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".
182180

183181
That's how we can add all the parameters we need for this lesson in our `template.yaml`:
184182

@@ -207,12 +205,9 @@ Resources:
207205
# ...
208206
```
209207

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

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:
216211

217212
```bash
218213
# .env
@@ -227,6 +222,8 @@ export SMTP_PASSWORD="yyy"
227222

228223
> 💡 **TIP**: If you do this, be sure to replace the `DEPLOYMENT_BUCKET`, `SMTP_USERNAME` and `SMTP_PASSWORD` with your actual value.
229224
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+
230227
Now when you start your development session you can run:
231228

232229
```bash
@@ -239,7 +236,6 @@ So now, for the entire duration of your shell session you can use the following
239236

240237
```bash
241238
sam deploy \
242-
--region eu-west-1 \
243239
--template-file packaged.yaml \
244240
--stack-name $STACK_NAME \
245241
--capabilities CAPABILITY_IAM \
@@ -251,12 +247,10 @@ sam deploy \
251247
"SmtpPassword=$SMTP_PASSWORD"
252248
```
253249

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.
256251
257252
> 💡 **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.
260254

261255

262256
## 08.04 - Defining the worker Lambda
@@ -336,20 +330,17 @@ Resources:
336330
# ...
337331
```
338332

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).
341334

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

345337
For example you can access the SMTP password with:
346338

347339
```javascript
348340
process.env.SMTP_PASSWORD
349341
```
350342

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).
353344

354345
The syntax is very simple in this case, for more elaborate schedule rules you can even use
355346
[cron expressions](http://docs.aws.amazon.com/lambda/latest/dg/tutorial-scheduled-events-schedule-expressions.html).
@@ -393,8 +384,7 @@ exports.sendMailWorker = (event, context, callback) => {
393384
If you feel lost or if you need some inspiration you can consult my implementation
394385
in [`resources/lambda/worker-lambda`](/resources/lambda/worker-lambda/src/index.js).
395386

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):
398388

399389
```bash
400390
sam package \
@@ -403,7 +393,6 @@ sam package \
403393
--output-template-file packaged.yaml
404394

405395
sam deploy \
406-
--region eu-west-1 \
407396
--template-file packaged.yaml \
408397
--stack-name $STACK_NAME \
409398
--capabilities CAPABILITY_IAM \
@@ -421,6 +410,8 @@ sam deploy \
421410
If you did everything correctly, every time you purchase a new ticket, after one or more
422411
minutes you should see an email appearing in your *Mailtrap* account.
423412

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 🙄
414+
424415

425416
## Closing off
426417

lessons/extra/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ I hope you had fun along the way and that's just the beginning of your Serverles
4444

4545
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:
4646

47-
- [Serverless framework](https://serverless.com)
47+
- [Serverless framework](https://serverless.com) (the most famous, works also for other cloud providers!)
4848
- [Apex framework](http://apex.run)
4949
- [Dawson.sh](https://github.com/dawson-org/dawson-cli)
5050
- [Cloudformation](https://aws.amazon.com/cloudformation) or [Terraform](https://www.terraform.io) (If you really want to go low level)
@@ -55,4 +55,4 @@ Writing Lambda code sometimes ends up in a lot of duplication around things like
5555
error management, response creation, input parsing, etc.
5656

5757
If you end up feeling that there should be a better way to write your Lambda code,
58-
check out [Middy](https://middy.js.org/), a middleware engine for AWS Lambda in Node.js
58+
check out [🛵 Middy](https://middy.js.org), a middleware engine for AWS Lambda in Node.js.

0 commit comments

Comments
 (0)