{"library":"awscrt","code":"import os\nimport time as t\nfrom awscrt import io, mqtt, auth, http\nfrom awsiot import mqtt_connection_builder\n\n# Replace with your AWS IoT Core endpoint, client ID, and certificate paths\nENDPOINT = os.environ.get(\"AWS_IOT_ENDPOINT\", \"YOUR_AWS_IOT_ENDPOINT\")\nCLIENT_ID = \"testDevice\"\nPATH_TO_CERTIFICATE = os.environ.get(\"AWS_IOT_CERT_PATH\", \"certificates/certificate.pem.crt\")\nPATH_TO_PRIVATE_KEY = os.environ.get(\"AWS_IOT_PRIVATE_KEY_PATH\", \"certificates/private.pem.key\")\nPATH_TO_AMAZON_ROOT_CA_1 = os.environ.get(\"AWS_IOT_ROOT_CA_PATH\", \"certificates/root-CA.pem\")\nTOPIC = \"test/topic\"\nMESSAGE = \"Hello from awscrt Python!\"\n\n# Spin up resources\nevent_loop_group = io.EventLoopGroup(1)\nhost_resolver = io.DefaultHostResolver(event_loop_group)\nclient_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver)\n\nmqtt_connection = mqtt_connection_builder.mtls_from_path(\n    endpoint=ENDPOINT,\n    cert_filepath=PATH_TO_CERTIFICATE,\n    pri_key_filepath=PATH_TO_PRIVATE_KEY,\n    ca_filepath=PATH_TO_AMAZON_ROOT_CA_1,\n    client_bootstrap=client_bootstrap,\n    client_id=CLIENT_ID,\n    clean_session=False,\n    keep_alive_secs=30\n)\n\nprint(f\"Connecting to {ENDPOINT} with client ID '{CLIENT_ID}'...\")\nconnect_future = mqtt_connection.connect()\nconnect_future.result()\nprint(\"Connected!\")\n\nprint(f\"Publishing message to topic '{TOPIC}': {MESSAGE}\")\nmqtt_connection.publish(topic=TOPIC, payload=MESSAGE, qos=mqtt.QoS.AT_LEAST_ONCE).result()\nprint(\"Published!\")\n\n# Disconnect\ndisconnect_future = mqtt_connection.disconnect()\ndisconnect_future.result()\nprint(\"Disconnected!\")\n","lang":"python","description":"This quickstart demonstrates how to establish an MQTT connection to AWS IoT Core and publish a message using `awscrt`. It sets up basic I/O resources, builds an mTLS connection, connects, publishes a message, and then disconnects. Environment variables are used for sensitive credentials and endpoint configuration.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}