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

Bug: Missing DockerContext doesn't fail validation #7017

Open palfrey opened 7 months ago

palfrey commented 7 months ago

Description:

If you set Dockerfile in a function's metadata, but not DockerContext, sam build seems to just work, except then sam local invoke complains Error: ImageUri not provided for Function: YourFunctionName of PackageType: Image which is very confusing. Note also sam validate doesn't complain abou this.

Steps to reproduce:

Create a correct docker image SAM template, then remove just the DockerContext metadata param.

Observed result:

sam local invoke fails with a confusing error

Expected result:

sam validate fails, or maybe sam build?

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

  1. OS: MacOS
  2. sam --version: 1.116.0
{
  "version": "1.116.0",
  "system": {
    "python": "3.11.6",
    "os": "macOS-14.0-arm64-arm-64bit"
  },
  "additional_dependencies": {
    "docker_engine": "23.0.6",
    "aws_cdk": "Not available",
    "terraform": "1.4.1"
  },
  "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"
  ]
}
fedor-pavlov commented 7 months ago

Same for me here! I was caught in this trap yesterday.

mildaniel commented 7 months ago

Thanks @palfrey for reporting. I was able to confirm the behaviour you're describing. The main issue to me here seems not properly handling the missing DockerContext during sam build since it doesn't end up building any images. I will mark it as a bug for the team to prioritize.

mildaniel commented 3 months ago

Looking at this again, I don't necessarily think that it's a bug. During a single build, SAM CLI will build all the functions in the template. This can include functions that have a DockerContext and some that don't. When running sam build --debug the following message would be seen if a function is missing the DockerContext field:

Skip Building HelloWorldFunction function, as it is missing either Dockerfile or DockerContext metadata properties.

While I understand that this behaviour might be confusing, changing it now to error out when encountering a function without the DockerContext could break existing customers utilizing this behaviour. One thing we might be able to do here to make things more clear is to change that message from being debug level to info level.