Skip to content

Commit

Permalink
Update Geofence Lambda to incorporate CF update condition for geofenc…
Browse files Browse the repository at this point in the history
…es update
  • Loading branch information
wadhawh committed Jun 16, 2024
1 parent 4f370e5 commit 8ac1913
Showing 1 changed file with 61 additions and 49 deletions.
110 changes: 61 additions & 49 deletions extra/cloudformation/default-unauth-resources-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ Resources:
from pip._internal import main
main(['install', '-I', '-q', 'boto3', '--target', '/tmp/', '--no-cache-dir', '--disable-pip-version-check'])
sys.path.insert(0,'/tmp/')
sys.path.insert(0, '/tmp/')
import json, boto3
import urllib.request
import cfnresponse
Expand All @@ -878,55 +878,67 @@ Resources:
geojson_url = "https://amazon-location-resources-setup.s3.amazonaws.com/BusRoutesData/busRoutesData.json"
with urllib.request.urlopen(geojson_url) as response:
data = json.load(response)
routes = data['busRoutesData']
if event['RequestType'] == "Create":
for route in routes:
id = route['id']
collectionName = ('location.aws.com.demo.geofences.' + route['geofenceCollection'])
geofence_collection = client.create_geofence_collection(
CollectionName=collectionName,
Description="You created this resource by running Amazon Location Demo's CloudFormation template."
)
for i in route['stopCoordinates']:
coordinates = i['geometry']['coordinates']
stopName = i['properties']['stop_name']
geo_id = (id + '-' + str(i['id']))
geofence_data = client.batch_put_geofence(
CollectionName=collectionName,
Entries=[
{
'GeofenceId': geo_id,
'GeofenceProperties': {
'stop_name' : stopName
routes = data['busRoutesData']
if event['RequestType'] in ["Create", "Update"]:
# On Update, first delete the existing geofences if any
if event['RequestType'] == "Update":
for route in routes:
collectionName = ('location.aws.com.demo.geofences.' + route['geofenceCollection'])
try:
geofence_collection = client.delete_geofence_collection(
CollectionName=collectionName
)
except client.exceptions.ResourceNotFoundException:
pass # If the collection does not exist, continue
# Create or recreate geofences
for route in routes:
id = route['id']
collectionName = ('location.aws.com.demo.geofences.' + route['geofenceCollection'])
geofence_collection = client.create_geofence_collection(
CollectionName=collectionName,
Description="You created this resource by running Amazon Location Demo's CloudFormation template."
)
for i in route['stopCoordinates']:
coordinates = i['geometry']['coordinates']
stopName = i['properties']['stop_name']
geo_id = (id + '-' + str(i['id']))
geofence_data = client.batch_put_geofence(
CollectionName=collectionName,
Entries=[
{
'GeofenceId': geo_id,
'GeofenceProperties': {
'stop_name': stopName
},
'Geometry': {
'Circle': {
'Center': coordinates,
'Radius': 200
}
}
},
'Geometry': {
'Circle': {
'Center': coordinates,
'Radius': 200
}
}
},
]
)
responseValue = 120
responseData = {}
responseData['Data'] = responseValue
cfnresponse.send(event, context, cfnresponse.SUCCESS,
responseData, 'AmazonLocationDemoGeofenceCreator')
if event['RequestType'] == "Delete":
for route in routes:
id = route['id']
collectionName = ('location.aws.com.demo.geofences.' + route['geofenceCollection'])
geofence_collection = client.delete_geofence_collection(
CollectionName=collectionName
)
responseValue = 120
responseData = {}
responseData['Data'] = responseValue
cfnresponse.send(event, context, cfnresponse.SUCCESS,
responseData, 'AmazonLocationDemoGeofenceCreator')
]
)
responseValue = 120
responseData = {'Data': responseValue}
cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData, 'AmazonLocationDemoGeofenceCreator')
elif event['RequestType'] == "Delete":
for route in routes:
id = route['id']
collectionName = ('location.aws.com.demo.geofences.' + route['geofenceCollection'])
try:
geofence_collection = client.delete_geofence_collection(
CollectionName=collectionName
)
except client.exceptions.ResourceNotFoundException:
pass # If the collection does not exist, continue
responseValue = 120
responseData = {'Data': responseValue}
cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData, 'AmazonLocationDemoGeofenceCreator')
Handler: index.lambda_handler
Role: !GetAtt [AmazonLocationGeoFenceLambdaExecutionRole, Arn]
Expand Down

0 comments on commit 8ac1913

Please sign in to comment.