Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SAM CLI Build Not Recognizing requirements.txt File #6962

Open
Mason-Stahl opened this issue Apr 18, 2024 · 3 comments
Open

SAM CLI Build Not Recognizing requirements.txt File #6962

Mason-Stahl opened this issue Apr 18, 2024 · 3 comments
Labels

Comments

@Mason-Stahl
Copy link

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.

@Mason-Stahl Mason-Stahl added the stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. label Apr 18, 2024
@hnnasit
Copy link
Contributor

hnnasit commented Apr 18, 2024

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.

@hnnasit hnnasit added type/question area/build sam build command and removed stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. labels Apr 18, 2024
@Mason-Stahl
Copy link
Author

Mason-Stahl commented Apr 19, 2024

@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
Copy link
Contributor

hnnasit commented Apr 19, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants