{"id":3933,"date":"2023-11-04T23:13:57","date_gmt":"2023-11-04T23:13:57","guid":{"rendered":"http:\/\/localhost:10003\/getting-started-with-aws-lambda-and-serverless-computing\/"},"modified":"2023-11-05T05:48:26","modified_gmt":"2023-11-05T05:48:26","slug":"getting-started-with-aws-lambda-and-serverless-computing","status":"publish","type":"post","link":"http:\/\/localhost:10003\/getting-started-with-aws-lambda-and-serverless-computing\/","title":{"rendered":"Getting Started with AWS Lambda and Serverless Computing"},"content":{"rendered":"

Serverless computing has become a popular way to deploy applications in recent years. With serverless computing, the cloud provider is responsible for executing code in response to events and managing the underlying infrastructure. AWS Lambda is a popular serverless computing platform offered by Amazon Web Services. In this tutorial, we’ll walk through the process of creating and deploying a simple AWS Lambda function.<\/p>\n

Prerequisites<\/h2>\n

Before we begin, you’ll need an AWS account. If you don’t have one, you can sign up for a free one here<\/a>. You’ll also need the AWS CLI installed on your machine. You can download it here<\/a>.<\/p>\n

Creating a Lambda function<\/h2>\n

To create a Lambda function, we’ll use the AWS CLI. Open a terminal and enter the following command:<\/p>\n

aws lambda create-function --function-name helloworld --runtime nodejs12.x --handler index.handler --zip-file fileb:\/\/function.zip --role <your-role-arn>\n<\/code><\/pre>\n

This command creates a new Lambda function called helloworld<\/code>. We’ve specified the runtime as Node.js 12.x, and the handler function as index.handler<\/code>. We’ll create that function in a moment. We’ve also specified the location of the deployment package. In this case, we’ve specified fileb:\/\/function.zip<\/code>. The fileb<\/code> prefix indicates that the file is in binary format. Finally, we’ve specified the ARN of the IAM role that the function will use. You’ll need to replace <your-role-arn><\/code> with the ARN of an appropriate IAM role on your account.<\/p>\n

Let’s create the index.js<\/code> and package.json<\/code> files now. In the terminal, enter the following command:<\/p>\n

mkdir helloworld && cd helloworld\n<\/code><\/pre>\n

This command creates a new directory called helloworld<\/code> and navigates to it. Next, we’ll create the index.js<\/code> and package.json<\/code> files. Enter the following commands:<\/p>\n

touch index.js\ntouch package.json\n<\/code><\/pre>\n

Open the index.js<\/code> file in your favorite text editor and add the following code:<\/p>\n

exports.handler = async (event) => {\n    console.log('Hello, world!', event);\n    const response = {\n        statusCode: 200,\n        body: JSON.stringify('Hello from Lambda!'),\n    };\n    return response;\n};\n<\/code><\/pre>\n

This code exports a function called handler<\/code> that will be executed when the Lambda function is called. When the function runs, it logs a message to the console and returns a response with a status code of 200 and a message of “Hello from Lambda!”.<\/p>\n

Now, open the package.json<\/code> file and add the following code:<\/p>\n

{\n    \"name\": \"helloworld\",\n    \"version\": \"1.0.0\",\n    \"description\": \"A simple Lambda function that returns a message\",\n    \"main\": \"index.js\",\n    \"dependencies\": {}\n}\n<\/code><\/pre>\n

This code defines some basic information about our Lambda function, including the name, version, and dependencies. Since our function doesn’t have any dependencies, the dependencies<\/code> object is empty.<\/p>\n

Once you’ve added these files, we need to create a deployment package that can be uploaded to Lambda. In the terminal, enter the following commands:<\/p>\n

npm install\nzip -r function.zip *\n<\/code><\/pre>\n

The first command installs any dependencies defined in the package.json<\/code> file. The second command creates a ZIP archive called function.zip<\/code> that contains all the files in the helloworld<\/code> directory.<\/p>\n

Now that we have our deployment package, let’s upload it to AWS Lambda. In the terminal, enter the following command:<\/p>\n

aws lambda update-function-code --function-name helloworld --zip-file fileb:\/\/function.zip\n<\/code><\/pre>\n

This command updates the function code for our helloworld<\/code> Lambda function with the contents of the function.zip<\/code> archive.<\/p>\n

Testing the Lambda function<\/h2>\n

With our function created and uploaded, we can test it using the AWS CLI. Enter the following command in the terminal:<\/p>\n

aws lambda invoke --function-name helloworld output --log-type Tail\n<\/code><\/pre>\n

This command invokes the helloworld<\/code> function and writes the output to a file called output<\/code>. The --log-type Tail<\/code> option tells Lambda to include the last few lines of the function’s logs in the output.<\/p>\n

Open the output<\/code> file and you should see something like this:<\/p>\n

\"Hello from Lambda!\"\n<\/code><\/pre>\n

If you scroll to the end of the file, you’ll also see the output of the function’s logs, which should look something like this:<\/p>\n

START RequestId: d498e6d5-573b-498a-8bc7-54d74cc5d0f8 Version: $LATEST\n2022-01-01T00:00:00.000Z    d498e6d5-573b-498a-8bc7-54d74cc5d0f8    INFO    Hello, world! {}\nEND RequestId: d498e6d5-573b-498a-8bc7-54d74cc5d0f8\nREPORT RequestId: d498e6d5-573b-498a-8bc7-54d74cc5d0f8  Duration: 3.17 ms   Billed Duration: 4 ms   Memory Size: 128 MB Max Memory Used: 63 MB  Init Duration: 81.76 ms\n<\/code><\/pre>\n

This output indicates that the function was executed successfully and that it logged a message to the console.<\/p>\n

Triggering the Lambda function<\/h2>\n

So far, we’ve only tested our Lambda function manually using the AWS CLI. Let’s set up a trigger that will cause our function to be executed automatically. In this case, we’ll use an API Gateway endpoint as our trigger.<\/p>\n

To create an API Gateway endpoint, we’ll use the AWS Management Console. Open the API Gateway console<\/a>, click “Create API”, and choose “REST API”. Give your API a name, then click “Create API”.<\/p>\n

Next, we’ll create a resource and a method for our endpoint. Expand the tree view on the left side of the console and click “Create Resource”. Give your resource a name, then click “Create Resource”. Next, click “Create Method” and choose “GET”.<\/p>\n

In the method configuration screen, choose “Lambda Function” as the integration type. Choose your Lambda function from the dropdown menu, and click “Save”. Finally, click “Deploy API” to deploy your API.<\/p>\n

With our API deployed, we can test it using curl or another HTTP client. In the terminal, enter the following command:<\/p>\n

curl https:\/\/<your-api-id>.execute-api.<your-region>.amazonaws.com\/<your-stage>\/hello\n<\/code><\/pre>\n

Replace <your-api-id><\/code>, <your-region><\/code>, and <your-stage><\/code> with the appropriate values from your API Gateway console. You should see “Hello from Lambda!” in the output.<\/p>\n

Conclusion<\/h2>\n

In this tutorial, we’ve walked through the process of creating and deploying a simple AWS Lambda function. We’ve also demonstrated how to test the function manually using the AWS CLI, and how to trigger the function using API Gateway. Serverless computing is a powerful paradigm for deploying applications, and AWS Lambda makes it easy to get started. With the concepts and techniques covered in this tutorial, you should be well on your way to building your own serverless applications.<\/p>\n","protected":false},"excerpt":{"rendered":"

Serverless computing has become a popular way to deploy applications in recent years. With serverless computing, the cloud provider is responsible for executing code in response to events and managing the underlying infrastructure. AWS Lambda is a popular serverless computing platform offered by Amazon Web Services. In this tutorial, we’ll Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[317,202,30,425,424,423],"yoast_head":"\nGetting Started with AWS Lambda and Serverless Computing - Pantherax Blogs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/localhost:10003\/getting-started-with-aws-lambda-and-serverless-computing\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Getting Started with AWS Lambda and Serverless Computing\" \/>\n<meta property=\"og:description\" content=\"Serverless computing has become a popular way to deploy applications in recent years. With serverless computing, the cloud provider is responsible for executing code in response to events and managing the underlying infrastructure. AWS Lambda is a popular serverless computing platform offered by Amazon Web Services. In this tutorial, we’ll Continue Reading\" \/>\n<meta property=\"og:url\" content=\"http:\/\/localhost:10003\/getting-started-with-aws-lambda-and-serverless-computing\/\" \/>\n<meta property=\"og:site_name\" content=\"Pantherax Blogs\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-04T23:13:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-05T05:48:26+00:00\" \/>\n<meta name=\"author\" content=\"Panther\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Panther\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t \"@context\": \"https:\/\/schema.org\",\n\t \"@graph\": [\n\t {\n\t \"@type\": \"Article\",\n\t \"@id\": \"http:\/\/localhost:10003\/getting-started-with-aws-lambda-and-serverless-computing\/#article\",\n\t \"isPartOf\": {\n\t \"@id\": \"http:\/\/localhost:10003\/getting-started-with-aws-lambda-and-serverless-computing\/\"\n\t },\n\t \"author\": {\n\t \"name\": \"Panther\",\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7\"\n\t },\n\t \"headline\": \"Getting Started with AWS Lambda and Serverless Computing\",\n\t \"datePublished\": \"2023-11-04T23:13:57+00:00\",\n\t \"dateModified\": \"2023-11-05T05:48:26+00:00\",\n\t \"mainEntityOfPage\": {\n\t \"@id\": \"http:\/\/localhost:10003\/getting-started-with-aws-lambda-and-serverless-computing\/\"\n\t },\n\t \"wordCount\": 828,\n\t \"publisher\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#organization\"\n\t },\n\t \"keywords\": [\n\t \"\\\"Amazon Web Services\\\"\",\n\t \"\\\"AWS\\\"\",\n\t \"\\\"cloud computing\\\"\",\n\t \"\\\"Getting Started\\\"]\",\n\t \"\\\"Serverless Computing\\\"\",\n\t \"[\\\"AWS Lambda\\\"\"\n\t ],\n\t \"inLanguage\": \"en-US\"\n\t },\n\t {\n\t \"@type\": \"WebPage\",\n\t \"@id\": \"http:\/\/localhost:10003\/getting-started-with-aws-lambda-and-serverless-computing\/\",\n\t \"url\": \"http:\/\/localhost:10003\/getting-started-with-aws-lambda-and-serverless-computing\/\",\n\t \"name\": \"Getting Started with AWS Lambda and Serverless Computing - Pantherax Blogs\",\n\t \"isPartOf\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#website\"\n\t },\n\t \"datePublished\": \"2023-11-04T23:13:57+00:00\",\n\t \"dateModified\": \"2023-11-05T05:48:26+00:00\",\n\t \"breadcrumb\": {\n\t \"@id\": \"http:\/\/localhost:10003\/getting-started-with-aws-lambda-and-serverless-computing\/#breadcrumb\"\n\t },\n\t \"inLanguage\": \"en-US\",\n\t \"potentialAction\": [\n\t {\n\t \"@type\": \"ReadAction\",\n\t \"target\": [\n\t \"http:\/\/localhost:10003\/getting-started-with-aws-lambda-and-serverless-computing\/\"\n\t ]\n\t }\n\t ]\n\t },\n\t {\n\t \"@type\": \"BreadcrumbList\",\n\t \"@id\": \"http:\/\/localhost:10003\/getting-started-with-aws-lambda-and-serverless-computing\/#breadcrumb\",\n\t \"itemListElement\": [\n\t {\n\t \"@type\": \"ListItem\",\n\t \"position\": 1,\n\t \"name\": \"Home\",\n\t \"item\": \"http:\/\/localhost:10003\/\"\n\t },\n\t {\n\t \"@type\": \"ListItem\",\n\t \"position\": 2,\n\t \"name\": \"Getting Started with AWS Lambda and Serverless Computing\"\n\t }\n\t ]\n\t },\n\t {\n\t \"@type\": \"WebSite\",\n\t \"@id\": \"http:\/\/localhost:10003\/#website\",\n\t \"url\": \"http:\/\/localhost:10003\/\",\n\t \"name\": \"Pantherax Blogs\",\n\t \"description\": \"\",\n\t \"publisher\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#organization\"\n\t },\n\t \"potentialAction\": [\n\t {\n\t \"@type\": \"SearchAction\",\n\t \"target\": {\n\t \"@type\": \"EntryPoint\",\n\t \"urlTemplate\": \"http:\/\/localhost:10003\/?s={search_term_string}\"\n\t },\n\t \"query-input\": \"required name=search_term_string\"\n\t }\n\t ],\n\t \"inLanguage\": \"en-US\"\n\t },\n\t {\n\t \"@type\": \"Organization\",\n\t \"@id\": \"http:\/\/localhost:10003\/#organization\",\n\t \"name\": \"Pantherax Blogs\",\n\t \"url\": \"http:\/\/localhost:10003\/\",\n\t \"logo\": {\n\t \"@type\": \"ImageObject\",\n\t \"inLanguage\": \"en-US\",\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/logo\/image\/\",\n\t \"url\": \"http:\/\/localhost:10003\/wp-content\/uploads\/2023\/11\/cropped-9e7721cb-2d62-4f72-ab7f-7d1d8db89226.jpeg\",\n\t \"contentUrl\": \"http:\/\/localhost:10003\/wp-content\/uploads\/2023\/11\/cropped-9e7721cb-2d62-4f72-ab7f-7d1d8db89226.jpeg\",\n\t \"width\": 1024,\n\t \"height\": 1024,\n\t \"caption\": \"Pantherax Blogs\"\n\t },\n\t \"image\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/logo\/image\/\"\n\t }\n\t },\n\t {\n\t \"@type\": \"Person\",\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7\",\n\t \"name\": \"Panther\",\n\t \"image\": {\n\t \"@type\": \"ImageObject\",\n\t \"inLanguage\": \"en-US\",\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/person\/image\/\",\n\t \"url\": \"http:\/\/2.gravatar.com\/avatar\/b8c0eda5a49f8f31ec32d0a0f9d6f838?s=96&d=mm&r=g\",\n\t \"contentUrl\": \"http:\/\/2.gravatar.com\/avatar\/b8c0eda5a49f8f31ec32d0a0f9d6f838?s=96&d=mm&r=g\",\n\t \"caption\": \"Panther\"\n\t },\n\t \"sameAs\": [\n\t \"http:\/\/localhost:10003\"\n\t ],\n\t \"url\": \"http:\/\/localhost:10003\/author\/pepethefrog\/\"\n\t }\n\t ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Getting Started with AWS Lambda and Serverless Computing - Pantherax Blogs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/localhost:10003\/getting-started-with-aws-lambda-and-serverless-computing\/","og_locale":"en_US","og_type":"article","og_title":"Getting Started with AWS Lambda and Serverless Computing","og_description":"Serverless computing has become a popular way to deploy applications in recent years. With serverless computing, the cloud provider is responsible for executing code in response to events and managing the underlying infrastructure. AWS Lambda is a popular serverless computing platform offered by Amazon Web Services. In this tutorial, we’ll Continue Reading","og_url":"http:\/\/localhost:10003\/getting-started-with-aws-lambda-and-serverless-computing\/","og_site_name":"Pantherax Blogs","article_published_time":"2023-11-04T23:13:57+00:00","article_modified_time":"2023-11-05T05:48:26+00:00","author":"Panther","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Panther","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/localhost:10003\/getting-started-with-aws-lambda-and-serverless-computing\/#article","isPartOf":{"@id":"http:\/\/localhost:10003\/getting-started-with-aws-lambda-and-serverless-computing\/"},"author":{"name":"Panther","@id":"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7"},"headline":"Getting Started with AWS Lambda and Serverless Computing","datePublished":"2023-11-04T23:13:57+00:00","dateModified":"2023-11-05T05:48:26+00:00","mainEntityOfPage":{"@id":"http:\/\/localhost:10003\/getting-started-with-aws-lambda-and-serverless-computing\/"},"wordCount":828,"publisher":{"@id":"http:\/\/localhost:10003\/#organization"},"keywords":["\"Amazon Web Services\"","\"AWS\"","\"cloud computing\"","\"Getting Started\"]","\"Serverless Computing\"","[\"AWS Lambda\""],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/localhost:10003\/getting-started-with-aws-lambda-and-serverless-computing\/","url":"http:\/\/localhost:10003\/getting-started-with-aws-lambda-and-serverless-computing\/","name":"Getting Started with AWS Lambda and Serverless Computing - Pantherax Blogs","isPartOf":{"@id":"http:\/\/localhost:10003\/#website"},"datePublished":"2023-11-04T23:13:57+00:00","dateModified":"2023-11-05T05:48:26+00:00","breadcrumb":{"@id":"http:\/\/localhost:10003\/getting-started-with-aws-lambda-and-serverless-computing\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/localhost:10003\/getting-started-with-aws-lambda-and-serverless-computing\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/localhost:10003\/getting-started-with-aws-lambda-and-serverless-computing\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/localhost:10003\/"},{"@type":"ListItem","position":2,"name":"Getting Started with AWS Lambda and Serverless Computing"}]},{"@type":"WebSite","@id":"http:\/\/localhost:10003\/#website","url":"http:\/\/localhost:10003\/","name":"Pantherax Blogs","description":"","publisher":{"@id":"http:\/\/localhost:10003\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/localhost:10003\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/localhost:10003\/#organization","name":"Pantherax Blogs","url":"http:\/\/localhost:10003\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/localhost:10003\/#\/schema\/logo\/image\/","url":"http:\/\/localhost:10003\/wp-content\/uploads\/2023\/11\/cropped-9e7721cb-2d62-4f72-ab7f-7d1d8db89226.jpeg","contentUrl":"http:\/\/localhost:10003\/wp-content\/uploads\/2023\/11\/cropped-9e7721cb-2d62-4f72-ab7f-7d1d8db89226.jpeg","width":1024,"height":1024,"caption":"Pantherax Blogs"},"image":{"@id":"http:\/\/localhost:10003\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7","name":"Panther","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/localhost:10003\/#\/schema\/person\/image\/","url":"http:\/\/2.gravatar.com\/avatar\/b8c0eda5a49f8f31ec32d0a0f9d6f838?s=96&d=mm&r=g","contentUrl":"http:\/\/2.gravatar.com\/avatar\/b8c0eda5a49f8f31ec32d0a0f9d6f838?s=96&d=mm&r=g","caption":"Panther"},"sameAs":["http:\/\/localhost:10003"],"url":"http:\/\/localhost:10003\/author\/pepethefrog\/"}]}},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts\/3933"}],"collection":[{"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/comments?post=3933"}],"version-history":[{"count":1,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts\/3933\/revisions"}],"predecessor-version":[{"id":4569,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts\/3933\/revisions\/4569"}],"wp:attachment":[{"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/media?parent=3933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/categories?post=3933"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/tags?post=3933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}