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.

图片转代码服务由CSDN问答提供 功能建议

我有以下复杂的结构:

  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_fn1:
  n -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 
    
 

如何为GoLang中的所有这些功能创建.YML部署文件? 如果所有这些功能都有自己的主要功能,会不会有任何问题? 我是GoLang的新手,但据我所知,包只能包含一个main.go文件,并且在YML文件中的 handler 属性中,我必须从 bin 。 这就是我的想法:

  service:myService 
 
provider:
名称:aws 
运行时:go1.x 
 
functions:
 function1  :
处理程序:bin / function1 / main 
说明:.. 
事件:.. 
 function2:
处理程序:bin / function2 / main 
事件:.. 
 function3:
处理程序:bin  / function3 / main 
   
 
 

由于我有多个表示多个Lambda函数的软件包,所以在每个函数中都包含main.go应该是正确的,对吗? 如果不是,那么正确的方法是什么? 另外,如果这很好,我如何为每个函数指定正确的 main 二进制文件,这真的是使用GoLang部署多个lambda的惯例吗?

< strong>注意: 在每个main.go中都有一个对应的函数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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题