Bootstrapping Function Compute for Web Using NodeJS SDK: Part 1

Considerations for Serverless Adoption

Simple Tasks

Latency

Serverless = Function as a Service (FaaS)

No Planning Required

Great Integrations

Geo Replications

Failover

Better Security

Better Improvements

  1. To upload a file to OSS and that will trigger a function execution and we get the output.
  2. Invoke the function directly using the Function Compute NodeJS SDK.
  1. Create function in Alibaba cloud Function Compute console, without using fcli.
  2. Creating & Integrating UI and testing the functionality.

Alibaba Cloud Function Compute

  1. Navigate to Alibaba Cloud console > Function Compute.
  2. If you are using it for the first time, Activate the service & Change the region to the desired one and you should console similar to below after that.
  1. You might not see the metrics If you don’t have any functions created in the region.
  2. We need to create a service first and then functions in the service. This is more in analogous to the Microservices architecture. Each service is a monolithic app and the function inside are tasks, this will increase the reusability and provides ease of maintenance.

Creating Service

  1. Click on the ‘+’ icon, to create a new service. Once click the + icon you will see a form to mention name and description as below
  1. Click OK
  2. You can also select the “Advanced Settings” option, to configure
  3. Network Config
  4. Whether or not your function will have internet access?
  5. NAS(Network Attached Storage) Service
  6. Log Config
  7. Pushing the logs to the log store for further processing
  8. Role Config
  9. Assigning any RAM roles to the functions

Creating a Function

  1. You can create a function by using ‘+’ icon or ‘Create Function’
  2. Creation of a function is 5 step process
  3. Selecting a template/empty function
  4. Configuring triggers
  5. Configuring function settings
  6. Configuring Function permissions
  7. Verification of Configurations

Select a Template

Configure Triggers

Configure Function Settings

Configure Function Permissions

Verify Configurations

var nodemailer = require('nodemailer');module.exports.handler = function (event, context, callback) {
var evt = JSON.parse(event); // Parsing the event object
console.log(JSON.stringify(evt)); // Logging it for debugging purposes
if (evt.events !== undefined) { // if it is a OSS Trigger then evt.events will not be null
callback(null, evt.events[0].oss.object.key);
} else { //Function invoked directly
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false, // use TLS
auth: {
user: '<YOUR EMAIL ID>',
pass: '<YOUR EMAIL PASSWORD>'
},
tls: {
// do not fail on invalid certs
rejectUnauthorized: false
}
});
if (evt.doMail) {
const mailOptions = {
from: '<YOUR EMAIL ID>', // sender address
to: evt.toMail, // list of receivers
subject: 'Serverless Demo with OSS Event', // Subject line
html: 'Hi '+(evt.firstname)+',<br><h1>'+(evt.message).toUpperCase()+'</h1>'// plain text body
};
transporter.sendMail(mailOptions, function (err, info) {
if (err) {
console.log(err);
// callback(null, 'Mail Failed');
callback(null, "Mail Failed :" + (evt.message).toUpperCase());
} else {
console.log(info);
// callback(null, 'Mail Sent');
callback(null, "Mail Sent :" + (evt.message).toUpperCase());
}
});
} else {
callback(null, (evt.message).toUpperCase());
}
}
};

How Does This Function Work?

Instructions for Bundling Your Own Code

  1. Create a root ‘index.js’ file and a ‘package.json’
  2. Add the dependencies in the package.json
  3. Write the function in ‘module.exports.’ in index.js
  4. ‘cd’ to the root directory and run ‘npm install’ or ‘npm i’
  5. you can zip it by right cliking the parent folder along with your generated node_modules.

Uploading Your Function to Function Compute Console

Creating Trigger

Testing Your Function

  1. Click on “Event” in the console as shown below
  1. Then we have to define a custom message to make a request, if you want to trigger using OSS, you can do that by uploading a file to OSS bucket mentioned.
  2. We use the below message to trigger the function
  • { "firstname": "Sai Sarath", "message": "This String will captialize", "doMail": true, "toMail": "<Receiver's mail ID>" }
  1. Then you can see the output here

--

--

--

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Flags or Objects — Deciding the victor

Setting Up Unit Testing For Java In VS Code (with Maven)

How to build a facebook post scheduler using python

CRM SYSTEM FOR AMBASSADORS CRODO

ACNA Explained: The 6 Key Capabilities behind Cloud-Native

Docker, containers, VMs and orchestration technology for the beginners

What is Docker?

Let’s talk about the GO lang, google’s language!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

More from Medium

Building a JavaScript Client Library for Amplify Video

AWS AppSync: Implementing the Simple Web Service - Serverless Pattern 🏗

Create a Background Job Service with DynamoDB Streams and Lambda Functions with “Serverless”…

How to deploy AWS Lambda functions as a container image using CodePipeline