【发布时间】:2020-11-17 02:22:57
【问题描述】:
我用 C++ 编写了一个 AWS Lambda 函数,需要通过 HTTP 发布到 IOT 主题。
C++ 函数在本地运行时有效。但是从 Lambda 环境运行时会失败。
我读到了这个帖子:Publishing message from AWS Lambda to AWS IoT,这似乎描述了我想做的事情。
C++ 代码:
options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Trace;
options.loggingOptions.logger_create_fn = GetConsoleLoggerFactory();
Aws::InitAPI(options);
Aws::Client::ClientConfiguration conf;
//conf.connectTimeoutMs = 30000;
//conf.endpointOverride = "xxxx.iot.eu-central-1.amazonaws.com";
Aws::IoTDataPlane::IoTDataPlaneClient client(conf);
Aws::IoTDataPlane::Model::PublishRequest request;
std::shared_ptr<Aws::StringStream> stream = std::make_shared<Aws::StringStream>("Something2 to publish.");
request.SetBody(stream);
request.SetTopic("MyTopic2");
Aws::IoTDataPlane::Model::PublishOutcome outcome(client.Publish(request));
if(outcome.IsSuccess())
{
printf("Success");
}
else
{
printf("%s", outcome.GetError().GetMessage().c_str());
}
Aws::ShutdownAPI(options);
Lambda 函数日志显示:
"....
2020-11-16T01:17:12.559+01:00 [DEBUG] 2020-11-16 00:17:12.559 CurlHandleContainer [140665310484032] Attempting to acquire curl connection.
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:12.559 CurlHandleContainer [140665310484032] No current connections available in pool. Attempting to create new connections.
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:12.559 CurlHandleContainer [140665310484032] attempting to grow pool size by 2
2020-11-16T01:17:13.555+01:00 [INFO] 2020-11-16 00:17:12.559 CurlHandleContainer [140665310484032] Pool grown by 2
2020-11-16T01:17:13.555+01:00 [INFO] 2020-11-16 00:17:12.559 CurlHandleContainer [140665310484032] Connection has been released. Continuing.
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:12.559 CurlHandleContainer [140665310484032] Returning connection handle 0x555555f758c0
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:12.559 CurlHttpClient [140665310484032] Obtained connection handle 0x555555f758c0
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:12.619 CURL [140665310484032] (Text) Trying 52.58.211.191:443...
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:12.619 CURL [140665310484032] (Text) TCP_NODELAY set
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.090 CURL [140665310484032] (Text) After 470ms connect time, move on!
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.090 CURL [140665310484032] (Text) connect to 52.58.211.191 port 443 failed: Connection timed out
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.090 CURL [140665310484032] (Text) Trying 35.156.92.155:443...
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.090 CURL [140665310484032] (Text) TCP_NODELAY set
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.325 CURL [140665310484032] (Text) After 234ms connect time, move on!
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.325 CURL [140665310484032] (Text) connect to 35.156.92.155 port 443 failed: Connection timed out
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.325 CURL [140665310484032] (Text) Trying 18.158.210.208:443...
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.325 CURL [140665310484032] (Text) TCP_NODELAY set
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.442 CURL [140665310484032] (Text) After 117ms connect time, move on!
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.442 CURL [140665310484032] (Text) connect to 18.158.210.208 port 443 failed: Connection timed out
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.442 CURL [140665310484032] (Text) Trying 3.120.61.10:443...
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.442 CURL [140665310484032] (Text) TCP_NODELAY set
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.500 CURL [140665310484032] (Text) After 58ms connect time, move on!
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.500 CURL [140665310484032] (Text) connect to 3.120.61.10 port 443 failed: Connection timed out
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.500 CURL [140665310484032] (Text) Trying 18.195.69.218:443...
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.500 CURL [140665310484032] (Text) TCP_NODELAY set
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.530 CURL [140665310484032] (Text) After 29ms connect time, move on!
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.530 CURL [140665310484032] (Text) connect to 18.195.69.218 port 443 failed: Connection timed out
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.530 CURL [140665310484032] (Text) Trying 18.196.9.115:443...
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.530 CURL [140665310484032] (Text) TCP_NODELAY set
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.544 CURL [140665310484032] (Text) After 14ms connect time, move on!
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.544 CURL [140665310484032] (Text) connect to 18.196.9.115 port 443 failed: Connection timed out
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.544 CURL [140665310484032] (Text) Trying 3.127.140.59:443...
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.544 CURL [140665310484032] (Text) TCP_NODELAY set
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.551 CURL [140665310484032] (Text) After 7ms connect time, move on!
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.551 CURL [140665310484032] (Text) connect to 3.127.140.59 port 443 failed: Connection timed out
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.551 CURL [140665310484032] (Text) Trying 52.57.218.228:443...
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.551 CURL [140665310484032] (Text) TCP_NODELAY set
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.555 CURL [140665310484032] (Text) After 3ms connect time, move on!
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.555 CURL [140665310484032] (Text) connect to 52.57.218.228 port 443 failed: Connection timed out
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.555 CURL [140665310484032] (Text) Failed to connect to data.iot.eu-central-1.amazonaws.com port 443: Connection timed out
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.555 CURL [140665310484032] (Text) Closing connection 0
2020-11-16T01:17:13.555+01:00 [ERROR] 2020-11-16 00:17:13.555 CurlHttpClient [140665310484032] Curl returned error code 7 - Couldn't connect to server
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.555 CurlHandleContainer [140665310484032] Destroy curl handle: 0x555555f758c0
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.555 CurlHandleContainer [140665310484032] Created replacement handle and released to pool: 0x555555f758c0
2020-11-16T01:17:13.555+01:00 [DEBUG] 2020-11-16 00:17:13.555 AWSClient [140665310484032] Request returned error. Attempting to generate appropriate error codes from response
2020-11-16T01:17:13.555+01:00 [ERROR] 2020-11-16 00:17:13.555 AWSClient [140665310484032] HTTP response code: -1
2020-11-16T01:17:13.555+01:00 Resolved remote host IP address:
2020-11-16T01:17:13.555+01:00 Request ID:
2020-11-16T01:17:13.555+01:00 Exception name:
2020-11-16T01:17:13.555+01:00 Error message: curlCode: 7, Couldn't connect to server
..."
Lambda 函数的作用
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"iot:*"
],
"Resource": "*"
}
]
}
我可以登录到同一 VPC 上的 EC2 实例并执行以下操作:
wget https://data.iot.eu-central-1.amazonaws.com
--2020-11-16 00:22:36-- https://data.iot.eu-central-1.amazonaws.com/
Resolving data.iot.eu-central-1.amazonaws.com (data.iot.eu-central-1.amazonaws.com)... 18.184.58.66, 18.194.185.52, 35.158.218.97, ...
Connecting to data.iot.eu-central-1.amazonaws.com (data.iot.eu-central-1.amazonaws.com)|18.184.58.66|:443... connected.
HTTP request sent, awaiting response... 404 Not Found
2020-11-16 00:22:36 ERROR 404: Not Found.
看来可以建立到端点的连接了。
没有出站安全组限制。
我也尝试将 conf.connectTimeoutMs 增加到 30s。事实上,我发现超时需要更长的时间。但仍然没有连接。
【问题讨论】:
标签: c++ amazon-web-services aws-lambda aws-iot