Skip to content

Integrate Cognito with IoT core

0

I have set up the Cognito user pool and identity pool. My identity pool is attached to the role with AWSIoTDataAccess and AWSIoTConfigAccess. I can log in using Cognito user pool and get credentials from the identity pool and assume the role correctly (the credentials can list IoT things, or access to S3 with modify policy). However, I cannot use this credential to connect to the IoT core endpoint via MQTT over WebSocket. When I view the log message in CloudWatch, the message indicates that authentication failed. Please help me solve this problem. I use the Python SDK for my project.

Thank you in advance.

1 Answer
1
Accepted Answer

It seems like you've set up most of the necessary components correctly, but you're missing a crucial step in the integration of Cognito with IoT Core. Even though you have the correct IAM permissions attached to your Cognito Identity Pool role, you still need to attach an AWS IoT policy to the authenticated Cognito identity.

Here's what you need to do:

  1. Create an AWS IoT policy that defines the permissions for your IoT operations. This policy should include permissions for connecting, publishing, and subscribing as needed for your application.

  2. After a user authenticates through Cognito and you obtain the Cognito Identity ID, you need to attach the IoT policy to this identity. You can do this using the AWS IoT Core AttachPolicy API.

Here's a general outline of the process in Python:

import boto3

# Assume you have already authenticated the user and obtained Cognito credentials

# Create an IoT client using the Cognito credentials
iot_client = boto3.client('iot', region_name='your-region', 
aws_access_key_id=credentials['AccessKeyId'],
aws_secret_access_key=credentials['SecretKey'],
aws_session_token=credentials['SessionToken'])

# Attach the IoT policy to the Cognito Identity
iot_client.attach_policy(
policyName='YourIoTPolicyName',
target=cognito_identity_id
)

After attaching the policy, your authenticated Cognito user should be able to connect to IoT Core via MQTT over WebSocket.

Remember to create an appropriate IoT policy that grants the necessary permissions for your use case. The policy should allow actions like 'iot:Connect', 'iot:Publish', 'iot:Subscribe', and 'iot:Receive' on the appropriate resources.

If you're still encountering issues after attaching the IoT policy, double-check your IoT policy to ensure it grants the correct permissions, and verify that you're using the correct IoT endpoint for your region when attempting to connect.
Sources
Amazon Cognito identities - AWS IoT Core
Authorization with Amazon Cognito identities - AWS IoT Core
Connecting and Authenticating Automotive iOS App to AWS IoT Core | AWS for Industries

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.