Welcome to this series of hands-on tutorials from the SAP HANA Academy.
I'm Philip Mugglestone. In this series we're covering serverless functions in the SAP Business
Technology Platform. In this video tutorial we're going to see how we can create an app
router as part of our serverless function. It'll go side by side. What this does is it provides the
opportunity to have interactive log on when you're working with authentication and authorizations.
And also in this case it will generate you a little UI. So why don't we go ahead and actually
create an example. So let's start our generator. We'll create our project. We'll use Node.js.
For example we can use the destination service. We can use SAP HANA Cloud. We'll have authentication
and authorization. And this time we're going to say yes to having a UI or an application router.
So let's hit yes to that. Now if you want you can actually configure session stickiness and failover
by using Redis. I won't do that on this occasion. So let's hit finish and it will scaffold our
project. So let's have a quick look at what was generated as part of our project. You'll notice
that there's now a third item, the app, which is basically our application router. And you'll see
there's a number of configuration files there. For example we've got an XS-app.yaml file.
This is where we can set the routes or the routes that we want this application router to
route. So basically we can say if it's /srv/ anything like that I want you to send it over
to our srv destination. So that's basically our service. But we can also have some locally stored
resources. So basically some static HTML here if we want as well. So in effect we're almost creating
a full stack application as a serverless function because we've got an app router for doing
interactive login and we've got a database layer and we've got the actual serverless function for
the business logic. We'll also see we've got a resources.yaml file and this is where for example
we've got our files, our index.html, they need to be done actually in yaml like this not individual
files. And of course we're going to have an API rule so that we can actually have a URL to access
this publicly. And we're going to have a deployment.yaml and what this will do is actually take our app
router and deploy it. Now if we look in our values.yaml we'll see that actually we're going to use the
SAP SE Docker ID and the approuter repository. So we're not going to need to create a container
ourselves. It's actually going to use one that's already been published to Docker Hub by SAP.
So it's a very easy way for us to access this app router functionality from a very lightweight
serverless environment. So we don't need Docker Hub for example to create our container. So why
do we go ahead and now deploy this? So let's open up a terminal, change to our project folder
and deploy. So you can see it's now actually deploying the application part as well as the
srv part. So we can go over and see that we've got different pods and deployments in process.
If we have a quick look at our workloads, we're going to have our function of course, our serverless
function that's still building. We're going to have different pods including a pod for the
application, also one for the database that's currently in process and one for the serverless
function which is currently running. If we go to discovery and network we can then see our API rules
and this is where we can see actually how we can access our app. So this is our application router
which means we can use it for interactive logon. Now of course if we were to go just to the /srv/ we
need actually to provide an access token. The whole point of the app router is that it will deal with
the login for you interactively and then it will send the access token in the headers to the
serverless function that's running on the back end. Now of course we've specified that we're going to
have authorizations here so we'll need to go to security and our users to authorize our user.
So let's go using the relevant identity provider, assign the role collection, I'll do admin and user
for my function and now once everything's up and running let's just check the pods.
Yeah everything's green, it's running, we should be good to go. So let's go to our API rules and
we'll actually launch the app router. So you can see well it actually logged us in here because
we're already logged in it didn't ask us to explicitly do that again so maybe I can just
make it a little bit more explicit if I open up an incognito window and now I put it in and you can
see the login is now asking us to log into that identity provider. So it really is dealing with
the authentication interactively for us and then we're actually able to run our application. You
can see we've got a little UI it's just a sample with the links to the back end so we've got the
home we've seen that before that's the /srv/ endpoint we've got the user endpoint which allows you to
get information what scopes who the user log is because we're using authentication we've got
destinations so we can get that information from Northwind out we've got SAP HANA Cloud we can look at the
sales etc. So basically if you want you can use an application router which will allow you to deal
with the authentication process interactively. Now this may not really be needed for most scenarios
when you're building serverless functions but if you want to build one that has its own UI if you
want to build one that deals with authentication the app router is a great way to go for that
interactive login. It's kind of a bit more creating a full stack application but of course it's
completely up to you how you actually want to build your serverless functions so if it fits your
scenario then do it. Also it can be quite useful if you're just testing and you've got authentication
instead of having to go to Postman each time and actually create an access token you can just go
through this app router and it will actually just allow you to log in interactively very very easily
and quickly. And of course if you want to tear all of that down then just as we've seen elsewhere we
would just simply do helm-undeploy so make helm-undeploy and it will tear down our whole
application. So it's now fully uninstalled all of the different components and if we actually go
ahead and have a quick look we can see the API rules have gone, the pods have all gone, the jobs,
the function has gone, everything that was configured basically has been removed so that's
also a very easy way to clean up just use that make helm-undeploy. So what we've seen in this
video tutorial is how we can use the app router as part of our serverless function
environment. It allows us to do interactive login. It may be something that's relevant for your
serverless function scenarios or it's something that can maybe save you time when you're doing
development and testing. You can find more video tutorials on our YouTube channel. If you'd like
to be informed as soon as new video tutorials are published then please subscribe. Thanks for watching.