Tech
2025-02-20
How to easily automatically create an AWS Lambda (lambda) function

Table of contents

I often create and use the tools I need for work. Among them, we usually use AWS Lambda (Lambda) when backend tasks are required. However, the process of creating a lambda function every time is repetitive and somewhat cumbersome. Therefore, by automating this, I created a lambda function generator project where anyone can easily create a lambda function.

Easy to use

The lambda function generator provides an intuitive UI, and allows you to create a lambda function in just a few simple steps.

1. Enter a lambda function name and select a region, etc.

Enter basic configuration information such as the name of the lambda function to be created, the AWS region where it will run, and the runtime. It's very simple because you only need to enter the information you need.

key

2. Creation results screen

Once the function has been created, you can view information such as the name, region, and function URL of the generated function on the results screen. By pasting the URL into a web browser, you can immediately test whether the Lambda function is working properly.

key

3. Check the generated lambda function

You can check the actual generated lambda function through the AWS console or other management tools. After creation, you can modify the function's settings or code to use it.

key

How to install and run

Here's how to install and run the lambda function generator:

1. Get the code and prepare it

The lambda function generator project is open to the public on GitHub, and the code in the repository can be cloned locally using the following command.

After cloning, go to the backend directory and install the required dependencies.

Then open the backend/index.mjs file and modify the ARN of the Lambda Execution Role (Execution Role) to the role you created in advance. This role is essential for the lambda function to be granted the necessary permissions while it is running.

Change [your-role-arn] to your lambda execution role ARN in the code above. The right permissions must be granted for the lambda function to work properly. Once setup is complete, the entire backend folder is compressed into a ZIP file and ready to be uploaded to AWS Lambda.

2. Create a lambda function

Here's how to create a new lambda function in the AWS Lambda console:

2.1. Connect to the AWS Lambda console and create a function

Go to the AWS Lambda console and click the Create Function button.

key

2.2. Function default settings

Type the desired function name and select the appropriate runtime and architecture for your project. In particular, arm architectures are worth considering because of their excellent cost-performance ratio. If the permission was created in advance, select the appropriate role; if not, create a new one. Here, we'll use the default settings to create a new one.

key

2.3. Select an execution role

You can select an existing execution role or create a new one. Here, we'll use the method of creating a new role with default settings.

2.4. Additional configuration — enable function URLs

In the further configuration step, select the “Enable function URL” option. The authentication type can be set to NONE for testing purposes, and it is recommended to change it to AWS_IAM or the like for security when operating the actual service.

key

2.5. CORS configuration selection

In the final step of further configuration, select the “Configure cross-origin resource sharing (CORS)” option.

key

After completing all settings, click the Create Function button to create the lambda function.

3. Lambda function code upload

Once the function creation is complete, select the function you created from within the AWS Lambda console and upload the ZIP file.

  • Click the “Upload from” button and select the “.zip file” menu.
  • Then select the.zip file in the zipped backend folder and upload it.
key

4. Lambda permission settings

If you created a new execution role when creating a lambda, you must grant permissions to that role. Here, we'll grant iamFullAccess and AWSLambdaFullAccess permissions for testing.

4.1. Check execution roles

  1. Go to the Configuration tab and click the Permissions menu.
  1. Go to the IAM console by clicking on the role name displayed under “Execution Role”.
key

4.2. Start linking policies

Click the “Add Permission” button and select the “Link Policy” menu.

key

4.3. Policy Choice - IamFullAccess

Search for IamFullAccess, select it, and click the “Add Permissions” button.

key

4.4. Policy Choice - AWSLamBDAfullAccess

Using the same method, search for the AWSLAMBDAfullAccess policy, select it, and then click the “Add Permissions” button.

key

Thus, we have set the required permissions for the lambda function. Lambda functions now have permission to create and manage other lambda functions.

5. Lambda function test

Now it's time to test by calling the lambda function on the frontend.

5.1. Front-end code modification

Open the frontend/index.html file and set the URL for the lambda function.

In the above code, change [your-lambda-url] to the URL of the generated lambda function. You can find this URL in the “Function URL” section of the AWS Lambda console.

key

5.2. Troubleshooting CORS settings

If a CORS-related error occurs during testing, go to the function's “Configuration” tab in the AWS Lambda console and click the “Edit” button in the “Function URL” settings. Afterwards, the CORS problem can be solved by modifying the “Allow Origin”, “Expose Headers”, “Allow Headers”, and “Allow Methods” fields to* as shown below.

5.3. tests

Open the frontend/index.html file in a web browser and call the lambda function to check if it works properly as shown in “Usage”.

key

To the end

By using a lambda function generator like this, you can easily create and test a lambda function without complicated processes. Development time can be shortened by automating repetitive configuration tasks, so please apply it to various task automation tools.

Happy coding!

Experience Monitoring with WhaTap!