This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: configure AWS IoT for just in time provisioning
- Loading branch information
1 parent
a2629d7
commit 623b78b
Showing
32 changed files
with
13,285 additions
and
5,087 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ npm-debug.log | |
.env | ||
dist/ | ||
cdk.out/ | ||
certificates/ |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { App } from '@aws-cdk/core' | ||
import { BifravstStack } from '../stacks/Bifravst' | ||
import { LayeredLambdas } from '@nrfcloud/package-layered-lambdas' | ||
import { BifravstLambdas } from '../cloudformation' | ||
|
||
export class BifravstApp extends App { | ||
public constructor(args: { | ||
stackId: string | ||
mqttEndpoint: string | ||
sourceCodeBucketName: string | ||
baseLayerZipFileName: string | ||
lambdas: LayeredLambdas<BifravstLambdas> | ||
}) { | ||
super() | ||
new BifravstStack(this, args.stackId, args) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { App } from '@aws-cdk/core' | ||
import { LambdaSourceCodeStorageStack } from '../stacks/LambdaSourceCodeStorage' | ||
|
||
/** | ||
* In order to deploy lambda functions written in TypeScript we need to publish | ||
* the compiled source code to an S3 bucket. | ||
* This app provides the bucket and run before the main app. | ||
*/ | ||
export class LambdaSourceCodeStorageApp extends App { | ||
public constructor(args: { stackId: string }) { | ||
super() | ||
|
||
new LambdaSourceCodeStorageStack(this, args.stackId) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { LambdaSourceCodeStorageApp } from './apps/LambdaSourceCodeStorage' | ||
import { stackId } from './stacks/LambdaSourceCodeStorage' | ||
|
||
const STACK_ID = process.env.STACK_ID || 'bifravst' | ||
|
||
new LambdaSourceCodeStorageApp({ | ||
stackId: stackId({ bifravstStackName: STACK_ID }), | ||
}).synth() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const { Iot } = require('aws-sdk') | ||
const response = require('cfn-response') | ||
const iot = new Iot() | ||
exports.handler = (event, context) => { | ||
const { RequestType, ResourceProperties: { ThingGroupName, ThingGroupProperties, PolicyName } } = event | ||
let p = Promise.resolve() | ||
switch (RequestType) { | ||
case 'Create': | ||
p = iot | ||
.createThingGroup({ thingGroupName: ThingGroupName, thingGroupProperties: ThingGroupProperties }) | ||
.promise() | ||
.then(({ thingGroupArn }) => iot.attachPolicy({ | ||
policyName: PolicyName, | ||
target: thingGroupArn, | ||
}).promise()) | ||
break | ||
} | ||
p | ||
.then(() => { | ||
response.send(event, context, response.SUCCESS, { ThingGroupName }, false, ThingGroupName) | ||
}) | ||
.catch(err => { | ||
response.send(event, context, response.FAILED, { Error: `${err.message} (${err})` }) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Iot } from 'aws-sdk' | ||
import * as response from 'cfn-response' | ||
import { Context, CloudFormationCustomResourceEvent } from 'aws-lambda' | ||
|
||
const iot = new Iot() | ||
|
||
export const handler = ( | ||
event: CloudFormationCustomResourceEvent, | ||
context: Context, | ||
) => { | ||
const { | ||
RequestType, | ||
ResourceProperties: { ThingGroupName, ThingGroupProperties, PolicyName }, | ||
} = event | ||
if (RequestType === 'Create') { | ||
iot | ||
.createThingGroup({ | ||
thingGroupName: ThingGroupName, | ||
thingGroupProperties: ThingGroupProperties, | ||
}) | ||
.promise() | ||
.then(async ({ thingGroupArn }) => { | ||
if (!thingGroupArn) { | ||
throw new Error(`Failed to create thing group ${ThingGroupName}!`) | ||
} | ||
return iot | ||
.attachPolicy({ | ||
policyName: PolicyName, | ||
target: thingGroupArn, | ||
}) | ||
.promise() | ||
}) | ||
.then(() => { | ||
response.send( | ||
event, | ||
context, | ||
response.SUCCESS, | ||
{ ThingGroupName }, | ||
ThingGroupName, | ||
) | ||
}) | ||
.catch(err => { | ||
response.send(event, context, response.FAILED, { | ||
Error: `${err.message} (${err})`, | ||
}) | ||
}) | ||
} else { | ||
console.log(`${RequestType} not supported.`) | ||
response.send( | ||
event, | ||
context, | ||
response.SUCCESS, | ||
{ ThingGroupName }, | ||
ThingGroupName, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { CloudFormation } from 'aws-sdk' | ||
import { stackId } from '../stacks/LambdaSourceCodeStorage' | ||
|
||
const cf = new CloudFormation({ | ||
region: process.env.AWS_DEFAULT_REGION, | ||
}) | ||
|
||
export const getLambdaSourceCodeBucketName = async ({ | ||
bifravstStackName, | ||
}: { | ||
bifravstStackName: string | ||
}): Promise<string> => | ||
cf | ||
.describeStacks({ | ||
StackName: stackId({ bifravstStackName }), | ||
}) | ||
.promise() | ||
.then(({ Stacks }) => { | ||
if (Stacks === undefined || !Stacks.length) { | ||
throw new Error( | ||
`${stackId({ bifravstStackName })} stack is not available.`, | ||
) | ||
} else { | ||
const stack = Stacks[0] | ||
const BucketOutput = | ||
stack.Outputs && | ||
stack.Outputs.find(({ OutputKey }) => OutputKey === 'bucketName') | ||
if ( | ||
BucketOutput === undefined || | ||
BucketOutput.OutputValue === undefined | ||
) { | ||
throw new Error(`${stackId({ bifravstStackName })} bucket not found.`) | ||
} | ||
return BucketOutput.OutputValue | ||
} | ||
}) |
Oops, something went wrong.