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.
Prerequisites
Before we begin, you’ll need an AWS account. If you don’t have one, you can sign up for a free one here. You’ll also need the AWS CLI installed on your machine. You can download it here.
Creating a Lambda function
To create a Lambda function, we’ll use the AWS CLI. Open a terminal and enter the following command:
aws lambda create-function --function-name helloworld --runtime nodejs12.x --handler index.handler --zip-file fileb://function.zip --role <your-role-arn>
This command creates a new Lambda function called helloworld
. We’ve specified the runtime as Node.js 12.x, and the handler function as index.handler
. 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
. The fileb
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>
with the ARN of an appropriate IAM role on your account.
Let’s create the index.js
and package.json
files now. In the terminal, enter the following command:
mkdir helloworld && cd helloworld
This command creates a new directory called helloworld
and navigates to it. Next, we’ll create the index.js
and package.json
files. Enter the following commands:
touch index.js
touch package.json
Open the index.js
file in your favorite text editor and add the following code:
exports.handler = async (event) => {
console.log('Hello, world!', event);
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
return response;
};
This code exports a function called handler
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!”.
Now, open the package.json
file and add the following code:
{
"name": "helloworld",
"version": "1.0.0",
"description": "A simple Lambda function that returns a message",
"main": "index.js",
"dependencies": {}
}
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
object is empty.
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:
npm install
zip -r function.zip *
The first command installs any dependencies defined in the package.json
file. The second command creates a ZIP archive called function.zip
that contains all the files in the helloworld
directory.
Now that we have our deployment package, let’s upload it to AWS Lambda. In the terminal, enter the following command:
aws lambda update-function-code --function-name helloworld --zip-file fileb://function.zip
This command updates the function code for our helloworld
Lambda function with the contents of the function.zip
archive.
Testing the Lambda function
With our function created and uploaded, we can test it using the AWS CLI. Enter the following command in the terminal:
aws lambda invoke --function-name helloworld output --log-type Tail
This command invokes the helloworld
function and writes the output to a file called output
. The --log-type Tail
option tells Lambda to include the last few lines of the function’s logs in the output.
Open the output
file and you should see something like this:
"Hello from Lambda!"
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:
START RequestId: d498e6d5-573b-498a-8bc7-54d74cc5d0f8 Version: $LATEST
2022-01-01T00:00:00.000Z d498e6d5-573b-498a-8bc7-54d74cc5d0f8 INFO Hello, world! {}
END RequestId: d498e6d5-573b-498a-8bc7-54d74cc5d0f8
REPORT 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
This output indicates that the function was executed successfully and that it logged a message to the console.
Triggering the Lambda function
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.
To create an API Gateway endpoint, we’ll use the AWS Management Console. Open the API Gateway console, click “Create API”, and choose “REST API”. Give your API a name, then click “Create API”.
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”.
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.
With our API deployed, we can test it using curl or another HTTP client. In the terminal, enter the following command:
curl https://<your-api-id>.execute-api.<your-region>.amazonaws.com/<your-stage>/hello
Replace <your-api-id>
, <your-region>
, and <your-stage>
with the appropriate values from your API Gateway console. You should see “Hello from Lambda!” in the output.
Conclusion
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.