0

서버리스 응용 프로그램을 처음 사용했습니다. 나는 codestar와 lambda를 가진 간단한 nodejs 서버리스 앱을 만들기 위해 aws 튜토리얼을 따라 갔다.서버리스 응용 프로그램의 구조

그러나이 노드 앱이 여러 가지 일을한다고 상상해보십시오. 결과적으로 index.js 내부에는 여러 함수가 있고, 하나는 functionnality A, 하나는 functionnality B 등입니다 (예를 들어).

이 코드 스타 프로젝트에 각 기능마다 하나씩 여러 개의 람다 식을 첨부해야합니까?

답변

1

질문 : 기능마다 하나씩 여러 개의 람다 식을 첨부해야합니까? 이 코드 목록 프로젝트? 답변 : 예

AWS CodeStar 프로젝트 세부 사항 :

AWS 코드 스타 프로젝트 파일 구조 아래에 포함 (reference link) :

README.md - this file 
buildspec.yml - this file is used by AWS CodeBuild to package your service for deployment to AWS Lambda 
app.js - this file contains the sample Node.js code for the web service 
index.js - this file contains the AWS Lambda handler code 
template.yml - this file contains the Serverless Application Model (SAM) used by AWS Cloudformation to deploy your service to AWS Lambda and Amazon API Gateway. 

은 아래처럼 template.yml 파일이 있다고 가정 :

AWSTemplateFormatVersion: 2010-09-09 
Transform: 
- AWS::Serverless-2016-10-31 
- AWS::CodeStar 
Resources: 
    HelloWorld: 
    Type: AWS::Serverless::Function 
    Properties: 
     Handler: index.first_handler 
     Runtime: nodejs4.3 
     Role: 
     Fn::ImportValue: 
      !Join ['-', [!Ref 'ProjectId', !Ref 'AWS::Region', 'LambdaTrustRole']] 
     Events: 
     GetEvent: 
      Type: Api 
      Properties: 
      Path: /first 
      Method: get 
    HelloWorld2: 
    Type: AWS::Serverless::Function 
    Properties: 
     Handler: index.second_handler 
     Runtime: nodejs4.3 
     Role: 
     Fn::ImportValue: 
      !Join ['-', [!Ref 'ProjectId', !Ref 'AWS::Region', 'LambdaTrustRole']] 
     Events: 
     GetEvent: 
      Type: Api 
      Properties: 
      Path: /second 
      Method: get 

위의 tamplate.yml 파일에서 "HelloWor ld "및"HelloWorld2 "구성을 지원합니다.

  • 하여 HelloWorld 구성 "인덱스"하는 index.js의 파일명이고 first_handler하는 index.js이 파일의 방법 즉 "index.first_handler"로 "처리기"값을 포함한다.
  • 마찬가지로 HelloWorld2 구성에는 "index"가 index.js의 파일 이름이고 "second_handler"가 index.js 파일의 메서드 인 "index.second_handler"라는 "Handler"값이 들어 있습니다.

결론 : 당신은 당신의하는 index.js (whatever.js) 파일에 람다 함수의 수를 지정할 수 있습니다

. 람다 함수를 식별하기 위해 적절한 핸들러 만 지정하면됩니다.

희망 사항입니다. 질문에 대한 답변입니다. 당신이 가진다면, 의심의 여지가 물어보십시오!

0

여러 핸들러 기능 (index.js)이 필요하지 않으며 여러 핸들러 기능을 사용할 수 없습니다. 다른 기능이 논리적으로 분리 된 작업을 수행하는 경우 여러 JS 파일을 추가하고 거기에 함수를 쓸 수 있지만 처리기 함수 (index.js)를 참조해야합니다. 또는 index.js 자체에 기능을 작성할 수 있지만 더 좋은 아이디어와 깨끗한 코드는 논리적으로 다른 기능을 다른 파일과 분리하여 참조하는 것입니다.

관련 문제