aws-samples / aws-sam-terraform-examples

MIT No Attribution
45 stars 14 forks source link

Error Message: Could not locate source_path "../src/auth/". Paths are relative to directory where `terraform plan` is being run (...) #17

Open MSoup opened 9 months ago

MSoup commented 9 months ago

I'm following this guide which was posted just two months ago, and I am trying to replicate the steps in the demo (api gateway v1 REST)

Upon cloning the repo and going into ga/api_gateway_v1/tf_resources, I get stuck when trying to run sam build.

Intended Outcome

Build succeeds and from here on I should be able to run sam local invoke

Actual Outcome

% sam build --hook-name terraform --terraform-project-root-path ../

Running Prepare Hook to prepare the current application                                    
Executing prepare hook of hook "terraform"                                                 
Skipping preparation stage, the metadata file already exists at                            
/Users/dev/_learning/aws-sam-terraform-examples/ga/api_gateway_v1/tf-resources/.aw
s-sam-iacs/iacs_metadata/template.json                                                     
Prepare hook completed and metadata file generated at:                                     
/Users/dev/_learning/aws-sam-terraform-examples/ga/api_gateway_v1/tf-resources/.aw
s-sam-iacs/iacs_metadata/template.json                                                     
Building codeuri:                                                                          
/Users/dev/_learning/aws-sam-terraform-examples/ga/api_gateway_v1/src/auth        
runtime: python3.9 metadata: {'SkipBuild': False, 'BuildMethod': 'makefile', 'ContextPath':
'/Users/dev/_learning/aws-sam-terraform-examples/ga/api_gateway_v1/tf-resources/.a
ws-sam-iacs/iacs_metadata', 'WorkingDirectory':                                            
'/Users/dev/_learning/aws-sam-terraform-examples/ga/api_gateway_v1/tf-resources', 
'ProjectRootDirectory':                                                                    
'/Users/dev/_learning/aws-sam-terraform-examples/ga/api_gateway_v1/tf-resources'} 
architecture: x86_64 functions: module.lambda_function_auth.aws_lambda_function.this[0]    
ModuleLambdaFunctionAuthAwsLambdaFunctionThis0A0AF47A8: Running                            
CustomMakeBuilder:CopySource                                                               
ModuleLambdaFunctionAuthAwsLambdaFunctionThis0A0AF47A8: Running CustomMakeBuilder:MakeBuild
ModuleLambdaFunctionAuthAwsLambdaFunctionThis0A0AF47A8: Current Artifacts Directory :      
/Users/dev/_learning/aws-sam-terraform-examples/ga/api_gateway_v1/tf-resources/.aw
s-sam/build/ModuleLambdaFunctionAuthAwsLambdaFunctionThis0A0AF47A8                         
python3 ".aws-sam-iacs/iacs_metadata/copy_terraform_built_artifacts.py" --expression "|values|root_module|child_modules|[?address==module.lambda_function_auth]|resources|[?address==\"module.lambda_function_auth.null_resource.sam_metadata_aws_lambda_function[0]\"]|values|triggers|built_output_path" --directory "/Users/dev/_learning/aws-sam-terraform-examples/ga/api_gateway_v1/tf-resources/.aws-sam/build/ModuleLambdaFunctionAuthAwsLambdaFunctionThis0A0AF47A8" --target "module.lambda_function_auth.null_resource.sam_metadata_aws_lambda_function[0]"

Initializing the backend...

Successfully configured the backend "local"! Terraform will automatically
use this backend unless the backend configuration changes.
Initializing modules...

Initializing provider plugins...
- Reusing previous version of hashicorp/aws from the dependency lock file
- Reusing previous version of hashicorp/external from the dependency lock file
- Reusing previous version of hashicorp/local from the dependency lock file
- Reusing previous version of hashicorp/null from the dependency lock file
- Using previously-installed hashicorp/null v3.2.2
- Using previously-installed hashicorp/aws v4.67.0
- Using previously-installed hashicorp/external v2.3.2
- Using previously-installed hashicorp/local v2.4.0

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
module.lambda_function_auth.data.external.archive_prepare[0]: Reading...
╷
│ Warning: Resource targeting is in effect
│ 
│ You are creating a plan with the -target option, which means that the
│ result of this plan may not represent all of the changes requested by the
│ current configuration.
│ 
│ The -target option is not for routine use, and is provided only for
│ exceptional situations such as recovering from errors or mistakes, or when
│ Terraform specifically suggests to use it as part of an error message.
╵

Build Failed
Error: CustomMakeBuilder:MakeBuild - Make Failed: ╷
│ Error: External Program Execution Failed
│ 
│   with module.lambda_function_auth.data.external.archive_prepare[0],
│   on .terraform/modules/lambda_function_auth/package.tf line 10, in data "external" "archive_prepare":
│   10:   program = [local.python, "${path.module}/package.py", "prepare"]
│ 
│ The data source received an unexpected error while attempting to execute
│ the program.
│ 
│ Program: /opt/homebrew/bin/python3
│ Error Message: Could not locate source_path "../src/auth/".  Paths are
│ relative to directory where `terraform plan` is being run
│ ("/private/var/folders/2l/7b1lkwg518l19gmfxshs2p7m0000gn/T/tmp96dbm08v")
│ 
│ State: exit status 1
╵
Traceback (most recent call last):
  File "/private/var/folders/2l/7b1lkwg518l19gmfxshs2p7m0000gn/T/tmp96dbm08v/.aws-sam-iacs/iacs_metadata/copy_terraform_built_artifacts.py", line 371, in <module>
    subprocess.check_call(["terraform", "apply", "-target", target, "-replace", target, "-auto-approve"])
  File "/opt/homebrew/Cellar/python@3.11/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/subprocess.py", line 413, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['terraform', 'apply', '-target', 'module.lambda_function_auth.null_resource.sam_metadata_aws_lambda_function[0]', '-replace', 'module.lambda_function_auth.null_resource.sam_metadata_aws_lambda_function[0]', '-auto-approve']' returned non-zero exit status 1.
make: *** [build-ModuleLambdaFunctionAuthAwsLambdaFunctionThis0A0AF47A8] Error 1

Something that stood out to me is that terraform plan is being run at │ ("/private/var/folders/2l/7b1lkwg518l19gmfxshs2p7m0000gn/T/tmp96dbm08v") -- is this behavior intended?

Here are potentially relevant details:

MacOS Ventura 13.0, M1 Chip Python path /opt/homebrew/bin/python3 Docker version 23.0.5, build bc4487a59e SAM CLI, version 1.103.0

For what it's worth, running terraform init, terraform plan, terraform apply works as intended, but the sam commands seem to give me a path issue.

Any potential hints?

Yoimer commented 6 months ago

@MSoup Hi, hope you're doing great. Did you fix this? I am facing exactly the same error. Thanks in advance.