Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First steps to lambda deploy #13

Merged
merged 7 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lgsf/aws_lambda/fixtures/sqs-message-der.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"Records": [
{
"messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb78",
"receiptHandle": "MessageReceiptHandle",
"body": "{\"scraper_type\": \"councillors\",\"council\": \"DER\"}",
"attributes": {
"ApproximateReceiveCount": "1",
"SentTimestamp": "1523232000000",
"SenderId": "123456789012",
"ApproximateFirstReceiveTimestamp": "1523232000001"
},
"messageAttributes": {},
"md5OfBody": "7b270e59b47ff90a553787216d55d91d",
"eventSource": "aws:sqs",
"eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:MyQueue",
"awsRegion": "eu-west-2"
}
]
}
20 changes: 20 additions & 0 deletions lgsf/aws_lambda/fixtures/sqs-message-sfk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"Records": [
{
"messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb78",
"receiptHandle": "MessageReceiptHandle",
"body": "{\"scraper_type\": \"councillors\",\"council\": \"SFK\"}",
"attributes": {
"ApproximateReceiveCount": "1",
"SentTimestamp": "1523232000000",
"SenderId": "123456789012",
"ApproximateFirstReceiveTimestamp": "1523232000001"
},
"messageAttributes": {},
"md5OfBody": "7b270e59b47ff90a553787216d55d91d",
"eventSource": "aws:sqs",
"eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:MyQueue",
"awsRegion": "eu-west-2"
}
]
}
20 changes: 20 additions & 0 deletions lgsf/aws_lambda/fixtures/sqs-message-wlv.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"Records": [
{
"messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb78",
"receiptHandle": "MessageReceiptHandle",
"body": "{\"scraper_type\": \"councillors\",\"council\": \"WLV\"}",
"attributes": {
"ApproximateReceiveCount": "1",
"SentTimestamp": "1523232000000",
"SenderId": "123456789012",
"ApproximateFirstReceiveTimestamp": "1523232000001"
},
"messageAttributes": {},
"md5OfBody": "7b270e59b47ff90a553787216d55d91d",
"eventSource": "aws:sqs",
"eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:MyQueue",
"awsRegion": "eu-west-2"
}
]
}
16 changes: 16 additions & 0 deletions lgsf/aws_lambda/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@
from lgsf.path_utils import load_scraper


def scraper_worker_handler(event, context):
print(f"EVENT: {event}")
GeoWill marked this conversation as resolved.
Show resolved Hide resolved
message = json.loads(event["Records"][0]["body"])
council = message["council"]
command_name = message["scraper_type"]
scraper_cls = load_scraper(council, command_name)
options = {"council": council, "verbose": True, "aws_lambda": True}
GeoWill marked this conversation as resolved.
Show resolved Hide resolved
console = Console(file=sys.stdout)
scraper = scraper_cls(options, console)
try:
scraper.run()
except Exception as e:
scraper.console.log(e)
scraper.delete_branch()


def queue_builder_handler(event, context):
councillors_command = Command(argv=["", "--all-councils"], stdout=sys.stdout)
councillors_command.options = {"all_councils": True, "exclude_missing": True}
Expand Down
16 changes: 16 additions & 0 deletions sam-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ Resources:
RepositoryName: CouncillorsRepo
RepositoryDescription: Repository for scraped councillor data.

ScraperWorkerFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: .
Handler: lgsf.aws_lambda.handlers.scraper_worker_handler
Runtime: python3.8
Events:
SQSEvent:
Type: SQS
Properties:
Queue: !GetAtt SQSScraperQueue.Arn
BatchSize: 1
GeoWill marked this conversation as resolved.
Show resolved Hide resolved
Enabled: true
Role: !Sub "arn:aws:iam::${AWS::AccountId}:role/LGSFLambdaExecutionRole"



Outputs:
# Find out more about other implicit resources you can reference within SAM
Expand Down