{"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
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 To create a Lambda function, we’ll use the AWS CLI. Open a terminal and enter the following command:<\/p>\n This command creates a new Lambda function called Let’s create the This command creates a new directory called Open the This code exports a function called Now, open the 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 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 The first command installs any dependencies defined in the Now that we have our deployment package, let’s upload it to AWS Lambda. In the terminal, enter the following command:<\/p>\n This command updates the function code for our With our function created and uploaded, we can test it using the AWS CLI. Enter the following command in the terminal:<\/p>\n This command invokes the Open the 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 This output indicates that the function was executed successfully and that it logged a message to the console.<\/p>\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 Replace 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":"\nCreating a Lambda function<\/h2>\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
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
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
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
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
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
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
dependencies<\/code> object is empty.<\/p>\n
npm install\nzip -r function.zip *\n<\/code><\/pre>\n
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
aws lambda update-function-code --function-name helloworld --zip-file fileb:\/\/function.zip\n<\/code><\/pre>\n
helloworld<\/code> Lambda function with the contents of the
function.zip<\/code> archive.<\/p>\n
Testing the Lambda function<\/h2>\n
aws lambda invoke --function-name helloworld output --log-type Tail\n<\/code><\/pre>\n
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
output<\/code> file and you should see something like this:<\/p>\n
\"Hello from Lambda!\"\n<\/code><\/pre>\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
Triggering the Lambda function<\/h2>\n
curl https:\/\/<your-api-id>.execute-api.<your-region>.amazonaws.com\/<your-stage>\/hello\n<\/code><\/pre>\n
<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