This is a sample project which shows how to create, deploy and start a simple Node.js IoT application, using CDK and MQTT.js. The code in this sample project was written by using the AI companionship of Amazon CodeWhisperer. The comments in the code show what Amazon CodeWhisperer was 'told' to generate.
The project is composed of two parts:
-
The Typescript CDK infrastructure as code, which creates the needed resources as follows:
- An IoT policy,
- An IoT Rule,
- An AWS Lambda function which is invoked as a Rule Action.
The
cdk.jsonfile tells the CDK Toolkit how to execute your app.
-
A Javascript application, which:
- Creates an AWS IoT Thing,
- Creates the needed IoT thing identity (certificate and keys),
- Attaches the policy to the certificate, and the certificate to the thing,
- Creates an MQTT client using the MQTT.JS open source library,
- Starts the MQTT client, connects to AWS IoT Core,
- Subscribes and publishes data on interval on an MQTT topic.
- To be able to deploy the CDK resources, as well as run your application, you need to:
- Ensure you provide the scripts the needed AWS credentials.
- Replace the
ENDPOINTvalue with your AWS Account and REGION IoT Endpoint in line 11 in here.
-
To prepare and deploy the AWS resources created using CDK:
npm installinstall dependencies as node modulesnpm run buildcompile typescript to jsnpm run watchwatch for changes and compilecdk deploydeploy this stack to your default AWS account/regioncdk diffcompare deployed stack with current statecdk synthemits the synthesized CloudFormation template
-
To run the Javascript application:
npm installinstall dependencies as node modulesnode test-app/app.jsrun JavaScript app
-
Clean up your app-created resources, certificates, thing etc.
node test-app/clean-up-app.jsrun JavaScript app
-
Clean up your CDK created resources:
cdk destroydelete resources from your AWS Account
See CONTRIBUTING for more information.
This library is licensed under the MIT-0 License. See the LICENSE file.