【问题标题】:Use IotDataPlaneClient to publish to topic from within an AWS C++ lambda function使用 IotDataPlaneClient 从 AWS C++ lambda 函数中发布到主题
【发布时间】: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


    【解决方案1】:

    事实证明,由于 VPC 配置错误,Lambda 函数无法访问任何公共互联网地址。

    最后我做到了:

    1. 创建 VCP
    2. 创建一个名为“private”的子网
    3. 创建一个名为“public”的子网
    4. 创建一个互联网网关,与“公共”关联
    5. 创建 NAT 网关,关联“public”
    6. 为私有和公共添加路由表
    7. 私有路由表:转发 0.0.0.0/0 到 NAT 网关
    8. 公共路由表将 0.0.0.0/0 转发到 Internet 网关
    9. 将 lambda 函数与“私有”子网关联

    【讨论】:

      猜你喜欢
      • 2019-11-04
      • 2017-02-22
      • 1970-01-01
      • 2019-01-28
      • 1970-01-01
      • 1970-01-01
      • 2018-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多