Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pub/Sub entering ConnectionDisrupted state, no errors #11304

Open
3 tasks done
Nikola-Milovic opened this issue Apr 26, 2023 · 13 comments
Open
3 tasks done

Pub/Sub entering ConnectionDisrupted state, no errors #11304

Nikola-Milovic opened this issue Apr 26, 2023 · 13 comments
Assignees
Labels
documentation Related to documentation feature requests PubSub Related to PubSub category question General question

Comments

@Nikola-Milovic
Copy link

Nikola-Milovic commented Apr 26, 2023

Before opening, please confirm:

JavaScript Framework

React

Amplify APIs

PubSub

Amplify Categories

auth

Environment information

 System:
    OS: Linux 5.15 Ubuntu 20.04.5 LTS (Focal Fossa)
    CPU: (16) x64 AMD Ryzen 7 2700 Eight-Core Processor
    Memory: 17.90 GB / 31.30 GB
    Container: Yes
    Shell: 5.0.17 - /usr/bin/bash
  Binaries:
    Node: 18.14.2 - ~/.nvm/versions/node/v18.14.2/bin/node
    Yarn: 1.22.19 - /usr/bin/yarn
    npm: 9.5.0 - ~/.nvm/versions/node/v18.14.2/bin/npm
    Watchman: 2023.03.20.00 - /home/linuxbrew/.linuxbrew/bin/watchman
  Browsers:
    Brave Browser: 107.1.45.118
    Chrome: 107.0.5304.87
    Chromium: 112.0.5615.49
    Firefox: 112.0.1
  npmGlobalPackages:
    corepack: 0.15.3
    npm: 9.5.0
    pnpm: 8.1.1

Describe the bug

When trying to connect to AWS IoT with Amplify and Pub/Sub I am stuck in the ConnectionDisturbed -> Connection loop

[INFO] 43:52.175 Main - Connecting
ConsoleLogger.ts:105 [INFO] 43:52.339 Main - ConnectionDisrupted
ConsoleLogger.ts:105 [INFO] 44:52.174 Main - Connecting
ConsoleLogger.ts:105 [INFO] 44:52.350 Main - ConnectionDisrupted
ConsoleLogger.ts:105 [INFO] 45:52.174 Main - Connecting
ConsoleLogger.ts:105 [INFO] 45:52.345 Main - ConnectionDisrupted

Expected behavior

It should be in connected state and stay in it

Reproduction steps

Have my CDK configuration with my amplify.ts file

Code Snippet

My setup is as follows

amplify.ts file that I import in the entrypoint to my app

Logger.LOG_LEVEL = 'DEBUG' ;

try {
	const cancel = Hub.listen('pubsub', (data: any) => {
		 console.log('PubSub', data) 
	});

	Amplify.configure({
		Auth: {
			region: env.AWS_REGION,
			userPoolId: env.AWS_COGNITO_USER_POOL_ID,
			userPoolWebClientId: env.AWS_COGNITO_ADMIN_USER_POOL_CLIENT_ID,
			identityPoolId: env.AWS_COGNITO_IDENTITY_POOL_ID,
			identityPoolRegion: env.AWS_REGION,
			mandatorySignIn: true
		},
        }


	Amplify.addPluggable(
		new AWSIoTProvider({
			aws_pubsub_region: env.AWS_REGION,
			aws_pubsub_endpoint: `wss://${env.AWS_IOT_ENDPOINT}/mqtt`,
		})
	);
} catch (error) {
	console.error("error occured during amplify setup", error);
}

All of my env variables are correct, I also have geo setup, and its working.

The endpoint also matches the one in IoT Core -> Settings.

This is my endpoint wss://a3ri7xxxxxxxxx-ats.iot.eu-central-1.amazonaws.com/mqtt

I receive no errors other than the connection being disrupted. If I try to publish I get [undefined] back without any errors.


I am not using the CLI but my own CDK and I have this

		const iotPolicy = new iam.Policy(this, `iot-amplify-policy`, {
			statements: [
				new iam.PolicyStatement({
					effect: iam.Effect.ALLOW,
					actions: ['iot:*'],
					resources: [`arn:aws:iot:${appConfig.region}:${appConfig.account}:*`],
				}),
			],
		});

		const { authenticatedRole } = props;

		authenticatedRole.attachInlinePolicy(iotPolicy);

		authenticatedRole.addManagedPolicy(
			iam.ManagedPolicy.fromAwsManagedPolicyName('AWSIoTDataAccess')
		);
		authenticatedRole.addManagedPolicy(
			iam.ManagedPolicy.fromAwsManagedPolicyName('AWSIoTConfigAccess')
		);

So my Authenticated users should have access to the IoT.

Log output

// Put your logs below this line


Additional information

There was an issue that mentions something related to access to Pub/Sub IoT but it was closed and not answered #749, doesn't seem to be the issue I am experiencing

Also if I try to connect over plain MQTT instead of websockets or with HTTPS, then I am stuck in Connecting state.

Not sure how to debug without any error logs, not sure what could cause such behaviour.

My only guess is that I am misunderstanding roles and users and that the Policy I gave to my authenticated users is in fact not doing what I expect it to. As in the docs its stated

The next step is attaching the policy to your Cognito Identity.

While I am not attaching it to Cognito Identity.

@Nikola-Milovic Nikola-Milovic added the pending-triage Issue is pending triage label Apr 26, 2023
@Nikola-Milovic
Copy link
Author

Nikola-Milovic commented Apr 26, 2023

Identity had to have the policy and not the authenticated user. This fixed the issue

Edit: I'll leave this open as a question on how to grant access to authenticated users without having to have a separate endpoint to handle adding the policy for identity. Maintainers please close this, I'll leave it open for visibility. As #749 left unanswered and I couldn't find the answer anywhere. If there is no way around it, any tips on how to implement it? As identities are temporary, how should I approach expiration and such

@nadetastic nadetastic added the PubSub Related to PubSub category label Apr 26, 2023
@cwomack cwomack self-assigned this Apr 28, 2023
@cwomack cwomack added investigating This issue is being investigated and removed pending-triage Issue is pending triage labels May 2, 2023
@Julz-afk
Copy link

Hi @Nikola-Milovic, did you get this issue resolved on your side?
I am experiencing similar issue.

@Nikola-Milovic
Copy link
Author

The comment I made is the fix, basically you need the policy on the identity itself. But I have no way of easily automating this, nor do I have an idea on why my setup with Authenticated role wasn't working.

@Julz-afk
Copy link

@Nikola-Milovic, Are you referring to the identity ID of the user or the Identity pool ID?

@Nikola-Milovic
Copy link
Author

@Julz-afk Identity of the authenticated user, I think attaching it to Identity Pool doesn't work. You can try and please let me know

@Julz-afk
Copy link

Julz-afk commented May 31, 2023

Yeah I have tried using the Auth user ID and also the pool ID, both not working on my side, still getting this error: connecting then connection disrupted. Then this error pops up: MqttOverWSProvider - Error forming connection Error: AMQJS0011E

@Nikola-Milovic
Copy link
Author

@Julz-afk follow the guide in their docs, if you attach the policy manually it works

@Julz-afk
Copy link

Got it up and running on my side, think I missed a step somewhere with the policy and identity stuff.

@mikey0000
Copy link

Also in nodejs applications WebSockets is missing as in never imported and is expected on the global.

@mufarrah
Copy link

mufarrah commented Aug 24, 2023

@Julz-afk Identity of the authenticated user, I think attaching it to Identity Pool doesn't work. You can try and please let me know

Thanks , that helped, you are right

@LorisNanchen
Copy link

LorisNanchen commented Dec 22, 2023

Where can I find that Identity Id ? I'm facing the same issue and nothing seems to fix it. Maybe I didn't understand the Policies and Identities.

Btw I'm using NextJs, don't know if it's supposed to work with it.

@waedg
Copy link

waedg commented Jan 27, 2024

I confirm, in my case i didn't created and added the policy to the user after authentification , i recommand reading this section of amplify documentation and understand it very well :
[https://docs.amplify.aws/javascript/build-a-backend/more-features/pubsub/set-up-pubsub/]
thank you @Nikola-Milovic for the hint.

@cwomack cwomack added pending-triage Issue is pending triage and removed investigating This issue is being investigated labels May 6, 2024
@cwomack cwomack added documentation Related to documentation feature requests question General question and removed pending-triage Issue is pending triage documentation Related to documentation feature requests labels Jul 2, 2024
@haverchuck
Copy link
Contributor

Is this issue happening on version 5 only?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Related to documentation feature requests PubSub Related to PubSub category question General question
Projects
None yet
Development

No branches or pull requests

9 participants