dpe77294 2019-05-13 07:04
浏览 55
已采纳

具有多个main.go文件以便部署基于AWS Lambda的应用程序

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.

  • 写回答

1条回答 默认 最新

  • douyi4912 2019-05-13 17:27
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联
  • ¥15 VB.NET操作免驱摄像头
  • ¥15 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目
  • ¥15 利用pthon计算薄膜结构的光导纳