Stefan Radonjic
Stefan Radonjic

Reputation: 1598

Having multiple main.go files in order to deploy AWS Lambda based application

I have following complex structure:

utils:
    - utils.go
function1:
    pkg1_specific_to_fn1:
        -pkg1_specific_to_fn1.go
    pkg2_specific_to_fn1:
        -pkg2_specific_to_fn1.go
    main.go
function2:
    pkg1_specific_to_fn1:
        -pkg1_specific_to_fn2.go
    pkg2_specific_to_fn1:
        -pkg2_specific_to_fn2.go
    main.go
function3:
    pkg1_specific_to_fn1:
        -pkg1_specific_to_fn3.go
    pkg2_specific_to_fn1:
        -pkg2_specific_to_fn3.go
    main.go

How do I create .YML deployment file for all these functions in GoLang? Will there be any issues if all of these functions have their own main? I am new to GoLang, but as far as I know, package can contain only one main.go file, and in YML file for handler property I have to specify executable from bin. Here is what I had in mind:

service: myService

provider:
  name: aws
  runtime: go1.x

functions:
  function1:
    handler: bin/function1/main
    description: ..
    events: ..
  function2:
    handler: bin/function2/main
    events: ..
  function3:
    handler: bin/function3/main

Since I have multiple packages representing multiple Lambda functions, it should be okay for me to have main.go in each of them, correct? If not then what is the right way to do this? Also, if this is fine, how do I specify correct main binary for each function, and is this really the convention to deploy multiple lambdas with GoLang?

NOTE: in each main.go there is a corresponding function Handler.

Upvotes: 3

Views: 1857

Answers (1)

Adrian
Adrian

Reputation: 2113

The deployment of lambda function comes down to package/module organization and automation deployment tools. The first looks like being address in your question where the shared code is placed to util and each lambda has an individual package. In the question, it is not clear what the deployment method is being used. There are various way to get lambda deployed

While I've been e2e and automation advocate, with endly runner multi lambda deployment workflow for various events may look like the following

init:
  fn1ZipLocation: somepath1.zip
  fn2ZipLocation: somepath2.zip
  fnXZipLocation: somepathX.zip

pipeline:

  build:
    fn1:
      action: exec:run
      target: $target
      sleepTimeMs: 1500
      errors:
        - ERROR
      commands:
        - cd ${appPath}aeroagg/app
        - unset GOPATH
        - export GOOS=linux
        - export GOARCH=amd64
        - go build -o function1
        - zip -j somepath1.zip function1

  ...

  deployFunctions:
    fn1:
      action: aws/lambda:deploy
      credentials: aws-e2e
      functionname: fn1
      runtime:  go1.x
      handler: main
      code:
        zipfile: $LoadBinary(${fn1ZipLocation})
      rolename: lambda-fn1-executor
      define:
        - policyname: xxx-resource-fn1-role
          policydocument: $Cat('${privilegePolicy}')
      attach:
        - policyarn: arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
      triggers:
        - source: somequeue
          type: sqs
          enabled: true
          batchSize: 20000
    fn2:
      action: aws/lambda:deploy
      credentials: aws-e2e
      functionname: fn2
      runtime:  go1.x
      handler: main
      code:
        zipfile: $LoadBinary(${fn2ZipLocation})
      rolename: lambda-fn2-executor
      define:
        - policyname: xxx-resource-fn2-role
          policydocument: $Cat('${privilegePolicy}')
      attach:
        - policyarn: arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

      notification:
        action: aws/s3:setupBucketNotification
        bucket: someBucket
        lambdaFunctionConfigurations:
          - functionName: fn2
            id: ObjectCreatedEvents
            events:
              - s3:ObjectCreated:*
            filter:
              prefix:
                - folderXXX
              suffix:
                - .csv

      ...
    fnX:
      action: aws/lambda:deploy
        functionname: fnX
        runtime:  go1.x
        handler: main
        timeout: 360
        vpcMatcher:
          instance:
            name: instanceWithVPC

        environment:
          variables:
            CONFIG: $AsString($config)
        code:
          zipfile: $LoadBinary(${fn2ZipLocation})
        rolename: lambda-fn3-executor
        define:
          - policyname: lambda-sns-execution-role
            policydocument: $Cat('${privilegePolicy}')
        attach:
          - policyarn: arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
          - policyarn: arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole
      setupSubscription:
        action: aws/sns:setupSubscription
        protocol: lambda
        endpoint: fnX
        topic: someTopic

    deployGatewayAPI:
      redeploy: true
      action: aws/apigateway:setupRestAPI
      '@name': myAPIName
      resources:
        - path: /
          methods:
            - httpMethod: GET
              functionname: fn3
        - path: /{proxy+}
          methods:
            - httpMethod: GET
              functionname: fn4
        - path: /v1/api/fn4
          methods:
            - httpMethod: GET
              functionname: fn5

Finally you can check serverless e2e with lambda e2e practical testing examples.

Upvotes: 2

Related Questions