subcollection | copyright | lastupdated | lasttested | content-type | services | account-plan | completion-time | use-case | ||
---|---|---|---|---|---|---|---|---|---|---|
solution-tutorials |
|
2024-01-05 |
2023-09-05 |
tutorial |
codeengine, Cloudant, cloud-object-storage |
paid |
1h |
ApplicationModernization |
{{site.data.keyword.attribute-definition-list}}
{: #serverless-webapp} {: toc-content-type="tutorial"} {: toc-services="codeengine, Cloudant, cloud-object-storage"} {: toc-completion-time="1h"}
This tutorial may incur costs. Use the Cost Estimator to generate a cost estimate based on your projected usage. {: tip}
In this tutorial, you will create a serverless web application using a bucket in {{site.data.keyword.cos_short}} and implementing the application backend using {{site.data.keyword.codeenginefull_notm}} and {{site.data.keyword.cloudant_short_notm}} as JSON document database. {: shortdesc}
Static websites are great for performance and security. Their architectural model is sometimes referred to as Jamstack{: external} in reference to JavaScript, API, and Markup. In this tutorial, you will create a static website hosted on {{site.data.keyword.cos_short}}. 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 {{site.data.keyword.codeenginefull_notm}}. The backend only runs when required and thereby occurs charges when in use.
{: #serverless-webapp-0}
- 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.
{: caption="Figure 1. Architecture diagram of the tutorial" caption-side="bottom"}
{: style="text-align: center;"}
- The user accesses the application hosted on the bucket in {{site.data.keyword.cos_short}}
- The web application calls a backend API.
- The app with the backend API is deployed to {{site.data.keyword.codeengineshort}}.
- The backend uses {{site.data.keyword.cloudant_short_notm}} to store and retrieve guestbook entries.
{: #serverless-webapp-2} {: step}
Let's start by creating a {{site.data.keyword.cloudant_short_notm}} service instance. {{site.data.keyword.cloudant_short_notm}} 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 {{site.data.keyword.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 {{site.data.keyword.cloud_notm}} Resource List, under Services, click on the {{site.data.keyword.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.
{: #serverless-webapp-3} {: step}
In this section, you will create the serverless backend app with {{site.data.keyword.codeengineshort}}. Serverless apps only incur charges for the execution time which is ideal for infrequently accessed solutions like a guestbook.
{: #serverless-webapp-4}
- Navigate to {{site.data.keyword.codeenginefull_notm}} 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.
{: #serverless-webapp-5} {: step}
- 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. - Go to and expand the section Runtime settings. 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.
- 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. - Click Create to deploy the new backend app for the guestbook.
- Create a service binding to the database.
- Click Service bindings tab.
- Click Create.
- Click IBM Cloud service instance and choose your database from the drop down.
- 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 {{site.data.keyword.codeengineshort}}. 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 {: tip}
{: #serverless-webapp-6} {: step}
Create a {{site.data.keyword.cos_short}} bucket configured with static website hosting containing the files for the guestbook JavaScript application that uses the {{site.data.keyword.cloudant_short_notm}} database.
Create a {{site.data.keyword.cos_short}} 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 {{site.data.keyword.cloudant_short_notm}} 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 (
/
). {: note} -
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.
{: caption="Guestbook Screenshot" caption-side="bottom"}
{: style="text-align: center;"}
{: #serverless-webapp-cleanup} {: step}
To delete the created bucket and {{site.data.keyword.cos_short}} service:
- Navigate to the {{site.data.keyword.cos_short}} 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 {{site.data.keyword.cos_short}} instance Actions menu select Delete Instance
To delete the created {{site.data.keyword.cloudant_short_notm}} 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 {{site.data.keyword.codeengineshort}},
- Navigate to the {{site.data.keyword.codeengineshort}} 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. {: tip}
{: #serverless-webapp-8}