Welcome back and welcome to part two
of the Pet Cuddle-O-Tron demo series.
In this part of the demo, we start at this point
where we just completed the configuration
of the Simple Email Service.
In part two of this demo series, we'll be extending on this,
we'll be adding Lambda and some configuration
to allow our state machine to use the Simple Email Service.
So this is the configuration that we'll be creating.
We'll be creating a Lambda function
and we'll be configuring the permissions
so that this Lambda function can utilize
the Simple Email Service to send us notification emails.
Now, this will mean that we'll need to create
a Lambda execution role,
which is the thing that gives our Lambda function
the permissions it needs to interact with SES,
and we'll need to set up the function itself.
So let's move across to the console and get started.
Now a link containing the full instructions
and architecture diagrams for all stages of the demo lesson
are attached to this and all the parts of the demo series,
so I do encourage you to check out
the full text instructions because every step that you'll do
in all stages of this demo series
are contained within these text-based instructions
and it will make it much easier to follow.
Now, the first step to performing
this part of the configuration
is that we'll need to create an IAM role that can be assumed
by our Lambda functions.
Now you've already created IAM roles earlier in the course,
and so I'm gonna go ahead and get you to apply
a CloudFormation template to do this on your behalf.
Now there's a one-click deployment link
attached to this lesson
and in this lesson's instructions.
If you just go ahead and open that,
it'll take you to the quick create stack screen.
The stack name should already be populated with LAMBDAROLE.
Just scroll down,
check the Capabilities box and click create stack.
Now this'll take a few minutes,
so go ahead and pause the video
and resume it once it's in create complete.
Now, once that is in a create complete,
if you move back across and go to the IAM console,
we should now have a role that can be assumed
by our Lambda functions.
So click on Roles.
Just scroll down, and you're looking for one
that's got LAMBDAROLE in the name.
So this is the one that's just been created
by our CloudFormation template.
Let's just open that up.
If you look at the trust relationships,
you can see that it's assumable by lambda.amazonaws.com,
so that means Lambda can assume this role.
And if we go to the permissions policy and just expand this,
you'll see that the Lambda function
has permissions on SES, SNS, and states.
And states means that this Lambda function
can interact with state machines inside step functions.
So this provides all of the permissions
that this Lambda function needs
to interact with these three services.
In addition, we also granted the ability
to log any of its logging and diagnostic information
into CloudWatch logs, so that's great,
we've got all the permissions that we need
inside this execution role.
Now that we've configured the role,
the next step is to create the Lambda function itself.
So click on Services and type Lambda
into the find services box
to move across the Lambda console.
Once we're in the Lambda console,
we need to create the email reminder Lambda function.
So go ahead and click on create function.
We'll be authoring the function from scratch.
The name of the function will be email_reminder_lambda.
And then for the runtime,
we're going to use Python,
but if you refer to the text instructions for this lesson
or this lesson's description,
it will tell you exactly which version of Python
to select in this dropdown.
Next, under permissions,
expand change default execution role.
Choose to use an existing role,
and then in the existing role dropdown,
click and select LAMBDAROLE.
And once you've done that,
go ahead and click on create function.
So this means this Lambda function
now has the ability to interact
with the three services that it needs
as part of our serverless application.
Next, just scroll down.
Right-click on lambda_function and select open.
Now, what I want you to do is select
all of the skeleton code that's pre-populated,
and just go ahead and delete it.
And then, attached to this lesson
and in the instructions for this stage of the demo series
is some Lambda code that you need to copy
into your clipboard.
And then just go ahead and paste in that code
from your clipboard and you'll spot right away
that there is a placeholder that you'll need to replace.
All this code does is import some libraries
that will be used during its execution.
It makes a connection to the SES service
using the standard boto3 client
and then it sends an email using data
that's passed to it by the state machine.
So when this Lambda function is invoked
by the state machine, it's given this event object.
And inside this object will be
the destination email address to send to,
as well as a brief message.
And so here, where we're sending the email using SES,
it's just extracting the destination email address to use,
as well as the brief message.
So all of this information is passed in
when the state machine invokes this Lambda function.
So before we save this,
I want you to replace the placeholder,
so I want you to select the text
that's REPLACE_ME in caps.
And I want you to replace that with the email address
that you want this application to send from.
This needs to be one of the two email addresses
that you verified in SES
in the previous part of this demo series.
This will be the email address
that the application will send from,
so it needs to be one of those addresses
that you verified that you control.
You can't use my address.
It won't function if you use my address.
You need to use one of your own.
Now, once you've replaced that placeholder,
go ahead and click on the Deploy button
and then just make sure that the function
is reporting as deployed.
Now, it will create the Lambda function.
And before we finish up this part of the demo series,
I'll want you to scroll all the way to the top
and copy down the ARN for this Lambda function.
So this will be unique to you.
It shows the region, your account number,
and then the name of the Lambda function
that you've created.
So if you're following along with this in your own account,
in the same region using the same name,
the only difference will be this account number.
It will have your account number here, rather than mine.
But you do need to make sure
that you copy your own ARN for this function.
And I'll just want you to note it down somewhere safe
and make a note that it's the ARN
for the email Lambda function
because we'll need this very shortly.
Now, in addition,
as you move through all the lessons of this demo series,
you might want to just leave the tabs open
to these various components.
So leave a tab open to the IAM console,
leave a tab open to this specific Lambda function,
and for each major component
of each part of this demo series,
just go ahead and leave the tab open to that component.
What that will allow you to do
is rather than having to locate all of these components
in the future parts of this demo series where you need them,
you can just return to the tab.
At this point, though,
that's all of the practical tasks that we need to complete
in this part of the demo series.
So now at this point, you've implemented this architecture.
You've created the email reminder Lambda function.
You've given it the permissions it requires to interact
with the SES service,
and you've edited the placeholder so that it will send email
from the address that you verified inside the SES service.
So at this point, this architecture is implemented
and we're good to move on to part three of this demo series,
which is where we'll implement the state machine itself.
But at this point, that's all you need to do
in this part of the demo series.
So go ahead, complete the video and when you're ready,
I'll look forward to you joining me
in the next part of this series.