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

Bug: Lambda functions containers initialization failed because of Resource ID was not provided #6194

Closed boozedog closed 8 months ago

boozedog commented 10 months ago

Description:

david@ubuntu-kvm:~/projects/lambda-sam$ sam validate
/home/david/projects/lambda-sam/template.yaml is a valid SAM Template
david@ubuntu-kvm:~/projects/lambda-sam$ sam local start-api
Initializing the lambda functions containers.                                                                                                                                                                                                                                      
Lambda functions containers initialization failed because of Resource ID was not provided                                                                                                                                                                                          
Error: Lambda functions containers initialization failed

Steps to reproduce:

Observed result:

Expected result:

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

  1. OS: Tested in Ubuntu 22.04 and NixOS unstable with same result
  2. sam --version: SAM CLI, version 1.100.0
  3. AWS region: us-east-1
# Paste the output of `sam --info` here
david@ubuntu-kvm:~/projects/lambda-sam$ sam --info
{
  "version": "1.100.0",
  "system": {
    "python": "3.11.3",
    "os": "Linux-6.2.0-36-generic-x86_64-with-glibc2.35"
  },
  "additional_dependencies": {
    "docker_engine": "24.0.5",
    "aws_cdk": "Not available",
    "terraform": "Not available"
  },
  "available_beta_feature_env_vars": [
    "SAM_CLI_BETA_FEATURES",
    "SAM_CLI_BETA_BUILD_PERFORMANCE",
    "SAM_CLI_BETA_TERRAFORM_SUPPORT",
    "SAM_CLI_BETA_RUST_CARGO_LAMBDA"
  ]
}

Add --debug flag to command you are running

david@ubuntu-kvm:~/projects/lambda-sam$ sam local start-api --debug
2023-11-02 18:13:22,152 | Config file location: /home/david/projects/lambda-sam/samconfig.toml                                                                                                                                                                                     
2023-11-02 18:13:22,154 | Loading configuration values from [default.['local', 'start-api'].parameters] (env.command_name.section) in config file at '/home/david/projects/lambda-sam/samconfig.toml'...                                                                           
2023-11-02 18:13:22,155 | Configuration values successfully loaded.                                                                                                                                                                                                                
2023-11-02 18:13:22,155 | Configuration values are: {'stack_name': 'lambda-sam', 'warm_containers': 'EAGER'}                                                                                                                                                                       
2023-11-02 18:13:22,158 | Using SAM Template at /home/david/projects/lambda-sam/template.yaml                                                                                                                                                                                      
2023-11-02 18:13:22,176 | Using config file: samconfig.toml, config environment: default                                                                                                                                                                                           
2023-11-02 18:13:22,177 | Expand command line arguments to:                                                                                                                                                                                                                        
2023-11-02 18:13:22,177 | --template_file=/home/david/projects/lambda-sam/template.yaml --host=127.0.0.1 --port=3000 --static_dir=public --layer_cache_basedir=/home/david/.aws-sam/layers-pkg --warm_containers=EAGER --container_host=localhost                                  
--container_host_interface=127.0.0.1                                                                                                                                                                                                                                               
2023-11-02 18:13:22,233 | local start-api command is called                                                                                                                                                                                                                        
2023-11-02 18:13:22,236 | No Parameters detected in the template                                                                                                                                                                                                                   
2023-11-02 18:13:22,250 | 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                                                                                                           
2023-11-02 18:13:22,251 | 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                                                                                                            
2023-11-02 18:13:22,252 | 0 stacks found in the template                                                                                                                                                                                                                           
2023-11-02 18:13:22,252 | No Parameters detected in the template                                                                                                                                                                                                                   
2023-11-02 18:13:22,264 | 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                                                                                                           
2023-11-02 18:13:22,264 | 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                                                                                                            
2023-11-02 18:13:22,265 | 2 resources found in the stack                                                                                                                                                                                                                           
2023-11-02 18:13:22,266 | Found Serverless function with name='HelloWorldFunction' and ImageUri='None'                                                                                                                                                                             
2023-11-02 18:13:22,266 | --base-dir is not presented, adjusting uri ./hello-world relative to /home/david/projects/lambda-sam/template.yaml                                                                                                                                       
2023-11-02 18:13:22,267 | --base-dir is not presented, adjusting uri . relative to /home/david/projects/lambda-sam/template.yaml                                                                                                                                                   
2023-11-02 18:13:22,268 | watch resource /home/david/projects/lambda-sam/template.yaml                                                                                                                                                                                             
2023-11-02 18:13:22,268 | Create Observer for resource /home/david/projects/lambda-sam/template.yaml with recursive True                                                                                                                                                           
2023-11-02 18:13:22,269 | watch resource /home/david/projects/lambda-sam/template.yaml's parent /home/david/projects/lambda-sam                                                                                                                                                    
2023-11-02 18:13:22,269 | Create Observer for resource /home/david/projects/lambda-sam with recursive False                                                                                                                                                                        
2023-11-02 18:13:22,272 | Initializing the lambda functions containers.                                                                                                                                                                                                            
2023-11-02 18:13:22,272 | Async execution started                                                                                                                                                                                                                                  
2023-11-02 18:13:22,273 | Invoking function functools.partial(<function InvokeContext._initialize_all_functions_containers.<locals>.initialize_function_container at 0x7fc41b32ec00>, Function(function_id='HelloWorldFunction', name='HelloWorldFunction',                        
functionname='HelloWorldFunction', runtime=None, memory=None, timeout=3, handler=None, imageuri=None, packagetype='Image', imageconfig=None, codeuri='/home/david/projects/lambda-sam', environment=None, rolearn=None, layers=[], events={'HelloWorld': {'Type': 'Api',           
'Properties': {'Path': '/hello', 'Method': 'get', 'RestApiId': 'ServerlessRestApi'}}}, metadata={'DockerTag': 'nodejs18.x-v1', 'DockerContext': '/home/david/projects/lambda-sam/hello-world', 'Dockerfile': 'Dockerfile', 'SamResourceId': 'HelloWorldFunction'}, inlinecode=None,
codesign_config_arn=None, architectures=['x86_64'], function_url_config=None, function_build_info=<FunctionBuildInfo.BuildableImage: ('BuildableImage', 'Regular IMAGE function which can be build with SAM CLI')>, stack_path='', runtime_management_config=None))                
2023-11-02 18:13:22,283 | Waiting for async results                                                                                                                                                                                                                                
2023-11-02 18:13:22,284 | No environment variables found for function 'HelloWorldFunction'                                                                                                                                                                                         
2023-11-02 18:13:22,285 | Loading AWS credentials from session with profile 'None'                                                                                                                                                                                                 
2023-11-02 18:13:22,293 | Exception raised during the execution                                                                                                                                                                                                                    
2023-11-02 18:13:22,296 | Lambda functions containers initialization failed because of Resource ID was not provided                                                                                                                                                                
2023-11-02 18:13:22,297 | Terminating all running warm containers                                                                                                                                                                                                                  
2023-11-02 18:13:22,297 | Cleaning all decompressed code dirs                                                                                                                                                                                                                      
2023-11-02 18:13:22,327 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics                                                                                                                                               
2023-11-02 18:13:22,333 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics                                                                                                                                               
2023-11-02 18:13:22,333 | Sending Telemetry: {'metrics': [{'commandRun': {'requestId': '22d842dd-9f65-4de7-9399-1a3fecba6d8a', 'installationId': '83b089c8-ceda-48b6-a39e-3163f9699aea', 'sessionId': '241fd410-1bb5-4ad2-acb6-c5a7cdb31a3b', 'executionEnvironment': 'CLI', 'ci': 
False, 'pyversion': '3.11.3', 'samcliVersion': '1.100.0', 'awsProfileProvided': False, 'debugFlagProvided': True, 'region': '', 'commandName': 'sam local start-api', 'metricSpecificAttributes': {'projectType': 'CFN', 'gitOrigin': None, 'projectName':                         
'90e0b8f272e54aa8501c761098e16655b59469cb7dbb23709b290dfcacc73c43', 'initialCommit': None}, 'duration': 150, 'exitReason': 'ContainersInitializationException', 'exitCode': 1}}]}                                                                                                  
2023-11-02 18:13:22,334 | Unable to find Click Context for getting session_id.                                                                                                                                                                                                     
2023-11-02 18:13:22,337 | Sending Telemetry: {'metrics': [{'events': {'requestId': 'dfcc9042-afe1-4436-9a4d-cdb08e0a5f1b', 'installationId': '83b089c8-ceda-48b6-a39e-3163f9699aea', 'sessionId': '241fd410-1bb5-4ad2-acb6-c5a7cdb31a3b', 'executionEnvironment': 'CLI', 'ci':     
False, 'pyversion': '3.11.3', 'samcliVersion': '1.100.0', 'commandName': 'sam local start-api', 'metricSpecificAttributes': {'events': [{'event_name': 'SamConfigFileExtension', 'event_value': '.toml', 'thread_id': 'f2dfcba6a3f547319aeef4eaacd01afa', 'time_stamp': '2023-11-02
22:13:22.152', 'exception_name': None}, {'event_name': 'SamConfigFileExtension', 'event_value': '.toml', 'thread_id': 'c8092f572ccf4c96a29e3d9138246d69', 'time_stamp': '2023-11-02 22:13:22.176', 'exception_name': None}]}}}]}                                                   
2023-11-02 18:13:22,722 | Telemetry response: 200                                                                                                                                                                                                                                  
Error: Lambda functions containers initialization failed
2023-11-02 18:13:22,942 | HTTPSConnectionPool(host='aws-serverless-tools-telemetry.us-west-2.amazonaws.com', port=443): Read timed out. (read timeout=0.1)                                                                                                                         
david@ubuntu-kvm:~/projects/lambda-sam$ 
moelasmar commented 10 months ago

Thanks @boozedog for raising this issue. Could you please share your template with us so we can investigate your problem.

boozedog commented 10 months ago

Thanks @boozedog for raising this issue. Could you please share your template with us so we can investigate your problem.

sure it is from the helloworld sample

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  lambda-sam

  Sample SAM Template for lambda-sam

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 3

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      PackageType: Image
      Architectures:
        - x86_64
      Events:
        HelloWorld:
          Type: Api
          Properties:
            Path: /hello
            Method: get
    Metadata:
      DockerTag: nodejs18.x-v1
      DockerContext: ./hello-world
      Dockerfile: Dockerfile

Outputs:
  # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
  # Find out more about other implicit resources you can reference within SAM
  # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
  HelloWorldApi:
    Description: "API Gateway endpoint URL for Prod stage for Hello World function"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
  HelloWorldFunction:
    Description: "Hello World Lambda Function ARN"
    Value: !GetAtt HelloWorldFunction.Arn
  HelloWorldFunctionIamRole:
    Description: "Implicit IAM Role created for Hello World function"
    Value: !GetAtt HelloWorldFunctionRole.Arn

@moelasmar

boozedog commented 10 months ago

@moelasmar i should also point out that sam build works fine but I want the live editing feature so I don't want to build prior to start-api

boozedog commented 10 months ago

@moelasmar any thoughts?

mndeveci commented 9 months ago

Hi @boozedog ,

As far as I understand, you want to run sam local start-api but you don't want to build it before since you want the live editing feature. That will work with ZIP lambda functions with interpreted runtimes, since we just mount the source folder and it let's you to live edit your code.

However the template you have above is a IMAGE type lambda function, which needs to be build before running it (we need the local docker image to invoke). For that reason, this won't work. You can either switch using a ZIP lambda function, or you can test in the cloud with sam sync command.

hnnasit commented 8 months ago

Closing this issue since it's been inactive for a while. Feel free to try out @mndeveci's suggestions and open a new issue if you still have questions/concerns.

github-actions[bot] commented 8 months 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.

brianz commented 7 months ago

@mndeveci Your explanation of this issue makes sense.

After doing a sam build, the output from sam local start-api states:

You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. If you used sam build before
running local commands, you will need to re-run sam build for the changes to be picked up. You only need to restart SAM CLI if you update your AWS SAM template

At the very minimum the output message should reflect reality, which is one does need to need to rebuild/restart/reload when using container images.