Skip to main content

Creates an AWS IoT thing, certificate, policy, and associates the three together

Project description

cdk-aws-iot-thing-certificate-policy

An L3 CDK construct to create and associate a singular AWS IoT Thing, Certificate, and IoT Policy. The construct also retrieves and returns AWS IoT account specific details such as the AWS IoT data endpoint and the AWS IoT Credential provider endpoint.

The certificate and its private key are stored as AWS Systems Manager Parameter Store parameters that can be retrieved via the AWS Console or programmatically via construct members.

Installation

TypeScript

npm install cdk-aws-iot-thing-certificate-policy

API Reference

TypeScript

pip install cdk-aws-iot-thing-certificate-policy

API Reference

Examples

TypeScript

import * as cdk from "aws-cdk-lib";
import { IotThingCertificatePolicy } from "cdk-aws-iot-thing-certificate-policy-dev";
/**
 * A minimum IoT Policy template using substitution variables for actual
 * policy to be deployed for "region", "account", and "thingname". Allows
 * the thing to publish and subscribe on any topics under "thing/*" topic
 * namespace. Normal IoT Policy conventions such as "*", apply.
 */
export const minimalIoTPolicy = `{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": ["iot:Connect"],
      "Resource": "arn:aws:iot:{{region}}:{{account}}:client/{{thingname}}"
    },
    {
      "Effect": "Allow",
      "Action": ["iot:Publish"],
      "Resource": [
        "arn:aws:iot:{{region}}:{{account}}:topic/{{thingname}}/*"
      ]
    },
    {
      "Effect": "Allow",
      "Action": ["iot:Subscribe"],
      "Resource": [
        "arn:aws:iot:{{region}}:{{account}}:topicfilter/{{thingname}}/*"
      ]
    },
    {
      "Effect": "Allow",
      "Action": ["iot:Receive"],
      "Resource": [
        "arn:aws:iot:{{region}}:{{account}}:topic/{{thingname}}/*"
      ]
    }
  ]
}`;

/**
 * Create the thing, certificate, and policy, then associate the
 * certificate to both the thing and the policy and fully activate.
 */
const fooThing = new IotThingCertificatePolicy(this, "MyFooThing", {
  thingName: "foo-thing", // Name to assign to AWS IoT thing, and value for {{thingname}} in policy template
  iotPolicyName: "foo-iot-policy", // Name to assign to AWS IoT policy
  iotPolicy: minimalIoTPolicy, // Policy with or without substitution parameters from above
  encryptionAlgorithm: "ECC", // Algorithm to use to private key (RSA or ECC)
  policyParameterMapping: [
    // substitution names and values for AWS IoT policy template, e.g., {{region}} and {{account}}
    {
      name: "region",
      value: cdk.Fn.ref("AWS::Region"),
    },
    {
      name: "account",
      value: cdk.Fn.ref("AWS::AccountId"),
    },
  ],
});

// The AWS IoT Thing Arn as a stack output
new cdk.CfnOutput(this, "ThingArn", {
  value: iotThing.thingArn,
});
// The AWS account unique endpoint for the MQTT data connection
// See API for other available public values that can be referenced
new cdk.CfnOutput(this, "IotEndpoint", {
  value: iotThing.dataAtsEndpointAddress,
});

Python

import awsk_cdk
from cdk_aws_iot_thing_certificate_policy_dev import (
    IotThingCertificatePolicy,
)

minimal_iot_policy = """{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": ["iot:Connect"],
      "Resource": "arn:aws:iot:{{region}}:{{account}}:client/{{thingname}}"
    },
    {
      "Effect": "Allow",
      "Action": ["iot:Publish"],
      "Resource": [
        "arn:aws:iot:{{region}}:{{account}}:topic/{{thingname}}/*"
      ]
    },
    {
      "Effect": "Allow",
      "Action": ["iot:Subscribe"],
      "Resource": [
        "arn:aws:iot:{{region}}:{{account}}:topicfilter/{{thingname}}/*"
      ]
    },
    {
      "Effect": "Allow",
      "Action": ["iot:Receive"],
      "Resource": [
        "arn:aws:iot:{{region}}:{{account}}:topic/{{thingname}}/*"
      ]
    }
  ]
}"""
foo_thing = IotThingCertificatePolicy(
    self,
    "MyFooThing",
    thing_name="foo-thin",
    iot_policy_name="foo-iot-policy",
    iot_policy=minimal_iot_policy,
    encryption_algorithm="ECC",
    policy_parameter_mapping=[
        {
            "name": "region",
            "value":aws_cdk.Fn.ref("AWS::Region")
        },
        {
            "name": "account",
            "value":aws_cdk.Fn.ref("AWS::AccountId")
        }
    ],
)
aws_cdk.CfnOutput(self, "ThingArn", value=foo.thing_arn)
aws_cdk.CfnOutput(self, "IotEndpoint", value=foo.data_ats_endpoint_address)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

File details

Details for the file cdk-aws-iot-thing-certificate-policy-dev-0.0.15.tar.gz.

File metadata

File hashes

Hashes for cdk-aws-iot-thing-certificate-policy-dev-0.0.15.tar.gz
Algorithm Hash digest
SHA256 931038878c2ae4c5f86ed93d4c3dbb7e4c5f6790197cf60fe472abe045d5aa30
MD5 af90226b6b7fcd8943d28e8cd250ad0b
BLAKE2b-256 8a7c56c1c0242bba465392c4ca54e43f3b7d66da51bd46d688d0b8e6716f14ab

See more details on using hashes here.

File details

Details for the file cdk_aws_iot_thing_certificate_policy_dev-0.0.15-py3-none-any.whl.

File metadata

File hashes

Hashes for cdk_aws_iot_thing_certificate_policy_dev-0.0.15-py3-none-any.whl
Algorithm Hash digest
SHA256 0c97918211fcc369e31b56404fe7bb07094e76f6f25809046dad985e7238d372
MD5 8d1ca2232c6f2da913f02c64d312c767
BLAKE2b-256 67d3c4c021f201ee00007f9ea478d91638270f0d31a79401a7a000d207d14ae2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page