{"id":10223,"date":"2021-07-05T11:29:53","date_gmt":"2021-07-05T02:29:53","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=10223"},"modified":"2021-07-05T11:19:10","modified_gmt":"2021-07-05T02:19:10","slug":"a-touch-to-serverless-framework-part-3","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/10223","title":{"rendered":"A touch to serverless framework &#8211; part 3"},"content":{"rendered":"\n<p>I wrote about setting up our severless project and deploying to the AWS last week. Today I will talk about invoking our functions and creating endpoints as I mentioned before. So lets get straight to it.<\/p>\n\n\n\n<h3>Function Invoking<\/h3>\n\n\n\n<p>Lets invoke our function which means execution of the serverless function. In invoking function, we can invoke either locally or cloud way.<\/p>\n\n\n\n<h4>Invoking locally<\/h4>\n\n\n\n<p>Go to your project root directory and run this command which invoke our function locally before submitting to the cloud.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sls invoke local --function hello<\/code><\/pre>\n\n\n\n<p>This will call to <code>hello<\/code> function and give a response like this.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"1024\" height=\"92\" src=\"\/appnews\/wp-content\/uploads\/sites\/4\/2021\/07\/12.sls-invoke-local-1024x92.png\" alt=\"\" class=\"wp-image-10224\" srcset=\"https:\/\/www.gigas-jp.com\/appnews\/wp-content\/uploads\/sites\/4\/2021\/07\/12.sls-invoke-local-1024x92.png 1024w, https:\/\/www.gigas-jp.com\/appnews\/wp-content\/uploads\/sites\/4\/2021\/07\/12.sls-invoke-local.png 1674w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h4>Invoking Cloud (Cloud execution)<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>sls invoke --function hello<\/code><\/pre>\n\n\n\n<p>This will also invoke our function but not from our local code. This will hit our function <code>hello<\/code> from the cloud and give us a response.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"1024\" height=\"94\" src=\"\/appnews\/wp-content\/uploads\/sites\/4\/2021\/07\/13.sls-invoke-cloud-1024x94.png\" alt=\"\" class=\"wp-image-10225\" srcset=\"https:\/\/www.gigas-jp.com\/appnews\/wp-content\/uploads\/sites\/4\/2021\/07\/13.sls-invoke-cloud-1024x94.png 1024w, https:\/\/www.gigas-jp.com\/appnews\/wp-content\/uploads\/sites\/4\/2021\/07\/13.sls-invoke-cloud.png 1676w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3>Creating Endpoints<\/h3>\n\n\n\n<p>Lets create a simple endpoints first before we tweak a bit more later. Remember the endpoint will be hit up when a specific event will come up which is the main concept of serverless (Event). So if we want to create an endpoint , we also need to create an event.<\/p>\n\n\n\n<p>Go to <code>serverless.yml<\/code> and find the functions parts and we will add an event and endpoint like this. Please be careful about the indentations as the yml file is sensitive.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>functions:\n  hello:\n    handler: handler.hello\n    events:\n      - http:\n          path: sample-endpoint\n          method: GET<\/code><\/pre>\n\n\n\n<p>We added an endpoint called <code>sample-endpoint<\/code> which is a GET method type and we have linked our endpoint with the <code>hello<\/code> function. Now lets deploy our project again.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sls deploy<\/code><\/pre>\n\n\n\n<p>As a result , we will get a response like this.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"1024\" height=\"450\" src=\"\/appnews\/wp-content\/uploads\/sites\/4\/2021\/07\/15.sls-path-deploy-1024x450.png\" alt=\"\" class=\"wp-image-10226\" srcset=\"https:\/\/www.gigas-jp.com\/appnews\/wp-content\/uploads\/sites\/4\/2021\/07\/15.sls-path-deploy-1024x450.png 1024w, https:\/\/www.gigas-jp.com\/appnews\/wp-content\/uploads\/sites\/4\/2021\/07\/15.sls-path-deploy.png 1612w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>You will see that we get an URL under the endpoints section which is the main response result of our function <code>hello<\/code>. Lets copy that URL and paste it in browser.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"1024\" height=\"510\" src=\"\/appnews\/wp-content\/uploads\/sites\/4\/2021\/07\/16.sls-path-deploy-response-1024x510.png\" alt=\"\" class=\"wp-image-10227\"\/><\/figure>\n\n\n\n<p>Voila ! Our endpoint works successfully. That is all for this week. I will be talking about tweaking our endpoint a bit more in next week.<\/p>\n\n\n\n<p>Thanks for reading. Yuuma<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/10223\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/10223\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"A touch to serverless framework &#8211; part 3\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/10223\" data-text=\"A touch to serverless framework &#8211; part 3\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/10223\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/10223\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>I wrote about setting up our severless project and deploying to the AWS last week. Today I will talk about inv [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[35],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/10223"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=10223"}],"version-history":[{"count":2,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/10223\/revisions"}],"predecessor-version":[{"id":10230,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/10223\/revisions\/10230"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=10223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=10223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=10223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}