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.51k stars 1.17k forks source link

Bug: hello-world in TS sam build generates warning about logical ID ServerlessRestApi #4290

Closed jric closed 1 year ago

jric commented 2 years ago

Description:

The out-of-the box template results in an ominous warning when running "sam build".

Steps to reproduce:

I followed the tutorial at https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-getting-started-hello-world.html but selected Node as the build environment and Typescript as the application template:

  1. sam init
  2. 1 - AWS Quick Start Templates
  3. 1 - Hello World Example
  4. N (most popular runtime and package type
  5. 10 - nodejs16.x
  6. 1 - Zip
  7. 2 - Hello World Example TypeScript
  8. N (X-Ray tracing)
  9. sam-app (name)
  10. cd sam-app
  11. sam build

Observed result:

Your template contains a resource with logical ID "ServerlessRestApi", which is a reserved logical ID in AWS SAM. It could result in unexpected behaviors and is not recommended.

Checking the template.yaml file, I see no resource defined as ServerlessRestApi, leading me to believe the warning is issued incorrectly. I do see a reference to ServerlessRestApi and a comment about it being an implicit resource created out of Events key under Serverless::Function.

Full --debug output included below.

Expected result:

No warnings.

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

  1. OS: Mac
  2. sam --version: SAM CLI, version 1.59.0
  3. AWS region: us-west-2

Full --debug output

sam build --debug

2022-10-09 14:09:43,190 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2022-10-09 14:09:43,190 | Using config file: samconfig.toml, config environment: default
2022-10-09 14:09:43,190 | Expand command line arguments to:
2022-10-09 14:09:43,190 | --template_file=/Users/jric/dev/tutorials/sam-app/template.yaml --build_dir=.aws-sam/build --cache_dir=.aws-sam/cache 
2022-10-09 14:09:43,805 | 'build' command is called
2022-10-09 14:09:43,812 | No Parameters detected in the template
2022-10-09 14:09:43,837 | There is no customer defined id or cdk path defined for resource HelloWorldFunction, so we will use the resource logical id as the resource id
2022-10-09 14:09:43,837 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use the resource logical id as the resource id
2022-10-09 14:09:43,838 | 0 stacks found in the template
2022-10-09 14:09:43,838 | No Parameters detected in the template
2022-10-09 14:09:43,852 | There is no customer defined id or cdk path defined for resource HelloWorldFunction, so we will use the resource logical id as the resource id
2022-10-09 14:09:43,852 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use the resource logical id as the resource id
2022-10-09 14:09:43,853 | 2 resources found in the stack 
2022-10-09 14:09:43,853 | Found Serverless function with name='HelloWorldFunction' and CodeUri='hello-world/'
2022-10-09 14:09:43,853 | --base-dir is not presented, adjusting uri hello-world/ relative to /Users/jric/dev/tutorials/sam-app/template.yaml
2022-10-09 14:09:43,861 | Your template contains a resource with logical ID "ServerlessRestApi", which is a reserved logical ID in AWS SAM. It could result in unexpected behaviors and is not recommended.
2022-10-09 14:09:43,861 | 2 resources found in the stack 
2022-10-09 14:09:43,861 | Found Serverless function with name='HelloWorldFunction' and CodeUri='hello-world/'
2022-10-09 14:09:43,861 | Found Serverless function with name='HelloWorldFunction' and CodeUri='hello-world/'
2022-10-09 14:09:43,862 | --base-dir is not presented, adjusting uri hello-world/ relative to /Users/jric/dev/tutorials/sam-app/template.yaml
2022-10-09 14:09:43,863 | Instantiating build definitions
2022-10-09 14:09:43,869 | Same function build definition found, adding function (Previous: BuildDefinition(nodejs16.x, /Users/jric/dev/tutorials/sam-app/hello-world, Zip, , bc9eff40-ccd6-4440-8e02-64422b846046, {'BuildMethod': 'esbuild', 'BuildProperties': {'Minify': True, 'Target': 'es2020', 'EntryPoints': ['app.ts']}}, {}, x86_64, []), Current: BuildDefinition(nodejs16.x, /Users/jric/dev/tutorials/sam-app/hello-world, Zip, , c1a132cb-8316-4ab7-9e0a-eaed816181ec, {'BuildMethod': 'esbuild', 'BuildProperties': {'Minify': True, 'Target': 'es2020', 'EntryPoints': ['app.ts']}}, {}, x86_64, []), Function: Function(function_id='HelloWorldFunction', name='HelloWorldFunction', functionname='HelloWorldFunction', runtime='nodejs16.x', memory=None, timeout=3, handler='app.lambdaHandler', imageuri=None, packagetype='Zip', imageconfig=None, codeuri='/Users/jric/dev/tutorials/sam-app/hello-world', environment=None, rolearn=None, layers=[], events={'HelloWorld': {'Type': 'Api', 'Properties': {'Path': '/hello', 'Method': 'get', 'RestApiId': 'ServerlessRestApi'}}}, metadata={'BuildMethod': 'esbuild', 'BuildProperties': {'Minify': True, 'Target': 'es2020', 'EntryPoints': ['app.ts']}, 'SamResourceId': 'HelloWorldFunction'}, inlinecode=None, codesign_config_arn=None, architectures=['x86_64'], function_url_config=None, stack_path=''))
2022-10-09 14:09:43,870 | Building codeuri: /Users/jric/dev/tutorials/sam-app/hello-world runtime: nodejs16.x metadata: {'BuildMethod': 'esbuild', 'BuildProperties': {'Minify': True, 'Target': 'es2020', 'EntryPoints': ['app.ts']}} architecture: x86_64 functions: HelloWorldFunction
2022-10-09 14:09:43,871 | Building to following folder /Users/jric/dev/tutorials/sam-app/.aws-sam/build/HelloWorldFunction
2022-10-09 14:09:43,872 | Loading workflow module 'aws_lambda_builders.workflows'
2022-10-09 14:09:43,880 | Registering workflow 'PythonPipBuilder' with capability 'Capability(language='python', dependency_manager='pip', application_framework=None)'
2022-10-09 14:09:43,884 | Registering workflow 'NodejsNpmBuilder' with capability 'Capability(language='nodejs', dependency_manager='npm', application_framework=None)'
2022-10-09 14:09:43,888 | Registering workflow 'RubyBundlerBuilder' with capability 'Capability(language='ruby', dependency_manager='bundler', application_framework=None)'
2022-10-09 14:09:43,891 | Registering workflow 'GoModulesBuilder' with capability 'Capability(language='go', dependency_manager='modules', application_framework=None)'
2022-10-09 14:09:43,897 | Registering workflow 'JavaGradleWorkflow' with capability 'Capability(language='java', dependency_manager='gradle', application_framework=None)'
2022-10-09 14:09:43,900 | Registering workflow 'JavaMavenWorkflow' with capability 'Capability(language='java', dependency_manager='maven', application_framework=None)'
2022-10-09 14:09:43,905 | Registering workflow 'DotnetCliPackageBuilder' with capability 'Capability(language='dotnet', dependency_manager='cli-package', application_framework=None)'
2022-10-09 14:09:43,911 | Registering workflow 'CustomMakeBuilder' with capability 'Capability(language='provided', dependency_manager=None, application_framework=None)'
2022-10-09 14:09:43,915 | Registering workflow 'NodejsNpmEsbuildBuilder' with capability 'Capability(language='nodejs', dependency_manager='npm-esbuild', application_framework=None)'
2022-10-09 14:09:43,916 | Found workflow 'NodejsNpmEsbuildBuilder' to support capabilities 'Capability(language='nodejs', dependency_manager='npm-esbuild', application_framework=None)'
2022-10-09 14:09:43,916 | executing NPM: ['npm', 'bin']
2022-10-09 14:09:44,510 | Lambda Builders found the following esbuild properties:
{"minify": true, "target": "es2020", "entry_points": ["app.ts"]}
2022-10-09 14:09:44,513 | Running workflow 'NodejsNpmEsbuildBuilder'
2022-10-09 14:09:44,513 | Running NodejsNpmEsbuildBuilder:CopySource
2022-10-09 14:09:44,513 | Copying source file (/Users/jric/dev/tutorials/sam-app/hello-world/.npmignore) to destination (/var/folders/rf/0_rbkb7d4dl7_ml7c0128lrh0000gp/T/tmp66tn_06t/.npmignore)
2022-10-09 14:09:44,514 | Copying source file (/Users/jric/dev/tutorials/sam-app/hello-world/app.ts) to destination (/var/folders/rf/0_rbkb7d4dl7_ml7c0128lrh0000gp/T/tmp66tn_06t/app.ts)
2022-10-09 14:09:44,515 | Creating target folders at /var/folders/rf/0_rbkb7d4dl7_ml7c0128lrh0000gp/T/tmp66tn_06t/tests
2022-10-09 14:09:44,515 | Copying directory metadata from source (/Users/jric/dev/tutorials/sam-app/hello-world/tests) to destination (/var/folders/rf/0_rbkb7d4dl7_ml7c0128lrh0000gp/T/tmp66tn_06t/tests)
2022-10-09 14:09:44,516 | Creating target folders at /var/folders/rf/0_rbkb7d4dl7_ml7c0128lrh0000gp/T/tmp66tn_06t/tests/unit
2022-10-09 14:09:44,516 | Copying directory metadata from source (/Users/jric/dev/tutorials/sam-app/hello-world/tests/unit) to destination (/var/folders/rf/0_rbkb7d4dl7_ml7c0128lrh0000gp/T/tmp66tn_06t/tests/unit)
2022-10-09 14:09:44,516 | Copying source file (/Users/jric/dev/tutorials/sam-app/hello-world/tests/unit/test-handler.test.ts) to destination (/var/folders/rf/0_rbkb7d4dl7_ml7c0128lrh0000gp/T/tmp66tn_06t/tests/unit/test-handler.test.ts)
2022-10-09 14:09:44,517 | Copying source file (/Users/jric/dev/tutorials/sam-app/hello-world/package.json) to destination (/var/folders/rf/0_rbkb7d4dl7_ml7c0128lrh0000gp/T/tmp66tn_06t/package.json)
2022-10-09 14:09:44,518 | Copying source file (/Users/jric/dev/tutorials/sam-app/hello-world/.prettierrc.js) to destination (/var/folders/rf/0_rbkb7d4dl7_ml7c0128lrh0000gp/T/tmp66tn_06t/.prettierrc.js)
2022-10-09 14:09:44,519 | Copying source file (/Users/jric/dev/tutorials/sam-app/hello-world/.eslintrc.js) to destination (/var/folders/rf/0_rbkb7d4dl7_ml7c0128lrh0000gp/T/tmp66tn_06t/.eslintrc.js)
2022-10-09 14:09:44,520 | Copying source file (/Users/jric/dev/tutorials/sam-app/hello-world/tsconfig.json) to destination (/var/folders/rf/0_rbkb7d4dl7_ml7c0128lrh0000gp/T/tmp66tn_06t/tsconfig.json)
2022-10-09 14:09:44,521 | Copying source file (/Users/jric/dev/tutorials/sam-app/hello-world/.eslintignore) to destination (/var/folders/rf/0_rbkb7d4dl7_ml7c0128lrh0000gp/T/tmp66tn_06t/.eslintignore)
2022-10-09 14:09:44,522 | Copying source file (/Users/jric/dev/tutorials/sam-app/hello-world/jest.config.ts) to destination (/var/folders/rf/0_rbkb7d4dl7_ml7c0128lrh0000gp/T/tmp66tn_06t/jest.config.ts)
2022-10-09 14:09:44,522 | NodejsNpmEsbuildBuilder:CopySource succeeded
2022-10-09 14:09:44,522 | Running NodejsNpmEsbuildBuilder:NpmInstall
2022-10-09 14:09:44,523 | NODEJS installing in: /var/folders/rf/0_rbkb7d4dl7_ml7c0128lrh0000gp/T/tmp66tn_06t
2022-10-09 14:09:44,523 | executing NPM: ['npm', 'install', '-q', '--no-audit', '--no-save', '--unsafe-perm', '--production']
2022-10-09 14:09:49,369 | NodejsNpmEsbuildBuilder:NpmInstall succeeded
2022-10-09 14:09:49,370 | Running NodejsNpmEsbuildBuilder:EsbuildBundle
2022-10-09 14:09:49,370 | NODEJS building ['/var/folders/rf/0_rbkb7d4dl7_ml7c0128lrh0000gp/T/tmp66tn_06t/app.ts'] using esbuild to /Users/jric/dev/tutorials/sam-app/.aws-sam/build/HelloWorldFunction
2022-10-09 14:09:49,371 | Using the following default args: ['--bundle', '--platform=node', '--outdir=/Users/jric/dev/tutorials/sam-app/.aws-sam/build/HelloWorldFunction', '--format=cjs']
2022-10-09 14:09:49,371 | Found the following args in the config: ['--minify', '--target=es2020']
2022-10-09 14:09:49,371 | checking for esbuild in: ['/private/var/folders/rf/0_rbkb7d4dl7_ml7c0128lrh0000gp/T/tmp66tn_06t/node_modules/.bin']
2022-10-09 14:09:49,372 | potential esbuild binaries: ['/private/var/folders/rf/0_rbkb7d4dl7_ml7c0128lrh0000gp/T/tmp66tn_06t/node_modules/.bin/esbuild']
2022-10-09 14:09:49,372 | executing Esbuild: ['/private/var/folders/rf/0_rbkb7d4dl7_ml7c0128lrh0000gp/T/tmp66tn_06t/node_modules/.bin/esbuild', 'app.ts', '--bundle', '--platform=node', '--outdir=/Users/jric/dev/tutorials/sam-app/.aws-sam/build/HelloWorldFunction', '--format=cjs', '--minify', '--target=es2020']
2022-10-09 14:09:49,388 | NodejsNpmEsbuildBuilder:EsbuildBundle succeeded
2022-10-09 14:09:49,397 | There is no customer defined id or cdk path defined for resource HelloWorldFunction, so we will use the resource logical id as the resource id
2022-10-09 14:09:49,398 | 2 resources found in the stack 
2022-10-09 14:09:49,398 | Found Serverless function with name='HelloWorldFunction' and CodeUri='hello-world/'

Build Succeeded

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

Commands you can use next
=========================
[*] Validate SAM template: sam validate
[*] Invoke Function: sam local invoke
[*] Test Function in the Cloud: sam sync --stack-name {stack-name} --watch
[*] Deploy: sam deploy --guided

2022-10-09 14:09:49,454 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2022-10-09 14:09:49,455 | Unable to find Click Context for getting session_id.
2022-10-09 14:09:49,456 | Sending Telemetry: {'metrics': [{'commandRun': {'requestId': '70b68b2a-ef59-4e78-9159-403e225ec3b7', 'installationId': '4f9e2d6d-e232-4b29-b103-6c1451ffa5cb', 'sessionId': '1de07958-73e9-4d94-b5ab-43cee33ab42f', 'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.8.14', 'samcliVersion': '1.59.0', 'awsProfileProvided': False, 'debugFlagProvided': True, 'region': '', 'commandName': 'sam build', 'metricSpecificAttributes': {'projectType': 'CFN', 'gitOrigin': None, 'projectName': '593ab2ca51e925b9f6c2f258bc55ed5926cf6d2c78239a685d65907e4ec7edd3', 'initialCommit': None}, 'duration': 6262, 'exitReason': 'success', 'exitCode': 0}}]}
2022-10-09 14:09:49,457 | Sending Telemetry: {'metrics': [{'events': {'requestId': '188af9c0-7c37-402f-b66a-cd9fefbfcfc2', 'installationId': '4f9e2d6d-e232-4b29-b103-6c1451ffa5cb', 'sessionId': '1de07958-73e9-4d94-b5ab-43cee33ab42f', 'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.8.14', 'samcliVersion': '1.59.0', 'metricSpecificAttributes': {'events': [{'event_name': 'BuildWorkflowUsed', 'event_value': 'nodejs-npm-esbuild', 'thread_id': 4782794240, 'time_stamp': '2022-10-09 21:09:43.871'}, {'event_name': 'BuildFunctionRuntime', 'event_value': 'nodejs16.x', 'thread_id': 4782794240, 'time_stamp': '2022-10-09 21:09:49.405'}]}}}]}
2022-10-09 14:09:49,685 | Telemetry response: 200
2022-10-09 14:09:49,708 | HTTPSConnectionPool(host='aws-serverless-tools-telemetry.us-west-2.amazonaws.com', port=443): Read timed out. (read timeout=0.1)
sriram-mv commented 2 years ago

Thanks for the issue! indeed the resource is created as part of the events described for the lambda function. This was added at https://github.com/aws/aws-sam-cli/pull/2907 to address https://github.com/aws/serverless-application-model/issues/2152

mndeveci commented 1 year ago

This issue have been fixed in SAM CLI 1.67.0 release.

github-actions[bot] commented 1 year 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.