Skip to content

Commit

Permalink
Update CloudFormation template to add Lambda error monitoring with Cl…
Browse files Browse the repository at this point in the history
…oudWatch and SNS notifications.
  • Loading branch information
hiroshinishio committed Jul 9, 2024
1 parent 5d5a1fa commit cf01ee5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:
run: |
{
echo "ECR_REGISTRY=${{ steps.login-ecr.outputs.registry }}"
echo "EMAIL=${{ secrets.EMAIL }}"
echo "IMAGE_TAG=${{ github.sha }}"
if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
echo "ECR_REPOSITORY=${{ secrets.PROD_LAMBDA_NAME }}"
Expand Down Expand Up @@ -65,7 +66,7 @@ jobs:
aws cloudformation deploy \
--stack-name ScheduleStack \
--template-file cloudformation.yml \
--parameter-overrides LambdaFunctionName=$LAMBDA_NAME LambdaFunctionArn=$LAMBDA_ARN \
--parameter-overrides LambdaFunctionName=$LAMBDA_NAME LambdaFunctionArn=$LAMBDA_ARN Email=$EMAIL \
--capabilities CAPABILITY_NAMED_IAM
- name: Notify Slack of deployment status
Expand Down
36 changes: 36 additions & 0 deletions cloudformation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ AWSTemplateFormatVersion: '2010-09-09'
Description: 'CloudFormation template to schedule a Lambda function using AWS EventBridge (formerly CloudWatch Events)'

Parameters:
Email:
Type: String
Description: Email address to receive notifications
LambdaFunctionName:
Type: String
Description: Name of the Lambda function to trigger
Expand Down Expand Up @@ -34,3 +37,36 @@ Resources:
Action: lambda:InvokeFunction
Principal: events.amazonaws.com
SourceArn: !GetAtt SchedulerEventRule.Arn

# SNS Topic for sending notifications
ErrorNotificationTopic:
Type: AWS::SNS::Topic
Properties:
TopicName: LambdaErrorNotificationTopic

# SNS Subscription to send notifications to an email
ErrorNotificationSubscription:
Type: AWS::SNS::Subscription
Properties:
TopicArn: !Ref ErrorNotificationTopic
Protocol: email
Endpoint: !Ref Email

# CloudWatch Alarm for Lambda function errors
LambdaErrorAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName: LambdaFunctionErrorAlarm
AlarmDescription: Alarm for Lambda function errors
MetricName: Errors
Namespace: AWS/Lambda
Statistic: Sum # Total number of errors in each period
Period: 300 # in seconds
EvaluationPeriods: 1
Threshold: 1
ComparisonOperator: GreaterThanOrEqualToThreshold
AlarmActions:
- !Ref ErrorNotificationTopic
Dimensions:
- Name: FunctionName
Value: !Ref LambdaFunctionName
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

if ENV != "local":
sentry_sdk.init(
dsn="https://b7ca4effebf7d7825b6464eade11734f@o4506827828101120.ingest.us.sentry.io/4506865231200256", # noqa
dsn="https://b7ca4effebf7d7825b6464eade11734f@o4506827828101120.ingest.us.sentry.io/4506865231200256",
environment=ENV,
integrations=[AwsLambdaIntegration()],
traces_sample_rate=1.0,
Expand Down

0 comments on commit cf01ee5

Please sign in to comment.