|
| 1 | +import { |
| 2 | + Duration, |
| 3 | + aws_iam as IAM, |
| 4 | + aws_iot as IoT, |
| 5 | + aws_lambda as Lambda, |
| 6 | + aws_logs as Logs, |
| 7 | + Stack, |
| 8 | +} from 'aws-cdk-lib' |
| 9 | +import { Construct } from 'constructs' |
| 10 | +import type { PackedLambda } from '../helpers/lambdas/packLambda.js' |
| 11 | +import { LambdaSource } from './LambdaSource.js' |
| 12 | +import type { WebsocketAPI } from './WebsocketAPI.js' |
| 13 | +import { IoTActionRole } from './IoTActionRole.js' |
| 14 | +import type { DeviceStorage } from './DeviceStorage.js' |
| 15 | + |
| 16 | +/** |
| 17 | + * Resolve device geo location based on network information |
| 18 | + */ |
| 19 | +export class SingleCellGeoLocation extends Construct { |
| 20 | + public constructor( |
| 21 | + parent: Construct, |
| 22 | + { |
| 23 | + lambdaSources, |
| 24 | + layers, |
| 25 | + websocketAPI, |
| 26 | + deviceStorage, |
| 27 | + }: { |
| 28 | + websocketAPI: WebsocketAPI |
| 29 | + deviceStorage: DeviceStorage |
| 30 | + lambdaSources: { |
| 31 | + resolveSingleCellGeoLocation: PackedLambda |
| 32 | + } |
| 33 | + layers: Lambda.ILayerVersion[] |
| 34 | + }, |
| 35 | + ) { |
| 36 | + super(parent, 'SingleCellGeoLocation') |
| 37 | + |
| 38 | + const fn = new Lambda.Function(this, 'fn', { |
| 39 | + handler: lambdaSources.resolveSingleCellGeoLocation.handler, |
| 40 | + architecture: Lambda.Architecture.ARM_64, |
| 41 | + runtime: Lambda.Runtime.NODEJS_18_X, |
| 42 | + timeout: Duration.seconds(60), |
| 43 | + memorySize: 1792, |
| 44 | + code: new LambdaSource(this, lambdaSources.resolveSingleCellGeoLocation) |
| 45 | + .code, |
| 46 | + description: 'Resolve device geo location based on network information', |
| 47 | + environment: { |
| 48 | + VERSION: this.node.tryGetContext('version'), |
| 49 | + LOG_LEVEL: this.node.tryGetContext('logLevel'), |
| 50 | + EVENTBUS_NAME: websocketAPI.eventBus.eventBusName, |
| 51 | + NODE_NO_WARNINGS: '1', |
| 52 | + DISABLE_METRICS: this.node.tryGetContext('isTest') === true ? '1' : '0', |
| 53 | + STACK_NAME: Stack.of(this).stackName, |
| 54 | + DEVICES_TABLE_NAME: deviceStorage.devicesTable.tableName, |
| 55 | + }, |
| 56 | + layers, |
| 57 | + logRetention: Logs.RetentionDays.ONE_WEEK, |
| 58 | + initialPolicy: [ |
| 59 | + new IAM.PolicyStatement({ |
| 60 | + actions: ['ssm:GetParameter'], |
| 61 | + resources: [ |
| 62 | + `arn:aws:ssm:${Stack.of(this).region}:${ |
| 63 | + Stack.of(this).account |
| 64 | + }:parameter/${Stack.of(this).stackName}/thirdParty/nrfcloud/*`, |
| 65 | + ], |
| 66 | + }), |
| 67 | + new IAM.PolicyStatement({ |
| 68 | + actions: ['ssm:GetParametersByPath'], |
| 69 | + resources: [ |
| 70 | + `arn:aws:ssm:${Stack.of(this).region}:${ |
| 71 | + Stack.of(this).account |
| 72 | + }:parameter/${Stack.of(this).stackName}/thirdParty/nrfcloud`, |
| 73 | + ], |
| 74 | + }), |
| 75 | + ], |
| 76 | + }) |
| 77 | + websocketAPI.eventBus.grantPutEventsTo(fn) |
| 78 | + deviceStorage.devicesTable.grantReadData(fn) |
| 79 | + |
| 80 | + const rule = new IoT.CfnTopicRule(this, 'topicRule', { |
| 81 | + topicRulePayload: { |
| 82 | + description: `Resolve device geo location based on network information`, |
| 83 | + ruleDisabled: false, |
| 84 | + awsIotSqlVersion: '2016-03-23', |
| 85 | + sql: ` |
| 86 | + select |
| 87 | + * as message, |
| 88 | + topic(4) as deviceId, |
| 89 | + timestamp() as timestamp |
| 90 | + from 'data/+/+/+/+' |
| 91 | + where messageType = 'DATA' |
| 92 | + and appId = 'DEVICE' |
| 93 | + `, |
| 94 | + actions: [ |
| 95 | + { |
| 96 | + lambda: { |
| 97 | + functionArn: fn.functionArn, |
| 98 | + }, |
| 99 | + }, |
| 100 | + ], |
| 101 | + errorAction: { |
| 102 | + republish: { |
| 103 | + roleArn: new IoTActionRole(this).roleArn, |
| 104 | + topic: 'errors', |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }) |
| 109 | + |
| 110 | + fn.addPermission('topicRule', { |
| 111 | + principal: new IAM.ServicePrincipal('iot.amazonaws.com'), |
| 112 | + sourceArn: rule.attrArn, |
| 113 | + }) |
| 114 | + } |
| 115 | +} |
0 commit comments