Coder Social home page Coder Social logo

Comments (3)

sebsto avatar sebsto commented on July 17, 2024

One solution that worked for me is to consider both MyLambda and Shared as one package, i.e.

  • delete MyLambda/Package.swift
  • delete Shared/Package.swift
  • create ./Package.swift at root directory level (Examples/LocalDebugging) with the following content
import PackageDescription

let package = Package(
    name: "MyLambda",
    platforms: [
        .macOS(.v12),
    ],
    products: [
        .executable(name: "MyLambda", targets: ["MyLambda"]),
        .library(name: "Shared", targets: ["Shared"]),
    ],
    dependencies: [
        // this is the dependency on the swift-aws-lambda-runtime library
        // in real-world projects this would say
        .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main"),
//        .package(name: "swift-aws-lambda-runtime", path: "../../.."),
    ],
    targets: [
        .executableTarget(
            name: "MyLambda",
            dependencies: [
                .product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
                .byName(name: "Shared"),
            ],
            path: "./MyLambda",
            exclude: ["scripts/", "Dockerfile"]
        ),
        .target(name: "Shared", 
                dependencies: [],
                path: "./Shared") 
    ]
)

That allows to build and archive the Shared library and the MyLambda lambda function.

➜  LocalDebugging git:(master) ✗ swift build
warning: 'localdebugging': found 1 file(s) which are unhandled; explicitly declare them as resources or exclude from the target
    /Users/stormacq/Documents/amazon/code/lambda/swift/swift-aws-lambda-runtime/Examples/LocalDebugging/Shared/Package.swift.ignore

warning: 'localdebugging': found 1 file(s) which are unhandled; explicitly declare them as resources or exclude from the target
    /Users/stormacq/Documents/amazon/code/lambda/swift/swift-aws-lambda-runtime/Examples/LocalDebugging/MyLambda/Package.swift.ignore

[1/1] Planning buildCompiling plugin AWSLambdaPackager...
Building for debugging...
Build complete! (0.30s)
➜  LocalDebugging git:(master) ✗ 
➜  LocalDebugging git:(master) ✗ swift package --disable-sandbox archive --verbose 1 --output-path dist 
-------------------------------------------------------------------------
configuration
-------------------------------------------------------------------------
{
  outputDirectory: dist
  products: ["MyLambda"]
  buildConfiguration: release
  baseDockerImage: swift:amazonlinux2
}
-------------------------------------------------------------------------
building "localdebugging" in docker
-------------------------------------------------------------------------
updating "swift:amazonlinux2" docker image
  amazonlinux2: Pulling from library/swift
  Digest: sha256:0b645a7589aa5f1759f976c42a9ffba48fd930b27b3481b0ab32e271e701d444
  Status: Image is up to date for swift:amazonlinux2
  docker.io/library/swift:amazonlinux2
/usr/local/bin/docker run --rm -v /Users/stormacq/Documents/amazon/code/lambda/swift/swift-aws-lambda-runtime/Examples/LocalDebugging:/workspace -w /workspace swift:amazonlinux2 bash -cl swift build -c release --show-bin-path
  /workspace/.build/aarch64-unknown-linux-gnu/release
building "MyLambda"
/usr/local/bin/docker run --rm -v /Users/stormacq/Documents/amazon/code/lambda/swift/swift-aws-lambda-runtime/Examples/LocalDebugging:/workspace -w /workspace swift:amazonlinux2 bash -cl swift build -c release --product MyLambda --static-swift-stdlib
  warning: 'workspace': found 1 file(s) which are unhandled; explicitly declare them as resources or exclude from the target
    /workspace/Shared/Package.swift.ignore

warning: 'workspace': found 1 file(s) which are unhandled; explicitly declare them as resources or exclude from the target
    /workspace/MyLambda/Package.swift.ignore
  [0/1] Planning build
  Building for production...
  Build complete! (2.00s)
-------------------------------------------------------------------------
archiving "MyLambda"
-------------------------------------------------------------------------
/usr/bin/zip --junk-paths --symlinks dist/MyLambda/MyLambda.zip dist/MyLambda/MyLambda dist/MyLambda/bootstrap
    adding: MyLambda
   (deflated 66%)
  adding: bootstrap
   (stored 0%)
1 archive created
  * MyLambda at dist/MyLambda/MyLambda.zip

from swift-aws-lambda-runtime.

tomerd avatar tomerd commented on July 17, 2024

interesting issue. the archive plugin assumes the lambda is self contained and no external file system dependencies outside the package directory. in concept, the command should be invoked from Examples/LocalDebugging/MyLambda and not the root of the example Examples/LocalDebugging, but as mentioned above the docker bind mount will not "see" the shared module. this requires more thinking. I think re-arranging the example as suggested defeats the idea of having a shared module across the lambda and iOS app?

from swift-aws-lambda-runtime.

sebsto avatar sebsto commented on July 17, 2024

Thank you Tom. Looks like the old debate about mono vs. multi repositories.

I re-arranged the package structure to share the same Package.swift for the targets MyLambda and Shared. This allows the plugin to build and archive MyLamba. At the same time, I also made minimum changes to the Xcode workspace to continue to be able to build and debug from Xcode.

That way, the iOS app still depends on the Swift package. However, with this approach, Xcode sees both MyLambda and Shared. This structure kind off transfers "ownership" of the shared library to the "MyLambda" project. It will not work in larger teams where three different teams would maintain these three artifacts.

Project now builds with swift build
It runs with export LOCAL_LAMBDA_SERVER_ENABLED=true && swift run MyLambda.
It packages with swift package --disable-sandbox archive
It runs by selecting the MyLambda target in Xcode
It runs by selecting the MyApp target in Xcode

Agree with you that more thinking or feedback is required on that one.

from swift-aws-lambda-runtime.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.