Skip to content

Commit 1573ae2

Browse files
authored
Merge pull request #4 from aws-samples/CR_chkqueue
Commit chk q
2 parents f788e6b + 124f594 commit 1573ae2

File tree

4 files changed

+63
-33
lines changed

4 files changed

+63
-33
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ This project contains source code and supporting files for a serverless applicat
1212
This application creates a serverless Amazon Rekognition Custom Label Detection workflow which runs on a pre-defined schedule (note that the schedule is enabled by default at deployment). It demonstrates the power of Step Functions to orchestrate Lambda functions and other AWS resources to form complex and robust workflows, coupled with event-driven development using Amazon EventBridge.
1313

1414
Solution Architecture Diagram:
15-
<img width="814" alt="image" src="https://user-images.githubusercontent.com/34427009/102906604-c5c34e80-446c-11eb-918e-42dabbcacc93.png">
15+
<img width="814" alt="Architecture Diagram" src="docs/Solution%20Architecture%20-%20Serverless%20Computer%20Vision%20Label%20Detection.png">
1616

1717
This application can also be used for creating a serverless image label inferencing pipeline for Amazon Lookout for Vision.
1818

375 KB
Loading

functions/toggle_trigger/app.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,49 +22,57 @@
2222

2323

2424
def lambda_handler(event, context):
25-
25+
2626
lambda_client = boto3.client('lambda')
2727
try:
2828
uuid_response = lambda_client.list_event_source_mappings(
29-
FunctionName = os.environ['analyze_lambda_arn']
30-
)
29+
FunctionName=os.environ['analyze_lambda_arn']
30+
)
3131
except Exception as e:
32-
print(e)
32+
print(e)
3333

3434
mylist = uuid_response['EventSourceMappings']
3535
uuiddata = mylist[0]['UUID']
3636
analyse_lambda_uuid = uuiddata
3737

3838
try:
3939
response = lambda_client.get_event_source_mapping(
40-
UUID = analyse_lambda_uuid
41-
)
40+
UUID=analyse_lambda_uuid
41+
)
4242
except Exception as e:
43-
print(e)
43+
print(e)
4444

4545
# State (string) -- The state of the event source mapping. It can be one of the following: Creating , Enabling , Enabled , Disabling , Disabled , Updating , or Deleting .
46-
running_states = ["Enabling", "Enabled", "Disabled", "Disabling"]
47-
if response['State'] in running_states:
48-
# Disable
49-
if (event[0]['Action'] == 'disable'):
46+
disabled_states = ["Disabled", "Disabling"]
47+
enabled_states = ["Enabling", "Enabled"]
48+
49+
# Disable
50+
if (event[0]['Action'] == 'disable'):
51+
if (response['State'] in disabled_states):
52+
# Do Nothing
53+
return 'Already disabled'
54+
else:
5055
try:
5156
response = lambda_client.update_event_source_mapping(
52-
UUID = analyse_lambda_uuid,
53-
Enabled = False
57+
UUID=analyse_lambda_uuid,
58+
Enabled=False
5459
)
5560
except Exception as e:
5661
print(e)
57-
else:
58-
# Enable
59-
if (event[0]['Action'] == 'enable'):
62+
else:
63+
# Enable
64+
if (event[0]['Action'] == 'enable'):
65+
if (response['State'] in enabled_states):
66+
# Do Nothing
67+
return 'Already_Running'
68+
else:
6069
try:
6170
response = lambda_client.update_event_source_mapping(
62-
UUID = analyse_lambda_uuid,
63-
Enabled = True
71+
UUID=analyse_lambda_uuid,
72+
Enabled=True
6473
)
6574
except Exception as e:
6675
print(e)
67-
else:
68-
print("Current state is:", response['State'])
69-
76+
77+
print("Current state is:", response['State'])
7078
return response['State']

template.yaml

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Parameters:
1212
Mappings:
1313
Solution:
1414
Constants:
15-
Version: "v0.7"
15+
Version: "v0.8"
1616

1717
Resources:
1818
SourceS3Bucket:
@@ -77,20 +77,20 @@ Resources:
7777
Type: AWS::Serverless::StateMachine
7878
Properties:
7979
Definition:
80-
StartAt: Check Queue
80+
StartAt: Check SQS Queue
8181
States:
82-
Check Queue:
82+
Check SQS Queue:
8383
Type: Task
8484
Resource: !GetAtt SQSPollerFunction.Arn
85-
ResultPath: "$.messageinqueue"
86-
Next: Choice State
87-
Choice State:
85+
ResultPath: $.messageinqueue
86+
Next: Are there images to process?
87+
Are there images to process?:
8888
Type: Choice
8989
Choices:
90-
- Variable: "$.messageinqueue"
90+
- Variable: $.messageinqueue
9191
StringEquals: incoming
9292
Next: Start Model
93-
Default: Disable SQS Trigger
93+
Default: Finish
9494
Start Model:
9595
Type: Task
9696
Resource: !GetAtt StartModelFunction.Arn
@@ -110,13 +110,33 @@ Resources:
110110
Keep Model running for 1 hr:
111111
Type: Wait
112112
Seconds: 3540
113-
Next: Disable SQS Trigger
113+
Next: Check Queue Again
114+
Check Queue Again:
115+
Type: Task
116+
Resource: !GetAtt SQSPollerFunction.Arn
117+
ResultPath: $.moremessagesinqueue
118+
Next: Are there more images?
119+
Are there more images?:
120+
Type: Choice
121+
Choices:
122+
- Variable: $.moremessagesinqueue
123+
StringEquals: stop
124+
Next: Disable SQS Trigger
125+
Default: Keep Model running for 1 hr
114126
Enable SQS Trigger:
115127
Type: Task
128+
ResultPath: $.alreadyrunning
116129
Parameters:
117130
- Action: enable
118131
Resource: !GetAtt ToggleTriggerFunction.Arn
119-
Next: Keep Model running for 1 hr
132+
Next: Is another machine already running?
133+
Is another machine already running?:
134+
Type: Choice
135+
Choices:
136+
- Variable: $.alreadyrunning
137+
StringEquals: Already_Running
138+
Next: Finish
139+
Default: Keep Model running for 1 hr
120140
Disable SQS Trigger:
121141
Type: Task
122142
Parameters:
@@ -126,7 +146,9 @@ Resources:
126146
Stop Model:
127147
Type: Task
128148
Resource: !GetAtt StopModelFunction.Arn
129-
End: true
149+
Next: Finish
150+
Finish:
151+
Type: Succeed
130152
Events:
131153
HourlyPollingSchedule:
132154
Type: Schedule # More info about Schedule Event Source: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-statemachine-schedule.html

0 commit comments

Comments
 (0)