Exercise: Preparing the Application for Deployment

Objectives
After completing this lesson, you will be able to:

After completing this lesson, you will be able to:

  • Prepare the application for deployment

Prepare the Application for Deployment

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:

  1. Live Environment – using the instructions provided below, you can perform the tasks in the SAP BTP Free Tier account
  2. Platform Simulation – follow the step-by-step instructions within the simulation
Note
We are strong recommend that you first perform the tasks in the live environment.

Prerequisite

Before starting this exercise, make sure you have completed the previous exercise or switched to the solution branch of the previous exercise.

Note

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

  1. 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.
    1. Enter the subaccount that you are using for this course.

    2. Enter the space that you are using for this course (for example, the dev space).

    3. In the navigation on the left, choose SAP HANA Cloud.

    4. 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.

    5. The Location settings should point to your Cloud Foundry org and space.

      Use the following settings for the remaining fields:

      KeyValue
      Instance Namehana-cloud
      DescriptionSAP HANA Cloud used for exercises
      Administrator Passwordcreate a password according to the password policy

    6. Choose the Next Step button.

    7. The default parameters for your SAP HANA Cloud database display. Select the Next Step button.

    8. You have to select an availability zone for the SAP HANA Cloud Database. Confirm the default settings by choosing Next Step.

    9. 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.

    10. Select the Allow all IP addresses option, then select the Review and Create button.

    11. The Review page displays. Review the values and choose theCreate Instance button.

    12. The overview of your database instances appears. There should be a new entry for your hana-cloud instance with StatusCREATING.

      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.

    13. Return to your BTP subaccount and to your cloud foundry space and select SAP HANA Cloud.

    14. Your hana-cloud instance displays in the SAP HANA Database Instances overview.

  2. 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.

    1. 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:

      Code snippet
      cds add hana --for production
      Expand

      Use the displayed code.

    2. Enable XSUAA-based Authentication.

      Code snippet
      cds add xsuaa --for production
      Expand
      Note

      This will also generate an xs-security.json file, with roles/scopes derived from authorization-related annotations in your CDS models. Ensure to rerun cds 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.

    3. Install the added npm packages.

      Note
      While cds add <module> adds the respective configuration to your project, it does not install these packages to your node_modules folder. By running npm install or equally npm 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:

      Code snippet
      npm i
      Expand

    4. 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.

      Code snippet
      cds add mta
      Expand

    5. 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.yamlfile.

      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:

        Code snippet
        cds add approuter --for production
        Expand

    6. Configure the approuter by opening the xs-app.jsonfile in the app/directory of your project.

      Change the value of the welcomeFile to the launchpad.html file in you app folder. It should look like the following:

      Code snippet
      "welcomeFile": "app/launchpad.html"
      Expand

      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.
    7. Freeze Dependencies.

      Deployed applications should freeze all their dependencies, including transient ones.

      Code snippet
      npm update --package-lock-only
      Expand
      Note
      You should regularly update your package-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.
    1. 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 the risk-management-auth resource in your mta.yaml file, by adding the following snippet to the config:

      Code snippet
      
              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
      Expand
      Note
      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.
      The entire risk-management-auth resource should look like the following. Compare or replace your resource definition with the following complete snippet:
      Code snippet
      
      resources:
        - 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
      Expand
      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).

  3. Build and assemble the application.

    1. 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. While cds build is included in the next step where mbt 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.

      Code snippet
      cds build --production
      Expand

    2. Assemble with mbt build

      Now, you use mbt build tool to assemble everything into a single mta.tar archive. The following command will build the project based on the mta.yamlfile. The output will be stored in the gen/ folder.

      Code snippet
      mbt build -t gen --mtar mta.tar
      Expand
      Note
      You can also inspect the generated mta.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.

Log in to track your progress & complete quizzes