From f028588f7a8a68b795b73b9dc47bd01913cf3439 Mon Sep 17 00:00:00 2001 From: "Hiroshi (Wes) Nishio" <4620828+hiroshinishio@users.noreply.github.com> Date: Mon, 1 Jul 2024 14:16:13 +0900 Subject: [PATCH] Revert "Add sample Lambda function for periodic execution using EventBridge and codify EventBridge infrastructure with CloudFormation" --- .github/workflows/deployment.yml | 26 +++++------------------- cloudformation.yml | 35 -------------------------------- main.py | 17 ++++------------ requirements.txt | 10 ++++----- scheduler.py | 5 ----- 5 files changed, 14 insertions(+), 79 deletions(-) delete mode 100644 cloudformation.yml delete mode 100644 scheduler.py diff --git a/.github/workflows/deployment.yml b/.github/workflows/deployment.yml index ae8e4a3c..d77eabb8 100644 --- a/.github/workflows/deployment.yml +++ b/.github/workflows/deployment.yml @@ -5,10 +5,6 @@ on: branches: - '*' -# Global environment variables -env: - AWS_REGION: us-west-1 - jobs: deploy_lambda: name: Publish and Deploy @@ -22,26 +18,24 @@ jobs: with: aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - aws-region: ${{ env.AWS_REGION }} + aws-region: us-west-1 - name: Login to Amazon ECR id: login-ecr uses: aws-actions/amazon-ecr-login@v2 - # LAMBDA_ARN is only used in production - name: Set deployment environment variables run: | { echo "ECR_REGISTRY=${{ steps.login-ecr.outputs.registry }}" echo "IMAGE_TAG=${{ github.sha }}" if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then - echo "ECR_REPOSITORY=${{ secrets.PROD_LAMBDA_NAME }}" - echo "LAMBDA_NAME=${{ secrets.PROD_LAMBDA_NAME }}" - echo "LAMBDA_ARN=arn:aws:lambda:${{ env.AWS_REGION }}:${{ secrets.AWS_ACCOUNT_ID }}:function:${{ secrets.PROD_LAMBDA_NAME }}" + echo "ECR_REPOSITORY=pr-agent-prod" + echo "LAMBDA_NAME=pr-agent-prod" echo "SLACK_WEBHOOK_URL=${{ secrets.SLACK_WEBHOOK_URL_FOR_PRD }}" else - echo "ECR_REPOSITORY=${{ secrets.STAGE_LAMBDA_NAME }}" - echo "LAMBDA_NAME=${{ secrets.STAGE_LAMBDA_NAME }}" + echo "ECR_REPOSITORY=stage" + echo "LAMBDA_NAME=stage" echo "SLACK_WEBHOOK_URL=${{ secrets.SLACK_WEBHOOK_URL_FOR_STG }}" fi } >> $GITHUB_ENV @@ -58,16 +52,6 @@ jobs: --function-name $LAMBDA_NAME \ --image-uri $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG - # Create or update the CloudFormation stack only for the main branch - - name: Deploy CloudFormation stack for scheduled event - if: github.ref == 'refs/heads/main' - run: | - aws cloudformation deploy \ - --stack-name ScheduleStack \ - --template-file cloudformation.yml \ - --parameter-overrides LambdaFunctionName=$LAMBDA_NAME LambdaFunctionArn=$LAMBDA_ARN \ - --capabilities CAPABILITY_NAMED_IAM - - name: Notify Slack of deployment status # Execute this step even if the previous steps fail if: always() diff --git a/cloudformation.yml b/cloudformation.yml deleted file mode 100644 index 269c2090..00000000 --- a/cloudformation.yml +++ /dev/null @@ -1,35 +0,0 @@ -AWSTemplateFormatVersion: '2010-09-09' -Description: 'CloudFormation template to schedule a Lambda function using AWS EventBridge (formerly CloudWatch Events)' - -Parameters: - LambdaFunctionName: - Type: String - Description: Name of the Lambda function to trigger - LambdaFunctionArn: - Type: String - Description: ARN of the Lambda function to trigger - -Resources: - # Lambda function is defined from AWS Console - # https://us-west-1.console.aws.amazon.com/lambda/home?region=us-west-1#/functions - - # Define AWS EventBridge (CloudWatch Events) rule to schedule the Lambda function - SchedulerEventRule: - Type: AWS::Events::Rule - Properties: - Name: SchedulerEventRule - Description: "Schedule Lambda function to run every weekday at 0 AM UTC" - ScheduleExpression: cron(0 0 ? * MON-FRI *) - State: ENABLED - Targets: - - Arn: !Ref LambdaFunctionArn - Id: "LambdaFunctionTarget" - - # Permission for AWS EventBridge to invoke the Lambda function - LambdaInvokePermission: - Type: AWS::Lambda::Permission - Properties: - FunctionName: !Ref LambdaFunctionName - Action: lambda:InvokeFunction - Principal: events.amazonaws.com - SourceArn: !GetAtt SchedulerEventRule.Arn diff --git a/main.py b/main.py index cee01f82..3db9481e 100644 --- a/main.py +++ b/main.py @@ -11,10 +11,12 @@ # Local imports from config import GITHUB_WEBHOOK_SECRET, ENV, PRODUCT_NAME, UTF8 -from scheduler import schedule_handler from services.github.github_manager import verify_webhook_signature from services.webhook_handler import handle_webhook_event +# Create FastAPI instance +app = FastAPI() + if ENV != "local": sentry_sdk.init( dsn="https://b7ca4effebf7d7825b6464eade11734f@o4506827828101120.ingest.us.sentry.io/4506865231200256", # noqa @@ -23,18 +25,7 @@ traces_sample_rate=1.0, ) -# Create FastAPI instance and Mangum handler -app = FastAPI() -mangum_handler = Mangum(app=app) - - -# Here is an entry point for the AWS Lambda function. Mangum is a library that allows you to use FastAPI with AWS Lambda. -def lambda_handler(event, context): - if "source" in event and event["source"] == "aws.events": - schedule_handler(event=event, context=context) - return {"statusCode": 200} - - return mangum_handler(event=event, context=context) +handler = Mangum(app=app) @app.post(path="/webhook") diff --git a/requirements.txt b/requirements.txt index ead395f1..77f8f50d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,13 +11,13 @@ deprecation==2.1.0 distro==1.9.0 exceptiongroup==1.2.1 fastapi==0.111.0 -gotrue==2.5.4 +gotrue==2.5.2 h11==0.14.0 httpcore==1.0.5 httpx==0.27.0 idna==3.7 mangum==0.17.0 -openai==1.35.7 +openai==1.35.3 packaging==24.1 postgrest==0.16.8 pre-commit==3.7.1 @@ -25,18 +25,18 @@ pycparser==2.22 pydantic==2.7.4 pydantic_core==2.18.4 PyJWT==2.8.0 -pylint==3.2.5 +pylint==3.2.3 python-dateutil==2.9.0.post0 python-dotenv==1.0.1 realtime==1.0.6 requests==2.32.3 -sentry-sdk==2.7.1 +sentry-sdk==2.6.0 six==1.16.0 sniffio==1.3.1 starlette==0.37.2 storage3==0.7.6 StrEnum==0.4.15 -stripe==10.1.0 +stripe==9.12.0 supabase==2.5.1 supafunc==0.4.6 tqdm==4.66.4 diff --git a/scheduler.py b/scheduler.py deleted file mode 100644 index 6787a061..00000000 --- a/scheduler.py +++ /dev/null @@ -1,5 +0,0 @@ -# This is scheduled to run by AWS Lambda -def schedule_handler(event, context) -> dict[str, int]: - print(f"Event: {event}") - print(f"Context: {context}") - return {"statusCode": 200}