DEV Community

陈杨
陈杨

Posted on

HarmonyOS5 Cloud Service Technology Sharing--Serverless Lottery Template Deployment

A Step-by-Step Guide to Deploying the HarmonyOS Serverless Lottery Campaign Template (with Helpful Tips)

Hey everyone! Today, I'm sharing a super practical tutorial on how to quickly set up a lottery campaign using Huawei HarmonyOS's Serverless template. No complex coding required, just follow the steps to get it done. Plus, there's a guide at the end to help you avoid common deployment pitfalls!~


I. Pre-deployment Preparation

1️⃣ Account Registration + Project Creation

  • First, register for an account on the Huawei Developer platform.
  • When creating a new project, remember to check the "Cloud Development (Serverless)" feature.
  • Tip: Currently, the project region can only be set to "China".

2️⃣ Cloud Function Safeguards

  • If you want to use custom lottery logic, create two cloud functions in advance:
    • A callback function to start the lottery (e.g., <font style="color:rgb(255, 80, 44);background-color:rgb(255, 245, 245);">startRaffle</font>)
    • A callback function to end the lottery (e.g., <font style="color:rgb(255, 80, 44);background-color:rgb(255, 245, 245);">endRaffle</font>)
  • Take note of the function names; you'll need them for configuration later.

3️⃣ Project Package Preparation

  • Download the official lottery template project package.
  • After unzipping, carefully check the <font style="color:rgb(255, 80, 44);background-color:rgb(255, 245, 245);">portal/dist</font> directory structure.
  • Remember to prepare a subdomain in advance (you'll need it for hosting later).

II. Four Steps to Deploy the Template

1️⃣ Template Selection

  • Go to AGC Console → Cloud Development → Serverless Templates.
  • Find the "Lottery Campaign Template" in the template market.
  • Read the template documentation carefully (focus on parameter configuration).

2️⃣ Parameter Configuration

Parameter Tips to Avoid Pitfalls
Authentication-free Select "Yes" for testing, "No" is recommended for production.
Auth Callback URL You need to implement your own user authentication API.
Custom Configuration JSON format! Pay attention to case sensitivity: <font style="color:rgb(255, 80, 44);background-color:rgb(255, 245, 245);">{"showPopup":true, "beginCallback":"YourFunctionName"}</font>

3️⃣ Deployment Execution

  • After confirming the parameters are correct, click "Start Deployment".
  • If it fails, try clearing the cache and deploying again.
  • It's recommended to take a screenshot of the configuration parameters during deployment.

4️⃣ Cloud Function Association

  • After deployment is complete, go to the Cloud Function list.
  • Find the auto-generated <font style="color:rgb(255, 80, 44);background-color:rgb(255, 245, 245);">draw-raffle</font> and <font style="color:rgb(255, 80, 44);background-color:rgb(255, 245, 245);">get-campaign-by-id</font> functions.
  • Modify the <font style="color:rgb(255, 80, 44);background-color:rgb(255, 245, 245);">function.js</font> file in the project package to sync the function names.

III. Key Points for Hosting and Go-live

1️⃣ A Major Pitfall in File Packaging

  • Go to the <font style="color:rgb(255, 80, 44);background-color:rgb(255, 245, 245);">portal/dist</font> directory, select all files, and compress them.
  • Do NOT! I repeat, do NOT directly compress the dist folder!
  • Correct example: After unzipping, you should see index.html directly.

2️⃣ Cloud Hosting Configuration

  • Enable Cloud Hosting service → Create a new site.
  • When uploading dist.zip, note:
    • The filename should not contain Chinese characters or spaces.
    • The compressed package must be ≤ 400MB.
  • Wait about 2 minutes and refresh the status.

3️⃣ Previewing the Effect

  • Access the hosting domain name with a mobile browser.
  • Press F12 to open developer tools → switch to mobile view.
  • Test the loading speed under different network conditions.

IV. Personalization and Customization Tips

UI Beautification

  • Modify the CSS stylesheet in <font style="color:rgb(255, 80, 44);background-color:rgb(255, 245, 245);">portal/dist</font>.
  • It's recommended to use rem layout for multi-device adaptation.

🎮 Interaction Enhancement

  • Add tracking analytics in the beginCallback function.
  • Implement prize notification push through endCallback.

🔒 Security Hardening

  • In non-authentication-free mode, it is recommended to:
    • Add request frequency limits.
    • Use HTTPS for encrypted callbacks.
    • Hash user IDs.

V. FAQ

❓ A blank page is displayed after deployment?

→ Check if the zip file contains an extra 'dist' directory layer.

❓ Callback function timeout?

→ It's recommended to set the cloud function timeout to 15 seconds.

❓ Lottery results are not random?

→ You can use a cryptographically secure random number generator in the cloud function.

❓ How to do stress testing?

→ Use Huawei Cloud Performance Test Service (PTS).


I hope this tutorial helps you get started quickly! If you encounter any problems during deployment, feel free to leave a comment for discussion. Also, welcome to follow my tech blog for weekly updates on HarmonyOS development practical skills. Wishing all developers a successful first deployment and a popular campaign launch! 🚀

Top comments (0)