-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.yaml
202 lines (196 loc) · 8.64 KB
/
template.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
A Step Functions Workflow that demonstrates callback and wait functionality
Resources:
##########################################################################
# STEP FUNCTION #
##########################################################################
StateMachineWaitForCallback:
Type: AWS::Serverless::StateMachine # More info about State Machine Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-statemachine.html
Properties:
DefinitionUri: statemachine/wait-for-callback.asl.json
DefinitionSubstitutions:
SQSSendMessageWaitForTaskToken: !Sub arn:${AWS::Partition}:states:::sqs:sendMessage.waitForTaskToken
WaitForCallbackDemoQueueUrl: !Ref WaitForCallbackDemoQueue
WaitForCallbackTopic: !Ref WaitForCallbackTopic
Role: !GetAtt StatesExecutionRole.Arn
Type: STANDARD
Logging:
Destinations:
- CloudWatchLogsLogGroup:
LogGroupArn: !GetAtt StateMachineLogGroup.Arn
IncludeExecutionData: false
Level: 'ALL'
##########################################################################
# STEP FUNCTION LOG GROUP #
##########################################################################
StateMachineLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Join [ "/", [ "stepfunctions", StateMachineWaitForCallback]]
##########################################################################
# Roles #
##########################################################################
StatesExecutionRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- !Sub states.${AWS::Region}.amazonaws.com
Action: "sts:AssumeRole"
Path: "/"
Policies:
- PolicyName: CWLogs
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "logs:CreateLogDelivery"
- "logs:CreateLogStream"
- "logs:GetLogDelivery"
- "logs:UpdateLogDelivery"
- "logs:DeleteLogDelivery"
- "logs:ListLogDeliveries"
- "logs:PutLogEvents"
- "logs:PutResourcePolicy"
- "logs:DescribeResourcePolicies"
- "logs:DescribeLogGroups"
Resource: "*"
- PolicyName: SQSSendMessagePolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "sqs:SendMessage"
Resource: !GetAtt WaitForCallbackDemoQueue.Arn
- PolicyName: SNSPublishPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "sns:Publish"
Resource: !Ref WaitForCallbackTopic
LambdaExecutionRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- !Sub lambda.amazonaws.com
Action: "sts:AssumeRole"
Path: "/"
Policies:
- PolicyName: StepFunctionCallbackPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "states:SendTaskSuccess"
- "states:SendTaskFailure"
- "states:SendTaskHeartbeat"
Resource: !Ref StateMachineWaitForCallback
- PolicyName: SQSSendMessage
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "sqs:ReceiveMessage"
- "sqs:DeleteMessage"
- "sqs:GetQueueAttributes"
- "sqs:ChangeMessageVisibility"
Resource: !GetAtt WaitForCallbackDemoQueue.Arn
##########################################################################
# SQS Queue #
##########################################################################
WaitForCallbackDemoQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: "WaitForCallbackDemoQueue"
VisibilityTimeout: 60
RedrivePolicy:
deadLetterTargetArn: !GetAtt WaitForCallbackDemoQueueDLQ.Arn
maxReceiveCount: 5
WaitForCallbackDemoQueueDLQ:
Type: AWS::SQS::Queue
Properties:
QueueName: "WaitForCallbackDemoQueueDLQ"
WaitForCallbackDemoNotificationTestQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: "WaitForCallbackDemoNotificationTestQueue"
VisibilityTimeout: 60
RedrivePolicy:
deadLetterTargetArn: !GetAtt WaitForCallbackDemoNotificationTestQueueDLQ.Arn
maxReceiveCount: 5
WaitForCallbackDemoNotificationTestQueueDLQ:
Type: AWS::SQS::Queue
Properties:
QueueName: "WaitForCallbackDemoNotificationTestQueueDLQ"
##########################################################################
# SNS Topic #
##########################################################################
WaitForCallbackTopic:
Type: AWS::SNS::Topic
Properties:
Subscription:
- Endpoint: !GetAtt WaitForCallbackDemoNotificationTestQueue.Arn
Protocol: sqs
TopicName: "WaitForCallbackTopic"
##########################################################################
# SNS to SQS Publish Policy #
##########################################################################
SnsToSqsPolicy:
Type: AWS::SQS::QueuePolicy
Properties:
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: sns.amazonaws.com
Action: sqs:SendMessage
Resource: !GetAtt WaitForCallbackDemoNotificationTestQueue.Arn
Condition:
ArnEquals:
aws:SourceArn: !Ref WaitForCallbackTopic
Queues:
- !Ref WaitForCallbackDemoNotificationTestQueue
##########################################################################
# Lambda Function #
##########################################################################
WaitForCallbackLambdaFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: source/
Handler: app.lambda_handler
Runtime: python3.12
Role: !GetAtt LambdaExecutionRole.Arn
Events:
SQSEvent:
Type: SQS
Properties:
Queue: !GetAtt WaitForCallbackDemoQueue.Arn
BatchSize: 1
##########################################################################
# Outputs #
##########################################################################
Outputs:
StateMachineWaitForCallbackArn:
Value: !Ref StateMachineWaitForCallback
Description: StateMachineWaitForCallback Arn
WaitForCallbackDemoNotificationTestQueueUrl:
Value: !GetAtt WaitForCallbackDemoNotificationTestQueue.QueueUrl
Description: WaitForCallbackDemoNotificationTestQueue Url