This guide will walk you through setting up a Car Parking Management System using AWS services. The system leverages Amazon SNS (Simple Notification Service), Amazon SQS (Simple Queue Service), and AWS Lambda to manage and process parking events, such as car entries, exits, and parking space updates. The guide will also cover testing the setup, monitoring logs in CloudWatch, and cleaning up resources afterward.
Imagine a smart parking lot in a busy city. The parking lot is equipped with sensors and cameras to detect when cars enter or exit and to update the status of parking spaces. These events need to be processed in real-time to update parking availability, calculate parking fees, and notify parking lot managers.
- Car Entry: When a car enters the parking lot, the system logs the car number and entry time.
- Car Exit: When a car exits, the system logs the car number and exit time, calculates the parking fee, and updates the parking space status.
- Parking Space Update: As soon as a car enters a parking space, the system updates the status of the space (e.g., occupied, under maintenance, reserved, or free) based on the car number and parking space ID.
Before you begin, make sure you have the following:
- An AWS account with necessary permissions to create and manage SNS, SQS, Lambda, and CloudWatch.
- Basic knowledge of AWS services, IAM roles, and Lambda functions.
- Navigate to the Amazon SNS Console.
- Click on Create topic.
- Select Standard as the topic type.
- Enter a Topic name (e.g.,
ParkingEvents
). - Click Create topic.
- Navigate to the Amazon SQS Console.
- Click on Create Queue.
- Choose Standard Queue.
- Enter a Queue Name (e.g.,
CarEntryQueue
). - Configure other settings as needed and click Create Queue.
- Repeat the steps to create another queue, but name it
CarExitQueue
.
- Repeat the steps to create another queue, but name it
ParkingSpaceUpdateQueue
.
-
Go back to the Amazon SNS Console.
-
Under the Subscriptions tab, click Create subscription.
-
For Topic ARN, choose
ParkingEvent
. -
For Protocol, choose SQS.
-
For Endpoint, select the ARN of
CarEntryQueue
. -
Checked checkbox Enable raw message delivery
-
In the Subscription filter policy section, add the following JSON to filter messages based on the
EventType
:{ "EventType": ["CarEntry"] }
-
Click Create subscription to complete the setup.
-
Repeat these steps to create a subscription for ParkingSpaceUpdateQueue, with the following filter policy:
{ "EventType": ["ParkingSpaceUpdate"] }
-
Repeat these steps to create a subscription for CarExitQueue, with the following filter policy:
{
"EventType": ["CarExit"]
}
To allow the ParkingEvent
SNS topic to send messages to your SQS queues, you need to configure the access policy for each queue.
-
Navigate to the SQS Console:
- Go to the Amazon SQS Console.
- Ensure the Region is set to Asia Pacific (Mumbai) ap-south-1.
-
Select
CarExitQueue
:- Choose the CarExitQueue from the list of SQS queues.
- Go to the Permissions tab.
- Click on Edit policy.
-
Add the Access Policy:
-
In the policy editor, add the following JSON policy:
{ "Sid": "Stmt1724523688641", "Effect": "Allow", "Principal": { "Service": "sns.amazonaws.com" }, "Action": "sqs:SendMessage", "Resource": "<arn of queue>", "Condition": { "ArnEquals": { "aws:SourceArn": "<arn of sns>" } } }
-
Replace
<arn of queue>
with the ARN of theCarExitQueue
. -
Replace
<arn of sns>
with the ARN of theParkingEvent
SNS Topic.
-
-
Save Changes:
- After replacing the placeholders with the actual ARNs, save the policy.
-
Repeat for Other Queues:
- Repeat the above steps for
CarEntryQueue
andParkingSpaceUpdateQueue
:- Replace
<arn of queue>
with the ARN ofCarEntryQueue
orParkingSpaceUpdateQueue
. - Use the same
<arn of sns>
for theParkingEvent
SNS Topic.
- Replace
- Repeat the above steps for
-
Verify the Policy:
- Ensure that each SQS queue (
CarEntryQueue
,CarExitQueue
,ParkingSpaceUpdateQueue
) has the correct policy allowing theParkingEvent
SNS topic to send messages.
- Ensure that each SQS queue (
{
"Sid": "Stmt1724523688641",
"Effect": "Allow",
"Principal": {
"Service": "sns.amazonaws.com"
},
"Action": "sqs:SendMessage",
"Resource": "arn:aws:sqs:ap-south-1:123456789012:CarExitQueue",
"Condition": {
"ArnEquals": {
"aws:SourceArn": "arn:aws:sns:ap-south-1:123456789012:ParkingEvents"
}
}
}
- Navigate to the IAM Console.
- Click on Roles and then Create role.
- Select Trusted entity type as AWS Service and Use case as Lambda.
- Attach the following policies:
- AmazonSQSFullAccess
- AWSLambdaBasicExecutionRole
- AmazonSNSFullAccess
- Name the role
CarParkingLambdaRole
and click Create role.
- Navigate to the AWS Lambda Console.
- Click on Create function.
- Choose Author from scratch.
- Enter a Function name (e.g.,
CarEntryLambda
). - Select .NET Core 8 as the runtime.
- Under Permissions, choose the IAM role created earlier (
CarParkingLambdaRole
). - Click Create function.
- Repeat the steps to create another function, but name it
CarExitLambda
.
- Repeat the steps to create another function, but name it
ParkingSpaceUpdateLambda
.
Refer src folder
- Install Amazon.Lambda.Tools Global Tools if not already installed.
dotnet tool install -g Amazon.Lambda.Tools
If already installed check if new version is available.
dotnet tool update -g Amazon.Lambda.Tools
- Run following command where your csproj file is stored in command prompt
dotnet lambda package *.csproj -o bin/package.zip
- Go back to the Lambda Console.
- In the Code tab, click on Upload from and select .zip file.
- Choose the ZIP file you created.
- Click Save to deploy the function.
- After the upload, navigate to the Runtime settings section under the Code tab.
- Set the Handler:
- Enter the handler as
"ParkingEventProcessorLambda::ParkingEventProcessorLambda.Function::FunctionHandler"
.
- Enter the handler as
- Save Changes:
- Click Save to apply the new handler settings.
- In the CarEntryLambda Lambda function, go to the Configuration tab.
- Click on Add Trigger.
- Select SQS and choose the
CarEntryQueue
. - Click Add.
- Repeat the steps to add a trigger to the
CarExitLambda
for theCarExitQueue
.
- Repeat the steps to add a trigger to the
ParkingSpaceUpdateLambda
for theParkingSpaceUpdateQueue
.
-
Navigate to the SNS Dashboard:
-
Select the Topic:
- In the SNS dashboard, click on Topics in the left-hand navigation pane.
- Find and select the ParkingEvents topic from the list.
-
Publish a Message:
- Click the Publish message button.
- In the Publish message form:
- Message body: Enter a JSON payload for testing, e.g.:
{ "EventType": "CarEntry", "CarId": "MH05SR7", "EntryTime": "2024-08-11T08:30:00Z" }
- Message body: Enter a JSON payload for testing, e.g.:
- Navigate to CloudWatch in the AWS Console.
- Go to Logs and select the log group for your Lambda function (e.g.,
/aws/lambda/CarEntryLambda
). - Review the logs to verify that events are processed correctly and that no errors occurred.
After testing, clean up the resources to avoid unnecessary charges:
- Delete the Lambda Functions: Go to the Lambda Console and delete
CarEntryLambda
,CarExitLambda
, andParkingSpaceUpdateLambda
. - Delete the SQS Queues: Go to the SQS Console and delete
CarEntryQueue
,CarExitQueue
, andParkingSpaceUpdateQueue
. - Delete the SNS Topic: Go to the SNS Console and delete the
ParkingEvents
topic. - Delete the IAM Role: Go to the IAM Console and delete the
CarParkingLambdaRole
role. - Delete CloudWatch Log Groups: Go to CloudWatch Logs and delete the log groups associated with your Lambda functions.
{
"EventType": "CarEntry",
"CarId": "ABC1234",
"EntryTime": "2024-08-11T08:30:00Z"
}
{
"EventType": "CarExit",
"CarId": "ABC1234",
"ExitTime": "2024-08-11T10:45:00Z"
}
{
"EventType": "ParkingSpaceUpdate",
"CarId": "ABC1234",
"ParkingSpaceId": "PS45",
"Status": "Occupied",
"UpdateTime": "2024-08-11T08:30:00Z"
}