‫Okay, so now let's practice event mappers
‫and for this we'll just author a new function from scratch
‫and the invent mapper we'll be using is SQS
‫so I will name my function lambda-SQS, python 3.8
‫and I will go ahead and create my function.
‫Also, we need to create an SQS queue for this
‫for my lambda function to be able to retrieve
‫the messages from our SQS queue.
‫So I'm just gonna to create a queue
‫and I will name my queue lambda-demo-SQS.
‫Okay. So this is a standard queue
‫and I will just scroll all the way down
‫and create this queue.
‫Okay. So my queue is created
‫and my lambda function is created as well
‫so now for my lambda function to
‫retrieve messages from our SQS queue,
‫what I need to do is to click on add trigger.
‫So add a trigger and here we have
‫all the options of triggers within,
‫for our lambda function so as you can see,
‫there's a lot of AWS type of integrations,
‫but also some partner events sources
‫that you can get directly into your lambda function,
‫but so for this example,
‫what I'm going to do is go into SQS,
‫which happens to be right here.
‫Okay. So we need to choose an SQS queue
‫so I will choose the lambda-demo-SQS
‫and next we have to specify the batch size.
‫So how many messages we want to
‫receive as part of a single batch.
‫And you can go anywhere from one message,
‫all the way up to maybe a bigger batch size.
‫Okay? And you need to look into the documentation
‫to see what is the biggest batch size.
‫I thought it used to be 10,
‫but now I guess it is longer.
‫So then the batch windows so how long to
‫gather records before invoking the functions
‫in seconds so this is to be more efficient
‫and invoke the function less while having
‫a bigger batch for your lambda function to process.
‫And finally, you need to make sure to enable the trigger.
‫So I will enable this trigger and click on add.
‫Now we're going to get an error saying that
‫the execution role does not have the permissions
‫to call ReceiveMessage on SQS.
‫So yes, because our lambda function
‫is reading from the SQS queue
‫and there is a IAM role attached to the lambda function,
‫then we need to make sure that it is properly configured
‫and has the permission to read it from SQS.
‫So fairly easy. Let's go back to our lambda SQS function.
‫We're going to configuration, permissions
‫and here is the role name.
‫So we're going to click on this role,
‫which is going to take us into the IAM console
‫and I can attach a policy to my role
‫and I will just type Lambda SQS
‫and there should be a role for that.
‫So let's just type SQS then
‫and we have an AWS Lambda SQS queue execution role.
‫And this role is enough for it
‫to read it from the SQS queue.
‫So attached to this policy,
‫this Lambda SQS queue execution role
‫and if I go back into my Lambda trigger and add again,
‫this time it works because we have the permission.
‫So as we can see now, SQS of the Lambda
‫function is linked to my SQS queue.
‫Okay so now in terms of what the code of
‫my Lambda function will be doing,
‫it's going to learn the function
‫and we're just going to print the events
‫yet again to get some information.
‫And instead of returning this, let's just return success.
‫Okay, let's deploy our changes.
‫Perfect. This is good.
‫And now let's test our Lambda function
‫by simply going into SQS and sending a message.
‫So in my SQS queue I'm going to send a message.
‫The message body is going to be hello world
‫and then we can add some message attributes
‫for example, the foo is going to be of value bar.
‫Just to try it out.
‫Then we're going to send it this message
‫so now the message is sent and is ready to be received.
‫And so, because my Lambda function is
‫continuously pulling from SQS,
‫then it should process this message.
‫And how do we make sure?
‫Well we are logging the events right here
‫by going into the console logs.
‫So if we go into monitoring and again
‫we open the logs in CloudWatch,
‫now we see one log stream
‫so this is perfect let's click on it
‫and we do have a request in here indeed.
‫So we have a look at everything.
‫and as we can see, the body is hello world
‫as well as the message attributes contains
‫foo bar in terms of attributes.
‫We get some information around when it was sent,
‫the event source being SQS and so on.
‫So that means that the invocation was working.
‫And also, well, if we go back to SQS into our queue
‫and go into send and receive messages,
‫as we can see there are zero
‫messages available in our queue,
‫well that's because the messages has been
‫processed by our lambda function.
‫So finally you need to make sure to
‫disable this event mapper.
‫So as we can see right now,
‫it should be in the enabled state,
‫but you need to disable it
‫because if you don't disable it,
‫then lambda is gonna be continuously
‫pulling from your SQS queue,
‫which can incur some costs in the long run
‫or count against your frequent out for SQS pulse.
‫So let's just disabled SQS queue,
‫but let's go ahead and add another trigger.
‫I just wanna show you the Kinesis trigger
‫because it is another event mapper
‫and I want to show you the different options
‫that are available when you use Kinesis to get lambda.
‫So Kinesis stream, you need to choose obviously
‫Kinesis stream to listen for updates on.
‫Then consumer, so this is if you have a enhance final
‫consumer then you can create a consumer application
‫and enable the enhance on that consumer mode right here.
‫But right now we have no so we'll be falling back
‫to the standard way of consuming from Kinesis.
‫Batch size, which is how many records
‫should read at once so 100.
‫Batch window again, if we want to have a bit
‫of wait time to create a bigger batch
‫before invoking our lender function.
‫Starting position so do we want to read the latest data?
‫Do we want to get the earliest data?
‫Or do we want to get a specific timestamp
‫to read from for our lambda function?
‫And then you can see some additional
‫settings such as on-failure destination
‫to discard some data in case it cannot be read.
‫How many retries, the maximum age of record,
‫if you wanna split batch on error,
‫how many concurrent batches per shard?
‫So if you want to process the same shard concurrently
‫and if you do so then you have more record processors,
‫but Lambda will be ensuring the fact that there
‫are still read in order at the partition key level.
‫Tumbling window duration in case
‫you want to do an aggregation
‫and report batch item failures.
‫So lots of options for Kinesis,
‫you don't need to know them all for the exam,
‫just need to know that Kinesis is an event
‫source member for your lambda function
‫and if you want to have more details into all these options,
‫then the documentation is going to be your best friend.
‫And then enable trigger if you're good,
‫but let's just cancel this cause
‫we just wanted to see the options.
‫So that's it for this lecture.
‫We've seen how event mappers work
‫and we've seen how to add the missing
‫item permissions for lambda functions to
‫be able to read from our event mapper.
‫So that's it, I hope you liked it
‫and I will see you in the next lecture.