首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法将Node.js AWS函数部署到Docker

无法将Node.js AWS函数部署到Docker
EN

Stack Overflow用户
提问于 2021-07-20 15:35:43
回答 2查看 211关注 0票数 0

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

roles.js

代码语言:javascript
复制
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

代码语言:javascript
复制
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时,我会得到以下错误

代码语言:javascript
复制
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']

现在,我有两个问题。

  1. 如何解决这个问题?
  2. 在AWS中在一个文件中拥有多个函数是一种糟糕的做法吗?
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-07-20 18:03:01

我发现了这个问题,我不得不以sam local invoke HelloWorldFunction的形式调用它。HelloWorldFunction是我需要部署的函数名。

票数 0
EN

Stack Overflow用户

发布于 2021-07-20 16:00:08

看起来,您正在尝试通过AWS为Javascipt构建Lambda函数。您看过AWS for JavaScript V3 DEV 中的AWS示例吗?有关于如何使用JS构建Lambda函数的端到端说明,可以帮助您。

请参阅本主题和TOC中的子主题:

用于JavaScript的AWS的跨服务示例

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68457647

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档