Business Example
You will deploy your CAP application into the Cloud Foundry environment of the SAP Business Technology Platform. Before the actual deployment process takes place, you have to prepare the application and you SAP BTP landscape.
Exercise Options
You can perform this exercise in two ways:
- Live Environment – using the instructions provided below, you can perform the tasks in the SAP BTP Free Tier account
- Platform Simulation – follow the step-by-step instructions within the simulation
Prerequisite
Before starting this exercise, make sure you have completed the previous exercise or switched to the solution branch of the previous exercise.
Creating the SAP HANA Cloud instance is not required if you are enrolled in an SAP classroom training. In all other scenarios, proceed to establish the SAP HANA Cloud instance within your SAP BTP subaccount.
Steps
Set Up SAP HANA Cloud Instance.
First, you need to set up the SAP HANA Cloud instance in your BTP account.
Note
Screens in the cloud may change. Therefore, the screenshots in this solution may differ slightly from the screens in your training system.Enter the subaccount that you are using for this course.
Enter the space that you are using for this course (for example, the
dev
space).In the navigation on the left, choose SAP HANA Cloud.
Select Create and choose SAP HANA database.
You will be redirected into SAP HANA Cloud Central for the instance creation. When being asked to authenticate, use your SAP Account user credentials that you used to create your SAP BTP account. You should then see the following dialog. Select
SAP HANA Cloud
and choose Next Step.The Location settings should point to your Cloud Foundry org and space.
Use the following settings for the remaining fields:
Key Value Instance Name hana-cloud Description SAP HANA Cloud used for exercises Administrator Password create a password according to the password policy Choose the Next Step button.
The default parameters for your SAP HANA Cloud database display. Select the Next Step button.
You have to select an availability zone for the SAP HANA Cloud Database. Confirm the default settings by choosing Next Step.
You will see the SAP HANA Database Advanced Settings dialog. Here, you can allow or deny certain IP addresses for connection to your database instance.
You also find a setting to connect the database to remote sources using the Cloud Connector. This is especially useful in replication use cases, where you need to replicate data from on-premise sources to your SAP HANA Cloud instance. In our use case, this setting is not required.
Select the Allow all IP addresses option, then select the Review and Create button.
The Review page displays. Review the values and choose theCreate Instance button.
The overview of your database instances appears. There should be a new entry for your hana-cloud instance with Status
CREATING
.The process will take several minutes to complete. When this is done, the Status should switch to
RUNNING
. Use the Refresh button to update the status. Your SAP HANA Cloud instance is now ready to be used.Return to your BTP subaccount and to your cloud foundry space and select SAP HANA Cloud.
Your hana-cloud instance displays in the SAP HANA Database Instances overview.
Prepare the application for production.
During the next steps, you will prepare the application for production. Several components are required for production and need to be added to the application and its configuration. For more detailed and always up-to-date information, have a look at the official Deploy to Cloud Foundry Guide in the CAP documentation.
Configure SAP HANA Cloud in the project.
While we used SQLite as a low-cost stand-in during development, we’re going to use a managed SAP HANA database for production:
Use the displayed code.
Enable XSUAA-based Authentication.
Note
This will also generate an
xs-security.json
file, with roles/scopes derived from authorization-related annotations in your CDS models. Ensure to reruncds compile srv/ --to xsuaa > xs-security.json
, as documented in the https://cap.cloud.sap/docs/guides/authorization#xsuaa-configuration whenever there are changes to these annotations.Install the added npm packages.
Note
Whilecds add <module>
adds the respective configuration to your project, it does not install these packages to your node_modules folder. By runningnpm install
or equallynpm i
, you will ensure that these modules are also installed. This is mandatory, since during the build process everything will be packaged and afterward shipped to the runtime. If these modules wouldn't be installed, they wouldn't be part of the final part and consequently the deployed application wouldn't run as expected or not at all.In your terminal, execute the following command to install the missing npm dependencies:
Create the MTA descriptor file for MTA-based deployment.
This will generate the mta.yaml file, which contains all the services for the multi-target application (MTA) deployment.
Add the managed approuter as gateway to the project.
The App Router acts as a single point-of-entry gateway to which you route requests. In particular, it ensures user login and authentication in combination with XSUAA. The following command will also add the approuter module to the
mta.yaml
file.Two deployment options are available:
Managed App Router: For scenarios where SAP Fiori Launchpad (FLP) is the entry point to access your applications, the Managed App Router provided by SAP Fiori Launchpad is available. See the end-to-end tutorial for the necessary configuration in
mta.yaml
and on each SAP Fiori application.Custom App Router: For scenarios without SAP Fiori Launchpad, the app router needs to be deployed along with your application. Use the following command to enhance the application configuration:
Configure the approuter by opening the
xs-app.json
file in theapp/
directory of your project.Change the value of the
welcomeFile
to thelaunchpad.html
file in you app folder. It should look like the following:This will set up the routing to the SAP Fiori Elements file and to the backend service.
Note
The previous steps are required only once in a project’s lifetime. With that done, you can repeatedly deploy the application.Freeze Dependencies.
Deployed applications should freeze all their dependencies, including transient ones.
Note
You should regularly update yourpackage-lock.json
to consume latest versions and bug fixes. Do this by running this command again, for example, each time you deploy a new version of your application.
Adjust the mta.yaml file
While most parts of the file are generated by the cds tooling, some parts have to adjusted. This includes the following:
- Add an oauth2-redirect URI to the xsuaa resource configuration. Read more in SAP Note 2864831.
- Add the role-collection definitions to the xsuaa resource configuration.
Extend the
config
section of therisk-management-auth
resource in yourmta.yaml
file, by adding the following snippet to the config:Code snippetExpandrole-collections: - name: 'RiskManager-${org}-${space}' description: Manage Risks role-template-references: - $XSAPPNAME.RiskManager - name: 'RiskViewer-${org}-${space}' description: View Risks role-template-references: - $XSAPPNAME.RiskViewer oauth2-configuration: redirect-uris: - https://**.hana.ondemand.com/login/callback # Wildcard redirect to SAP BTP (You might want to make this more exclusive) # example: - https://risk-management-approuter.cfapps.eu10-004.hana.ondemand.com/login/callback
The entireNote
As you have learned at the beginning of this learning journey, YAML files are very sensitive when it comes to indentation. Carefully insert the aforementioned snippet. Otherwise it could happen that the configuration is not read by the xsuaa service.risk-management-auth
resource should look like the following. Compare or replace your resource definition with the following complete snippet:Code snippetExpandresources: - name: risk-management-auth type: org.cloudfoundry.managed-service parameters: service: xsuaa service-plan: application path: ./xs-security.json config: xsappname: risk-management-${org}-${space} tenant-mode: dedicated role-collections: - name: 'RiskManager-${org}-${space}' description: Manage Risks role-template-references: - $XSAPPNAME.RiskManager - name: 'RiskViewer-${org}-${space}' description: View Risks role-template-references: - $XSAPPNAME.RiskViewer oauth2-configuration: redirect-uris: - https://**.hana.ondemand.com/login/callback # Wildcard redirect to SAP BTP (You might want to make this more exclusive) # example: - https://risk-management-approuter.cfapps.eu10-004.hana.ondemand.com/login/callback
Note
You could also delete the destination-service from your mta.yaml file, hence it's not needed in our scenario. This is optional, since keeping it there will not harm the application. Furthermore, the destination-serivice is currently always free (subject to change).
Build and assemble the application.
Build deployables with
cds build
.Run
cds build
to generate additional deployment artifacts and prepare everything for production in a local./gen
folder as a staging area. Whilecds build
is included in the next step wherembt build
is used, you can also run it selectively as a test, and to inspect what is generated. For example, you can have a look into the generated SAP HANA Cloud artifacts (.hdbtables, …) and other artifacts.Assemble with
mbt build
Now, you use
mbt
build tool to assemble everything into a singlemta.tar
archive. The following command will build the project based on themta.yaml
file. The output will be stored in thegen/
folder.Note
You can also inspect the generatedmta.tar
archive. If you're working in the SAP Business Application Studio, download the archive and open it. Sometimes it's required to change the extension to.zip
Result
You have now successfully prepared your application for a multi-target deployment to Cloud Foundry on SAP BTP.