-
Notifications
You must be signed in to change notification settings - Fork 1
/
provision.ts
45 lines (39 loc) · 1.19 KB
/
provision.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { DescribeEndpointCommand, IoTClient } from '@aws-sdk/client-iot'
import chalk from 'chalk'
import { provision } from 'device/provision'
import { stat } from 'node:fs/promises'
import path from 'node:path'
const { magenta, yellow, green, blue } = chalk
const certificateId = process.argv[process.argv.length - 1]
console.debug(magenta('Certificate ID'), yellow(certificateId))
const privateKeyFile = path.join(
process.cwd(),
'certificates',
`${certificateId}.pem.key`,
)
const certificateFile = path.join(
process.cwd(),
'certificates',
`${certificateId}.pem.crt`,
)
await stat(privateKeyFile)
await stat(certificateFile)
export const deviceId = `node-${Math.floor(Math.random() * 100000000)}`
console.debug(magenta('Device ID'), yellow(deviceId))
const brokerHostname = (
await new IoTClient({}).send(
new DescribeEndpointCommand({
endpointType: 'iot:Data-ATS',
}),
)
).endpointAddress as string
console.debug(magenta('Endpoint'), yellow(brokerHostname))
await provision({
deviceId,
brokerHostname,
privateKeyFile,
certificateFile,
})
console.log('')
console.log(green(`You can now connect with the new credentials:`))
console.log(blue(`npx tsx connect.ts`), yellow.dim(deviceId))