question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

sam build includes absolute paths in Golang binaries

See original GitHub issue

Description:

When you run sam build and sam package in two instances of the same projects with different absolute paths and upload to the same S3 bucket, the compiled binaries will be uploaded both times, even though the binaries are identical. According to #2110 this has been fixed, but I can still reproduce the issue on Windows.

Steps to reproduce:

  1. Clone this repository into a directory on your local Windows machine.
  2. cd into the directory.
  3. Run sam build.
  4. Run sam package --s3-bucket YOUR_BUCKETNAME
  5. Clone the same repository into a second directory on your local windows machine.
  6. Copy the second directory into a third directory using xcopy seconddirectory thirddirectory /s /e.
  7. cd into the second directory.
  8. Run sam build.
  9. Run sam package --s3-bucket YOUR_BUCKETNAME
  10. cd into the third directory.
  11. Run sam build.
  12. Run sam package --s3-bucket YOUR_BUCKETNAME

Observed result:

The calculated hashes change, when the parent directory changes. This results in the compiled binaries being re-uploaded for every directory, although no files have been modified.

Expected result:

Hashes should not depend on the parent directory. Compiled binaries should only be uploaded, when they have been modified.

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

  1. OS: Windows 10 Pro Version 20H2 (Build 19042.1165)
  2. sam --version: 1.31.0
  3. AWS region: eu-central-1

Further notes:

I can imagine this is also the cause for the issue #2872, where running sam package in an ephemeral container results in the files being uploaded every time.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
scrofunguluscommented, Oct 6, 2022

@sriram-mv I have opened a pull request in aws-lambda-builders allowing this option to be passed in.

Once this PR is reviewed and merged I believe this project can be updated here to something along the lines of:

        _build_options: Dict = {
            "go": {
                "artifact_executable_name": handler,
                "trim_go_path": build_props.get("TrimGoPath", False),
            },
            "provided": {"build_logical_id": function_name},
            "nodejs": {"use_npm_ci": build_props.get("UseNpmCi", False)},
        }

I believe this will allow SAM template users to define functions like the following:

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: functions/hello-world/
      Handler: main
      Runtime: go1.x
      Architectures:
        - x86_64
      Events:
        HelloWorld:
          Type: Api
          Properties:
            Path: /hello
            Method: get
    Metadata:
      BuildProperties:
        TrimGoPath: True

Which fixes the issue described above by @suroh1994 and @jaglade

0reactions
sriram-mvcommented, Sep 19, 2022

@jaglade This will be a welcome contribution to the aws-lambda-builders golang build workflow and we can make this feature opt-in (atleast at this point, I have not considered the ramifications of the change in behavior causing breaks).

Read more comments on GitHub >

github_iconTop Results From Across the Web

sam build - AWS Serverless Application Model
Resolves relative paths to the function's or layer's source code with respect to this directory. Use this option if you want to change...
Read more >
Learning AWS SAM: sam build - YouTube
In this video I do an in depth view of the AWS SAM CLI command, sam init. The ` sam build ` command...
Read more >
Build and deploy a multi-route, serverless web app ... - Medium
Build and deploy a multi-route, serverless web app with Golang on AWS ... organizing our binaries by their public facing paths and actions....
Read more >
how to get the full path of current process' executable file
I think the points made previously about the application behavior remaining predictable no matter where the binary is, the per-user config capability, and...
Read more >
Develop Lambdas And Debug Them Locally Using SAM
You can find the full code used in this video at GitHub. Who Is Sam? Sam (Serverless Application Model) is an AWS framework...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found