'AWS SAM Golang app using containers, error when building the image with local modules

I have an application in AWS SAM consisting of lambda functions in Golang that use docker image build and deploy, I have a problem with build lambda image along with other local modules

The structure of the project looks like this:

├── src
│   ├── configuration       # module1
│   │   ├── go.mod
│   │   └── values.go
│   ├── logger              # module2
│   │   ├── go.mod
│   │   ├── go.sum
│   │   └── logger.go
│   └── dockerTestLambda    # lambda function that use module#1, module#2
│       ├── Dockerfile
│       ├── go.mod
│       ├── go.sum
│       └── main.go
└── template.yml

Two separate modules configuration and logger which are used in the dockerTest Lambda function. The go.mod in the dockerTestLambda module looks like this:

module my-private-repo.io/my-app/dockerTest

go 1.18

replace my-private-repo.io/my-app/logger => ../logger

replace my-private-repo.io/my-app/configuration => ../configuration

require (
    github.com/aws/aws-lambda-go v1.31.1
    my-private-repo.io/my-app/configuration v0.0.0-00010101000000-000000000000
    my-private-repo.io/my-app/logger v0.0.0-00010101000000-000000000000
)

require (
    github.com/pkg/errors v0.9.1 // indirect
    github.com/rs/zerolog v1.26.1 // indirect
)

And the lambda itself (file main.go in dockerTestLambda) looks like this:


import (
    "context"
    "fmt"

    "github.com/aws/aws-lambda-go/cfn"
    "github.com/aws/aws-lambda-go/lambda"
    "my-private-repo.io/my-app/configuration"
    "my-private-repo.io/my-app/logger"
)

func HandleRequest(ctx context.Context, event cfn.Event) error {
    logger.Initialize()
    fmt.Println(configuration.GetRegion())
    logger.Log(nil, logger.LOG_LEVEL_INFO, "Hello from external deps, logger")

    return nil
}

func main() {
    lambda.Start(HandleRequest)
}

With definition in tempalte.yml

  DockerTestFunction:
    Type: AWS::Serverless::Function
    Properties:
      PackageType: Image
    Metadata:
      DockerTag: docker-test-v1
      DockerContext: ./src/dockerTestLambda
      Dockerfile: Dockerfile

The Dockerfile in the lambda function looks like this:

FROM golang:1.18 AS build

WORKDIR /app

COPY go.mod go.mod
COPY go.sum .

RUN go mod download

COPY main.go .

RUN go build -o /usr/local/bin/lambda .

FROM ubuntu:latest

COPY --from=build /usr/local/bin/lambda /usr/local/bin/lambda

CMD [ "/usr/local/bin/lambda" ]

During the sam build command I get an error: go: my-private-repo.io/my-app/[email protected] (replaced by ../configuration): reading /configuration/go.mod: open /configuration/go.mod: no such file or directory

I have no idea how to build an image so that each lambda has its own Dockerfile and each lambda function has all external dependencies packed into it. Do you have any ideas how to solve this, or some resources that will allow me to better structure the application code to make this problem easier to solve?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source