简体   繁体   English

在nodejs中,我可以有每个子文件夹都托管一个AWS Lambda函数的子文件夹吗?

[英]In nodejs, can I have sub-folders that each sub-folder host a AWS lambda function?

I am trying to implement a serverless application on AWS. 我正在尝试在AWS上实现无服务器应用程序。 I wish to know a good way to organize my codes. 我想知道一种组织代码的好方法。 Let's say, I want to have multiple lambda functions to be organized under a git repo, each lambda function and it's associated codes are in its own sub-folder. 假设我想在git repo下组织多个lambda函数,每个lambda函数及其相关代码都在其自己的子文件夹中。

This is what I have tried, separate sub-folders for each lambda function: 这是我尝试过的,每个lambda函数的单独的子文件夹:

(venv367) dummy@dummy-VirtualBox:~/workspace/serverless-apps/DEVOPS-dailybackup-ebs$ ls -l
total 28
-rw-rw-r-- 1 dummy dummy 4061 Jan 13 19:23 index.js
drwxrwxr-x 3 dummy dummy 4096 Jan 13 19:23 node_modules
-rw-rw-r-- 1 dummy dummy  352 Jan 13 19:23 package.json
-rw-rw-r-- 1 dummy dummy  392 Jan 13 19:23 package-lock.json
-rw-rw-r-- 1 dummy dummy 1220 Jan 14 00:10 README.md

(venv367) dummy@dummy-VirtualBox:~/workspace/serverless-apps/DEVOPS-dailybackup-s3$ ls -l
total 28
-rw-rw-r-- 1 dummy dummy 4061 Jan 13 19:23 index.js
drwxrwxr-x 3 dummy dummy 4096 Jan 13 19:23 node_modules
-rw-rw-r-- 1 dummy dummy  352 Jan 13 19:23 package.json
-rw-rw-r-- 1 dummy dummy  392 Jan 13 19:23 package-lock.json
-rw-rw-r-- 1 dummy dummy 1220 Jan 14 00:10 README.md

In the top level project, I have SAM template.yaml as below 在顶层项目中,我有如下的SAM template.yaml

(venv367) dummy@dummy-VirtualBox:~/workspace/serverless-apps$ ls -l
total 16
drwxrwxr-x 3 dummy dummy 4096 Jan 14 05:14 00_DEVOPS-dailybackup-ebs
drwxrwxr-x 3 dummy dummy 4096 Jan 14 05:14 00_DEVOPS-dailybackup-s3
-rw-rw-r-- 1 dummy dummy  702 Jan 14 05:15 packaged.yaml
-rw-rw-r-- 1 dummy dummy 1402 Jan 13 23:45 README.md
-rw-rw-r-- 1 dummy dummy  939 Jan 14 05:15 template.yaml

In the template.yaml, I deploy Lambda functions by specifying Handler with its parent directory 在template.yaml中,我通过使用其父目录指定Handler来部署Lambda函数。

Resources:
    EbsBackupFunction:
        Type: AWS::Serverless::Function 
        Properties:
            Handler: DEVOPS-dailybackup-ebs/index.handler
            Runtime: nodejs8.10
            FunctionName: DEVOPS-dailybackup-ebs

    S3BackupFunction:
        Type: AWS::Serverless::Function 
        Properties:
            Handler: DEVOPS-dailybackup-s3/index.handler
            Runtime: nodejs8.10
            FunctionName: DEVOPS-dailybackup-s3

(1) I haven't look at serverless framework in detail though. (1)我没有详细讨论无服务器框架。 Any better approach for managing serverless applications using AWS SAM? 还有使用AWS SAM管理无服务器应用程序的更好方法吗?

(2) Is there a way to do npm install for nodejs code under each sub-folder other than write a script to do 'cd ${dir} && npm install' (2)除了编写脚本来执行“ cd $ {dir} && npm install”之外,是否有办法为每个子文件夹下的nodejs代码进行npm安装。

(3) If above is a nasty idea, can you tell me what would you do? (3)如果以上是一个令人讨厌的想法,您能告诉我您会怎么做吗?

Tks in advance for any helps given. 事先感谢任何帮助。

  1. Your method for handling multiple functions is the currently suggested method. 当前建议的用于处理多种功能的方法。

  2. Different approaches can be taken based on your CI/CD tool. 根据您的CI / CD工具,可以采用不同的方法。 This is my current buildspec.yml for use with AWS CodeBuild: 这是我目前buildspec.yml与AWS CodeBuild使用:

version: 0.2
phases:
  pre_build:
    commands:
      - for d in lambdas/*/; do npm --prefix $d install $d; done
  build:
    commands:
      - |
        aws cloudformation package \
          --template-file template.yml \
          --s3-bucket "<bucket name>" \
          --s3-prefix sam \
          --output-template-file packaged.yml
  post_build:
    commands:
      - |
        aws cloudformation deploy \
          --template-file packaged.yml \
          --stack-name "<stack name>" \
          --capabilities CAPABILITY_NAMED_IAM

This allows me to keep my local directory/code repository clean (no node_modules folder) via .gitignore , but dependencies are installed before deploying. 这使我可以通过.gitignore保持本地目录/代码存储库的干净(没有node_modules文件夹),但是在部署之前已安装依赖项。 Note that including your package-lock.json file is necessary to avoid errors with different package versions. 请注意,必须包含package-lock.json文件,以避免不同软件包版本的错误。

  1. As stated above, this is the same setup that I'm currently using. 如上所述,这是我当前使用的相同设置。 So far, the only drawbacks I've seen are not being able to update a single function without updating all of them, but because of the way my environment is set up (all functions share the same version number), a change to one function requires changes to the others anyway. 到目前为止,我所看到的唯一缺点是无法在不更新所有功能的情况下更新单个功能,但是由于设置环境的方式(所有功能共享相同的版本号),因此只能更改一个功能无论如何都需要对其他人进行更改。

Understand you are using SAM but if you are open to consider serverless framework then that's how you can manage it in your CI/CD pipeline with just one command. 了解您正在使用SAM,但是如果您愿意考虑使用无服务器框架,那么您就可以通过一个命令在CI / CD管道中对其进行管理。 All Lambdas will be deployed individually and each function will have dependencies in node_modules that are required by the function 所有Lambda都将单独部署,并且每个功能将在该功能所需的node_modules中具有依赖node_modules

You will need to add following to in your package.json devDependencies. 您将需要在package.json devDependencies中添加以下内容。

"devDependencies": {
    "serverless-plugin-optimize": "^4.0.2-rc.1"
 }

serverless-plugin-optimize is used for optimising Lambda code and its dependencies. serverless-plugin-optimize用于优化Lambda代码及其依赖项。

Note: When using serverless-plugin-optimize you must ensure that all require(..) are placed above module.exports in your handler. 注意:使用serverless-plugin-optimize ,必须确保将所有require(..)放在处理程序中的module.exports上方。

You will also need serverless framework , you can install it globally. 您还将需要无服务器框架 ,您可以在全局安装它。

npm install -g serverless

Repository structure: 仓库结构:

devops-backup/
 - node_modules/
 - package.json
 - serverless.yml
 - handlers/
   - s3.js
   - ebs.js

serverless.yml configuration: serverless.yml配置:

service: DevOpsBackup
frameworkVersion: '>=1.2.1 <2.0.0'
provider:
  name: aws
  runtime: nodejs8.10
  region: eu-west-1
  stage: ${opt:stage} # Stage that you define when deploy using `serverless deploy --stage`
  deploymentBucket: ${env:SLS_BUCKET} # Your deployment bucket name. CI must have access to this bucket.

iamRoleStatements:
  - Effect: Allow
    Action:
      - s3:"*"
    Resource: arn:aws:s3:::<bucket_name>
  # Add more permissions as required by the functions

plugins:
  - serverless-plugin-optimize

package:
  individually: true

functions:
  backupEbs:
    handler: handlers/s3.handler
    description: Create user
  backupS3:
    handler: handlers/ebs.handler
    description: Update user

Example s3.js 例子s3.js

module.exports.handler = async (event) => {
 // ... your code
}

Deployment 部署方式

You can deploy all Lambdas using one command, run it from the repository root. 您可以使用一个命令部署所有Lambda,然后从存储库根目录运行它。

serverless deploy --stage prod -v # prod is the name of stage/env serverless deploy --stage prod -vprod是stage / env的名称

If your build is successful, you will be able to find you functions in Lambda console. 如果构建成功,则可以在Lambda控制台中找到您的功能。

  • DevOpsBackup-prod-backupS3
  • DevOpsBackup-prod-backupEbs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM