AWS Lambda无法在没有VPC的情况下调用另一个AWS Lambda-NodeJs

时间:2019-05-06 04:09:54

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

我有两个AWS Lambda函数,函数A和函数B,
两个Lambda函数都在No VPC中,因为它们都不要求任何VPC资源。
两个Lambda函数都附加了LambdaFull访问角色。

我可以从本地调用并执行Lambda B,
但无法从Lambda A调用或执行LambdaB。

我需要同步行为-

line 1 ..
line 2, invoke lambda, get a response,
line 3 to use the response from line 2

以下是本地和Lambda代码-

1。本地-

let AWS = require('aws-sdk');

let client = new AWS.Lambda({
    region: "us-east-1"
});

let payload = {
    "param1": "ABC",
    "param2": 123
};
payload = JSON.stringify(payload);

let params = {
    FunctionName: 'arn:aws:lambda:us-east-1:awsAccoutNumber:function:test2',
    InvocationType: "RequestResponse", 
    Payload: payload
};

console.log('Invoking Lambda ...');

client.invoke(params, function(err, data) {
    console.log('Lambda invoked!');

    if (err){ 
        console.log('Fail Case');
        console.log(err, err.stack);
    }
    else{     
        console.log('Success Case');
        console.log(data.Payload);
    }
});

2。 Lambda A-

let AWS = require('aws-sdk');

exports.handler = async (event) => {

  let client = new AWS.Lambda({
      region: "us-east-1"
  });

  let payload = {
      "param1": "ABC",
      "param2": 123
  };
  payload = JSON.stringify(payload);

  let params = {
      FunctionName: 'arn:aws:lambda:us-east-1:awsAccountNumber:function:test2',
      InvocationType: "RequestResponse", 
      Payload: payload
  };

  console.log('Payload => \n', payload);
  console.log('\nParams => \n', params);
  console.log('\nAWS => \n', AWS.Lambda);
  console.log('\nClient => \n', client);

  console.log('Invoking Lambda ...');

  client.invoke(params, function(err, data) {
      console.log('Lambda invoked!');

      if (err){ 
          console.log('Fail Case');
          console.log(err, err.stack);
          return err;
      }
      else{     
          console.log('Success Case');
          console.log(data.Payload);
          return data.Payload;
      }
  });

};

3。 Lambda B-

exports.handler = async (event) => {
    console.log("Event => ", event);

    // TODO implement
    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
};

IAM Lambda完全访问策略-

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "cloudformation:DescribeChangeSet",
                "cloudformation:DescribeStackResources",
                "cloudformation:DescribeStacks",
                "cloudformation:GetTemplate",
                "cloudformation:ListStackResources",
                "cloudwatch:*",
                "cognito-identity:ListIdentityPools",
                "cognito-sync:GetCognitoEvents",
                "cognito-sync:SetCognitoEvents",
                "dynamodb:*",
                "ec2:DescribeSecurityGroups",
                "ec2:DescribeSubnets",
                "ec2:DescribeVpcs",
                "events:*",
                "iam:GetPolicy",
                "iam:GetPolicyVersion",
                "iam:GetRole",
                "iam:GetRolePolicy",
                "iam:ListAttachedRolePolicies",
                "iam:ListRolePolicies",
                "iam:ListRoles",
                "iam:PassRole",
                "iot:AttachPrincipalPolicy",
                "iot:AttachThingPrincipal",
                "iot:CreateKeysAndCertificate",
                "iot:CreatePolicy",
                "iot:CreateThing",
                "iot:CreateTopicRule",
                "iot:DescribeEndpoint",
                "iot:GetTopicRule",
                "iot:ListPolicies",
                "iot:ListThings",
                "iot:ListTopicRules",
                "iot:ReplaceTopicRule",
                "kinesis:DescribeStream",
                "kinesis:ListStreams",
                "kinesis:PutRecord",
                "kms:ListAliases",
                "lambda:*",
                "logs:*",
                "s3:*",
                "sns:ListSubscriptions",
                "sns:ListSubscriptionsByTopic",
                "sns:ListTopics",
                "sns:Publish",
                "sns:Subscribe",
                "sns:Unsubscribe",
                "sqs:ListQueues",
                "sqs:SendMessage",
                "tag:GetResources",
                "xray:PutTelemetryRecords",
                "xray:PutTraceSegments"
            ],
            "Resource": "*"
        }
    ]
}

日志仅在行后变为空白-

client.invoke(params, function(err, data) {

“正在调用Lambda ...” 是我在执行功能时获得的最后一条日志

我发现了一些类似的案例,但仍然无法解决问题-
 1. AWS lambda invoke not calling another lambda function - Node.js
 2. Nodejs - Invoke an AWS.Lambda function from within another lambda function
3. invoke aws lambda from another lambda asynchronously

1 个答案:

答案 0 :(得分:3)

问题是您有一个异步lambda,但是您正在返回带有回调的同步函数。

这意味着当AWS调用Lambda A时,它将等待调用print(a[row,col]) array([[ 0, 2], [ 9, 11]]) ,后者立即返回(您的回调将在以后的事件中被调用)。这导致Lambda A完成并且AWS停止执行。

在本地运行时,Node实际上会继续执行,直到完成所有回调为止,这就是在本地成功的原因。

要使其正常工作,您应该返回一个等待的值,例如在Lambda A中:

client.invoke()