aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

Nested Application from the Local File System build does not work properly

Open lucasam opened this issue 7 years ago • 27 comments

Nested Application from the Local File System build does not work properly

Issue description

According the documentation https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-template-nested-applications.html

The AWS::Serverless::Application can point to another template file (check Defining a Nested Application from the Local File System)

if you trigger the commands sam build; sam package, sam deploy the result of the nested application is a broken zip file.

Scenario

/ - javaproject
  | - template.yaml
  | - pom.xml
  | - src/main/java...
/ - template.yaml  (parent)

Parent template references javaproject/template.yaml as AWS::Serverless::Application`

Resources:
  AvatarApplication:
    Type: AWS::Serverless::Application
    Properties:
      Location: javaproject/template.yaml

The sequence sam build; sam package; sam deploy when triggered from parent directory completes. But the zipfile for the function under the nested application is wrongly built.

Let's check step by step using the sample code https://github.com/lucasam/sambuildissue2

Cloning sample code

git clone https://github.com/lucasam/sambuildissue2
cd sambuildissue2

sam build


lucasam$ .sam build --debug 
Using SAM Template at /Users/lucasam/Documents/src/sambuildissue2/template.yaml
Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
'build' command is called
No Parameters detected in the template
1 resources found in the template

Build Succeeded

Built Artifacts  : .aws-sam/build
Built Template   : .aws-sam/build/template.yaml

Commands you can use next
=========================
[*] Invoke Function: sam local invoke
[*] Package: sam package --s3-bucket <yourbucket>
    
Sending Telemetry: {'metrics': [{'commandRun': {'awsProfileProvided': False, 'debugFlagProvided': True, 'region': '', 'commandName': 'sam build', 'duration': 21, 'exitReason': 'success', 'exitCode': 0, 'requestId': '7178268e-8384-4ad2-a044-cfd938769ca7', 'installationId': '893c5aa8-d107-4b3b-8f82-e7cb5c9ce2fe', 'sessionId': '907647c9-698e-428a-a1ca-d86ca056821e', 'executionEnvironment': 'CLI', 'pyversion': '3.7.4', 'samcliVersion': '0.22.0'}}]}
HTTPSConnectionPool(host='aws-serverless-tools-telemetry.us-west-2.amazonaws.com', port=443): Read timed out. (read timeout=0.1)

sam package

lucasam$ sam package --output-template-file packaged.yaml  --s3-bucket $BUCKET 
Uploading to 41c665fe10f20b0417eb7f684d7a8072.template  791 / 791.0  (100.00%)
Successfully packaged artifacts and wrote output template to file packaged.yaml.
Execute the following command to deploy the packaged template
aws cloudformation deploy --template-file /Users/lucasam/Documents/src/sambuildissue2/packaged.yaml --stack-name <YOUR STACK NAME>

sam deploy

lucasam$ sam deploy  --template-file packaged.yaml  --stack-name sam-bug  --capabilities CAPABILITY_IAM CAPABILITY_AUTO_EXPAND \
    --region us-east-1
Waiting for changeset to be created..
Waiting for stack create/update to complete
Successfully created/updated stack - sam-bug

Everything looks fine so far. BUT

If you inspect the nested application you will find

f01898820a1c:x2 lucasam$ aws s3 cp s3://<BUCKET>/261b18ebec06a0f13ad2709228e60cca . 
download: s3://<BUCKET>/261b18ebec06a0f13ad2709228e60cca to ./261b18ebec06a0f13ad2709228e60cca
f01898820a1c:x2 lucasam$ unzip 261b18ebec06a0f13ad2709228e60cca
Archive:  261b18ebec06a0f13ad2709228e60cca
  inflating: template.yaml           
  inflating: sam-issue2.iml          
  inflating: pom.xml                 
  inflating: Readme.md               
  inflating: License.txt             
  inflating: src/main/java/com/test/FunctionOne.java  

Issue

  • Observe that the zip file representing the nested application is just a zip of the Nested application folder.
  • Expected. SAM should interpret template.yaml of the nested application and build it properly.

Workaround

  • Build use sam build and sam package at the nested application. Then point at parent template the packaged.yaml instead of template.yaml

lucasam avatar Oct 17 '19 15:10 lucasam

@lucasam Thanks for the issue. We currently do not support doing nested stack building/packaging/publishing of templates. This is very similar to #1443.

Marking as a feature request.

jfuss avatar Oct 17 '19 15:10 jfuss

@jfuss How can we update the documentation? Because there is a particular session that states exactly this scenario. And the cli completes successfully. It even generates a zip file with the 'unbuild' app. If we claim this is not supported i have 2 suggestions.

  • Update the documentation instructing users to point to an 'already packaged' sam application -
  • Fail build when the parent application is pointing to a sam file that still needs transformation. The fact that sam cli take action and zip the folder is extremely misleading

lucasam avatar Oct 17 '19 15:10 lucasam

@lucasam Which part(s) of the docs leads you to this? Skimming through, I can't find where we have claimed this to work. You can also suggest updates by submitting the changes to https://github.com/awsdocs/aws-sam-developer-guide

jfuss avatar Oct 17 '19 15:10 jfuss

we really need support for nested stacks :-( Is there an idea if that will come?

marcschroeter avatar Nov 19 '19 14:11 marcschroeter

Hey guys,

@jfuss Do you already have some news about this request? I think this is must have feature since I don't see any advantage of sam over cloudformation without this being supported.

UPDATE: I also created my own small shell script that just executes sam build --build-dir <BUILD_DIR> --template <TEMPLATE_PATH> before executing sam build on the top level of the application. This workaround is okay for me.

henkesde avatar Nov 19 '19 17:11 henkesde

+1 for this feature

jeswanthamazon avatar Jan 20 '20 19:01 jeswanthamazon

Hi,

I have followed the workaround above for a python nested application. However, it is still does not work.

I have the following folder structure

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

I followed the instructions above to build the nested app apps/account using sam build followed by sam package. The results was then a new packaged.yaml and the .aws-sam/ folder with the following structure

apps/account/.aws-sam/
└── build
    ├── GetAccountInfo
    │   ├── __init__.py
    │   ├── blinker
    │   ├── blinker-1.4.dist-info
    │   ├── boto3
    │   ├── boto3-1.11.13.dist-info
    │   ├── botocore
    │   ├── botocore-1.14.13.dist-info
    │   ├── dateutil
    │   ├── docutils
    │   ├── docutils-0.15.2.data
    │   ├── docutils-0.15.2.dist-info
    │   ├── dynamorm
    │   ├── dynamorm-0.10.0.dist-info
    │   ├── endpoints.py
    │   ├── jmespath
    │   ├── jmespath-0.9.4.data
    │   ├── jmespath-0.9.4.dist-info
    │   ├── marshmallow
    │   ├── marshmallow-3.4.0.dist-info
    │   ├── models.py
    │   ├── python_dateutil-2.8.1.dist-info
    │   ├── requirements.txt
    │   ├── s3transfer
    │   ├── s3transfer-0.3.3.dist-info
    │   ├── schema.yaml
    │   ├── six-1.14.0.dist-info
    │   ├── six.py
    │   ├── template.yaml
    │   ├── urllib3
    │   └── urllib3-1.25.8.dist-info
    ├── UpdateAccountInfo
    │   ├── __init__.py
    │   ├── blinker
    │   ├── blinker-1.4.dist-info
    │   ├── boto3
    │   ├── boto3-1.11.13.dist-info
    │   ├── botocore
    │   ├── botocore-1.14.13.dist-info
    │   ├── dateutil
    │   ├── docutils
    │   ├── docutils-0.15.2.data
    │   ├── docutils-0.15.2.dist-info
    │   ├── dynamorm
    │   ├── dynamorm-0.10.0.dist-info
    │   ├── endpoints.py
    │   ├── jmespath
    │   ├── jmespath-0.9.4.data
    │   ├── jmespath-0.9.4.dist-info
    │   ├── marshmallow
    │   ├── marshmallow-3.4.0.dist-info
    │   ├── models.py
    │   ├── python_dateutil-2.8.1.dist-info
    │   ├── requirements.txt
    │   ├── s3transfer
    │   ├── s3transfer-0.3.3.dist-info
    │   ├── schema.yaml
    │   ├── six-1.14.0.dist-info
    │   ├── six.py
    │   ├── template.yaml
    │   ├── urllib3
    │   └── urllib3-1.25.8.dist-info
    └── template.yaml

However, I get the error Unable to import module 'endpoints': No module named 'dynamorm'.

Am I missing something?

Thanks for the help!

inception-cto avatar Feb 10 '20 02:02 inception-cto

+1 on fixing this issue.

ngander-amfam avatar Feb 26 '20 21:02 ngander-amfam

+1 - Nested applications are a common pattern!

jessedoyle avatar Apr 01 '20 23:04 jessedoyle

+1

codergeorgej avatar May 29 '20 03:05 codergeorgej

+1

eallais avatar Jun 05 '20 13:06 eallais

@jfuss https://github.com/awslabs/aws-sam-cli/issues/1470#issuecomment-543225894, any timeline on when we can get this feature?

codergeorgej avatar Jun 08 '20 00:06 codergeorgej

Without this feature I'm not sure how I'm going to prevent my template.yaml file from becoming 10,000+ lines without resorting to some kind of YAML or build tool.

divmgl avatar Jun 26 '20 06:06 divmgl

It's a must have. We need to use a bunch of nasty workarounds to fix this problem. +1 to implement this feature.

liszi avatar Jul 16 '20 10:07 liszi

+1

cstolcser avatar Jul 16 '20 11:07 cstolcser

+1

asifpy avatar Jul 29 '20 21:07 asifpy

+1

klembee avatar Aug 26 '20 18:08 klembee

+1

dtelaroli avatar Aug 31 '20 17:08 dtelaroli

+1

mdkotafutami avatar Sep 03 '20 03:09 mdkotafutami

+1

wdalmut avatar Oct 31 '20 11:10 wdalmut