Skip to content
This repository was archived by the owner on Dec 25, 2022. It is now read-only.

Commit 2ec8528

Browse files
author
Michael Bahr
committed
updated docs with stage info and setup scripts
1 parent 337a114 commit 2ec8528

File tree

2 files changed

+55
-29
lines changed

2 files changed

+55
-29
lines changed

README.md

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,35 @@ You can deploy this yourself or use our SAAS offer.
1010

1111
## SAAS Offer
1212

13-
While we're ironing things out you can use our service free of charge. Just publish an event to `arn:aws:sns:us-east-1:256608350746:scheduler-input-dev`. This topic is public so anyone can publish to it.
13+
While we're ironing things out you can use our service free of charge. Just publish an event to `arn:aws:sns:us-east-1:256608350746:scheduler-input-prod`. This topic is public so anyone can publish to it.
1414

1515
If you become a heavy user with more than 100.000 events per month we might want to get in touch with you, so make sure to fill out the `user` field with some way to contact you (email, twitter handle, ...).
1616

1717
## Usage
1818

1919
### Setup
20-
First of all you need an output topic that we can publish events to once the scheduled datetime arrives. To do this go to the AWS Console, then open the service SNS and create a topic that includes the following access policy.
20+
First of all you need an output topic that we can publish events to once the scheduled datetime arrives. To do this run `python setup/init_output_topic.py <stage>`. This will create a topic called `scheduler-output-<stage>` and grant our account the right to publish messages. You can see the added policy below.
2121

2222
```json
23-
{
24-
"Sid": "HereComesYourSidName",
25-
"Effect": "Allow",
26-
"Principal": {
27-
"AWS": "arn:aws:sts::256608350746:assumed-role/aws-scheduler-dev-us-east-1-lambdaRole/aws-scheduler-dev-emitter"
28-
},
29-
"Action": "SNS:Publish",
30-
"Resource": "arn:aws:sns:us-east-1:256608350746:scheduler-output-dev"
31-
}
23+
{
24+
"Sid": "scheduler-output-<stage>-publish-access",
25+
"Effect": "Allow",
26+
"Principal": {
27+
"AWS": "arn:aws:iam::256608350746:root"
28+
},
29+
"Action": "SNS:Publish",
30+
"Resource": "arn:aws:sns:us-east-1:256608350746:scheduler-output-<stage>"
31+
}
3232
```
3333

34-
This policy allows our emitter function to publish events to your topic which you can then process with e.g. AWS Lambda.
34+
If you don't want to grant our account the right to publish, you can pass your own accountId as a third argument.
35+
36+
```
37+
Creating topic scheduler-output-<stage>
38+
Created topic scheduler-output-<stage> with arn arn:aws:sns:us-east-1:<your-account-id>:scheduler-output-<stage>
39+
Granting publish rights to scheduler-output-<stage> for accountId 256608350746
40+
Done
41+
```
3542

3643
Write down the ARN of your output topic as you will need it for the input events.
3744

@@ -47,7 +54,27 @@ To schedule a trigger you have to publish an event which follows the structure b
4754
}
4855
```
4956

50-
All fields are mandatory. If you submit an event that does not follow the spec, it will be dropped. Future versions will improve on this.
57+
First string encode the json structure and then publish it to the input topic.
58+
59+
```python
60+
# Python example
61+
import json
62+
import boto3
63+
64+
client = boto3.client('sns')
65+
66+
event = {
67+
"date": "2019-07-27T12:20:24.919071",
68+
"target": "arn:aws:sns:us-east-1:256608350746:scheduler-output-prod",
69+
"user": "Twitter @michabahr",
70+
"payload": "46607451-3e67-49bc-972b-425c150c5456"
71+
}
72+
73+
input_topic = 'arn:aws:sns:us-east-1:256608350746:scheduler-input-prod'
74+
client.publish(TopicArn=input_topic, Message=json.dumps(event))
75+
```
76+
77+
All fields are mandatory. Please make sure that the `payload` can be utf-8 encoded. If you submit an event that does not follow the spec, it will be dropped. Future versions will improve on this.
5178

5279
So far there is no batch publishing available for SNS. Make sure the event stays within the 256KB limit of SNS. We recommend that you only submit IDs and don't transfer any real data to the service.
5380

@@ -69,9 +96,11 @@ You must have the following tools installed:
6996
- python3
7097
- pip
7198

72-
Run `setup/init_table.py` to setup the database.
99+
Run `setup/init_table.py <stage>` to setup the database. Replace `<stage>` with the stage of your application.
100+
101+
Run `setup/init_input_topic.py <stage> [public]`. Replace `<stage>` with the stage of your application. You may append the parameter `public` to grant public publish rights.
73102

74-
Create an SNS topic named `scheduler-input-{stage}` and a SQS queue named `scheduler-queue-{stage}`. Replace `{stage}` with the stage that you use for the serverless deployment, e.g. `dev`. Adjust the access policies as necessary.
103+
Run `setup/init_queue.py <stage>`. Replace `<stage>` with the stage of your application.
75104

76105
### Deploy
77106
1. Navigate into the project folder
@@ -91,8 +120,8 @@ Our results showed that most of the events arrive within one second of the speci
91120

92121
The charts show the amount of events received on the y axis and the distribution by delay on the x axis.
93122

94-
![Regular Scaled 100000 events wihtin 10 minutes](https://github.com/bahrmichael/aws-scheduler/raw/master/pictures/regular-scaled-100k-10m.png)
95-
![Log Scaled 100000 events wihtin 10 minutes](https://github.com/bahrmichael/aws-scheduler/raw/master/pictures/log-scaled-100k-10m.png)
123+
![Regular Scaled 100000 events within 10 minutes](https://github.com/bahrmichael/aws-scheduler/raw/master/pictures/regular-scaled-100k-10m.png)
124+
![Log Scaled 100000 events within 10 minutes](https://github.com/bahrmichael/aws-scheduler/raw/master/pictures/log-scaled-100k-10m.png)
96125

97126
## Limitations
98127
Events may arrive more than once at the output topic.
@@ -101,11 +130,12 @@ Events may arrive more than once at the output topic.
101130
Contributions are welcome, both issues and code. Get in touch at twitter [@michabahr](https://twitter.com/michabahr) or create an issue.
102131

103132
## TODOs
104-
- test user check
105133
- test with huge messages (first make as big as possible until fails, then execute them with more than 10 minutes target so they get bulked)
106-
- prod deployment and update arns/policies
134+
- attach to own project
135+
- helper script to create an output topic with proper access policy (should take name and region as parameters)
107136
- use a proper logger
108137
- secure the PoC with test
109138
- include a failure queue and adjust the docs
110139
- add a (video) guide on how to create a proper output queue
111140
- add a safe guard that pulls messages from dead letter queues back into the circuit
141+
- handling for messages that can't be utf-8 encoded

serverless.yml

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ service: aws-scheduler
33
provider:
44
name: aws
55
runtime: python3.7
6-
stage: dev
7-
region: us-east-1
6+
stage: ${opt:stage, 'dev'}
7+
region: stage: ${opt:region, 'us-east-1'}
88
iamRoleStatements:
99
- Effect: Allow
1010
Action:
@@ -21,10 +21,6 @@ provider:
2121
Action:
2222
- SQS:SendMessage
2323
Resource: "${self:custom.queue.arn}"
24-
- Effect: Allow
25-
Action:
26-
- dynamodb:*
27-
Resource: { "Fn::Join": [":", ["arn:aws:dynamodb:${self:provider.region}", { "Ref": "AWS::AccountId" }, "table/aws-scheduler-measurement" ] ] }
2824
- Effect: Allow
2925
Action:
3026
- lambda:InvokeFunction
@@ -39,11 +35,11 @@ custom:
3935
arn: { "Fn::Join": [":", ["arn:aws:sqs:${self:provider.region}", { "Ref": "AWS::AccountId" }, "${self:custom.queue.name}" ] ] }
4036
url: { "Fn::Join": ["/", ["https://sqs.${self:provider.region}.amazonaws.com", { "Ref": "AWS::AccountId" }, "${self:custom.queue.name}" ] ] }
4137
table:
42-
arn: { "Fn::Join": [":", ["arn:aws:dynamodb:${self:provider.region}", { "Ref": "AWS::AccountId" }, "table/aws-scheduler-events" ] ] }
38+
arn: { "Fn::Join": [":", ["arn:aws:dynamodb:${self:provider.region}", { "Ref": "AWS::AccountId" }, "table/aws-scheduler-events-${self:provider.stage}" ] ] }
4339
index:
4440
name: "status-date-index"
45-
arn: { "Fn::Join": [":", ["arn:aws:dynamodb:${self:provider.region}", { "Ref": "AWS::AccountId" }, "table/aws-scheduler-events/index/${self:custom.table.index.name}" ] ] }
46-
scheduleFunction: "aws-scheduler-${self:provider.stage}-scheduleBatch"
41+
arn: { "Fn::Join": [":", ["arn:aws:dynamodb:${self:provider.region}", { "Ref": "AWS::AccountId" }, "table/aws-scheduler-events-${self:provider.stage}/index/${self:custom.table.index.name}" ] ] }
42+
scheduleFunction: "aws-scheduler-${self:provider.stage}-scheduler"
4743
pythonRequirements:
4844
pythonBin: python3
4945

@@ -62,7 +58,7 @@ functions:
6258
topicName: "${self:custom.inbound.name}"
6359
environment:
6460
SCHEDULE_FUNCTION: "${self:custom.scheduleFunction}"
65-
ENFORCE_USER: true
61+
ENFORCE_USER: false
6662
eventLoader:
6763
handler: handler.event_loader
6864
events:

0 commit comments

Comments
 (0)