34
loading...
This website collects cookies to deliver better user experience
package.json
(poetry.toml
) and package-lock.json
(poetry.lock
), Poetry maintains dependency tree, virtual environments, and also comes with a CLI. curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
for osx, linux; and for windows run (Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python -
in Powershell. More instructions herecurl -o- -L https://slss.io/install | bash
or npm install -g serverless
poetry new my-project # change project name to whatever you want
poetry add fastapi uvicorn[standard]
my-project/my-project
we create our app.py, a pretty basic one for the sake of this project. We write two endpoints for now as follows-from fastapi import FastAPI
import os
stage = os.environ.get('STAGE', 'dev')
app = FastAPI()
@app.get("/")
def index():
return {"Hello": "World"}
@app.get("/users/{user_id}")
def read_item(user_id: int):
return {"user_id": user_id}
uvicorn my-project.app:app
serverless.yml
in the root folder my-project
service: My-Project
package:
individually: true
provider:
name: aws
profile: ${opt:aws-profile, "default"}
region: "us-west-2"
stage: ${opt:stage, "dev"}
runtime: python3.8
plugins:
- serverless-offline
- serverless-python-requirements
custom:
pythonRequirements:
dockerizePip: true
usePoetry: true
functions:
app:
handler: my-project.app.handler #I will explain why we have handler here
environment:
STAGE: ${self:provider.stage}
events:
- http:
method: get
path: /
- http:
method: any
path: /{proxy+}
serverless-python-requirements
. It packages the python project that contains a requirements.txt
or a poetry/pipenv package. We point our handler to the app.py
. app.app
instead. This is because we have to wrap our ASGI app to adapt AWS Lambda and API Gateway. For that we use Mangum.poetry add mangum
app.py
from mangum import Mangum
handler = Mangum(app)
serverless-python-requirements
to it, since serverless is a node package.npm init --yes
npm install serverless-python-requiremnts
serverless plugin install -n serverless-lift
serverless.yml
which should look likeplugins:
- serverless-offline
- serverless-python-requirements
- serverless-lift
serverless.yml
we will have the following handled automatically:pay per req
and TTL enabled.constructs:
myTable:
type: database/dynamodb-single-table
functions:
app:
handler: my-project.app.handler
environment:
STAGE: ${self:provider.stage}
TABLE_NAME: ${construct:myTable.tableName}
events:
- http:
method: get
path: /
- http:
method: any
path: /{proxy+}
app.py
we can access this table name and don't have to worry about assigning the lambda an IAM role.@app.get("/users/user_id")
def read_item(user_id: int):
table_name = os.environ.get('TABLE_NAME', '')
table = boto3.resource("dynamodb", region_name='us-west-2').Table(table_name)
response = table.get_item(
Key={
'PK': user_id
}
)
return {"user_obj": response['Item']}
poetry add boto3
serverless offline --stage dev --noPrependStageInUrl
--noPrependStageInUrl
flag, it will run your server at localhost:3000/dev/{proxy}+
. If you to run it like that, make sure you include root_path='dev'
parameter in app=FastAPI()
to see the docsserverless deploy --stage dev #or prod
const res = spawnSync(
'poetry',
[
'export',
'--without-hashes',
'-f',
'requirements.txt',
'-o',
'requirements.txt',
'--with-credentials',
],
{
cwd: this.servicePath,
shell: true // <- we added this
}
);
Hi,
I'm on Windows10. Serverless environment:
Your Environment Information --------------------------- Operating System: win32 Node Version: 14.15.4 Framework Version: 2.42.0 Plugin Version: 5.1.2 SDK Version: 4.2.2 Components Version: 3.10.0
Version of plugin:
5.1.1
I'm using poetry and according to the documentation, this should work fine. From the doc:
If you include a pyproject.toml and have poetry installed instead of a requirements.txt this will use poetry export --without-hashes -f requirements.txt -o requirements.txt --with-credentials to generate them.
But I ran into this error when I tried to deploy:
PS > serverless deploy Serverless: Generating requirements.txt from pyproject.toml...
Error ---------------------------------------------------
Error: poetry not found! Install it according to the poetry docs. at ServerlessPythonRequirements.pyprojectTomlToRequirements (C:<path replaced>\node_modules\serverless-python-requirements\lib\poetry.js:34:13)
After some research I found this comment: https://stackoverflow.com/a/54515183/5759828 suggesting to use {shell:true}. As part of my testing I found that the output of spawnSync (line 17 in poetry.js) is:
error: Error: spawnSync poetry ENOENT
I then added shell:true to poetry.js like this:
const res = spawnSync(
'poetry',
[
'export',
'--without-hashes',
'-f',
'requirements.txt',
'-o',
'requirements.txt',
'--with-credentials',
],
{
cwd: this.servicePath,
shell: true <--- added this
}
);
and now it works fine.