2. Creating Your First Lambda Function
Let's create a simple "Hello World" Lambda function through the AWS Console. This example uses the Node.js runtime.
2.1. Steps to Create a Lambda Function
- Log in to the AWS Console and navigate to the Lambda service.
- Click the "Create function" button.
- Select "Author from scratch".
- Configure basic information:
- Function name:
my-first-lambda - Runtime: Node.js 18.x (or your preferred language)
- Architecture: `x86_64`
- Function name:
- Click "Change default execution role" and select "Create a new role with basic Lambda permissions". (In production, follow the principle of least privilege.)
- Click "Create function".
2.2. Writing the Function Code
Once the function is created, you'll see the default handler file in the Code source section. Update its content with the following code:
exports.handler = async (event) => {
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
return response;
};
Save and deploy your code. Your first Lambda function is now ready!