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 map (ex: using !Sub) fails with type error #7117

Closed
rt-joe opened this issue May 30, 2024 · 7 comments
Labels
area/build sam build command area/deploy sam deploy command stage/waiting-for-release Fix has been merged to develop and is waiting for a release type/bug

Comments

@rt-joe
Copy link

rt-joe commented May 30, 2024

Description:

sam deploy fails with error Error: expected str, bytes or os.PathLike object, not OrderedDict

We use a !Sub plus a !FindInMap to resolve the ImageUri to a pre-built image for a AWS::Serverless::Function based on aws account id. Something like:

  ExampleLambda:
    Type: AWS::Serverless::Function
    Properties:
      PackageType: Image
      ImageUri: !Sub
        - ${LambdaImageRepoUri}:${imageTag}
        - imageTag: !FindInMap
            - AccountResourceValueLookup
            - !Ref AWS::AccountId
            - ExampleLambdaImageTag
      # other properties...

Sam cli tries to parse the ImageUri value as a string path, not expecting a map.

May have been introduced by #6930.

It was working prior to v1.117.0

Steps to reproduce:

Define resource AWS::Serverless::Function with PackageType: Image and ImageUri that uses !Sub with variable map (not using short form).

Run sam deploy.

See this sample repo

Observed result:

2024-05-30 15:33:54,088 | Found Serverless function with name='ExampleModelLambda' and ImageUri='OrderedDict({'Fn::Sub': ['${LambdaImageRepoUri}:${imageTag}', OrderedDict({'imageTag': OrderedDict({'Fn::FindInMap': ['AccountResourceValueLookup', OrderedDict({'Ref': 'AWS::AccountId'}), 'ExampleLambdaImageTag']})})]})'
2024-05-30 15:33:54,088 | --base-dir is not presented, adjusting uri . relative to /var/folders/bq/vf__th7n6xxg6smh74ql1bp40000gn/T/tmpijnpgr5k
2024-05-30 15:33:54,088 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2024-05-30 15:33:54,124 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2024-05-30 15:33:54,124 | Unable to find Click Context for getting session_id.
2024-05-30 15:33:54,125 | Sending Telemetry: {'metrics': [{'events': {'requestId': 'e67419b9-7159-46b0-90f3-760fc9fff7c7', 'installationId': '781031a4-ebae-4722-bc16-0af351578ec4', 'sessionId': 'ab6c6198-d75c-4b73-a20c-3498f6bc94df', 'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.12.3', 'samcliVersion': '1.117.0', 'commandName': 'sam deploy', 'metricSpecificAttributes': {'events': [{'event_name': 'SamConfigFileExtension', 'event_value': '.toml', 'thread_id': '7c30013f92124a818d789c131ffc13a7', 'time_stamp': '2024-05-30 13:33:52.269', 'exception_name': None}, {'event_name': 'SamConfigFileExtension', 'event_value': '.toml', 'thread_id': 'ab40e738b1714a82958c9275942b9c72', 'time_stamp': '2024-05-30 13:33:52.290', 'exception_name': None}]}}}]}
2024-05-30 15:33:54,126 | Sending Telemetry: {'metrics': [{'commandRun': {'requestId': '9afd29fd-fdb0-4094-b3d9-556c7ee2513d', 'installationId': '781031a4-ebae-4722-bc16-0af351578ec4', 'sessionId': 'ab6c6198-d75c-4b73-a20c-3498f6bc94df', 'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.12.3', 'samcliVersion': '1.117.0', 'awsProfileProvided': False, 'debugFlagProvided': True, 'region': 'us-east-1', 'commandName': 'sam deploy', 'metricSpecificAttributes': {'projectType': 'CFN', 'gitOrigin': None, 'projectName': 'b7bd55c11b781b0ccc43aa6e57f9dadf0660e9d1d4e27e0979ee43a407d454ae', 'initialCommit': None}, 'duration': 1789, 'exitReason': 'TypeError', 'exitCode': 255}}]}
2024-05-30 15:33:54,619 | HTTPSConnectionPool(host='aws-serverless-tools-telemetry.us-west-2.amazonaws.com', port=443): Read timed out. (read timeout=0.1)
2024-05-30 15:33:54,637 | HTTPSConnectionPool(host='aws-serverless-tools-telemetry.us-west-2.amazonaws.com', port=443): Read timed out. (read timeout=0.1)

Error: expected str, bytes or os.PathLike object, not OrderedDict
Traceback:
  File "/opt/homebrew/Cellar/aws-sam-cli/1.117.0/libexec/lib/python3.12/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/aws-sam-cli/1.117.0/libexec/lib/python3.12/site-packages/click/core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/aws-sam-cli/1.117.0/libexec/lib/python3.12/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/aws-sam-cli/1.117.0/libexec/lib/python3.12/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/aws-sam-cli/1.117.0/libexec/lib/python3.12/site-packages/samcli/cli/cli_config_file.py", line 347, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/aws-sam-cli/1.117.0/libexec/lib/python3.12/site-packages/samcli/lib/cli_validation/image_repository_validation.py", line 111, in wrapped
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/aws-sam-cli/1.117.0/libexec/lib/python3.12/site-packages/click/decorators.py", line 92, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/aws-sam-cli/1.117.0/libexec/lib/python3.12/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/aws-sam-cli/1.117.0/libexec/lib/python3.12/site-packages/samcli/lib/telemetry/metric.py", line 185, in wrapped
    raise exception  # pylint: disable=raising-bad-type
    ^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/aws-sam-cli/1.117.0/libexec/lib/python3.12/site-packages/samcli/lib/telemetry/metric.py", line 150, in wrapped
    return_value = func(*args, **kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/aws-sam-cli/1.117.0/libexec/lib/python3.12/site-packages/samcli/lib/utils/version_checker.py", line 43, in wrapped
    actual_result = func(*args, **kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/aws-sam-cli/1.117.0/libexec/lib/python3.12/site-packages/samcli/cli/main.py", line 95, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/aws-sam-cli/1.117.0/libexec/lib/python3.12/site-packages/samcli/commands/_utils/cdk_support_decorators.py", line 40, in wrapped
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/aws-sam-cli/1.117.0/libexec/lib/python3.12/site-packages/samcli/commands/_utils/command_exception_handler.py", line 89, in wrapper_command_exception_handler
    raise ex
  File "/opt/homebrew/Cellar/aws-sam-cli/1.117.0/libexec/lib/python3.12/site-packages/samcli/commands/_utils/command_exception_handler.py", line 69, in wrapper_command_exception_handler
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/aws-sam-cli/1.117.0/libexec/lib/python3.12/site-packages/samcli/commands/deploy/command.py", line 201, in cli
    do_cli(
  File "/opt/homebrew/Cellar/aws-sam-cli/1.117.0/libexec/lib/python3.12/site-packages/samcli/commands/deploy/command.py", line 372, in do_cli
    deploy_context.run()
  File "/opt/homebrew/Cellar/aws-sam-cli/1.117.0/libexec/lib/python3.12/site-packages/samcli/commands/deploy/deploy_context.py", line 166, in run
    return self.deploy(
           ^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/aws-sam-cli/1.117.0/libexec/lib/python3.12/site-packages/samcli/commands/deploy/deploy_context.py", line 241, in deploy
    auth_required_per_resource = auth_per_resource(stacks)
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/aws-sam-cli/1.117.0/libexec/lib/python3.12/site-packages/samcli/commands/deploy/auth_utils.py", line 36, in auth_per_resource
    sam_function_provider = SamFunctionProvider(stacks, ignore_code_extraction_warnings=True)
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/aws-sam-cli/1.117.0/libexec/lib/python3.12/site-packages/samcli/lib/providers/sam_function_provider.py", line 71, in __init__
    self.functions = SamFunctionProvider._extract_functions(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/aws-sam-cli/1.117.0/libexec/lib/python3.12/site-packages/samcli/lib/providers/sam_function_provider.py", line 246, in _extract_functions
    function = SamFunctionProvider._convert_sam_function_resource(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/aws-sam-cli/1.117.0/libexec/lib/python3.12/site-packages/samcli/lib/providers/sam_function_provider.py", line 315, in _convert_sam_function_resource
    return SamFunctionProvider._build_function_configuration(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/aws-sam-cli/1.117.0/libexec/lib/python3.12/site-packages/samcli/lib/providers/sam_function_provider.py", line 451, in _build_function_configuration
    normalized_image_uri = SamLocalStackProvider.normalize_resource_path(stack.location, imageuri)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/aws-sam-cli/1.117.0/libexec/lib/python3.12/site-packages/samcli/lib/providers/sam_stack_provider.py", line 379, in normalize_resource_path
    if os.path.isabs(path):
       ^^^^^^^^^^^^^^^^^^^
  File "<frozen posixpath>", line 62, in isabs

An unexpected error was encountered while executing "sam deploy".
Search for an existing issue:
https://github.com/aws/aws-sam-cli/issues?q=is%3Aissue+is%3Aopen+Bug%3A%20sam%20deploy%20-%20TypeError
Or create a bug report:
https://github.com/aws/aws-sam-cli/issues/new?template=Bug_report.md&title=Bug%3A%20sam%20deploy%20-%20TypeError

Expected result:

ImageUri is resolved correctly, deployment succeeds

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

{
  "version": "1.117.0",
  "system": {
    "python": "3.12.3",
    "os": "macOS-14.5-arm64-arm-64bit"
  },
  "additional_dependencies": {
    "docker_engine": "Not available",
    "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"
  ]
}
@rt-joe rt-joe added the stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. label May 30, 2024
@rt-joe rt-joe changed the title Bug: AWS::Serverless::Function ImageUri with !Sub fails with type error Bug: AWS::Serverless::Function ImageUri with map (ex: using !Sub) fails with type error May 30, 2024
@rt-joe rt-joe changed the title Bug: AWS::Serverless::Function ImageUri with map (ex: using !Sub) fails with type error Bug: AWS::Serverless::Function ImageUri with map (ex: using !Sub) fails with type error May 30, 2024
@rt-joe
Copy link
Author

rt-joe commented Jun 4, 2024

My only workaround is to stay on v1.116.0

@jysheng123 jysheng123 added type/bug area/deploy sam deploy command 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 Jun 4, 2024
@hnnasit
Copy link
Contributor

hnnasit commented Jun 4, 2024

Hi @rt-joe, thanks for reporting the issue. We are actively working on a fix for this and will keep you posted.

@jysheng123
Copy link
Contributor

Hey @rt-joe, would you be able to share your SAM project or a sample one that has your failing case, I want to make sure that I am able to reproduce your exact issue.

@rt-joe
Copy link
Author

rt-joe commented Jun 4, 2024

@jysheng123 I unfortunately can't share the original SAM project since it's private but I created a sample project to repro my issue as close as possible

https://github.com/rt-joe/aws-sam-cli-image-uri-error

@jysheng123
Copy link
Contributor

This PR will be included in the next SAM CLI regular release, fixing your issue.

@hnnasit hnnasit added the stage/waiting-for-release Fix has been merged to develop and is waiting for a release label Jun 6, 2024
Copy link
Contributor

Patch is released in v1.119.0. Closing

@rt-joe
Copy link
Author

rt-joe commented Jun 18, 2024

Verified it fixes my issue. Thanks for quick turnaround!

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

No branches or pull requests

3 participants