aws-sam-cli: Bug: sam init returns "Stopping generation because post_gen_project hook script didn't exit successfully"

Description:

➜ sam init --location https://github.com/aws-samples/cookiecutter-aws-sam-python


	SAM CLI now collects telemetry to better understand customer needs.

	You can OPT OUT and disable telemetry collection by setting the
	environment variable SAM_CLI_TELEMETRY=0 in your shell.
	Thanks for your help!

	Learn More: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-telemetry.html

project_name [Name of the project]: testing
include_safe_deployment [n]: 
env: python: No such file or directory
2023-04-21 11:07:52 Stopping generation because post_gen_project hook script didn't exit successfully
Error: An error occurred while generating this project : Hook script failed (exit status: 127)

Steps to reproduce:

  1. Install SAM CLI using homebrew: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html
  2. Verify successful SAM install: sam --version
  3. Attempt to generate project from template: sam init --location https://github.com/aws-samples/cookiecutter-aws-sam-python

Observed result:

➜ sam init --debug --location https://github.com/aws-samples/cookiecutter-aws-sam-pythona
2023-04-21 11:22:13,869 | Config file location: /Users/jpermar/Developer/sam/samconfig.toml
2023-04-21 11:22:13,869 | Config file '/Users/jpermar/Developer/sam/samconfig.toml' does not exist
2023-04-21 11:22:13,869 | Using config file: samconfig.toml, config environment: default
2023-04-21 11:22:13,869 | Expand command line arguments to:
2023-04-21 11:22:13,869 | --location=https://github.com/aws-samples/cookiecutter-aws-sam-pythona --package_type=Zip --output_dir=. 
2023-04-21 11:22:14,008 | Parameters dict created with input given
2023-04-21 11:22:14,009 | {'template': 'https://github.com/aws-samples/cookiecutter-aws-sam-pythona', 'output_dir': '.', 'no_input': False, 'extra_context': {'architectures': {'value': ['x86_64']}}}
2023-04-21 11:22:14,009 | Baking a new template with cookiecutter with all parameters
2023-04-21 11:22:14,350 | Unable to find cookiecutter.json in the project. Downloading it directly without treating it as a cookiecutter template
2023-04-21 11:22:14,351 | Downloading project from https://github.com/aws-samples/cookiecutter-aws-sam-pythona to .
2023-04-21 11:22:14,351 | https://github.com/aws-samples/cookiecutter-aws-sam-pythona location is a source control repository
2023-04-21 11:22:14,672 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2023-04-21 11:22:14,674 | Sending Telemetry: {'metrics': [{'commandRun': {'requestId': 'f406bf87-98e2-43ee-be8a-40a9b14715c1', 'installationId': '6745f11f-aa23-4b75-a329-13d9d51b1eb0', 'sessionId': 'b4c8365a-fac9-4330-ad7b-e545bd98b52d', 'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.11.3', 'samcliVersion': '1.81.0', 'awsProfileProvided': False, 'debugFlagProvided': True, 'region': '', 'commandName': 'sam init', 'metricSpecificAttributes': {'projectType': 'CFN'}, 'duration': 802, 'exitReason': 'ArbitraryProjectDownloadFailed', 'exitCode': 1}}]}
2023-04-21 11:22:15,171 | HTTPSConnectionPool(host='aws-serverless-tools-telemetry.us-west-2.amazonaws.com', port=443): Read timed out. (read timeout=0.1)
Error: Please verify your location. The following types of location are supported:

* Github: gh:user/repo (or) https://github.com/user/repo (or) git@github.com:user/repo.git
          For Git repositories, you must use location of the root of the repository.

* Mercurial: hg+ssh://hg@bitbucket.org/repo

* Http(s): https://example.com/code.zip

* Local Path: /path/to/code.zip

Expected result:

Successful project creation.

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

➜ brew --version
Homebrew 4.0.14
Homebrew/homebrew-core (git revision 3a7cd771717; last commit 2023-04-21)
➜  python --version
Python 3.9.16
➜  brew info aws-sam-cli
==> aws-sam-cli: stable 1.81.0 (bottled)
CLI tool to build, test, debug, and deploy Serverless applications using AWS SAM
https://aws.amazon.com/serverless/sam/
/Users/jpermar/Developer/homebrew/Cellar/aws-sam-cli/1.81.0 (8,782 files, 204.9MB) *
  Poured from bottle on 2023-04-21 at 09:52:07
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/aws-sam-cli.rb
License: Apache-2.0
==> Dependencies
Build: rust ✘
Required: python-typing-extensions ✔, python@3.11 ✔, six ✔
==> Analytics
install: 19 (30 days), 6,237 (90 days), 7,432 (365 days)
install-on-request: 19 (30 days), 6,232 (90 days), 7,427 (365 days)
build-error: 0 (30 days)
  1. OS: Mac Ventura 13.2.1
  2. sam --version: SAM CLI, version 1.81.0
  3. AWS region: us-east-1
# Paste the output of `sam --info` here
{
  "version": "1.81.0",
  "system": {
    "python": "3.11.3",
    "os": "macOS-13.2.1-arm64-arm-64bit"
  },
  "additional_dependencies": {
    "docker_engine": "Not available",
    "aws_cdk": "Not available",
    "terraform": "Not available"
  }
}

Add --debug flag to command you are running

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 18 (11 by maintainers)

Most upvoted comments

Thanks for your quick help @heitorlessa !

And thanks for bringing this issue to us @justinpermar , I will be resolving this issue since we already find a workaround and resolved the problem at https://github.com/aws-samples/cookiecutter-aws-sam-python/ side.

Please let us know if you have further questions.

All done from our side (Powertools) - thank you SAM CLI team (@mndeveci!) for helping Justin troubleshoot his python setup, and thank you Justin for your perseverance on this.

And Justin, if there’s anything at all we can do in your Powertools journey please shout out! I’d be happy to do a 1:1 or you can join our community on Discord with 300+ other fellows.

Powertools Python team chiming in - thanks @mndeveci for notifying us.

@justinpermar I’m so sorry to hear you had this bumpy road to start with that template – we’ll revamp that entirely after hearing this painful experience.

For now, if you’re using the latest SAM CLI, we now have a built-in hello world Powertools template as part of the “sam init” command.

sam init --app-template hello-world-powertools-python --name sam-app --package-type Zip --runtime python3.9 --no-tracing

This should do it.

For that other template, we’ll rewrite it to remove that hook to prevent issues like this (first I heard), and take the opportunity to add some production quality opinionated tools to fast track development.

Again, sorry to hear this. That’s not the experience we want customers using Powertools to have at all.

We’ll revert and do a much improved template on top.

Thanks

I am glad that you find a resolution for your problem!

  1. This current method of running python, from my perspective, is brittle. Adding fallback python install/location detection options to the SAM scripts/hooks that are running would be helpful. The obvious simple workaround is to look for python3 on the path, or even other specific versions python3.9 etc.
  2. Any improved output helpful to humans about what’s going on would also be helpful.

For your suggestions above, unfortunately this is very generic from our perspective. SAM CLI does execute cookiecutter commands to generate the given template. And since this template was not maintained by us, we can’t assume or catch any errors and provide better messaging. For another template, an after hook might try to execute some other command (let’s say javac for instance) which might not be on the PATH of the user, that will fail for some other customer.

I’ve created an issue on the other repository where they can provide more details about the required setup before initializing the template. https://github.com/aws-samples/cookiecutter-aws-sam-python/issues/45