youtype/mypy_boto3_builder

Unable to deploy code using explicit type imports to aws lambda

Closed this issue · 3 comments

Describe the bug
I have recently started using boto3-stubs and importing types explicitly in order to add typechecks in my code. however, when deploying code to a lambda, i'm unable to run since the package mypy_boto3_sqs cannot be found.

{
  "errorMessage": "Unable to import module 'e2e.get_queues_to_purge': No module named 'mypy_boto3_sqs'",
  "errorType": "Runtime.ImportModuleError",
  "requestId": "d74903fc-fbdf-4c49-8153-71d06e56926d",
  "stackTrace": []
}

To Reproduce
Steps to reproduce the behavior:

  1. pipenv install boto3-stubs[sqs]
  2. sls deploy
import boto3
from mypy_boto3_sqs.type_defs import SendMessageBatchRequestEntryTypeDef, SendMessageBatchResultTypeDef, SendMessageResultTypeDef, ReceiveMessageResultTypeDef, DeleteMessageBatchRequestEntryTypeDef
from mypy_boto3_sqs.client import SQSClient

Pipfile

[packages] # tried this both in dev-packages and normal packages
boto3-stubs = {version = "==1.26.66", extras = ["dynamodb", "ecs", "s3", "sqs", "stepfunctions"]}

[dev-packages]
boto3 = "==1.26.66"

Actual output

fails to run, since the python code cannot find the mypy_boto3_sqs package to import from

Expected output
runs and imports packages successfully

Additional context
the code does deploy to the lambda, but then fails to run. there's a bit more context around how our serverless is setup, but i'm hoping someone can point me in the right direction to be able to get these packages imported correctly. thanks!

vemel commented

Hello!

It is highly recommended to add boto3-stubs as a dev dependency, because you do not really need it in production.

[packages]
boto3 = "==1.26.66"

[dev-packages]
boto3-stubs = {version = "==1.26.66", extras = ["dynamodb", "ecs", "s3", "sqs", "stepfunctions"]}

Use TYPE_CHECKING to avoid import errors:

import boto3
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from mypy_boto3_sqs.type_defs import SendMessageBatchRequestEntryTypeDef, SendMessageBatchResultTypeDef, SendMessageResultTypeDef, ReceiveMessageResultTypeDef, DeleteMessageBatchRequestEntryTypeDef
    from mypy_boto3_sqs.client import SQSClient

Alternatively, you can add boto3-stubs and all subpackages that you need to a Lambda layer. This way you can avoid code changes. But I strongly recommend to use it as a dev package.

Please let me know if it helps.

ah, interesting. thank you for the quick response!

we initially had the stubs in our dev packages, so we can move them back. i'll try that out quickly and get back to you, thanks!

that worked! i also had to replace top-level type defs with a string, per the docs here.

thank you for the suggestion, i learned something new today!