![Building Serverless Python Web Services with Zappa](https://wfqqreader-1252317822.image.myqcloud.com/cover/614/36699614/b_36699614.jpg)
Creating a Lambda function
Here, we are going to create a new Lambda function with the aws lambda create-function command. To run this command, we need to pass the required and optional arguments.
Make sure you have a role with permission for the lambda:CreateFunction action.
Previously, in the AWS Lambda console, we chose the code entry point as inline editing. Now, we will be using a ZIP file as a deployment package.
Before creating the Lambda function, we should create a Lambda function deployment package.
This deployment package will be a ZIP file consisting of your code and any dependencies.
If your project has some dependencies, then you must install the dependencies in the root directive of the project. For example:
$ pip install requests -t <project-dir> OR $ pip install -r requirements.txt -t <project-dir>
Here, the -t option indicates the target directory.
Create a simple lambda_handler function in a file named as handler.py, as shown in the following screenshot:
![](https://epubservercos.yuewen.com/CACC6B/19470390501551006/epubprivate/OEBPS/Images/3a9b7112-b0e6-40cb-91d1-125352f66e98.png?sign=1738858673-qzoGm4Qor5IvngcQf3BdBOdoz4IJrjX0-0-44ae971dceb06ccaa6f6338a6cfd9024)
Now, let's make a deployment package as a ZIP file consisting of the preceding code:
![](https://epubservercos.yuewen.com/CACC6B/19470390501551006/epubprivate/OEBPS/Images/0387785d-3198-4ad7-935a-6941ac8bf166.png?sign=1738858673-v8bZzc5RMhBiMPaGDFxqUppt16SPZgw6-0-bd48f44b3559f9e9c28dadd0d689bb2d)
Now, we are ready to create the Lambda function. The following screenshot describes the command execution:
![](https://epubservercos.yuewen.com/CACC6B/19470390501551006/epubprivate/OEBPS/Images/c2d6a8cd-d6da-4c45-a5af-7165a11b8db9.png?sign=1738858673-kxihIlrG7roX2k2wuyRxsS5qY3sQH08a-0-8bd103443f86fd80fa5bc39ef377b320)
You can see that, in the AWS Lambda console, the Lambda function immediately got created:
![](https://epubservercos.yuewen.com/CACC6B/19470390501551006/epubprivate/OEBPS/Images/e96c7fde-60de-4d89-8f49-bfce574aba97.png?sign=1738858673-WsMEb4LwjqEnWt1qL4p1kgtqr6VYhTvu-0-653807e73c24aa3b8c9a8d939ad0e833)
Let's discuss the required and optional parameters that we used with the aws lambda create-function command:
- --function-name (required): The name is self-explanatory. We need to pass the Lambda function name that we are intending to create.
- --role (required): This is a required parameter where we need to use the AWS role ARN as a value. Make sure this role has permissions to create the Lambda function.
- --runtime (required): We need to mention the runtime environment for the Lambda function execution. As we mentioned earlier, AWS Lambda supports Python, Node.js, C#, and Java. So these are the possible values:
- python2.7
- python3.6
- nodejs
- nodejs4.3
- nodejs6.10
- nodejs4.3-edge
- dotnetcore1.0
- java8
- --handler (required): Here, we mention the function path that will be an execution entry point by the AWS Lambda. In our case, we used handler.lambda_function, where the handler is the file that contains the lambda_function.
- --description: This option lets you add some text description about your Lambda function.
- --zip-file: This option is used to upload the deployment package file of your code from your local environment/machine. Here, you need to add fileb:// as a prefix to your ZIP file path.
- --code: This option helps you upload the deployment package file from the AWS S3 bucket.
You should pass a string value with a pattern, such as the one shown here:
"S3Bucket=<bucket-name>,S3Key=<file-name>,S3ObjectVersion=<file-version-id>".
There are many other optional parameters available that you can see with the help command, such as aws lambda create-function help. You can use them as per your requirement.
Now we will see the Lambda function invocation using the command $ aws lambda invoke.