powered by open knowledge
During the last exercise we have build up step-by-step a cloud based scalable backend service making use of various different cloud managed services and components.
In this exercise we will use serverless functions - aka AWS Lambda - to add additional functionality to our application setup without any changes to the existing backend code.
During this exercise you will learn how to:
- Log into the AWS Cloud
- Create and change Lambda functions
- Trigger Lambda functions with a cloud event
- Monitor Lambda functions
Note: This step is only necessary if you are not already logged in.
First of all we have to connect to the AWS Cloud:
- Go to https://console.aws.amazon.com/console/home
- Select "IAM user sign in" if not already preselected
- Use the user information provided to you to sign in
- Account ID: [WORKSHOP ACCOUNT ID]
- IAM username: [YOUR ANIMAL]
- Password: [YOUR ANIMAL PWD]
After successfully logged in you should see the AWS CLoud main dashboard.
Note: Make sure the region in the upper right corner of the browser window is set to "Europe (Frankfurt)" aka eu-central-1.
A serverless function - aka AWS Lambda - can be used to add behaviour to our current setup without changing the backend service itself. Normally you will use such a functions, whenever you want to do "something" in reaction to a cloud event.
So let's say we want to listen to if and what new topics have been added to our forum and trigger some action afterwards. To do this we can use any DynamoDB change in our table as a trigger for our to be implemented Lambda function.
To create a Lambda function we have to call the AWS Lambda dashboard first. There are several ways to do so:
- use global quick search and lookup for "Lambda"
- select Lambda service from service overview via "Compute"
- select Lambda service from "recently visited" (if available)
Use the Lambda dashboard to create and configure a new Lambda function:
- Click "Create a function". This will lead you to the "Create function" page.
- Fill in the following values (leave everything else as is)
- Select "Author from Scratch"
- Name function with your animal as prefix, e.g. dog-serverless-function
- Select "Node.js ..." as runtime
- Select "Use an existing role" for "execution role"
- Existing role: Lambda
- Click "Create function" button
You will see a success page showing your serverless function when done everything right. Test your Lambda function with the help of the web interface:
- Switch to "Test" tab
- Click "Test" button
You should see an output indicating a successful function invocation.
Next, we want to set a trigger - changes in our DynamoDB table - for the serverless function:
- Click "+ Add Trigger"
- Select "DynamoDb" as source of the trigger configuration
- Select YOUR DynamoDB table as trigger
- Click "Add" button to create the trigger
You should see the overview page of your serverless function showing DynamoDB as a trigger afterwards.
Now, it is time to adapt the code of our serverless function and to deploy it afterwards.
-
Select "Code" tab
-
Replace
//TODO implementwith following codeconsole.log("Hello <your-username>!") // YOUR USER NAME HERE
-
Click "Test" button left to the code editor to test the changes
-
Click "Deploy" button left to the code editor to make the function available
After successfully being deployed it is time to finish this exercise and connect our frontend to the AppRunner service and add some topics to trigger and test the serverless function:
-
Goto AppRunner service page and select your AppRunner instance
-
Copy the AppRunner service default domain
-
Go to the typescript file showcases.ts that can be found in ./frontend/src of your frontend project.
-
Replace the fake URL baseUrl: http://todo.invalid of the entry "4 - Lambda" with the valid URL of the backend. The result should look like.
export const SHOWCASES: ShowcaseConfig = { ... "4 – Lambda": { baseUrl: "http://[APP_RUNNER_SERVICE_ADDRESS]", }, ... }
To test the Lambda we have to insert new data into the DynamoDB table. This can be done by adding one or more topics to our ok-forum application. So, open the ok-forum app in a browser and add some topics.
Ok, we have added one or more topics to our DynamoDB. But how to check if our serverless function was triggered and has worked properly?
- Open Lambda dashboard
- Select "Functions" in Lambda menu
- Select YOUR function from functions overview page
- Select "Monitor" tab of your function management page
- Click "View CloudWatch logs" to open function specific log
- Look for "Hello ... " which is the output of your
console.log
Feel free to play around with the Lambda function, and it's DynamoDB triggered event. For example, try to print out the individual elements of the DynamoDB event, like the topic name, in the log.
You can use
console.log(JSON.stringify(event, null, 2));or
if (record.eventName === 'INSERT') {
console.log(`- Description: ${record.dynamodb.NewImage.description.S}`);
console.log(`- Title: ${record.dynamodb.NewImage.title.S}`);
}to log detailed event information inside your Lambda function.
Great, we managed to monitor our lambda function and its DynamoDB trigger. But let us assume we are only interested in INSERT events but not in UPDATE or DELETE events of our DynamoDB table. Fortunately there is a way to set a filter criteria to our DynamoDB trigger:
- Open Lambda dashboard
- Select "Functions" in Lambda menu
- Select YOUR function from functions overview page
- Click DynamoDB trigger icon in diagram
- Select YOUR trigger (checkbox)
- Click "Edit" button.
- Collapse "Advanced settings"
- Insert filter criteria
{ "eventName" : [ "INSERT" ] }
- Click "Save" button
Note: Saving the changed trigger configuration will redeploy your Lambda function automatically.
To test the new configuration add a new topic. You can do this with the help of
our ok-forum frontend or directly by copying and pasting an existing topic item
to the DynamoDB table (don't forget to change the pk). Go to the CloudWatch log
of the Lambda function and search for a corresponding INSERT entry - there
should be one.
Delete the DynamoDB entry afterwards with the help of the AWS DynamoDB dashboard of your DynamoDB table. Go to the CloudWatch log of the Lambda function and search for a corresponding REMOVE entry - there should be NONE.
In this exercise, you have entered the universe of serverless functions - that's really all you can do. SERVERLESS, REDUCE TO THE MAX!