dawn

Cloud computing has evolved over the years to the point where users only need to focus on the business and the resources they need. Swarm, K8S, and other orchestration tools make container services a perfect experience for developers. I used to think that Docker could replace virtual machines. Users only need to pay attention to their own calculations and the resources they need, without worrying about the machine layer. However, because Docker’s isolation of resources is not good enough, the practice of major cloud manufacturers is still one Docker for one virtual machine, which is not only costly, but also redundant to expose virtual machines to users.

Why do you need to pay attention to the CPU, memory, and network requirements for service running? Is there a better solution? The Serverless architecture was born to let people stop worrying about the resources they need to run, focus on their business logic and pay for the resources they actually consume. It can be said that the true era of cloud computing has arrived with the rise of the Serverless architecture.

There are no new ideas in the container development model, people are still using the traditional development model, need to write a large and comprehensive back-end service. In contrast, the Serverless architecture is event-driven, making the back-end development experience very similar to that of the front-end and mobile. According to the needs of different customers, first let them buy relevant resources, and then fill a hole, add a variety of event processing logic to different products on the line. It’s like iOS development, you write the interface, you handle the events, it’s easy to understand.


image.png

AWS Lambdaexperience

AWS launched Lambda at the Re :Invent Conference in November 2014. After nearly three years of development, Lambda has reached a very perfect level. Lambda has three main functions.

  1. Combine with the API Gateway to provide API services quickly and easily.
  2. Concatenation of key products, such as after DDB inserts a new piece of data, triggers a Lambda execution that reads the new record and sends it to the search engine for indexing.
  3. Extended features such as the Cognito User Pool provide a wide range of points for users to add their own processing logic when logging in.


    image.png

AWS Lambda supports multiple languages such as C#, Java, node.js, and Python, and has a broad audience.

AWS Lambda is available in all regions except Beijing. For the products supported by AWS China, please refer to: Regional table.


image.png

Serverless Reference Architecture: Mobile Backend is a good example of how to implement an App using the Serverless Architecture.

The App’s main functions are similar to Those of Evernote, including uploading images and writing and uploading articles. The features are very simple, but there are a lot of products involved, and the gameplay is very sophisticated.

1 2 3
image.png image.png image.png

The cloud products used throughout the demo and their relationships to each other are shown below. In addition to Lambda itself, IAM, API Gateway and other products also play a huge role.

$tree cloudformation Lambda-function Cloudformation ├─ config-helper. Template ├─ Mobile-backend-no-cloudfront. Template // Delete the template file configured for Cloudfront. Upload the file on the CloudFormation console. ├ ── mobile-back. template // Upload template file if CloudFront is available. Lambda-functions // The lambda code is compressed and placed in a common S3 bucket, so don't bother with the code. ├─ search │ ├─ ├─ design.txt // ├─ design.txt // class.txt // class.txt // class.txt // class.txt // class.txt // class.txt // class.txt Index.js // Add article interface code, mainly to write DDB.Copy the code


image.png

configuration

CloudFormation is really convenient. After the Template is uploaded, the related resources are created and set up. There are two template files in the Cloudformation directory. You only need to upload mobile-backend.template and the config-helper.template file will be loaded properly. Ali Cloud’s corresponding product is: Resource orchestration ROS.


image.png

It seems that API Gateway, Cognito, and CloudSearch do not support CloudFormation well, so there are a lot of command line and Web console Settings in this article.

To run these commands, the AWS CLI is configured with region set to US-East-1 (Northern Virginia) because S3, where the Lambda package is stored in this article, is also in the US-East-1 region.

$ aws configure              
AWS Access Key ID [****************X3CA]: 
AWS Secret Access Key [****************Qo3J]: 
Default region name [us-east-1]:

$ cat ~/.aws/config 
[default]
region = us-east-1
Copy the code
Configure some pits inside

One pitfall is that CloudFront may not be properly initialized, resulting in a CloudFormation creation failure. I am too tired to configure CloudFront, so I simply deleted the CloudFront configuration in CloudFormation. It doesn’t affect the experience.


image.png

image.png

CloudFormation Rollback when a resource fails to be created. It treats the creation of a resource as a transaction, only if it succeeds.


image.png

Written by the client using Swift 2.3. Since the code is also simpler, Convert to 3.0 will do. This will be followed by an Ambiguous use of ‘continue’ error, with code like the following enclosing a block in a pair of small parentheses.

let noteApiClient = APINotesApiClient(forKey: "USEast1NoteAPIManagerClient") noteApiClient? .notesPost(noteRequest).continue ({ (task) -> AnyObject! in if let error = task? .error { print("Failed creating note: [\(error)]") } if let exception = task? .exception { print("Failed creating note: [\(exception)]") } if let noteResponse = task? .result as? APICreateNoteResponse { if((noteResponse.success) ! = nil) { print("Saved note successfully") }else { print("Unable to save note due to unknown error") } } return task })Copy the code

After the program runs, there is no problem with Upload Image to S3. However, the forbidden error will be reported when uploading the article. Xcode will print the following error. Charles captured the packet and found that the server gave an error message.


image.png

Need to Add API Stage in Usage Plans for operation, API and Stage will be matched. This configuration is not mentioned in the article.


image.png

Some technical details

To access the API Gateway and S3, you need to obtain an Unauthenticated ID from the Cognito Identity Pool. This Pool corresponds to the MobileClientRole role. Allow for S3 and API Gateway related actions. The SDK generated by the API Gateway is used directly here, and can be easily used with the Cognito Identity Pool. The API Gateway also supports using the Cognito UserPool validator, which does not require an SDK and is easier to use. For more information, see: Understanding AWS Cognito.


image.png

image.png

The/Notes POST interface is handled by the NotesApiFunction Lambda, which is clearly visible on the console.


image.png

DDB change triggers execute DynamoStreamHandlerFunction the Lambda, from the inside also can clearly see the trigger.


image.png

The effect

You can see pictures in S3.


image.png

You can view the Post data in Dynamo DB.


image.png

However, Searchable Documents in CloudSearch is always 0.


image.png

Can see the data, the Lambda DynamoStreamHandlerFunction found call failed.


image.png

Check it out inside CloudWatch. TypeError: Cannot read property’s ‘of’ undefined


image.png

Dynamo DB = Dynamo DB = Dynamo DB = Dynamo DB = Dynamo DB = Dynamo DB S converts it to a string type. Look at the document again, in fact, there is no problem, so I do not know how to solve this problem.

function createSearchDocuments(records) {
    var searchDocuments = [];

    for(var i = 0; i<records.length; i++) {
        var record = records[i];

        if (record.eventName === "INSERT") {
            var searchDocument = {
                type : 'add',
                id : record.dynamodb.Keys.noteId.S,
                fields : {
                    headline : record.dynamodb.NewImage.headline.S,
                    note_text : record.dynamodb.NewImage.text.S
                }
            };
            searchDocuments.push(searchDocument);
        }
    }
    return searchDocuments;
}
Copy the code

All of a sudden, the problem disappeared, indexing and retrieval functions are normal, amazing~


image.png

image.png

cost

Lambda charges based on memory usage and number of calls. The minimum memory is 128MB. See Lambda Pricing Details for more information.


image.png

image.png

Play hard with this App, it won’t cost a few bucks. Lambda ran for 240 seconds and cost nothing, mainly S3 and data transfers.


image.png

image.png

The key to Serverless’s success

Having rich products and getting through all cloud products is the prerequisite of Serverless’s success. Lambda is not well suited for handling complex business logic and is better suited as glue code to glue key products together. In addition, no matter how perfect Lambda is, it may only solve 80% of the problems, and the remaining 20% of the logic needs to be written by users themselves, released through Docker, and then used by Lambda or users. This hybrid approach to coding may be the future of mainstream development.


image.png

The main advantages of Serverless

  1. Developers are more focused on business logic and are more productive. Developing a typical server-side project takes a lot of time dealing with dependencies, threads, logging, publishing and consuming services, deployment and maintenance, and so on, which the Serverless architecture does not.
  2. Users pay for the resources they actually use. Users typically buy less than 50 percent of their ECS, but pay for the other 50 percent of their unused time. Lambda is charged for the time it runs, which would be much cheaper.
  3. NO Architecture, NO Ops. The architect’s responsibility is to design a highly available, highly extensible architecture. O&m is responsible for the stable and reliable operation of the entire system, reducing and increasing resources appropriately. Large cloud vendors guarantee high availability, and the Serverless architecture itself is highly scalable. Serverless eliminates the need for server-side staff, saving customers a lot of resources. Architects and operations students should think about the way forward. Architects can turn to sales, collate user needs, and write CloudFormation templates.
  4. Cost again. Some of the leading companies in the IT industry have very good infrastructure, and IT feels very convenient for developers to write code and then distribute IT through a distribution platform. The cost is still considerably higher than the Serverless architecture.
    1. Machine cost. Daily, pre-delivery, online, 1+1+2=4 servers.
    2. Keep an eye on business data and check resources to see if they need to be expanded or reduced. Expansion is easy, but reduction is difficult, resulting in a large number of idle resources.
    3. Is full link manometry boring?

The main disadvantages of Serverless

  1. Troubleshooting is difficult because logic is scattered everywhere and a single operation can trigger hundreds or thousands of lambdas. AWS products such as X-ray and CloudWatch help troubleshoot problems.


    image.png

  2. It takes time to prepare the runtime, which is prone to timeout due to instantaneous traffic outbreak.

  3. Lambda with state is hard to write.

  4. Lambda runs with many resource constraints, such as running time, memory, disk, number of open files, and so on.


    image.png

  5. Vendor locking. Cloud computing is a winner-takes-all industry, with large and complete cloud vendors having a huge advantage, and Serverless exacerbates this trend. Before, users had to write a lot of their own server-side logic, and when migrating, the server-side code was redeployed. With the Serverless architecture, the code is Lambda snippets for each platform and cannot be migrated. From the customer’s point of view, they don’t want to be held hostage by a cloud vendor. So the cloud computing industry needs to do a lot of standardization to make it easier for users to move seamlessly between different clouds.

Ali Cloud to Serverless support

Ali Cloud launched its own Serverless product: Function Computing, which currently only supports API Gateway and OSS, and can only be used in East China 2 region. Has not formed a system, it is difficult to meet the diverse needs of users.

It is not easy to promote Serverless. First, there are a lot of things that cloud should access to existing products, such as sales, permissions, risk control, service level, etc., and Serverless needs to be accessed in the future. The development team is tired. Second, it is not easy to transform existing products one by one.

However, Ali Cloud is also working very hard to improve the Serverless support, the future can be expected. Function computation works hand-in-hand with API Gateway to easily practice the Serverless architecture


image.png

Cloud community has some related articles: Ali cloud Serverless Computing, very good, you can know about it.

Why MBaaS/MPaaS don’t make money?

In the field of mobile development, some earliest manufacturers provide basic services such as mobile push, Crash collection and analysis, and mobile data analysis, also known as MPaaS. Then, some vendors gradually began to provide database, storage, configuration and other related services. Administrators operate on the Web console, and mobile terminals directly use these services without server transfer. This is called MBaaS.

Current mobile development service providers like Facebook’s Parse (closed), Firebase (acquired by Google and now strong), and domestic LeanCloud are not doing well. I think the main reason is that the product line is not rich enough, which can only meet the needs of some small apps or the initial development of apps. MBaaS/MPaaS should do well by leveraging the rich product lines of major cloud vendors and connecting them through a Lambda-like mechanism.

The resources

  1. Ten years of gathering, ten years of lessons — cloud computing in my eyes
  2. The dynamics of Liming
  3. Summer Breeze – Minimal Serverless practices based on Docker Swarm
  4. InfoQ Virtual Workshop: A practical approach to serverless computing
  5. Attractive but not omnipotent, rational view of Serverless landing
  6. Some understanding of AWS Cognito