Skip to content

Lambda could not load resource bundle #501

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

Closed
johnmorrell opened this issue Mar 11, 2025 · 20 comments
Closed

Lambda could not load resource bundle #501

johnmorrell opened this issue Mar 11, 2025 · 20 comments
Assignees
Labels
kind/support Adopter support requests.

Comments

@johnmorrell
Copy link

johnmorrell commented Mar 11, 2025

I have a swift target including static resources referenced in the package target. This target is included in the Lambda executable. The static resources are available when running locally via a CLI executable. When deployed to Lambda via the archive plugin using 1.0.0-alpha.3 these resources don't seem to be available. The error from CloudWatch is:

App/resource_bundle_accessor.swift:12: Fatal error: could not load resource bundle: from /var/task/Sandbox_App.resources or /workspace/.build/x86_64-unknown-linux-gnu/release/Sandbox_App.resources
@sebsto
Copy link
Contributor

sebsto commented Mar 12, 2025

Thank you for having reported this.
Can you update to the v2 of the project ? Currently, you can get it from the main branch and we will tag a 2.0.0-alpha.1 shortly

This issue is fixed in the main branch and there is an example project you can use as a reference.

https://github.com/swift-server/swift-aws-lambda-runtime/tree/main/Examples/ResourcesPackaging

@sebsto sebsto self-assigned this Mar 12, 2025
@sebsto sebsto added the kind/support Adopter support requests. label Mar 12, 2025
@johnmorrell
Copy link
Author

Thanks for the quick response. We are using this in conjunction with:

https://github.com/hummingbird-project/hummingbird-lambda

It looks like their v2 development branch has fallen behind main, but thank you for confirming the issue and fix.

@sebsto
Copy link
Contributor

sebsto commented Mar 12, 2025

I will ask @adam-fowler to check - thanks for having reported this.

@adam-fowler
Copy link
Member

The issue being reported is unrelated to the version of Hummingbird. Although I should release a new beta, with 1.0.0 of swift-aws-lambda-events. This is probably related to the archive plugin not packaging the resources.

@adam-fowler
Copy link
Member

There is the start of an implementation of hummingbird-lambda with the new v2 runtime in the lambda-runtime-v2 branch of hummingbird-lambda. It involves a few changes. Unfortunately undocumented at the moment, but it moves the lambda API closer to the server API. Application is replaced with LambdaFunction. It requires a release of the v2 runtime though (@sebsto is there going to be an alpha soon) before it can get a proper release.

@johnmorrell
Copy link
Author

Thank you @adam-fowler I have been looking at lambda-runtime-v2. Are you able to upload the example in the README?

@sebsto
Copy link
Contributor

sebsto commented Mar 12, 2025

@adam-fowler @johnmorrell
Yes, there will be a Swift lambda Runtime 2.0.0-alpha.1 soon. We've made significant progress last week with @fabianfett
Still a few things to iron out before a release though

@johnmorrell
Copy link
Author

@sebsto @adam-fowler thanks for all the work on theses projects, I'll look out for the new releases.

@adam-fowler
Copy link
Member

@johnmorrell I've updated the lambda-runtime-v2 branch so it compiles again and updated the README with a simple example

@sebsto
Copy link
Contributor

sebsto commented Mar 12, 2025

@johnmorrell if the Lambda packager plugin continues to fail including resources in the zip, please let us know.
Include the directory layout, the output of of zip -r <your zip file>, relevant portions of the Package.swift file, and any relevant error messages or steps to reproduce.

@johnmorrell
Copy link
Author

@johnmorrell I've updated the lambda-runtime-v2 branch so it compiles again and updated the README with a simple example

Thank you. Are you able to include the equivalent of:

@main
struct Lambda: APIGatewayLambdaFunction {

}

@adam-fowler
Copy link
Member

You could add your own wrapper for that

@main
struct MyApp {
    static func run() async throws {
         let router = Router()
         ...etc
    }
}

@johnmorrell
Copy link
Author

I was able to get up and running with APIGW v2 using the two projects, however I still hit the original issue. Using the same hello.txt package reference as the example project above I am able to access the file running the server locally and calling with a JSON body via /invoke. But it does not make it in to the .zip file buillt with the archive plugin:

% zipinfo Lambda.zip
Archive:  Lambda.zip
Zip file size: 34568915 bytes, number of entries: 1
-rwxr-xr-x  3.0 unx 108939824 bx defN 25-Mar-13 12:15 bootstrap
1 file, 108939824 bytes uncompressed, 34568747 bytes compressed:  68.3%

The CloudWatch error on trying to access the resource is:

App/resource_bundle_accessor.swift:12: Fatal **error**: could not load resource bundle: from /var/task/Sandbox_App.resources or /workspace/.build/x86_64-unknown-linux-gnu/release/Sandbox_App.resources

So *.resources is not deployed. The closest to getting something working is to manually add the hello.txt file to the .zip before deployment and access via:

URL(fileURLWithPath: "/var/task/hello.txt")

Which did allow we to access the file via APIGW/Lambda.

@sebsto
Copy link
Contributor

sebsto commented Mar 13, 2025

Thank you @johnmorrell for the extra checks.
Can you share your Package.swift file ? It must contain a resource section to trigger the copy of your resources to the zip file.
You can see an example here

targets: [
        .executableTarget(
            name: "MyLambda",
            dependencies: [
                .product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime")
            ],
            path: ".",
            resources: [
                .process("hello.txt")
            ]
        )
    ]

@sebsto
Copy link
Contributor

sebsto commented Mar 13, 2025

I don't access the resource file with URL(fileUrlWithPath:) as you wrote.
I use Bundle.module.url(forResource: "hello", withExtension: "txt") instead.

You can see the code example here.

let runtime = LambdaRuntime {
    (event: String, context: LambdaContext) in
    guard let fileURL = Bundle.module.url(forResource: "hello", withExtension: "txt") else {
        fatalError("no file url")
    }
    return try String(contentsOf: fileURL, encoding: .utf8)
}

I just build and deployed the ZIP file, and it returned the content of the file.

Image

 » cd Examples/ResourcesPackaging 
 » swift package archive --allow-network-connection docker 
...

  Build of product 'MyLambda' complete! (40.63s)
-------------------------------------------------------------------------
archiving "MyLambda"
-------------------------------------------------------------------------
1 archive created
  * MyLambda at /Users/stormacq/Documents/amazon/code/swift/lambda/swift-aws-lambda-runtime/Examples/ResourcesPackaging/.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/MyLambda/MyLambda.zip
  * 

 » unzip -l .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/MyLambda/MyLambda.zip 
Archive:  .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/MyLambda/MyLambda.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
 80804136  03-13-2025 14:27   bootstrap
        0  03-13-2025 14:27   ResourcesPackaging_MyLambda.resources/
       12  03-13-2025 14:27   ResourcesPackaging_MyLambda.resources/hello.txt
---------                     -------
 80804148                     3 files

@johnmorrell
Copy link
Author

Thanks @sebsto I can confirm package file contains the resource reference as your example. It would not compile or work locally without it when using the Bundle.module accessor - this is the crash I am seeing under Lambda.

I have just tried running the swift package archive locally on macos and I do get a .zip with the resources included. The issue now looks to be on building on GitHub action ubuntu-latest

@sebsto
Copy link
Contributor

sebsto commented Mar 13, 2025

I'm glad we identified the issue. To summarize the above discussion : it looks like the resources are not bundled when running the archive plugin on Linux.
I will try to reproduce that and check what happens.

@johnmorrell
Copy link
Author

That's right. I have just added a step to add the static resource *.resources/hello.txt to the zip before deployment and works as expected.

@sebsto
Copy link
Contributor

sebsto commented Mar 13, 2025

Yes, this is just a workaround. I'll try to fix the root cause.

@sebsto
Copy link
Contributor

sebsto commented Mar 13, 2025

Thank you for having reported this problem. I can reproduce the issue with main branch on Ubuntu 24.04, with Swift 6.0.3
I opened this issue #505 to track the search for root cause and resolution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/support Adopter support requests.
Projects
None yet
Development

No branches or pull requests

3 participants