AWS SAM Nested Application in Python with Dynamorm

邮差的信 提交于 2020-06-16 11:09:45

问题


I am using AWS SAM to build a Serverless application. I followed the instruction to build a nested application.

My application structure is basically the following:

.
├── MAKEFILE
├── README.md
├── __init__.py
├── apps
│   ├── __init__.py
│   ├── account
│   │   ├── __init__.py
│   │   ├── endpoints.py
│   │   ├── models.py
│   │   ├── requirements.txt
│   │   └── template.yaml
├── samconfig.toml
└── template.yaml

The requirements.txt in the folder apps/account/ has the following python packages: boto3 marshmallow and dynamorm.

The sam build and sam deploy works fine and the lambda functions are deployed correctly. However, I receive an error when calling the lambda function. The logs show the following error Unable to import module 'endpoints': No module named 'dynamorm'.

Here are excerpts from my code:

endpoints.py

import json
import boto3

from models import Account

print('Loading function')

def account_info(event, context):

    apiKey = event["requestContext"]["identity"]["apiKeyId"]

    account_info = Account.get(id= apiKey)

    return {
        "statusCode": 200,
        "body": json.dumps(account_info)
    }

models.py

import datetime

from dynamorm import DynaModel, GlobalIndex, ProjectAll

from marshmallow import Schema, fields, validate, validates, ValidationError

class Account(DynaModel):
    # Define our DynamoDB properties
    class Table:
        name = 'XXXXXXXXXX'
        hash_key = 'id'
        read = 10
        write = 5

    class Schema:
        id = fields.String(required=True)
        name = fields.String()
        email = fields.String()
        phonenumber = fields.String()
        status = fields.String()

I am not sure what am I missing? Are there additional instructions to build a nested app in SAM?

Thank you so much for the help!


回答1:


According to https://github.com/awslabs/aws-sam-cli/issues/1213, this feature is not supported yet.

In my case, I did 'sam build' on every nested stacks and fix parent yaml template as following (use template.yaml generated by sam build command), then works. But just workaround and not nice way.

  XXX_APP:
    Type: AWS::Serverless::Application
    Properties:
      Location: nest_application/.aws-sam/build/template.yaml


来源:https://stackoverflow.com/questions/60141845/aws-sam-nested-application-in-python-with-dynamorm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!