|
| 1 | +import { |
| 2 | + Duration, |
| 3 | + aws_events_targets as EventTargets, |
| 4 | + aws_events as Events, |
| 5 | + aws_iam as IAM, |
| 6 | + aws_lambda as Lambda, |
| 7 | + Stack, |
| 8 | +} from 'aws-cdk-lib' |
| 9 | +import { Construct } from 'constructs' |
| 10 | +import { type Settings as BridgeSettings } from '../../bridge/settings.js' |
| 11 | +import type { PackedLambda } from '../helpers/lambdas/packLambda.js' |
| 12 | +import type { DeviceStorage } from './DeviceStorage.js' |
| 13 | +import { LambdaLogGroup } from './LambdaLogGroup.js' |
| 14 | +import type { WebsocketAPI } from './WebsocketAPI.js' |
| 15 | + |
| 16 | +export type BridgeImageSettings = BridgeSettings |
| 17 | + |
| 18 | +export class HealthCheckMqttBridge extends Construct { |
| 19 | + public constructor( |
| 20 | + parent: Construct, |
| 21 | + { |
| 22 | + websocketAPI, |
| 23 | + deviceStorage, |
| 24 | + layers, |
| 25 | + lambdaSources, |
| 26 | + }: { |
| 27 | + websocketAPI: WebsocketAPI |
| 28 | + deviceStorage: DeviceStorage |
| 29 | + layers: Lambda.ILayerVersion[] |
| 30 | + lambdaSources: { |
| 31 | + healthCheck: PackedLambda |
| 32 | + } |
| 33 | + }, |
| 34 | + ) { |
| 35 | + super(parent, 'healthCheckMqttBridge') |
| 36 | + |
| 37 | + const scheduler = new Events.Rule(this, 'scheduler', { |
| 38 | + description: `Scheduler to health check mqtt bridge`, |
| 39 | + schedule: Events.Schedule.rate(Duration.minutes(1)), |
| 40 | + }) |
| 41 | + |
| 42 | + // Lambda functions |
| 43 | + const healthCheck = new Lambda.Function(this, 'healthCheck', { |
| 44 | + handler: lambdaSources.healthCheck.handler, |
| 45 | + architecture: Lambda.Architecture.ARM_64, |
| 46 | + runtime: Lambda.Runtime.NODEJS_18_X, |
| 47 | + timeout: Duration.seconds(15), |
| 48 | + memorySize: 1792, |
| 49 | + code: Lambda.Code.fromAsset(lambdaSources.healthCheck.zipFile), |
| 50 | + description: 'End to end test for mqtt bridge', |
| 51 | + environment: { |
| 52 | + VERSION: this.node.tryGetContext('version'), |
| 53 | + LOG_LEVEL: this.node.tryGetContext('logLevel'), |
| 54 | + NODE_NO_WARNINGS: '1', |
| 55 | + STACK_NAME: Stack.of(this).stackName, |
| 56 | + DEVICES_TABLE_NAME: deviceStorage.devicesTable.tableName, |
| 57 | + WEBSOCKET_URL: websocketAPI.websocketURI, |
| 58 | + }, |
| 59 | + initialPolicy: [], |
| 60 | + layers, |
| 61 | + }) |
| 62 | + const ssmReadPolicy = new IAM.PolicyStatement({ |
| 63 | + effect: IAM.Effect.ALLOW, |
| 64 | + actions: ['ssm:GetParametersByPath'], |
| 65 | + resources: [ |
| 66 | + `arn:aws:ssm:${Stack.of(this).region}:${ |
| 67 | + Stack.of(this).account |
| 68 | + }:parameter/${Stack.of(this).stackName}/thirdParty/nrfcloud`, |
| 69 | + ], |
| 70 | + }) |
| 71 | + healthCheck.addToRolePolicy(ssmReadPolicy) |
| 72 | + scheduler.addTarget(new EventTargets.LambdaFunction(healthCheck)) |
| 73 | + deviceStorage.devicesTable.grantWriteData(healthCheck) |
| 74 | + new LambdaLogGroup(this, 'healthCheckLog', healthCheck) |
| 75 | + } |
| 76 | +} |
0 commit comments