From b6e22b2840d340edaecd999f284aaf68491055ab Mon Sep 17 00:00:00 2001 From: JD <46619169+rudiejd@users.noreply.github.com> Date: Fri, 15 Mar 2024 13:11:05 -0400 Subject: [PATCH] fix: get yankee stuff to be deployable, add cloudformation for it (#79) * fix: get yankee stuff to be deployable, add cloudformation for it * fix: add yankee function to resources --------- Co-authored-by: JD Rudie --- deploy.sh | 6 +-- ingestor/.chalice/config.json | 4 ++ ingestor/.chalice/policy-yankee.json | 38 ++++++++++++++++ ingestor/.chalice/resources.json | 66 ++++++++++++++++++++++++++++ ingestor/chalicelib/yankee.py | 4 +- 5 files changed, 113 insertions(+), 5 deletions(-) create mode 100644 ingestor/.chalice/policy-yankee.json diff --git a/deploy.sh b/deploy.sh index 62a294d..8813405 100755 --- a/deploy.sh +++ b/deploy.sh @@ -1,7 +1,7 @@ #!/bin/bash -x -if [[ -z "$MBTA_V2_API_KEY" || -z "$DD_API_KEY" ]]; then - echo "Must provide MBTA_V2_API_KEY and DD_API_KEY in environment" 1>&2 +if [[ -z "$MBTA_V2_API_KEY" || -z "$DD_API_KEY" || -z "$YANKEE_API_KEY"]]; then + echo "Must provide MBTA_V2_API_KEY, YANKEE_API_KEY, and DD_API_KEY in environment" 1>&2 exit 1 fi @@ -24,4 +24,4 @@ poetry run chalice package --stage prod --merge-template .chalice/resources.json aws cloudformation package --template-file cfn/sam.json --s3-bucket $BUCKET --output-template-file cfn/packaged.yaml aws cloudformation deploy --template-file cfn/packaged.yaml --stack-name $STACK_NAME \ --capabilities CAPABILITY_NAMED_IAM --no-fail-on-empty-changeset \ - --parameter-overrides MbtaV2ApiKey=$MBTA_V2_API_KEY DDApiKey=$DD_API_KEY GitVersion=$GIT_VERSION DDTags=$DD_TAGS + --parameter-overrides MbtaV2ApiKey=$MBTA_V2_API_KEY DDApiKey=$DD_API_KEY YankeeApiKey=$YANKEE_API_KEY GitVersion=$GIT_VERSION DDTags=$DD_TAGS diff --git a/ingestor/.chalice/config.json b/ingestor/.chalice/config.json index efe34c3..3fefcac 100644 --- a/ingestor/.chalice/config.json +++ b/ingestor/.chalice/config.json @@ -100,6 +100,10 @@ "update_time_predictions": { "iam_policy_file": "policy-time-predictions.json", "lambda_timeout": 300 + }, + "yankee": { + "iam_policy_file": "policy-yankee.json", + "lambda_timeout": 900 } } } diff --git a/ingestor/.chalice/policy-yankee.json b/ingestor/.chalice/policy-yankee.json new file mode 100644 index 0000000..a118c9d --- /dev/null +++ b/ingestor/.chalice/policy-yankee.json @@ -0,0 +1,38 @@ +{ + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "logs:CreateLogGroup", + "logs:CreateLogStream", + "logs:PutLogEvents" + ], + "Effect": "Allow", + "Resource": "arn:*:logs:*:*:*" + }, + { + "Action": ["dynamodb:BatchWriteItem"], + "Effect": "Allow", + "Resource": [ + "arn:aws:dynamodb:us-east-1:473352343756:table/ShuttleTravelTimes", + "arn:aws:dynamodb:us-east-1:473352343756:table/ShuttleTravelTimes" + ] + }, + { + "Action": ["dynamodb:Query"], + "Effect": "Allow", + "Resource": ["arn:aws:dynamodb:us-east-1:473352343756:table/ShuttleTravelTimes"] + }, + { + "Action": [ + "s3:GetObject", + "s3:PutObject" + ], + "Effect": "Allow", + "Resource": [ + "arn:aws:s3:::tm-shuttle-positions", + "arn:aws:s3:::tm-shuttle-positions/*" + ] + } + ] +} diff --git a/ingestor/.chalice/resources.json b/ingestor/.chalice/resources.json index dab89ef..21b818e 100644 --- a/ingestor/.chalice/resources.json +++ b/ingestor/.chalice/resources.json @@ -4,6 +4,10 @@ "Type": "String", "Description": "MBTA Performance API Key" }, + "YankeeApiKey": { + "Type": "String", + "Description", "Yankee shuttle API key" + } "DDApiKey": { "Type": "String", "Description": "Datadog API key." @@ -36,6 +40,27 @@ } } }, + "Yankee": { + "Type": "AWS::Serverless::Function", + "Properties": { + "Environment": { + "Variables": { + "DD_API_KEY": { + "Ref": "DDApiKey" + }, + "DD_VERSION": { + "Ref": "GitVersion" + }, + "DD_TAGS": { + "Ref": "DDTags" + }, + "YANKEE_API_KEY": { + "Ref": "YankeeApiKey" + } + } + } + } + }, "PopulateAggDeliveredTripMetrics": { "Type": "AWS::Serverless::Function", "Properties": { @@ -559,6 +584,47 @@ } ] } + }, + "ShuttleTravelTimes": { + "Type": "AWS::DynamoDB::Table", + "Properties": { + "TableName": "ShuttleTravelTimes", + "AttributeDefinitions": [ + { + "AttributeName": "routeId", + "AttributeType": "S" + }, + { + "AttributeName": "date", + "AttributeType": "S" + } + ], + "KeySchema": [ + { + "AttributeName": "routeId", + "KeyType": "HASH" + }, + { + "AttributeName": "date", + "KeyType": "RANGE" + } + + ], + "BillingMode": "PAY_PER_REQUEST", + "Tags": [ + { + "Key": "service", + "Value": "t-performance-dash" + } + ] + } + }, + "S3Bucket": { + "Type": "AWS::S3::Bucket", + "DeletionPolicy": "Retain", + "Properties": { + "BucketName": "tm-shuttle-positions" + } } } } diff --git a/ingestor/chalicelib/yankee.py b/ingestor/chalicelib/yankee.py index 526f4ae..d56d5dc 100644 --- a/ingestor/chalicelib/yankee.py +++ b/ingestor/chalicelib/yankee.py @@ -2,7 +2,7 @@ import requests import json import boto3 -from keys import YANKEE_API_KEY +from .keys import YANKEE_API_KEY from botocore.exceptions import ClientError from datetime import datetime from tempfile import TemporaryDirectory @@ -38,7 +38,7 @@ class ShuttleTravelTime: # line of the trip (for now always line-shuttle) line: str # route of the trip e.g. Shuttle-AlewifeParkSt - route: str + routeId: str date: datetime # distance in miles of the trip distance_miles: float