@@ -12,6 +12,8 @@ import { StateMachineType } from '@aws-cdk/aws-stepfunctions'
1212import { Role } from '@aws-cdk/aws-iam'
1313import * as SQS from '@aws-cdk/aws-sqs'
1414import { LambdasWithLayer } from './LambdasWithLayer'
15+ import { CORE_STACK_NAME } from '../stacks/stackName'
16+ import { enabledInContext } from '../helper/enabledInContext'
1517
1618/**
1719 * Provides the resources for geolocating LTE/NB-IoT network cells
@@ -28,10 +30,8 @@ export class CellGeolocation extends CloudFormation.Resource {
2830 id : string ,
2931 {
3032 lambdas,
31- enableUnwiredApi,
3233 } : {
3334 lambdas : LambdasWithLayer < AssetTrackerLambdas >
34- enableUnwiredApi : boolean
3535 } ,
3636 ) {
3737 super ( parent , id )
@@ -165,31 +165,37 @@ export class CellGeolocation extends CloudFormation.Resource {
165165
166166 // Optional step
167167 let fromUnwiredLabs : Lambda . IFunction | undefined = undefined
168- if ( enableUnwiredApi ) {
169- fromUnwiredLabs = new Lambda . Function ( this , 'fromUnwiredLabs' , {
170- layers : lambdas . layers ,
171- handler : 'index.handler' ,
172- runtime : Lambda . Runtime . NODEJS_12_X ,
173- timeout : CloudFormation . Duration . seconds ( 10 ) ,
174- memorySize : 1792 ,
175- code : lambdas . lambdas . geolocateCellFromUnwiredLabsStepFunction ,
176- description : 'Resolve cell geolocation using the UnwiredLabs API' ,
177- initialPolicy : [
178- logToCloudWatch ,
179- new IAM . PolicyStatement ( {
180- actions : [ 'ssm:GetParametersByPath' ] ,
181- resources : [
182- `arn:aws:ssm:${ parent . region } :${ parent . account } :parameter/asset-tracker/cellGeoLocation/unwiredlabs` ,
183- ] ,
184- } ) ,
185- ] ,
186- environment : {
187- VERSION : this . node . tryGetContext ( 'version' ) ,
188- } ,
189- } )
168+ const checkFlag = enabledInContext ( this . node )
169+ const unwiredLabsEnabled = checkFlag ( {
170+ key : 'unwiredlabs' ,
171+ component : 'UnwiredLabs API' ,
172+ onUndefined : 'disabled' ,
173+ onEnabled : ( ) => {
174+ fromUnwiredLabs = new Lambda . Function ( this , 'fromUnwiredLabs' , {
175+ layers : lambdas . layers ,
176+ handler : 'index.handler' ,
177+ runtime : Lambda . Runtime . NODEJS_12_X ,
178+ timeout : CloudFormation . Duration . seconds ( 10 ) ,
179+ memorySize : 1792 ,
180+ code : lambdas . lambdas . geolocateCellFromUnwiredLabsStepFunction ,
181+ description : 'Resolve cell geolocation using the UnwiredLabs API' ,
182+ initialPolicy : [
183+ logToCloudWatch ,
184+ new IAM . PolicyStatement ( {
185+ actions : [ 'ssm:GetParametersByPath' ] ,
186+ resources : [
187+ `arn:aws:ssm:${ parent . region } :${ parent . account } :parameter/${ CORE_STACK_NAME } /cellGeoLocation/unwiredlabs` ,
188+ ] ,
189+ } ) ,
190+ ] ,
191+ environment : {
192+ VERSION : this . node . tryGetContext ( 'version' ) ,
193+ } ,
194+ } )
190195
191- new LambdaLogGroup ( this , 'fromUnwiredLabsLogs' , fromUnwiredLabs )
192- }
196+ new LambdaLogGroup ( this , 'fromUnwiredLabsLogs' , fromUnwiredLabs )
197+ } ,
198+ } )
193199
194200 const isGeolocated = StepFunctions . Condition . booleanEquals (
195201 '$.cellgeo.located' ,
@@ -243,7 +249,7 @@ export class CellGeolocation extends CloudFormation.Resource {
243249 )
244250 . otherwise (
245251 ( ( ) => {
246- if ( ! fromUnwiredLabs ) {
252+ if ( ! unwiredLabsEnabled ) {
247253 return new StepFunctions . Fail ( this , 'Failed (No API)' , {
248254 error : 'NO_API' ,
249255 cause :
@@ -255,7 +261,7 @@ export class CellGeolocation extends CloudFormation.Resource {
255261 'Resolve using UnwiredLabs API' ,
256262 {
257263 task : new StepFunctionTasks . InvokeFunction (
258- fromUnwiredLabs ,
264+ ( fromUnwiredLabs as unknown ) as Lambda . IFunction ,
259265 ) ,
260266 resultPath : '$.cellgeo' ,
261267 } ,
0 commit comments