-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.yaml
More file actions
109 lines (99 loc) · 3.3 KB
/
template.yaml
File metadata and controls
109 lines (99 loc) · 3.3 KB
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
AWSTemplateFormatVersion: 2010-09-09
Description: >-
task-scheduler
Transform:
- AWS::Serverless-2016-10-31
# Resources declares the AWS resources that you want to include in the stack
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resources-section-structure.html
Resources:
# Each Lambda function is defined by properties:
# https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
# ✅ Task Scheduling Lambda (POST /schedule-task)
scheduleTaskFunction:
Type: AWS::Serverless::Function
Properties:
Handler: src/handlers/schedule-task.scheduleTaskHandler
Runtime: nodejs22.x
MemorySize: 128
Timeout: 100
Description: Schedule a new task
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref TasksTable
- Statement:
- Effect: Allow
Action:
- events:PutRule
- events:PutTargets
Resource: "*"
Environment:
Variables:
TASKS_TABLE: !Ref TasksTable
EXECUTE_TASK_LAMBDA_ARN: !GetAtt executeTaskFunction.Arn
Events:
Api:
Type: Api
Properties:
Path: /schedule-task
Method: POST
# ✅ Lambda for Executing Due Tasks
executeTaskFunction:
Type: AWS::Serverless::Function
Properties:
Handler: src/handlers/execute-task.executeTaskHandler
Runtime: nodejs22.x
MemorySize: 128
Timeout: 60
Description: Executes scheduled tasks that are due
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref TasksTable
Environment:
Variables:
TASKS_TABLE: !Ref TasksTable
# ✅ EventBridge Rule for Triggering Task Execution
InvokePermissionForEventBridge:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt executeTaskFunction.Arn
Action: lambda:InvokeFunction
Principal: events.amazonaws.com
# Simple syntax to create a DynamoDB table with a single attribute primary key, more in
# https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlesssimpletable
# ✅ DynamoDB Table for Storing Tasks
TasksTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: tasks-table
AttributeDefinitions:
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
ApplicationResourceGroup:
Type: AWS::ResourceGroups::Group
Properties:
Name:
Fn::Sub: ApplicationInsights-SAM-${AWS::StackName}
ResourceQuery:
Type: CLOUDFORMATION_STACK_1_0
ApplicationInsightsMonitoring:
Type: AWS::ApplicationInsights::Application
Properties:
ResourceGroupName:
Ref: ApplicationResourceGroup
AutoConfigurationEnabled: "true"
Outputs:
WebEndpoint:
Description: API Gateway endpoint URL for Prod stage
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
LoggingConfig:
LogFormat: JSON
Tracing: Active