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.
The lambda function generator provides an intuitive UI, and allows you to create a lambda function in just a few simple steps.
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.
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.
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.
Here's how to install and run the lambda function generator:
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.
Here's how to create a new lambda function in the AWS Lambda console:
Go to the AWS Lambda console and click the Create Function button.
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.
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.
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.
In the final step of further configuration, select the “Configure cross-origin resource sharing (CORS)” option.
After completing all settings, click the Create Function button to create the lambda function.
Once the function creation is complete, select the function you created from within the AWS Lambda console and upload the ZIP file.
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.
Click the “Add Permission” button and select the “Link Policy” menu.
Search for IamFullAccess, select it, and click the “Add Permissions” button.
Using the same method, search for the AWSLAMBDAfullAccess policy, select it, and then click the “Add Permissions” button.
Thus, we have set the required permissions for the lambda function. Lambda functions now have permission to create and manage other lambda functions.
Now it's time to test by calling the lambda function on the frontend.
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.
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. testsOpen the frontend/index.html file in a web browser and call the lambda function to check if it works properly as shown in “Usage”.
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!