Azure / iotedge

The IoT Edge OSS project
MIT License
1.46k stars 461 forks source link

Access to iothub connection string dynamically from custom iot edge module. #7137

Closed malavvakharia closed 11 months ago

malavvakharia commented 1 year ago

Expected Behavior

Tell us what should happen

Current Behavior

Tell us what happens instead of the expected behavior

Steps to Reproduce

Provide a detailed set of steps to reproduce the bug. 1. 2. 3. 4.

Context (Environment)

Output of iotedge check

Click here ``` ```

Device Information

Runtime Versions

Note: when using Windows containers on Windows, run docker -H npipe:////./pipe/iotedge_moby_engine version instead

Logs

aziot-edged logs ``` ```
edge-agent logs ``` ```
edge-hub logs ``` ```

Additional Information

Please provide any additional information that may be helpful in understanding the issue.

malavvakharia commented 1 year ago

Just like we can get the deviceId and moduleId dynamically can we get the iot hub connection string?

https://github.com/Azure/iotedge/issues/986

IOTEDGE_DEVICEID IOTEDGE_MODULEID

ggjjj commented 1 year ago

do you want to get the connection string in the module?

malavvakharia commented 1 year ago

do you want to get the connection string in the module?

Hello @ggjjj ,

I want the IOT hub connection string in custom azure module. Not the module connection string.

ggjjj commented 1 year ago

You can build it yourself by quering the values of IOTEDGE_IOTHUBHOST and IOTEDGE_DEVICEID from the env var.

connection_string = f"HostName={iot_hub_hostname};DeviceId={device_id};SharedAccessKey="

malavvakharia commented 1 year ago

Hello @ggjjj ,

So, what is the best practice of defining the SharedAccessKey in code?

And I want iot hub connection string not the device connection string.

Format looks like below: connection_string = f"HostName={iot_hub_hostname};SharedAccessKeyName=;SharedAccessKey="

ggjjj commented 1 year ago

You can set an env variable by setting the output of az iot hub connection-string show and use that in your module to read from

Command Link source - https://learn.microsoft.com/en-us/cli/azure/iot/hub/connection-string?view=azure-cli-latest

david-emakenemi commented 12 months ago

Hey @malavvakharia did you try @ggjjj suggestion above?

david-emakenemi commented 11 months ago

Hey @malavvakharia closing this out since no response but feel free to re-open this if you are still experiencing issues.