Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: AWS::Serverless::Function ImageUri with function (ex: using !If) fails with type error #7270

Closed
ib353 opened this issue Jul 22, 2024 · 3 comments
Labels
area/build sam build command stage/waiting-for-release Fix has been merged to develop and is waiting for a release

Comments

@ib353
Copy link

ib353 commented Jul 22, 2024

Description:

This looks very similar to issue #7117 (fixed in 1.117) but was found on 1.120 and is with !If rather than !Sub.

Setting an ImageUri in an AWS::Serverless::Function with Metadata to an !If results in the following error when running sam build:

Error: expected str, bytes or os.PathLike object, not collections.OrderedDict
Traceback:
  File "click/core.py", line 1078, in main
  File "click/core.py", line 1688, in invoke
  File "click/core.py", line 1434, in invoke
  File "click/core.py", line 783, in invoke
  File "samcli/cli/cli_config_file.py", line 347, in wrapper
  File "click/decorators.py", line 92, in new_func
  File "click/core.py", line 783, in invoke
  File "samcli/lib/telemetry/metric.py", line 185, in wrapped
  File "samcli/lib/telemetry/metric.py", line 150, in wrapped
  File "samcli/lib/utils/version_checker.py", line 43, in wrapped
  File "samcli/cli/main.py", line 95, in wrapper
  File "samcli/commands/build/command.py", line 169, in cli
  File "samcli/commands/build/command.py", line 258, in do_cli
  File "samcli/commands/build/build_context.py", line 284, in run
  File "samcli/lib/build/app_builder.py", line 226, in build
  File "samcli/lib/build/build_strategy.py", line 83, in build
  File "samcli/lib/build/build_strategy.py", line 93, in _build_functions
  File "samcli/lib/build/build_strategy.py", line 168, in build_single_function_definition
  File "samcli/lib/build/app_builder.py", line 665, in _build_function
  File "pathlib.py", line 1042, in __new__
  File "pathlib.py", line 683, in _from_parts
  File "pathlib.py", line 667, in _parse_args

Steps to reproduce:

Set up an AWS::Serverless::Function with an ImageUri that uses Fn::If to reproduce - note that the function must have Metadata defined for the error to occur.

Here's a minimal example:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: 'Minimal repro for ImageUri function issue'

Parameters:
  ScriptParameter:
    Type: String
    Value: 'Hello'

Conditions:
  ScriptParameterCondition:
    !Equals [!Ref ScriptParameter, Hello]

Resources:

  Func:
    Type: AWS::Serverless::Function
    Properties:
      ImageUri:
        Fn::If: [ !Condition ScriptParameterCondition, !Ref AWS::NoValue, !Ref ScriptParameter]
      PackageType: Image
      Architectures:
        - x64
      MemorySize: 128
      Timeout: 60
    Metadata:
      DockerContext: .
      Dockerfile: Dockerfile

Note that if you comment out the Metadata block it all works fine.

I realise that having both the ImageUri and the Metadata defined may be an odd combination. It allows me to use a single template for an existing build system that I'm migrating away from, as well as local usage with sam.

The detailed log contains a lot of information that I don't want to paste in here, but if you run the minimal repro script above you'll see the line:

Unable to resolve property ImageUri: OrderedDict([('Fn::If', ['LambdaImageUnset', {'Ref': 'AWS::NoValue'}, {'Ref': 'LambdaImage'}])]). Leaving as is.

Expected result:

Correct evaluation of Fn::If

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

{
  "version": "1.120.0",
  "system": {
    "python": "3.8.13",
    "os": "macOS-14.5-arm64-arm-64bit"
  },
  "additional_dependencies": {
    "docker_engine": "27.0.3",
    "aws_cdk": "2.149.0 (build c8e5924)",
    "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"
  ]
}
@ib353 ib353 added the stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. label Jul 22, 2024
@sidhujus
Copy link
Contributor

Hi, @ib353, I was able to reproduce the issue and have opened a PR that should fix this. We seem to have missed applying the fix for this case in the fix for the previous issue you linked

@sidhujus sidhujus added stage/waiting-for-release Fix has been merged to develop and is waiting for a release area/build sam build command and removed stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. labels Jul 24, 2024
@ib353
Copy link
Author

ib353 commented Jul 25, 2024

That's great, thank you - be great to have this fixed, as my workaround without !If is messy.

Copy link
Contributor

Patch is released in v1.121.0. Closing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/build sam build command stage/waiting-for-release Fix has been merged to develop and is waiting for a release
Projects
None yet
Development

No branches or pull requests

2 participants