.NET Core IoT and IotData in the same app - how do I configure this?

0

I'm trying to write a simple .NET core web app that makes some calls to interact with AWS IoT. I wanted to be able to search or list things and get their current shadow. I believe I need to use the IoT service to search and list, and IotData to get thing shadows. I'm running into a problem however where I can't seem to find a configuration that works for both.

If I don't have ServiceURL specified in the Appconfig.Development.json, IotData throws

AmazonClientException: No RegionEndpoint or ServiceURL configured

If I provide ServiceURL with the endpoint listed in our AWS IoT dashboard "ENDPOINT.iot.REGION.amazonaws.com", IotData calls begin to work but the IoT service calls fail with the exception

ResourceNotFoundException: Not Found

If I remove all of the IotData code and add the "Region" field to Appconfig.Development.json with our region, the IoT calls begin to work.

If I uncomment the IotData calls with the "Region" field, I get

AmazonClientException: The RegionEndpoint property is not applicable for AmazonIotDataClient and should not be set.

In the past I've used Boto3 in Python and didn't have to configure any of this this way; the region was specified in ~/.aws/config and never had to provide a Service URL, and I've written scripts that used both services, so I'm very confused as to what the issue is.

I'm also certain I have the correct permissions with the keys I'm using as I'm able to perform all of these operations from the AWS CLI. Does anyone have any recommendations on how to configure this so that both services work?

For reference, this is a simple example of what I'm trying to do: https://gist.github.com/nathan-miller-actigraph/6cd18c99ee2ee4ca05fb5f74598287ae (the actual application is a .NET Core 2.1 SPA).

已提問 5 年前檢視次數 518 次
1 個回答
0

I figured it out. For anyone who runs into this same problem, you can create a second section in your appsettings.json:

    "AwsIotDataConfig": {
        "Profile": "default",
        "ServiceURL": "https://data.iot.us-east-1.amazonaws.com/"
    },
    "AwsConfig": {
        "Profile": "default",
        "Region": "us-east-1"
    }

then in your Startup.cs, when you call GetAWSOptions, you can pass in the name of the config section:

            var awsGenericOptions = Configuration.GetAWSOptions("AwsConfig");
            services.AddDefaultAWSOptions(awsGenericOptions);

            var awsIotDataOptions = Configuration.GetAWSOptions("AwsIotDataConfig");

and when you add the IotData service, specify that it should use the IotData specific options:

            services.AddAWSService<IAmazonIoT>();
            services.AddAWSService<IAmazonIotData>(awsIotDataOptions);
已回答 5 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南