{"id":653,"date":"2017-06-25T12:28:05","date_gmt":"2017-06-25T12:28:05","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=653"},"modified":"2017-06-25T12:28:05","modified_gmt":"2017-06-25T12:28:05","slug":"serverless-framework-tutorial","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=653","title":{"rendered":"Serverless Framework Tutorial for Beginners Using AWS Lambda"},"content":{"rendered":"<p><a href=\"https:\/\/devopscube.com\/serverless-architecture\/\" rel=\"noopener\">Serverless architecture<\/a> is gaining popularity and it is been used by many organizations for all its operational tasks. Ther are many companies which use serverless services like Lamba for its microservices architecture.<\/p>\n<p>AWS, Google Cloud, and Azure provide good web portal, CLI, and SDK for their respective serverless services. However, When you go through the CI\/CD process, you need a good framework other than CLI&#8217;s and SDKs for good traceability and management. Here is where <a href=\"https:\/\/serverless.com\/?ref=devopscube.com\" rel=\"noopener\">serverless framework<\/a> comes into play.<\/p>\n<p>Serverless.com provides a framework for deploying serverless code to <a href=\"https:\/\/aws.amazon.com\/lambda\/?ref=devopscube.com\" rel=\"noopener\">AWS Lambda<\/a>, <a href=\"https:\/\/cloud.google.com\/functions\/?ref=devopscube.com\" rel=\"noopener\">Google Cloud Functions<\/a> and <a href=\"https:\/\/azure.microsoft.com\/en-in\/services\/functions\/?ref=devopscube.com\" rel=\"noopener\">Azure Functions<\/a>. You can organize your serverless deployment using configuration files provided by this framework.<\/p>\n<p>We have done a basic deployment on AWS Lambda using this framework and we loved it. This guide will help you to get started with the serverless framework on AWS Lambda.<\/p>\n<h2 id=\"installation-and-configuration\">Installation and Configuration<\/h2>\n<p>You should have <strong>awscli<\/strong> installed and configured on your system. Serverless framework will use the <strong>~\/.aws\/credentials<\/strong> file for deploying lambdas<\/p>\n<ol>\n<li>Install node js. Follow steps from here from here \u2013 &gt; <a href=\"https:\/\/discuss.devopscube.com\/t\/how-to-install-nodejs-on-rhel-7-ubuntu-linux-ec2-instance\/50?ref=devopscube.com\" rel=\"noopener\">Latest Nodejs Installation<\/a><\/li>\n<li>Install the serverless framework.<\/li>\n<\/ol>\n<pre><code>npm install -g serverless<\/code><\/pre>\n<p><!--kg-card-begin: html--><\/p>\n<ol start=\"3\">\n<li>Check the installation using the following command.<\/li>\n<\/ol>\n<p><!--kg-card-end: html--><\/p>\n<pre><code>serverless<\/code><\/pre>\n<h3 id=\"getting-started\">Getting Started<\/h3>\n<p>Let&#8217;s get started with a demo python application.<\/p>\n<p><strong>Step1:<\/strong> cd in your project directory. You can use any folder of your choice.<\/p>\n<p><strong>Step 2:<\/strong>. Create a basic python service template. It will create a python based serverless template.<\/p>\n<pre><code>serverless create --template aws-python --path devopscube-demo<\/code><\/pre>\n<p>The output would look like the following.<\/p>\n<pre><code>Serverless: Generating boilerplate...\nServerless: Generating boilerplate in \"\/serverless-demo\/devopscube-demo\"\n _______                             __\n|   _   .-----.----.--.--.-----.----|  .-----.-----.-----.\n|   |___|  -__|   _|  |  |  -__|   _|  |  -__|__ --|__ --|\n|____   |_____|__|  \\___\/|_____|__| |__|_____|_____|_____|\n|   |   |             The Serverless Application Framework\n|       |                           serverless.com, v1.15.3\n -------'\n\nServerless: Successfully generated boilerplate for template: \"aws-python\"<\/code><\/pre>\n<p>It will create the following folder structure<\/p>\n<pre><code>|-- devopscube-demo\n|   |-- handler.py\n|   |-- serverless.yml<\/code><\/pre>\n<p><strong>Step 3:<\/strong> cd into <code>devopscube-demo<\/code> This folder would contain two files named <code>handler.py<\/code> and <code>serverless.yml<\/code>. handler.py file contains a python code that will be deployed to lambda. serverless.yml contains the configuration which tells the serverless framework about how and what events it should associate with the given lambda function. You can specify the function name in serverless.yml file. In our case its the default <code>hello<\/code><\/p>\n<pre><code>cd devopscube-demo<\/code><\/pre>\n<p><strong>Step 4:<\/strong> deploy the basic service to AWS. This would create all the basic configurations on AWS for deploying the lambda function. (IAM Role, S3 bucket to keep the artifact, Cloudformation Template for Lambda and AWS log group for Lambda logs.)<\/p>\n<pre><code>serverless deploy -v<\/code><\/pre>\n<p>In your AWS dashboard, you can see the created lambda function.<\/p>\n<p><strong>Step 5:<\/strong> Now, let&#8217;s invoke our basic python function.<\/p>\n<pre><code>serverless invoke -f hello -l<\/code><\/pre>\n<p>You will see a successful execution with the following output<\/p>\n<pre><code>  {\n    \"body\": \"{\\\"input\\\": {}, \\\"message\\\": \\\"Go Serverless v1.0! Your function executed successfully!\\\"}\",\n    \"statusCode\": 200\n}\n--------------------------------------------------------------------\nSTART RequestId: ebb938e0-5803-11e7-825b-8f51036d398a Version: $LATEST\nEND RequestId: ebb938e0-5803-11e7-825b-8f51036d398a\nREPORT RequestId: ebb938e0-5803-11e7-825b-8f51036d398a\tDuration: 0.26 ms\tBilled Duration: 100 ms \tMemory Size: 1024 MB\tMax Memory Used: 19 MB<\/code><\/pre>\n<p><strong>Step 6:<\/strong> Now let&#8217;s remove the default python program and add our own program which returns the sum of two numbers. Our new <code>handler.py<\/code> would look like the following.<\/p>\n<pre><code>  import json\n\n  def hello(event, context):\n      a = 1\n      b = 4\n      return a+b<\/code><\/pre>\n<p><strong>Step 7:<\/strong> Since we have updated our function, we need to deploy the function again for updating the lambda for new code. let&#8217;s deploy our code using the following command.<\/p>\n<pre><code>serverless deploy function -f hello<\/code><\/pre>\n<p>Once deployed, you can invoke the function again using the invoke command.<\/p>\n<pre><code>serverless invoke -f hello -l<\/code><\/pre>\n<h3 id=\"adding-more-function\">Adding More Function<\/h3>\n<p>You can add more functions to your existing template. Here is what you have to do.<\/p>\n<ol>\n<li>Create a Python file named <code>demo-function.py<\/code> in your project directory where you have the <code>handler.py<\/code> file.<\/li>\n<li>Add the following code to the file.<\/li>\n<\/ol>\n<pre><code>  import json\n\n  def demo(event, context):\n      body = {\n          \"message\": \"Go Serverless v1.0! Your function executed successfully!\",\n          \"input\": event\n      }\n\n      response = {\n          \"statusCode\": 200,\n          \"body\": json.dumps(body)\n      }\n\n      return response\n\n      # Use this code if you don't use the http event with the LAMBDA-PROXY integration\n      \"\"\"\n      return {\n          \"message\": \"Go Serverless v1.0! Your function executed successfully!\",\n          \"event\": event\n      }\n      \"\"\"<\/code><\/pre>\n<p>The above code contains the python definition called demo. In our first example, it was hello.<\/p>\n<p><!--kg-card-begin: html--><\/p>\n<ol start=\"3\">\n<li>Add a new function called <code>demo-function<\/code> in the serverless.yml file along with your first hello function. Your function definition would look like the following.<\/li>\n<\/ol>\n<p><!--kg-card-end: html--><\/p>\n<pre><code>  functions:\n    hello:\n      handler: handler.hello\n    demo-function:\n      handler: testing.demo\n<\/code><\/pre>\n<p><!--kg-card-begin: html--><\/p>\n<ol start=\"4\">\n<li>Now that we have added a new handler and a function, we should redeploy the service.<\/li>\n<\/ol>\n<p><!--kg-card-end: html--><\/p>\n<pre><code>serverless deploy -v<\/code><\/pre>\n<p><!--kg-card-begin: html--><\/p>\n<ol start=\"5\">\n<li>Once deployed, invoke our new function.<\/li>\n<\/ol>\n<p><!--kg-card-end: html--><\/p>\n<pre><code>serverless invoke -f demo -l<\/code><\/pre>\n<p><!--kg-card-begin: html--><\/p>\n<ol start=\"6\">\n<li>If you edit the code, you can update and re-invoke it using the following command.<\/li>\n<\/ol>\n<p><!--kg-card-end: html--><\/p>\n<pre><code>serverless deploy function -f demo\nserverless invoke -f demo -l<\/code><\/pre>\n<p>There are more functionalities in this framework. If you are using or thinking about using AWS Lambda, Google Cloud Functions or Azure functions, you should definitely try out the serverless framework. In this serverless framework tutorial, we have just scratched the surface. We will be covering more topics on the serverless framework.<\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/serverless-framework-tutorial\/\" target=\"_blank\" rel=\"noopener noreferrer\">Serverless Framework Tutorial for Beginners Using AWS Lambda \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/serverless-framework-tutorial\/<\/p>\n","protected":false},"author":1,"featured_media":654,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-653","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=653"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/653\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/654"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}