AWS IoT Device SDK V2 for Python
μλ° μΈμ΄κ° IoT λλ°μ΄μ€λ₯Ό μν΄ λ§λ€μ΄μ§ κ²μΌλ‘ μλ €μ Έ μμ§λ§ μ€μ λ‘λ λΌμ¦λ² 리νμ΄μ κ°μ μν λλ°μ΄μ€μ νμ΄μ¬ λλ Cλ‘ μμ±λ μ ν리μΌμ΄μ μΌλ‘ μμ±λμ΄ ν¬ν¨λλ κ² κ°λ€. μλ₯Ό λ€μ΄, νμ΄μ¬ μΈμ΄λ‘ AWS IoT μ ν΅μ νλ μ ν리μΌμ΄μ μ λ§λ€λλ AWS IoT Device SDK v2 for Pythonλ₯Ό μ¬μ©ν μ μλ€. basic-connect.py λλ pubsub.py μν μ½λλ₯Ό μ°Έκ³ νμ¬ ν΄λΌμ΄μΈνΈ λλ°μ΄μ€λ‘ μμ§λ λ°μ΄ν°λ₯Ό AWS IoTλ₯Ό ν΅ν΄ λͺ¨λν°λ§ μμ€ν μΌλ‘ μ λ¬ν μ μμμ ν μ€νΈ ν΄λ³Ό μ μλ€. νμ΄μ¬ μΈμ΄λ ν μ€νΈ μμ§λμ΄λ μ¬μ©νλ λ²μ©μ μΈ μΈμ΄μ΄κΈ° λλ¬Έμ κ°λ°μμ QA λͺ¨λ μ¬μ©ν μ μλ ν μ€νΈ μ½λκ° λλ€.
νμ΄μ¬ νκ²½ μ€λΉνκΈ°
νμ΄μ¬ μ½λλ₯Ό μ€ννκΈ° μν΄μλ νμ΄μ¬ κ°μ νκ²½
μ μ€λΉν΄μΌνλ€. νμ΄μ¬μ κ°μ νκ²½μ ꡬμ±νλ 건 μκ°λ³΄λ€ μ½μ§ μμ λΆλΆμΌ μ μκΈ° λλ¬Έμ νμ΄μ¬μ μ£Όλ‘ λ€λ£¨μ§ μλ κ°λ°μλΌλ©΄ λ¬μ€νΈλ‘ μμ±λ UVλ₯Ό νμ΄μ¬ ν¨ν€μ§ κ΄λ¦¬μλ‘ μ€μΉνμ¬ νμ΄μ¬ κ°μ νκ²½(.venv)λ₯Ό μ½κ² ꡬμ±νλκ² μ’μ κ² κ°λ€. μ°Έκ³ λ‘, μ¬λ¬κ°μ§ νμ΄μ¬μΌλ‘ μμ±λ MCPλ₯Ό μ€μΉν λμλ UV λͺ
λ Ήμ΄κ° μ¬μ©λκ³ μλ€.
Windows TerminalPS C:\Users\Mambo> uv python install 3.12 Installed Python 3.12.9 in 7.79s + cpython-3.12.9-windows-x86_64-none PS C:\Users\Mambo> uv python pin 3.12 Pinned `.python-version` to `3.12` PS C:\Users\Mambo> uv python dir C:\Users\Mambo\AppData\Roaming\uv\python PS C:\Users\Mambo> uv init --app awsiot Initialized project `awsiot` at `C:\Users\Mambo\awsiot` PS C:\Users\Mambo\awsiot> uv run main.py Using CPython 3.12.9 Creating virtual environment at: .venv Hello from awsiot!
μΌλ°μ μΌλ‘ PyCharmμ μ¬μ©ν΄μ νμ΄μ¬ νλ‘μ νΈλ₯Ό μ΄κ² λλ©΄ Python μΈν°ν리ν°λ₯Ό μ€μ ν΄μΌ λ©λλ€.
UV λ₯Ό μ¬μ©νλ νλ‘μ νΈλ νμ΄μ¬ κ°μ νκ²½μ΄ μλμΌλ‘ ꡬμ±λμκΈ° λλ¬Έμ Python μΈν°ν리ν°κ° κΈ°λ³Έ μ€μ λ¨μ νμΈν μ μμ΅λλ€.
νμ΄μ¬ ν¨ν€μ§ μ€μΉνκΈ°
νμ΄μ¬ μ½λμμ νμμ μΌλ‘ μ¬μ©ν΄μΌν AWS IoT Device SDK v2 for Python (awsiotsdk)μ ν¨κ» νκ²½ λ³μλ₯Ό νμΌλ‘ μ μ©ν μ μλ python-dotenv λ₯Ό μ€μΉνμ. python-dotenv λ λ°λμ νμνμ§ μμ§λ§ AWS IoT Endpoint μ ClientID μ κ°μ μΌλΆ μ 보λ€μ νλμ½λ©νλ κ²λ³΄λ€λ λμ κ²μ΄λ€.
Windows TerminalPS C:\Users\Mambo\awsiot> uv add awsiotsdk python-dotenv Resolved 4 packages in 57ms Installed 3 packages in 16ms + awscrt==0.27.4 + awsiotsdk==1.24.0 + python-dotenv==1.1.1
mqtt_connection_builder λ‘ AWS IoTμ μ°κ²°νκΈ°
μμ μ½λ μ€ basic_connect.py λ₯Ό 보면 awsiotsdk μ mqtt_connection_builderλ₯Ό μ¬μ©νλ©° MQTT μ°κ²°μ μμ±ν μ μμμ μ μ μλ€. μλμ κ°μ΄ μ½λλ₯Ό μμ±νκ³ νμ΄μ¬ μ½λλ₯Ό μ€νν΄λ³΄λ©΄ AWS IoT μλν¬μΈνΈμ μ°κ²°λκ³ λμ μ’ λ£λ¨μ μ μ μλ€.
main.pyimport os from awscrt import mqtt from awsiot import mqtt_connection_builder from dotenv import load_dotenv load_dotenv() ENDPOINT = os.getenv("ENDPOINT") # iot:Data-ATS endpoint CLIENT_ID = os.getenv("CLIENT_ID") TOPIC = os.getenv("TOPIC") CERTIFICATE_PATH = os.getenv("CERTIFICATE_PATH") PRIVATE_KEY_PATH = os.getenv("PRIVATE_KEY_PATH") ROOT_CA_PATH = os.getenv("ROOT_CA_PATH") if __name__ == '__main__': print("\nStarting MQTT PubSub sample...") # Create a MQTT connection mqtt_connection = mqtt_connection_builder.mtls_from_path( endpoint=ENDPOINT, ca_filepath=ROOT_CA_PATH, cert_filepath=CERTIFICATE_PATH, pri_key_filepath=PRIVATE_KEY_PATH, client_id=CLIENT_ID, ) # Connect MQTT Broker... print(f"Connecting to {ENDPOINT} with client ID '{CLIENT_ID}'...") connect_future = mqtt_connection.connect() connect_future.result() # Future.result() waits until a result is available print("Connected!")
Windows Terminal(awsiot) PS C:\Users\Mambo\awsiot> uv run main.py Starting MQTT PubSub sample... Connecting to $prefix$-ats.iot.ap-northeast-2.amazonaws.com with client ID '$client_id$'... Connected!
Topic ꡬλ νκ³ MQTT λ©μμ§ κ²μνκΈ°
μμ μ½λ μ€ pubsub.py λ₯Ό μ°Έκ³ νλ©΄ mqtt_connection μ subscribe ν¨μλ‘ λ©μμ§λ₯Ό ꡬλ νκ³ publish ν¨μλ‘ λ©μμ§λ₯Ό κ²μνλ κ²μ μ μ μλ€.
main.pyimport os import time from awscrt import mqtt from awsiot import mqtt_connection_builder from dotenv import load_dotenv load_dotenv() # AWS IoT Core ENDPOINT = os.getenv("ENDPOINT") # iot:Data-ATS endpoint CLIENT_ID = os.getenv("CLIENT_ID") # Recommended Thing ID TOPIC = os.getenv("TOPIC") # AWS IoT Core - Thing Certificate CERTIFICATE_PATH = os.getenv("CERTIFICATE_PATH") PRIVATE_KEY_PATH = os.getenv("PRIVATE_KEY_PATH") ROOT_CA_PATH = os.getenv("ROOT_CA_PATH") # Optional def on_message_received(topic, payload): print(f"Received message from {topic}: \n<<< {payload.decode()}") if __name__ == '__main__': print("\nStarting MQTT PubSub sample...") # Create a MQTT connection mqtt_connection = mqtt_connection_builder.mtls_from_path( endpoint=ENDPOINT, ca_filepath=ROOT_CA_PATH, cert_filepath=CERTIFICATE_PATH, pri_key_filepath=PRIVATE_KEY_PATH, client_id=CLIENT_ID, ) # Connect MQTT Broker... print(f"Connecting to {ENDPOINT} with client ID '{CLIENT_ID}'...") connect_future = mqtt_connection.connect() connect_future.result() # Future.result() waits until a result is available print("Connected!") # Subscribe print(f"Subscribing to topic '{TOPIC}'...") subscribe_future, packet_id = mqtt_connection.subscribe( topic=TOPIC, qos=mqtt.QoS.AT_LEAST_ONCE, callback=on_message_received) subscribe_result = subscribe_future.result() print(f"Subscribed with {subscribe_result}") # Publish print(f"Publishing message to topic '{TOPIC}'...") payload = "PING" mqtt_connection.publish( topic=TOPIC, payload=payload.encode('ascii'), qos=mqtt.QoS.AT_LEAST_ONCE) print(f"Published message to {TOPIC}: \n>>> {payload}") time.sleep(2) # Disconnect print("Disconnecting...") disconnect_future = mqtt_connection.disconnect() disconnect_future.result() print("Disconnected!")
Windows Terminal(awsiot) PS C:\Users\Mambo\awsiot> uv run main.py Starting MQTT PubSub sample... Connecting to $prefix$-ats.iot.ap-northeast-2.amazonaws.com with client ID '$client_id$'... Connected! Subscribing to topic '$topic$'... Subscribed with {'packet_id': 1, 'topic': '$topic$', 'qos': <QoS.AT_LEAST_ONCE: 1>} Publishing message to topic '$topic$'... Published message to $topic$: >>> PING Received message from $topic$: <<< PING Disconnecting... Disconnected!
λ©μμ§ κ²μμ λμΌν ν ν½μ ꡬλ
ν κ²μΌλ‘ MQTT λ©μμ§ λΈλ‘μ»€μΈ AWS IoT μμ λ©μμ§λ₯Ό μ λ¬λ¨μ νμΈνλ€. λ€λ§, AWS IoT λ‘ κ²μλ λ©μμ§κ° λͺ¨λν°λ§ μμ€ν
μμ μꡬνλ νμ΄λ‘λ νμμ λ§λμ§λ μ μ μλ€. AWS IoT μ λ©μμ§ λΌμ°ν
κ·μΉ
μ λ§λ μ£Όμ μ λ©μμ§λ₯Ό κ²μνκ³ (λ©μμ§λ₯Ό SQS λκΈ°μ΄λ‘ μ μ‘νλλ‘ μ€μ νλ€λ©΄) Amazon SQS μ κ²μλ λ©μμ§κ° μ λ¬λλμ§κΉμ§ νμΈν΄μΌνλ€. λν, κ²μν λ©μμ§μ ν¬ν¨λ λ°μ΄ν°κ° λͺ¨λν°λ§ μμ€ν
μμ νμΈν μ μλ κ°λ¨ν μΉνμ΄μ§λ₯Ό κ°λ°νλ κ²λ μ’μ λ°©λ²μ΄λ€.
AWS IoT λ£¨νΈ CA μΈμ¦μ
ν
μ€νΈ μ½λμμ λ£¨νΈ CA μΈμ¦μκ° λ°λμ νμν κ²μ μλμ§λ§ μ»΄ν¨ν° νκ²½λ§λ€ μ λ’° κ°λ₯ν μΈμ¦μ λͺ©λ‘μ ν¬ν¨λμ΄μμ§ μμ μ μκΈ° λλ¬Έμ μνΈ μΈμ¦(mTLS)
μ μννκΈ° μν΄μ λͺ
μμ μΌλ‘ μ§μ ν νμκ° μλ€. AWS IoT μμ μΈμ¦μλ₯Ό λ€μ΄λ‘λ λ°κ² λλ©΄ Amazon Trust Services Repository μμ μ 곡νλ AmazonRootCA1.pem μ AmazonRootCA3.pemκ° ν¬ν¨λλλ° RSA 2048λΉνΈ ν€μ ν΄λΉλλ AmazonRootCA1 κ° μΌλ°μ μΌλ‘ μ¬μ©λλ€. νμ¬ λ΄ κ°λ° νκ²½μΌλ‘ ꡬμ±λ AWS IoT μ μ°κ²°ν΄λ³Έ κ²°κ³Όλ‘λ ECC 256λΉνΈ ν€μ ν΄λΉλλ AmazonRootCA3λ₯Ό μ¬μ©νλ awscrt.exceptions.AwsCrtError: AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE: TLS (SSL) negotiation failed
λΌλ μ€λ₯κ° λ°μνλ€.