【问题标题】:Unable to deploy the Node.js AWS Lambda function to Docker无法将 Node.js AWS Lambda 函数部署到 Docker
【发布时间】:2021-09-28 03:06:48
【问题描述】:

我正在使用 Node.Js 开发一个 REST API。我的技术栈是 AWS Lambda、API Gateway 和 RDS (MySQL)。下面是我的代码

roles.js

const mysql = require('mysql');
const con = mysql.createConnection({
  host     : "*****.rds.amazonaws.com",
  user     : "*****",
  password : "*****",
  port     : 3306,
  database : "******"
});

exports.lambdaHandler = (event, context, callback) => {
  // allows for using callbacks as finish/error-handlers
  context.callbackWaitsForEmptyEventLoop = false;
  const sql = "select * from role";
  con.query(sql, function (err, result) {
    if (err) throw err;

    var response = {
        "statusCode": 200,
        "headers": {
            "Content-Type": "application/json"
        },
        "body": JSON.stringify(result),
        "isBase64Encoded": false
    };
    callback(null, response)
  });
};

exports.selectRoleByIDHandler = (event, context, callback) => {

    const { id } = event.queryStringParameters;
    console.log("id", id); 

    // allows for using callbacks as finish/error-handlers
    context.callbackWaitsForEmptyEventLoop = false;
    const sql = "select * from role where idRole = "+id;
    con.query(sql, function (err, result) {
      if (err) throw err;
  
      var response = {
          "statusCode": 200,
          "headers": {
              "Content-Type": "application/json"
          },
          "body": JSON.stringify(result),
          "isBase64Encoded": false
      };
      callback(null, response)
    });
  };

下面是我的template.yaml

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  node2

  Sample SAM Template for node2
  
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 100
    VpcConfig:
        SecurityGroupIds:
          - sg-sdsdsdsd
        SubnetIds:
          - subnet-ssdsds

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: hello-world/
      Handler: app.lambdaHandler
      Runtime: nodejs14.x
      Events:
        HelloWorld:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /hello
            Method: get
      Role: !GetAtt LambdaRole.Arn
      
  RoleFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: hello-world/
      Handler: roles.lambdaHandler
      Runtime: nodejs14.x
      Events:
        HelloWorld:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /roles
            Method: get
      Role: !GetAtt LambdaRole.Arn

  SelectRolesByIDFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: hello-world/
      Handler: roles.selectRoleByIDHandler
      Runtime: nodejs14.x
      Events:
        HelloWorld:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /selectRoleByIDHandler
            Method: get
      Role: !GetAtt LambdaRole.Arn
 
  LambdaRole:
    Type: 'AWS::IAM::Role'
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action:
              - 'sts:AssumeRole'
      Path: /
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
      Policies:
        - PolicyName: root
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: Allow
                Action:
                  - ec2:DescribeNetworkInterfaces
                  - ec2:CreateNetworkInterface
                  - ec2:DeleteNetworkInterface
                  - ec2:DescribeInstances
                  - ec2:AttachNetworkInterface
                Resource: '*'

当我尝试执行sam local invoke 时,出现以下错误

Error: You must provide a function logical ID when there are more than one functions in your template. Possible options in your template: ['HelloWorldFunction', 'RoleFunction', 'SelectRolesByIDFunction']

现在,我有 2 个问题。

  1. 如何解决这个问题?
  2. 在 AWS Lambda 的一个文件中包含多个函数是一种不好的做法吗?

【问题讨论】:

    标签: javascript node.js amazon-web-services aws-lambda aws-api-gateway


    【解决方案1】:

    您似乎正在尝试通过 AWS SDK for Javascipt 构建 Lambda 函数。您是否查看过 AWS SDK for JavaScript V3 DEV Guide 中的 AWS 示例。有关于如何使用可以帮助您的 JS API 构建 Lambda 函数的端到端说明。

    查看此主题和目录中的子主题:

    Cross-service examples for the AWS SDK for JavaScript

    【讨论】:

    • 感谢您的回复。这在部署到 aws 时工作正常。唯一的问题是当它在本地环境中时,Docker。我实际上为 Node.JS 编辑了 SAM Hello World 模板,所以我猜设置和一切都很好。我没有添加数据库创建部分等,我在这个项目中不需要。
    • 我发现了这个问题,我不得不将其调用为 sam local invoke HelloWorldFunctionHelloWorldFunction 是我需要部署的函数名。你知道我怎样才能调用整个函数集,而不是一个一个地调用吗?
    【解决方案2】:

    我发现了问题,我必须将其调用为 sam local invoke HelloWorldFunctionHelloWorldFunction 是我需要部署的函数名。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-17
      • 2019-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-12
      相关资源
      最近更新 更多