\nReplace the existing code with the following sample code:<\/p>\n<\/li>\n<\/ol>\n
exports.handler = function(event, context, callback) {\n console.log('Hello World!');\n callback(null, 'Lambda function executed successfully!');\n};\n<\/code><\/pre>\n\n- Click the Save button to save the function code.<\/li>\n<\/ol>\n
Testing the Function<\/h3>\n
Now that you have written your function code, it’s time to test it.<\/p>\n
\n- Click on Test button on your function dashboard.<\/p>\n<\/li>\n
- \n
In the Configure test event dialog box, enter the name and other details for your test event.<\/p>\n<\/li>\n
- \n
Click on the Create button to create the test event.<\/p>\n<\/li>\n
- \n
Click on the Test button again and wait for the results.<\/p>\n<\/li>\n<\/ol>\n
You should see the output “Hello World!” and “Lambda function executed successfully!” in the Log Output section. This confirms that your function is working correctly.<\/p>\n
Invoking the Function via AWS SDK<\/h2>\n
You can also use the AWS SDK to invoke your function programmatically.<\/p>\n
\n- Open a new file in your text editor and save it as index.js.<\/p>\n<\/li>\n
- \n
Add the following code to the file:<\/p>\n<\/li>\n<\/ol>\n
const AWS = require('aws-sdk');\n\nconst lambda = new AWS.Lambda();\n\nexports.handler = function(event, context, callback) {\n const params = {\n FunctionName: 'my-function',\n Payload: JSON.stringify(event),\n };\n\n lambda.invoke(params, function(error, data) {\n if (error) {\n console.log(error);\n callback(error);\n } else {\n console.log(data);\n callback(null, data.Payload);\n }\n });\n};\n<\/code><\/pre>\n\n- \n
Save the file and exit the text editor.<\/p>\n<\/li>\n
- \n
Install the AWS SDK using npm:<\/p>\n<\/li>\n<\/ol>\n
npm install aws-sdk --save\n<\/code><\/pre>\n\n- \n
Open the AWS Management Console and navigate to the IAM console.<\/p>\n<\/li>\n
- \n
Create a new IAM user with programmatic access and attach the AWSLambdaFullAccess policy to the user.<\/p>\n<\/li>\n
- \n
Copy the Access key ID and Secret access key of the user.<\/p>\n<\/li>\n
- \n
Open the terminal and configure the AWS CLI with the access key ID and secret access key of the user:<\/p>\n<\/li>\n<\/ol>\n
aws configure\n<\/code><\/pre>\n\n- \n
Enter the access key ID and secret access key of the IAM user.<\/p>\n<\/li>\n
- \n
Create a test event file and add the following code:<\/p>\n<\/li>\n<\/ol>\n
{\n \"test\": \"value\"\n}\n<\/code><\/pre>\n\n- \n
Save the file as test-event.json.<\/p>\n<\/li>\n
- \n
Invoke the function using the following command:<\/p>\n<\/li>\n<\/ol>\n
aws lambda invoke --function-name my-function --payload file:\/\/test-event.json output.txt\n<\/code><\/pre>\n\n- Check the output.txt file to see the results of the invocation.<\/li>\n<\/ol>\n
Conclusion<\/h2>\n
In this tutorial, we walked you through the process of using AWS Lambda to run your code without managing servers. We started by creating a Lambda function, adding function code, and testing it. We then showed you how to invoke the function programmatically using the AWS SDK. Now that you have learned how to use AWS Lambda, you can explore its other features, such as triggered events, AWS Serverless Application Model (SAM), and more.<\/p>\n","protected":false},"excerpt":{"rendered":"
Introduction AWS Lambda is a compute service that lets you run your code without provisioning or managing servers. It is a serverless architecture that executes your code only when needed and scales with your usage. Lambda automatically manages compute resources, so you don’t have to worry about server maintenance, capacity 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":[1292,1155,30,1291,203,423],"yoast_head":"\nUsing AWS Lambda to run code without managing servers - 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\/using-aws-lambda-to-run-code-without-managing-servers\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using AWS Lambda to run code without managing servers\" \/>\n<meta property=\"og:description\" content=\"Introduction AWS Lambda is a compute service that lets you run your code without provisioning or managing servers. It is a serverless architecture that executes your code only when needed and scales with your usage. Lambda automatically manages compute resources, so you don’t have to worry about server maintenance, capacity Continue Reading\" \/>\n<meta property=\"og:url\" content=\"http:\/\/localhost:10003\/using-aws-lambda-to-run-code-without-managing-servers\/\" \/>\n<meta property=\"og:site_name\" content=\"Pantherax Blogs\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-04T23:14:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-05T05:48:01+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=\"4 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\/using-aws-lambda-to-run-code-without-managing-servers\/#article\",\n\t \"isPartOf\": {\n\t \"@id\": \"http:\/\/localhost:10003\/using-aws-lambda-to-run-code-without-managing-servers\/\"\n\t },\n\t \"author\": {\n\t \"name\": \"Panther\",\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7\"\n\t },\n\t \"headline\": \"Using AWS Lambda to run code without managing servers\",\n\t \"datePublished\": \"2023-11-04T23:14:03+00:00\",\n\t \"dateModified\": \"2023-11-05T05:48:01+00:00\",\n\t \"mainEntityOfPage\": {\n\t \"@id\": \"http:\/\/localhost:10003\/using-aws-lambda-to-run-code-without-managing-servers\/\"\n\t },\n\t \"wordCount\": 641,\n\t \"publisher\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#organization\"\n\t },\n\t \"keywords\": [\n\t \"\\\"AWS Lambda functions\\\"]\",\n\t \"\\\"AWS services\\\"\",\n\t \"\\\"cloud computing\\\"\",\n\t \"\\\"code execution\\\"\",\n\t \"\\\"serverless architecture\\\"\",\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\/using-aws-lambda-to-run-code-without-managing-servers\/\",\n\t \"url\": \"http:\/\/localhost:10003\/using-aws-lambda-to-run-code-without-managing-servers\/\",\n\t \"name\": \"Using AWS Lambda to run code without managing servers - Pantherax Blogs\",\n\t \"isPartOf\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#website\"\n\t },\n\t \"datePublished\": \"2023-11-04T23:14:03+00:00\",\n\t \"dateModified\": \"2023-11-05T05:48:01+00:00\",\n\t \"breadcrumb\": {\n\t \"@id\": \"http:\/\/localhost:10003\/using-aws-lambda-to-run-code-without-managing-servers\/#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\/using-aws-lambda-to-run-code-without-managing-servers\/\"\n\t ]\n\t }\n\t ]\n\t },\n\t {\n\t \"@type\": \"BreadcrumbList\",\n\t \"@id\": \"http:\/\/localhost:10003\/using-aws-lambda-to-run-code-without-managing-servers\/#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\": \"Using AWS Lambda to run code without managing servers\"\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":"Using AWS Lambda to run code without managing servers - 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\/using-aws-lambda-to-run-code-without-managing-servers\/","og_locale":"en_US","og_type":"article","og_title":"Using AWS Lambda to run code without managing servers","og_description":"Introduction AWS Lambda is a compute service that lets you run your code without provisioning or managing servers. It is a serverless architecture that executes your code only when needed and scales with your usage. Lambda automatically manages compute resources, so you don’t have to worry about server maintenance, capacity Continue Reading","og_url":"http:\/\/localhost:10003\/using-aws-lambda-to-run-code-without-managing-servers\/","og_site_name":"Pantherax Blogs","article_published_time":"2023-11-04T23:14:03+00:00","article_modified_time":"2023-11-05T05:48:01+00:00","author":"Panther","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Panther","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/localhost:10003\/using-aws-lambda-to-run-code-without-managing-servers\/#article","isPartOf":{"@id":"http:\/\/localhost:10003\/using-aws-lambda-to-run-code-without-managing-servers\/"},"author":{"name":"Panther","@id":"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7"},"headline":"Using AWS Lambda to run code without managing servers","datePublished":"2023-11-04T23:14:03+00:00","dateModified":"2023-11-05T05:48:01+00:00","mainEntityOfPage":{"@id":"http:\/\/localhost:10003\/using-aws-lambda-to-run-code-without-managing-servers\/"},"wordCount":641,"publisher":{"@id":"http:\/\/localhost:10003\/#organization"},"keywords":["\"AWS Lambda functions\"]","\"AWS services\"","\"cloud computing\"","\"code execution\"","\"serverless architecture\"","[\"AWS Lambda\""],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/localhost:10003\/using-aws-lambda-to-run-code-without-managing-servers\/","url":"http:\/\/localhost:10003\/using-aws-lambda-to-run-code-without-managing-servers\/","name":"Using AWS Lambda to run code without managing servers - Pantherax Blogs","isPartOf":{"@id":"http:\/\/localhost:10003\/#website"},"datePublished":"2023-11-04T23:14:03+00:00","dateModified":"2023-11-05T05:48:01+00:00","breadcrumb":{"@id":"http:\/\/localhost:10003\/using-aws-lambda-to-run-code-without-managing-servers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/localhost:10003\/using-aws-lambda-to-run-code-without-managing-servers\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/localhost:10003\/using-aws-lambda-to-run-code-without-managing-servers\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/localhost:10003\/"},{"@type":"ListItem","position":2,"name":"Using AWS Lambda to run code without managing servers"}]},{"@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\/4101"}],"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=4101"}],"version-history":[{"count":1,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts\/4101\/revisions"}],"predecessor-version":[{"id":4457,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts\/4101\/revisions\/4457"}],"wp:attachment":[{"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/media?parent=4101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/categories?post=4101"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/tags?post=4101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}