The objective of this last step in Lab 1 is to correctly configure the ReduceLambda
function, connect it to the DynamoDB stream of ReduceTable
, and ensure the total aggregates are written to the AggregateTable
. When you successfully complete this step, you will begin to accumulate points on the scoreboard.
We start by setting the concurrency of the ReduceLambda
function to 1
. This ensures that there is only a single active instance of the ReduceLambda
function at any time. This is desired because we want to avoid write conflicts, where multiple instances attempt to update the AggregateTable
at the same time. From a performance point-of-view, a single Lambda instance can handle the aggregation of the entire pipeline because incoming messages are already pre-aggregated by the MapLambda
functions.
ReduceLambda
to edit its configuration (see figure below).Configuration
tab, then select Concurrency
on the left.Edit
button in the top right corner, select Reserve concurrency
and enter 1
.Save
, your configuration should look like the image below.Next, we want to connect the ReduceLambda
function to the DynamoDB stream of the ReduceTable
.
The function overview shows that the ReduceLambda
function does not have a trigger. Click on the button Add trigger
.
Specify the following configuration:
DynamoDB
as the data source.ReduceTable
.Batch size
to 1000
.Click the Add
button in the bottom right corner.
You will see an error here! Before we can enable this trigger we need to add IAM permissions to this Lambda functions.
The error message above informs you that the ReduceLambda
function doesn’t have the necessary permissions to read from the stream of the ReduceTable
. While we have already assigned IAM roles with the required privileges to the StateLambda
and the MapLambda
, it’s left to you to do it for the ReduceLambda
function:
ReduceLambda
function. Shortly you will need it open to retry the request.ReduceLambda
function.Configuration
tab and click on Permissions
. You should see the Lambda execution role called ReduceLambdaRole
. Click on this role to modify it.ReduceLambdaRole
. There is a policy associated with this role, the ReduceLambdaPolicy
. Expand the view to see the current permissions of the ReduceLambda
function. Now, click on the button Edit
to add additional permissions.There is already an IAM permission in place for DynamoDB: this is necessary to ensure the workshop runs as expected. Don’t get confused by this and please don’t delete the permissions we’ve already granted! All of the Lambda functions need to be able to access the ParameterTable to check the current progress of the lab and the respective failure modes.
ReduceLambda
function is able to read messages from the stream of the ReduceTable
.
Add new statement
Service
, select DynamoDB
Access level - read
, check the following four checkboxes: DescribeStream
, GetRecords
, GetShardIterator
, and ListStreams
Now we need to associate these permissions with specific resources (e.g. we want the ReduceLambda
to be able to read exclusively from the ReduceTable
alone). Hence, under Add a resouce
, and click on Add
. Then in Resource type
choose stream
. Next, fill out the following:
{Region}
- The lab defaults to us-west-2, but verify your region and ensure the correct one is entered{Account}
- The AWS account id. You can put an asterisk here if you don’t want to get the exact account id.{TableName}
- The name should be ReduceTable
{StreamLabel}
- Add an asterisk *
so that any stream label is supported. A Stream label is a unique identifier for a DynamoDB stream.Add resource
. You’ve now granted permission for the ReduceLambda
to read from the ReduceTable
stream, but there is more to be done still.Be sure to remove all curly braces from your ARN before clicking Add resource
If we make no further change, the ReduceLambda
function will not be able to update the final results in the AggregateTable
. We must modify the policy to add additional permissions to grant UpdateItem
access to the function.
Add new statement
Service
, select DynamoDB
Access level - read or write
, select the checkbox UpdateItem
ReduceLambda
to be able to write to the AggregateTable
alone. Hence, click on Add a resource
and in the Resource type
drop down choose table
. Next, enter the values for Region
(using the same region as before), Account
(consider using an asterisk *
), and TableName
(this time it should be AggregateTable
).Add resource
.Next
and then Save changes
in the bottom right corner.If all of the above steps are executed correctly you will be able to connect the ReduceLambda
to the DynamoDB stream of the ReduceTable
by switching back to the open tab and again trying to click on Add
. You may need to wait a couple of seconds for the IAM policy changes to propagate.
If you’re not able to add the trigger, this may be due to a misconfiguration of the IAM policy. If you need help, go to Summary & Conclusions
on the left, then Solutions
, and you should see the desired ReduceLambdaPolicy
.
If everything was done right, then the DynamoDB stream of the ReduceTable
should trigger the ReduceLambda
. Therefore, you should be able to see logs for each Lambda invocation under the Monitor
-> Logs
tab.
Another way to verify it is working is to observe the items written by ReduceLambda
to the DynamoDB table AggregateTable
. To do that, navigate to the DynamoDB service in the AWS console, click Items
on the left, and select AggregateTable
. At this stage you should see multiple rows similar to the image below.
AWS Event: If Steps 1, 2, and 3 of Lab 1 were completed successfully you should start gaining score points within one to two minutes. Please check the scoreboard! Ask your lab moderator to provide a link to the scoreboard.