Template file is readed in multiple times
- Dominant language
- Python
- Stars
- 6.7k
- Forks
- 1.2k
- Avg merge
- 1d 10h
- Merged PRs (30d)
- 52
Description
### Description:
Template file is loaded multiple times.
This problem is not serious but may have a performance impact.
### Steps to reproduce:
1. apply this diff
```diff
diff --git a/samcli/commands/_utils/template.py b/samcli/commands/_utils/template.py
index 61b1c497..ff52d4c5 100644
--- a/samcli/commands/_utils/template.py
+++ b/samcli/commands/_utils/template.py
@@ -46,6 +46,8 @@ def get_template_data(template_file):
if not pathlib.Path(template_file).exists():
raise TemplateNotFoundException("Template file not found at {}".format(template_file))
+ print("-"*10 + " Reading template file: {}".format(template_file))
with open(template_file, "r", encoding="utf-8") as fp:
try:
return yaml_parse(fp.read())
```
2. save this file as a `sample.yml`
```yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: python3.8
Events:
ApiEvent:
Type: Api
Properties:
Path: /hello
Method: GET
InlineCode: |
def handler(event, context):
return {
'statusCode': 200,
'body': 'Hello World!'
}
Outputs:
HelloWorldAPI:
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
```
3. deploy that template via sam
```
$ samdev deploy -t sample.yml --stack-name dev-sam-sample --capabilities CAPABILITY_IAM
```
4. you can see multiple log that indicates load template multiple times.
```
$ samdev deploy -t sample.yml --stack-name dev-sam-sample --capabilities CAPABILITY_IAM --debug
---------- Reading template file: /home/conao/dev/forks/aws-sam-cli/sample.yml
---------- Reading template file: /home/conao/dev/forks/aws-sam-cli/sample.yml
---------- Reading template file: /home/conao/dev/forks/aws-sam-cli/sample.yml
---------- Reading template file: /home/conao/dev/forks/aws-sam-cli/sample.yml
2021-11-05 00:23:40,100 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2021-11-05 00:23:40,100 | Using config file: samconfig.toml, config environment: default
2021-11-05 00:23:40,100 | Expand command line arguments to:
2021-11-05 00:23:40,101 | --template_file=/home/conao/dev/forks/aws-sam-cli/sample.yml --stack_name=dev-sam-sample --capabilities=('CAPABILITY_IAM',) --fail_on_empty_changeset
Deploying with following values
===============================
Stack name : dev-sam-sample
Region : None
Confirm changeset : False
Disable rollback : False
Deployment s3 bucket : None
Capabilities : ["CAPABILITY_IAM"]
Parameter overrides : {}
Signing Profiles : {}
Initiating deployment
=====================
---------- Reading template file: /tmp/tmpzlyqfig1
2021-11-05 00:23:40,357 | No Parameters detected in the template
2021-11-05 00:23:40,376 | 2 stacks found in the template
2021-11-05 00:23:40,376 | No Parameters detected in the template
2021-11-05 00:23:40,392 | 2 resources found in the stack
2021-11-05 00:23:40,392 | No Parameters detected in the template
2021-11-05 00:23:40,408 | Found Serverless function with name='HelloWorldFunction' and InlineCode
2021-11-05 00:23:40,408 | No Parameters detected in the template
2021-11-05 00:23:40,424 | Detected Inline Swagger definition
2021-11-05 00:23:40,424 | Auth checks done on swagger are not exhaustive!
HelloWorldFunction may not have authorization defined.
Waiting for changeset to be created..
2021-11-05 00:23:46,236 | Sending Telemetry: {'metrics': [{'commandRun': {'requestId': '773d4d98-dcf8-4ed1-a876-a983f71f83de', 'installationId': '9f216b6c-37e0-4f2d-a53c-6a0b984cac79', 'sessionId': 'ebb91497-6b8c-40c7-a0d8-835bf9f9a04f', 'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.8.9', 'samcliVersion': '123.456.789', 'awsProfileProvided': False, 'debugFlagProvided': True, 'region': '', 'commandName': 'samdev deploy', 'duration': 6136, 'exitReason': 'ChangeEmptyError', 'exitCode': 1}}]}
2021-11-05 00:23:46,774 | HTTPSConnectionPool(host='aws-serverless-tools-telemetry.us-west-2.amazonaws.com', port=443): Read timed out. (read timeout=0.1)
Error: No changes to deploy. Stack dev-sam-sample is up to date
```
### Observed result:
As you can see, the template file is loaded 4 times.
### Expected result:
The template file is loaded one time and if you want to use the contents in another place, get from memory instead of reading the filesystem.
### Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
1. OS: Linux
2. `sam --version`:
```
$ samdev --version
SAM CLI, version 1.35.0
```
3. AWS region:
```
ap-northeast-1
```
`Add --debug flag to command you are running`
Contributor guide
Assessment
This issue has not been assessed yet.