简体   繁体   English

节点 Lambda AWS TimeoutError:套接字超时,未建立与 cloudformation 的连接

[英]Node Lambda AWS TimeoutError: Socket timed out without establishing a connection to cloudformation

I am running a Node(12.x) Lambda in AWS.我在 AWS 中运行 Node(12.x) Lambda。 The purpose of this lambda is to interact with Cloudformation stacks, and I'm doing that via the aws-sdk.这个 lambda 的目的是与 Cloudformation 堆栈进行交互,我正在通过 aws-sdk 进行此操作。 When testing this lambda locally using lambda-local , it executes successfully and the stack can be seen in CREATING state in AWS console.当使用lambda-local在本地测试这个 lambda 时,它成功执行并且可以在 AWS 控制台的CREATING state 中看到堆栈。 However, when I push and run this lambda in AWS, it fails after 15 seconds, and I get this error:但是,当我在 AWS 中推送并运行此 lambda 时,它在 15 秒后失败,并且出现此错误:

{"errorType":"TimeoutError","errorMessage":"Socket timed out without establishing a connection","code":"TimeoutError","message":"Socket timed out without establishing a connection","time":"2020-06-29T03:10:27.668Z","region":"us-east-1","hostname":"cloudformation.us-east-1.amazonaws.com","retryable":true,"stack":["TimeoutError: Socket timed out without establishing a connection","    at Timeout.connectTimeout [as _onTimeout] (/var/task/node_modules/aws-sdk/lib/http/node.js:69:15)","    at listOnTimeout (internal/timers.js:549:17)","    at processTimers (internal/timers.js:492:7)"]}

This lead me to investigate the lambda timeout and the possible configuration changes I could make found in https://aws.amazon.com/premiumsupport/knowledge-center/lambda-function-retry-timeout-sdk/ and https://aws.amazon.com/premiumsupport/knowledge-center/lambda-vpc-troubleshoot-timeout/ but nothing worked. This lead me to investigate the lambda timeout and the possible configuration changes I could make found in https://aws.amazon.com/premiumsupport/knowledge-center/lambda-function-retry-timeout-sdk/ and https://aws .amazon.com/premiumsupport/knowledge-center/lambda-vpc-troubleshoot-timeout/但没有任何效果。

I found a couple of similar issues such as AWS Lambda: Task timed out which include possible suggestions such as lambda timeout and lambda memory issues, but Ive set mine to 30 seconds and the logs show max memory used is 88MB out of possible 128MB, but I tried with an increase anyway, and no luck. I found a couple of similar issues such as AWS Lambda: Task timed out which include possible suggestions such as lambda timeout and lambda memory issues, but Ive set mine to 30 seconds and the logs show max memory used is 88MB out of possible 128MB, but无论如何,我尝试增加,但没有运气。

The curious part is that it fails without establishing a connection to hostname cloudformation.us-east-1.amazonaws.com .奇怪的是,它没有与主机名cloudformation.us-east-1.amazonaws.com建立连接就失败了。 How is that possible when the role assigned to the lambda has full Cloudformation privileges?当分配给 lambda 的角色拥有完整的 Cloudformation 权限时,这怎么可能? I'm completely out of ideas so any help would be greatly appreciated.我完全没有想法,所以任何帮助将不胜感激。 Heres my code:这是我的代码:

TEST EVENT:测试事件:

{
  "stackName": "mySuccessfulStack",
  "app": "test"
}

Function my handler calls (createStack): Function 我的处理程序调用(createStack):

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

const templates = {
    "test": {
      TemplateURL: "https://<bucket>.s3.amazonaws.com/<path_to_file>/test.template",
      Capabilities: ["CAPABILITY_IAM"],
      Parameters: {
        "HostingBucket": "test-hosting-bucket"
      }
    }
}

async function createStack(event) {
  AWS.config.update({
    maxRetries: 2,
    httpOptions: {
      timeout: 30000,
      connectTimeout: 5000
    }
  });
  const cloudformation = new AWS.CloudFormation();
  const { app, stackName } = event;
  let stackParams = templates[app];
  stackParams['StackName'] = app + "-" + stackName;
  let formattedTemplateParams = [];
  for (let [key, value] of Object.entries(stackParams.Parameters)) {
    formattedTemplateParams.push({"ParameterKey":key, "ParameterValue": value})
  }
  stackParams['Parameters'] = formattedTemplateParams;
  const result = await cloudformation.createStack(stackParams).promise();
  return result;
}

Lambda function in a VPC does not public IP address nor internet access. VPC 中的 Lambda function 不公开 IP 地址或互联网访问权限。 From docs :来自文档

Connect your function to private subnets to access private resources.将您的 function 连接到私有子网以访问私有资源。 If your function needs internet access, use NAT.如果您的 function 需要互联网访问,请使用 NAT。 Connecting a function to a public subnet does not give it internet access or a public IP address .将 function 连接到公共子网不会为其提供Internet 访问权限或公共 IP 地址

There are two common solutions for that:两种常见的解决方案

  • place lambda function in a private subnet and setup NAT gateway in public subnet.将 lambda function 放在私有子网中,并在公共子网中设置NAT 网关 Then set route table from private subnet to the NAT device.然后设置从私有子网到 NAT 设备的路由表。 This will enable the lambda to access the internet and subsequently CloudFormation service.这将使 lambda 能够访问互联网并随后访问 CloudFormation 服务。
  • setup a VPC interface endpoint for CloudFormation . 为 CloudFormation 设置 VPC 接口端点 This will allow your lambda function in private subnet to access CloudFormation without the internet .这将允许您在私有子网中的 lambda function无需互联网即可访问 CloudFormation。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM