aws / aws-sam-cli

CLI tool to build, test, debug, and deploy Serverless applications using AWS SAM
https://aws.amazon.com/serverless/sam/
Apache License 2.0
6.5k stars 1.17k forks source link

SAM CLI Build Not Recognizing requirements.txt File #6962

Closed Mason-Stahl closed 3 months ago

Mason-Stahl commented 5 months ago

When running sam build, the CLI does not recognize the presence of the requirements.txt file and continues the build without dependencies, even though the file exists at the specified location.

I am trying to install dependencies so i can import for my python file at DiscordBots\recommendations\src\file.py

"C:\Users\turtl\root_lambda\DiscordBots\recommendations\src\requirements.txt" requirements.txt contains exclusively :

aiohttp
asyncpg

Within root_lambda\template.yaml :

Resources:
  RefreshSpotifyTokens:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: DiscordBots/
    Metadata:
      BuildMethod: python3.12 
      BuildProperties:
        RequirementsFile: recommendations/src/requirements.txt

I run the command:

PS C:\Users\turtl\root_lambda> sam build Starting Build use cache Building codeuri: C:\Users\turtl\root_lambda\DiscordBots runtime: python3.12 metadata: {'BuildMethod': 'python3.12', 'BuildProperties': {'RequirementsFile': 'C:/Users/turtl/root_lambda/DiscordBots/recommendations/src/requirements.txt'}} architecture: x86_64 functions: RefreshSpotifyTokens requirements.txt file not found. Continuing the build without dependencies.

OS: Windows SAM CLI Version: (SAM CLI, version 1.115.0) Python Version: (Python 3.12.0)

Troubleshooting steps already taken: Ensured requirements.txt is in the correct location. Verified file encoding is UTF-8 without BOM. Recreated requirements.txt to rule out hidden/special characters. Checked for correct file naming without additional extensions or whitespace. Ran sam build with and without the virtual environment activated. Tried both relative and absolute paths in template.yaml's BuildProperties. Ran sam build with the --debug flag for detailed output.

hnnasit commented 5 months ago

Hi @Mason-Stahl, thanks for reporting the issue. Can you try changing the CodeUri value to DiscordBots/recommendations/src and check if this works.

Resources:
  RefreshSpotifyTokens:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: DiscordBots/recommendations/src/

This is assuming the Lambda handler is defined in DiscordBots\recommendations\src\file.py.

RequirementsFile does not seem like a valid value in Metadata to me. The Metadata field is useful when you want to run a custom build of your own. More details can be found here. Let me know if that makes sense or have questions.

Mason-Stahl commented 4 months ago

@hnnasit Thank you so much! That fixed it!

I'm new to this kind of stuff and wondering what the best practice would be if file.py has some imports in other parts of the code like in DiscordBots/my_package? Now that URI points specifically to recommendations/src it seems like I have two options to get the higher level my_package files into recommendations/src, i can use symlink or a custom build script. Is there another better option?

My end goal, I believe, is for DiscordBots folder to be in an ec2 instance, with lambda scheduling the execution of certain commands/files within the recommendations folder.

if it helps at all one of the imports is get_connection_pool() with my_package/src/db/async_db.py having a function to initiate a connection pool that is checked when accessing the database for any necessary DiscordBots command

hnnasit commented 4 months ago

The options you mentioned would work. Another option is to use CodeUri: DiscordBots/, move the requirements.txt to the root folder and specify the handler Handler: recommendations/src/app.lambda_handler. Take a look at the example below:

.
├── DiscordBots
│   ├── __init__.py
│   ├── my_package
│   │   ├── __init__.py
│   │   └── src
│   │       └── db
│   │           ├── __init__.py
│   │           └── async_db.py
│   ├── recommendations
│   │   ├── __init__.py
│   │   └── src
│   │       ├── __init__.py
│   │       └── app.py
│   └── requirements.txt
├── README.md
├── __init__.py
├── samconfig.toml
└── template.yaml

app.py

import json
from my_package.src.db.async_db import get_connection_pool

def lambda_handler(event, context):
    """
    """
    get_connection_pool()
    return {
        "statusCode": 200,
        "body": json.dumps({
            "message": "hello world",
        }),
    }

async_db.py

def get_connection_pool():
    print("DB connection")

template.yaml

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  sam-app-6962

  Sample SAM Template for sam-app-6962

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 3
    MemorySize: 128

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: DiscordBots/
      Handler: recommendations/src/app.lambda_handler
      Runtime: python3.8
      Architectures:
        - x86_64
      Events:
        HelloWorld:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /hello
            Method: get

If you want to have multiple lambda functions with shared files, you could also checkout Lambda layers. Let me know if that helps or if you have more questions.

hnnasit commented 3 months ago

I am closing this issue due to inactivity. Please open a new issue if you still have questions.

github-actions[bot] commented 3 months ago

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.