Serverless web application and API with Code Engine
This tutorial may incur costs. Use the Cost Estimator to generate a cost estimate based on your projected usage.
In this tutorial, you will create a serverless web application using a bucket in Object Storage and implementing the application backend using IBM Cloud Code Engine and IBM Cloudant as JSON document database.
Static websites are great for performance and security. Their architectural model is sometimes referred to as Jamstack in reference to JavaScript, API, and Markup. In this tutorial, you will create a static website hosted on Object Storage. The website has a guestbook which uses JavaScript to access an API for retrieving entries or to add a new entry. The API for interaction with the backing database is implemented using a serverless approach. It is deployed as backend app or microservice to IBM Cloud Code Engine. The backend only runs when required and thereby occurs charges when in use.
Objectives
- Deploy a serverless backend and a database
- Expose a REST API as serverless app
- Host a static website
The application shown in this tutorial is a simple guestbook website where users can post messages.
- The user accesses the application hosted on the bucket in Object Storage
- The web application calls a backend API.
- The app with the backend API is deployed to Code Engine.
- The backend uses IBM Cloudant to store and retrieve guestbook entries.
Create the Guestbook database
Let's start by creating a IBM Cloudant service instance. IBM Cloudant is a fully managed JSON document database. It is built upon and compatible with Apache CouchDB.
- In the Catalog, under Services, go to the Databases category. Click on the IBM Cloudant tile. In the new
dialog:
-
Under Multitenant select a region.
-
Under Configure Cloudant instance pick a unique name for the service, such as
<yourinitials>-guestbook-db
. -
Select a resource group.
-
Select IAM as authentication method.
-
Select the Lite plan. If you already have a Lite plan in your account, select another service plan.
-
Click Create.
-
- Back in the IBM Cloud Resource List, under Services, click on the IBM Cloudant instance you created to open the instance full details page. Note: You may be required
to wait until the status of the service changes to
Active
. - Click on Launch Dashboard to open the dashboard in a new browser tab.
- In the upper right, click on Create Database. Enter
guestbook
as name and select Non-Partioned under Partitioning. Click Create to create the database.
Create serverless backend
In this section, you will create the serverless backend app with Code Engine. Serverless apps only incur charges for the execution time which is ideal for infrequently accessed solutions like a guestbook.
Create a Code Engine project
- Navigate to IBM Cloud Code Engine Overview page.
- On the left pane, click on Projects and then click Create,
- Select a location.
- Use
<yourinitials>-guestbook
as project name and select the same resource group as before. - Click on Create.
- Wait until the project
status
changes to Active.
- Click on the project name to enter the project dashboard.
Create and deploy backend app
- Within the project dashboard, click on Applications, then Create.
- In the new dialog, enter
guestbook-backend
as name. - Leave Container image selected and use
icr.io/solution-tutorials/tutorial-serverless-api-webapp:latest
as Image reference. It uses an already existing container image. - Under Instance resources, select
0.25 vCPU / 0.5 GB
for CPU and memory. Not much of resources is needed for this type of app. - Increase the Min number of instances to 1 and reduce Max number of instances to 2. The minimum of one makes the app more responsive during the initial tests. You could reduce it later to zero again.
- Click Create to deploy the new backend app for the guestbook. Note that without the next step of creating a service binding the deployment will fail with a code error.
- Create a service binding to the database.
- Click Service bindings tab.
- Click Create.
- Click IBM Cloud service instance and choose your database from the dropdown.
- Leave Role as Writer.
- Click Add.
- Wait for the provisioning to report as green and ready. Click on Test application, then on Application URL. The backend app should load and return a page saying
healthy
. Remember or copy the application URL because it is needed for the next part.
Instead of using the pre-built container image, you could build the image on your own. This can be done either outside of or with the help of Code Engine. If not using the pre-built container image and if using a private container registry additional steps might be needed. You can find the source at https://github.com/IBM-Cloud/serverless-guestbook/tree/ce
Deploy the web app
Create a Object Storage bucket configured with static website hosting containing the files for the guestbook JavaScript application that uses the IBM Cloudant database.
Create a Object Storage instance:
- Select Object Storage from the catalog.
- Select IBM Cloud for the Infrastructure and Standard for the plan.
- Enter a unique service name for the instance, such as
<yourinitials>-guestbook-cos
. - Select a resource group.
- Click Create
Create a bucket configured for static website hosting:
- Click Create a bucket.
- Click Customize your bucket.
- Enter a bucket name that is unique across all IBM accounts. Try
<yourinitials>-guestbook
. - Select resiliency as Regional.
- Select a Location consistent with the IBM Cloudant instance.
- Keep the Storage class default
- Scroll down to the Static website hosting and click Add+.
- Keep the Routing rules (individual) selected and add the Index document index.html.
- Click Public access to On
- Click Save
- Scroll to the bottom and click Create bucket
Copy the files in the docs
directory of https://github.com/IBM-Cloud/serverless-guestbook/tree/ce into the bucket:
-
Open https://github.com/IBM-Cloud/serverless-guestbook/tree/ce in a new tab.
-
Download a zip file by clicking Code then Download ZIP.
-
Unzip the file and navigate to the
docs
directory of the unzipped file. -
Edit guestbook.js - replace the value of apiUrl with the application URL from the previous section.
Make sure that the URI does not end on a slash (
/
). Else the app will not work. -
Open the bucket Objects view and drag and drop the guestbook.js and index.html files to the COS bucket.
-
Navigate to the Configuration tab for the bucket. In the Endpoints section locate the Static website hosting endpoints section. Copy the Public endpoint into a browser tab.
-
You should see the guestbook page.
-
Add new entries to the guestbook.
Remove resources
To delete the created bucket and Object Storage service:
- Navigate to the Object Storage bucket objects
- Check the box in the title row to select all objects in the bucket
- Click Delete objects
- In the upper right of the bucket object page Actions menu select Delete bucket
- In the upper right of the Object Storage instance Actions menu select Delete Instance
To delete the created IBM Cloudant service,
- Navigate to the resource list
- Under Databases, click on the action menu next to
<yourinitials>-guestbook-db
service - Click Delete
To delete the application and project Code Engine,
- Navigate to the Code Engine landing page.
- On the left pane, click on Projects.
- In the list of projects, check the guestbook project, then click Delete.
Depending on the resource it might not be deleted immediately, but retained (by default for 7 days). You can reclaim the resource by deleting it permanently or restore it within the retention period. See this document on how to use resource reclamation.