[PYTHON] AWS Lambda Development My Best Practices

I will write the best practices of AWS Lambda development that I think with Java Web application development (JavaEE) as the main skill, and tips on building a development environment.

I have a JavaEE qualification, so I think I have some programming skills, but I have been working on AWS Lambda for about a month. For languages other than java, the level is "If you read the reference, you can write it like that".

Service configuration

I chose the serverless architecture of API Gateway + Lambda + DynamoDB, depending on the application to be realized. I tried to tempt myself not to take care of the server.

For source code management, use CodeCommit (git) and summarize it on AWS.

Language selection

As of May 2017, the languages that can be used with AWS Lambda are C #, Node.js (JavaScript), Java, and Python. In addition, the difference in version is not included in the options because it is recognized that the difference in language specifications is not so big.

The selection criteria are based on development productivity and ease of deployment. (In my opinion)

--First from the story of deployment

For C # and Java, zip the binary file (there is also a jar for Java) and upload it or put it in S3. In addition to the above, Node.js and Python can be edited directly from the management console. (It can be done because the language is an interprinter) However, if both Node.js and Python are modularized (file division), they will be zipped and uploaded.

I think that Node.js and Python are superior because it is easy to check the operation of the coded one immediately.

For me as a Java shop, asynchronous processing of Node.js cost a lot of learning. As soon as I started coding, I got stuck in callback hell and couldn't write business logic. It was very stressful because I couldn't simply write the simple process of returning the processing result in the function as a return value and continuing the processing of the caller.

So after using it for a month, Python is a good language for productivity and deployment.

Integrated Development Environment (IDE)

For me as a Java shop, I chose Eclipse

There is a package that introduces Plugin for Python on the Pleiades site that I am always indebted to, so I will use this. If you use the IDE, it is convenient that you can check the syntax even in the interprinter language and execute the program from the IDE.

You can also use the AWS Toolkit for Eclipse with Eclipse. CodeCommit can also be used from Eclipse's EGit, so you can save the trouble of preparing other tools.

Local execution environment

If you use it within the free AWS framework, you don't have to worry about it. I think you can also create a local execution environment and develop it without hesitation.

The following work is required to build the environment.

~~-Install Pleiades (for Python) ~~ ~~ ⇒ Install from here (http://mergedoc.osdn.jp/) ~~

~~-Introduction of AWS Toolkit for Eclipse ~~ ~~ ⇒ Reference (http://docs.aws.amazon.com/ja_jp/toolkit-for-eclipse/v1/user-guide/setup-install.html) ~~

Correct the above installation procedure of Eclipse and AWS Toolkit for Eclipse. If you use Pleiades customized for Python, you cannot install the main plugins of AWS Toolkit for Eclipse because there is no JDT (Eclipse Java development tools).

Here are the steps I actually took:

--Pleiades (for Eclipse 4.6 Neon 3 Java) installation ⇒Install from here (http://mergedoc.osdn.jp/)

--Introduction of AWS Toolkit for Eclipse ⇒Reference (http://docs.aws.amazon.com/ja_jp/toolkit-for-eclipse/v1/user-guide/setup-install.html)

--Pydev installation ⇒ Install Pydev from Eclipse Marketplace --Python 3.6 installation for Windows ⇒ Install from here (https://www.python.org/) --Install boto3 with pip ⇒Use pip.exe under the Python installation directory. Execute the following command from the DOS prompt

```
>pip install boto3
```

The installed boto3 is passed through Path so that it can be referenced at runtime. python path.png

--Introduction of DynamoDB Local Select "Window"-> "Settings" from Eclipse with AWS Toolkit for Eclipse installed Install from DynamoDB local test tool dynamodb local.png

As a caveat, DynamoDB Local seems to have a fixed region of us-east-1, so it is necessary to match the dynamodb connection destination on the program side.

You can understand that us-east-1 is fixed because Region is added to the file name when you create a table. us-east-1.png

So ~~, modify the source code for the local environment. ~~ ~~ * This is a problem when I put it in the AWS environment, so I want to do something about it. ~~ This was solved by using environment variables. (Thank you for your comment, wukann)

env.py


#File that describes environment-dependent settings
import os
import boto3

if os.environ.get('EXEC_ENV') == 'TEST':
    #Change the port for the connection for DynamoDB Local
    print('Environment is TEST')
    dynamodb = boto3.resource('dynamodb', region_name='us-east-1', endpoint_url="http://localhost:8001")
else:
    #Connection when running on AWS
    dynamodb = boto3.resource('dynamodb')

After that, we will create a driver to run the AWS Lambda Function, but since the input from API Gateway will be in JSON format, write a driver that passes the input parameters in JSON format.

handler driver



#In the environment variable to return the DynamoDB connection'TEST'The set
import os
os.environ['EXEC_ENV'] = 'TEST'

#Tested function
from xxx_function import xxx_function_handler

if __name__ == '__main__':
    event = {
        "body": "{\"param1\":\"aaaa\",\"param2\":\"bbbb\",\"param3\":\"cccc\"}"
    }
    result = xxx_function_handler(event ,None)

    logger.debug('result{}'.format(result))

This completes the development environment and the execution environment where Unit tests can be performed locally.

Recommended Posts

AWS Lambda Development My Best Practices
Self-style Raspeye Remote Development Settings Best Practices
Lambda function deploy best practices with CircleCI + Lamvery
My best here document
Personal best practices for VS Code-fronted Python development environments
Try AWS Lambda Destinations
[Python] Scraping in AWS Lambda
The simplest AWS Lambda implementation
Learn best practices from cookiecutter-django
__version__ traps and best practices
Web scraping using AWS lambda
Best practice for logging in JSON format on AWS Lambda / Python
I stopped my instance at a specific time using AWS Lambda