Skip to content

sam build: Optional flag to preserve CodeUri parent directory #3285

Description

@heitorlessa

Describe your idea/feature/enhancement

For Python applications, it'd be ideal to have a flag in sam build to preserve the parent folder in CodeUri when building and copying artifacts to $ARTIFACTS_DIR.

For example, I wish SAM CLI could allow Python developers to keep their layout for modules like we do elsewhere:

Ideal

.
├── loyalty
│   └── __init__.py
│   └── aggregate
│       ├── __init__.py
│       └── app.py
│   └── get
│       ├── __init__.py
│       └── app.py
│   └── ingest
│       ├── __init__.py
│       └── app.py
│   └── shared
│       ├── __init__.py
│       ├── constants.py
│       ├── functions.py
│       ├── models.py
│       └── storage.py
├── template.yaml
└── tests
    ├── conftest.py
    ├── events
    │   ├── aggregate_insert_event.json
    │   ├── get_event.json
    │   └── ingest_event.json
    ├── storage
    │   ├── __init__.py
    │   └── test_dynamodb.py
    └── unit
        ├── __init__.py
        ├── test_aggregate.py
        ├── test_get_loyalty_points.py
        └── test_ingest.py

Template

...
  IngestFunc:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: !Sub ServerlessAirline-IngestLoyalty-${Stage}
      CodeUri: loyalty
      Handler: loyalty.ingest.app.lambda_handler

Import line as well as tests

from loyalty.shared.models import LoyaltyPoint, LoyaltyPointAggregate
from loyalty.shared.storage import FakeStorage, DynamoDBStorage
...

To workaround this issue, I have to create another folder src to get the intended result[1], but this creates other problems in PYTHONPATH which I need to adjust in my IDE and tools later. Alternatively, I could use BuildMethod: makefile in SAM, but then I to lose dedup functionality.

Steps to reproduce the issue:

  1. Use loyalty as CodeUri
  2. Run sam build

Observed result: loyalty parent folder will not be copied over but its contents

Expected result: A flag to ensure folder in CodeUri is preserved when building artifacts, e.g. --preserve-, or perhaps respect foldervsfolder/`.

Additional environment details (Ex: Windows, Mac, Amazon Linux etc) Mac

Implementation in question that deliberately removes the parent folder: https://github.com/aws/aws-lambda-builders/blob/41804217a18ff47cce5b62eff5cf884a7f81a0c5/aws_lambda_builders/utils.py#L16

Lambda Builders related issue: aws/aws-lambda-builders#283


[1] Current layout to ensure loyalty folder is copied over (CodeUri: src):

.
├── src
│   └── loyalty
│       ├── __init__.py
│       ├── aggregate
│       │   ├── __init__.py
│       │   └── app.py
│       ├── get
│       │   ├── __init__.py
│       │   └── app.py
│       ├── ingest
│       │   ├── __init__.py
│       │   └── app.py
│       └── shared
│           ├── __init__.py
│           ├── constants.py
│           ├── functions.py
│           ├── models.py
│           └── storage.py
├── template.yaml
└── tests
    ├── conftest.py
    ├── events
    │   ├── aggregate_insert_event.json
    │   ├── get_event.json
    │   └── ingest_event.json
    ├── storage
    │   ├── __init__.py
    │   └── test_dynamodb.py
    └── unit
        ├── __init__.py
        ├── test_aggregate.py
        ├── test_get_loyalty_points.py
        └── test_ingest.py

Proposal

A new CLI flag --preserve-code-uri, --preserve, etc.

Things to consider:

  1. Will this require any updates to the SAM Spec

Additional Details

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions