NodeJS 如何从位于不同模板中的另一个Lambda访问一个AWS Lambda?

ca1c2owp  于 2023-06-22  发布在  Node.js
关注(0)|答案(1)|浏览(110)

我正在使用AWS lambda和AWS SAM。我的SAM CLI版本是1.39.0。我有两个模板文件。它们是“template-outbound.yaml”和“template-user-related.yaml”。在“template-outbound.yaml”中,我有一个名为“SendEmailFunction”的函数。下面是AWS SAM代码。

SendEmailFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: SendEmailFunction
      CodeUri: xxx-restapi/
      Handler: source/email/send-email.sendEmail
      Runtime: nodejs14.x
      Events:
        SendEmailAPIEvent:
          Type: HttpApi
          Properties:
            Path: /email/send-email
            Method: post
            ApiId: !Ref AuthGatewayHttpApiOutbound

在“template-user-related.yaml”中,我有一个名为“LoginFunction”的函数。下面是它的代码。

LoginFunction:
    Type: AWS::Serverless::Function 
    Properties:
      FunctionName: LoginFunction
      CodeUri: xxx-restapi/
      Handler: source/user/login.login
      Runtime: nodejs14.x
      Events:
        LoginAPIEvent:
          Type: HttpApi 
          Properties:
            Path: /user/login
            Method: get
            ApiId: !Ref AuthGatewayHttpApi2

下面是我的NodeJS代码,从“LoginFunction”访问“SendEmailFunction”。

try {

        var lambda = new AWS.Lambda({
            region: 'us-east-1'
        });

        var params = {
            FunctionName: 'SendEmailFunction',
            InvocationType: 'RequestResponse',
            LogType: 'Tail',
            Payload: JSON.stringify({
                "text": text            })
        };

        const result = await lambda.invoke(params).promise();
        const response = JSON.parse(result.Payload);
        console.log(response);

        let fraudDetectedJson = {
            "statusCode": 400,
            "headers": {
                "Content-Type": "application/json"
            },
            "body": JSON.stringify({
                "error": "personal information detected"
            }),
            "isBase64Encoded": false
        };

    } catch (error) {
        let errorJson = {
            "statusCode": 400,
            "headers": {
                "Content-Type": "application/json"
            },
            "body": JSON.stringify({
                "error": "error "
            }),
            "isBase64Encoded": false
        };
        console.log(error);
        return errorJson;
    }

我需要从“LoginFunction”执行“SendEmailFunction”。如果这两个函数都在“template-outbound.yaml”中,我可以毫无问题地执行它,如下所示。

LoginFunction:
    Type: AWS::Serverless::Function 
    Properties:
      FunctionName: LoginFunction
      CodeUri: xxx-restapi/
      Handler: source/user/login.login
      Runtime: nodejs14.x
      Events:
        LoginAPIEvent:
          Type: HttpApi 
          Properties:
            Path: /user/login
            Method: get
            ApiId: !Ref AuthGatewayHttpApi2
      Policies: 
        - LambdaInvokePolicy:
            FunctionName:
              !Ref SendEmailFunction
      Environment:
        Variables:
          CREATE_EMAIL_MICROSERVICE_FUNCTION: !Ref SendEmailFunction

我需要在我的模板文件中做什么改变来执行“template-outbound.yaml”中的函数从“template-user-related.yaml”中的函数?

w8rqjzmb

w8rqjzmb1#

Lambda的名称不是您在模板文件中定义的SendEmailFunction。它通常看起来像SendEmailFunction-<some_id>。您应该避免将函数名/ARN硬编码到AWS SSM中,然后通过内部函数从“template-user-related.yaml”引用该变量。
定义SSM参数,如下所示:

SendEmailFunctionName:
    Type: AWS::SSM::Parameter
    Properties:
      Name: "SOMETHING"
      Type: String
      Value: !Select [1, !Split ["/", !GetAtt SendEmailFunction.Arn]]

在另一个模板中,您可以使用!Sub引用它:

Environment:
    Variables:
      CREATE_EMAIL_MICROSERVICE_FUNCTION:  !Sub "{{resolve:ssm:/SOMETHING}}"

相关问题