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

Need samples for AWS IoT #655

Open
1 of 2 tasks
entest-hai opened this issue Apr 10, 2022 · 3 comments
Open
1 of 2 tasks

Need samples for AWS IoT #655

entest-hai opened this issue Apr 10, 2022 · 3 comments
Labels
effort/medium Medium work item – a couple days of effort feature-request A feature should be added or improved. p2

Comments

@entest-hai
Copy link

entest-hai commented Apr 10, 2022

Describe the feature

Samples for AWS IoT

Use Case

I want to use CDK to build a AWS IoT Core example.

  • Create IoT things
  • Create a x509 certificate
  • Create a policy
  • Attach the policy to the certificate
  • Attach the certificate to the thing
  • Further integrate with other services such as Kinesis

Proposed Solution

I am stuck at how to create the IoT x509 certificate using CDK. So I have to create a certificate from AWS CLI then pass the certificate ARN into CDK

aws iot create-keys-and-certificate \
--set-as-active \
--certificate-pem-outfile esp-certificate.crt \
--public-key-outfile esp-public.key \
--private-key-outfile esp-private.key \
--region ap-southeast-1

CDK stack

import { aws_iam, aws_iot, Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';

interface AwsIotDemoStackProps extends StackProps {
  certificateArn: string
}

export class AwsIotDemoStack extends Stack {
  constructor(scope: Construct, id: string, props: AwsIotDemoStackProps) {
    super(scope, id, props);

    // create a thing 
    const thing = new aws_iot.CfnThing(
      this,
      'DemoDeviceThing', {
      thingName: 'DemoDevice'
    }
    )

    // create a policy 
    const policy = new aws_iot.CfnPolicy(
      this,
      'PolicyForDemoDevice',
      {
        policyName: 'PolicyForDemoDevice',
        policyDocument: new aws_iam.PolicyDocument(
          {
            statements: [
              new aws_iam.PolicyStatement(
                {
                  actions: ['iot:*'],
                  resources: ['*'],
                  effect: aws_iam.Effect.ALLOW
                }
              )
            ]
          }
        )
      }
    )

    // attach the policy to certificate 
    const attachPolicy = new aws_iot.CfnPolicyPrincipalAttachment(
      this,
      'AttachPolicyForDemoDevice',
      {
        policyName: policy.policyName!.toString(),
        principal: props.certificateArn
      }
    )

    attachPolicy.addDependsOn(
      policy
    )

    // attach the certificate to the IoT thing
    const attachCert = new aws_iot.CfnThingPrincipalAttachment(
      this,
      'AttachCertificiateToThing',
      {
        thingName: thing.thingName!.toString(),
        principal: props.certificateArn
      }
    )

    attachCert.addDependsOn(
      thing
    )
  }
}


Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

Language

Typescript

@entest-hai entest-hai added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Apr 10, 2022
@entest-hai entest-hai changed the title (short issue description) Need samples for AWS IoT Apr 10, 2022
@peterwoodworth peterwoodworth added effort/medium Medium work item – a couple days of effort p2 and removed needs-triage This issue or PR still needs to be triaged. labels Jun 3, 2022
@peterwoodworth
Copy link
Contributor

Would you be able to help us out with this @yamatatsu?

@yamatatsu
Copy link

@peterwoodworth
I'll try to create the example.

@entest-hai
CDK (and CloudFormation) does not have the feature of create-keys-and-certificate.
If you wanna create certs with cdk, you can create the cert with using a csr created on your local machine. See, aws/aws-cdk#19303 (comment) .

Or you can create thing and cert in only cdk with the 3rd party constructs.
https://constructs.dev/packages/cdk-iot-core-certificates/v/0.0.3?lang=typescript

If you use this 3rd party constructs, you can get cert from AWS SSM parameter store.

@prashantchaudhary11
Copy link

I am working on this FR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
effort/medium Medium work item – a couple days of effort feature-request A feature should be added or improved. p2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants