Serverless is toolkit for deploying and operating serverless framework due to focus your application.
In this article, I would like to introduce how to build basic lambda API by Node.js/Python with Docker.
🚌 Installation
yarn global add serverless |
😀 Docker Config for Python
If you want to develop a function with Python, it is better to use Dockerfile, because it is easy to use pip library in lambda.
Create Dockerfile
for development:
FROM python:3.6 |
Also, create docker-compose.yml
:
version: '3.5' |
After then, create .env.docker
:
aws_access_key_id=YOUR_ACCESS_KEY_ID |
Please add your AWS Access information and do not commit the .env.docker
file in git repository.
🏀 Start the image for dev
docker-compose up -d |
🐠 Set AWS Access key
sls config credentials --provider aws --key $AWS_ACCESS_KEY_ID --secret $AWS_SECRET_ACCESS_KEY |
🐡 Create lambda function with Pytnon3 template
Create new service:
# Create service with Python 3.x |
Modify serverless.yml
configuration file:
(diff --no-index --unified=1 serverless.org.yml serverless.yml
):
@@ -25,3 +25,3 @@ provider: |
🐯 Confirm developing function
Enable to confirm your devloping function on local
# Call the function |
🗻 Deploy code to AWS
After development, you can deploy your code by one command:
sls deploy -v |
🎂 Confirm the deployed function
Enable to confirm your devloping function on local or you can call your API by curl
:
# Call the function |
🐞 Add pip libraries for lambda
If you want to use library, please install pip libraries as follows:
# In docker |
🎃 Remove deployed function
sls remove |
🍮 Configuration (serverless.yml)
Memory Size, Timeout, etc in AWS lambda
You can configure memory size and timeout for function in AWS lambda:
# https://serverless.com/framework/docs/providers/aws/guide/functions/ |
If you want to know more detail, please see https://serverless.com/framework/docs/providers/aws/guide/functions/ .
Including/excluding file or folder
Please configure including/excluding a file or a folder.
# you can add packaging information here |
If you want to know more detail, please see https://serverless.com/framework/docs/providers/aws/guide/packaging/
Cron(Scheduled job) configuration
Enable to configure cron/scheduled job.
# Cron (scheduled job) information |
If you want to know more detail, please see https://serverless.com/framework/docs/providers/aws/events/schedule/
😼 Impression
My impression for serverless is as follows:
- A deploy command (
serverless deploy
) is simple and useful to create the required IAM, set up CloudWatch logs, set up S3 for deployment, deploy, etc. - Cron (CloudWatch Events) needs just a line to YAML, there are a lot of useful configurations.
- I thought that it was easy for using Google Cloud Function to manage credential on GCP, I tried a little, but it was buggy. I think that serverless is useful in only AWS lambda.
- There are some problems in GCF and serverless. Also, GCF is beta yet.
- I found https://github.com/serverless/serverless-graphql . If it becomes better, it is a good option for GraphQL server.
🐮 [Appendix] Google Cloud function with Node.js
Supported Node.js version
Node version is 6.14.0, so please be careful: https://cloud.google.com/functions/docs/writing/ .
echo '6.14.0' > .nvmrc |
Quick Start
Create a project:
serverless create --template google-nodejs --path gcf-nodejs |
Install npm libraries:
cd gcf-nodejs |
Create your credential
https://serverless.com/framework/docs/providers/google/guide/credentials
Deploy your code
serverless deploy |
Confirmation
# Confirm your code by function |
More detail
If you want to know more detail, please see https://serverless.com/framework/docs/providers/google/guide/quick-start/ .
Concerns
- SLA is not guaranteed because it is in beta test.
- It is impossible to configure environment variable like AWS.
- There is no mechanism like cron, scheduled job.
- It is thought that the test with GCP is not done much.
🎉 Appendix
Manage permission for your Lambda function
If your function read/edit data in DynamoDB table, you can add these additional permission statements directly in your serverless.yml
:
provider: |
If you want to know more details, please see https://serverless.com/blog/abcs-of-iam-permissions/ .
Python / Load and delete a credential
Load and delete a credential data from environment variable in AWS lambda:
def load_credential(): |
🍄 References
- https://dev.classmethod.jp/cloud/aws/serverless-framework-with-python-3-6/
- https://qiita.com/bremen/items/7e9407349d74a6a62822
- https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html
🖥 Recommended VPS Service
VULTR provides high performance cloud compute environment for you.
Vultr has 15 data-centers strategically placed around the globe, you can use a VPS with 512 MB memory for just $ 2.5 / month ($ 0.004 / hour).
In addition, Vultr is up to 4 times faster than the competition, so please check it => Check Benchmark Results!!