From 794b61e0da685a25d8decbf240e5cf73044dc4c1 Mon Sep 17 00:00:00 2001 From: Jordan Roth <41999487+jordanaroth@users.noreply.github.com> Date: Fri, 7 May 2021 13:06:35 -0700 Subject: [PATCH] org member deployment beta --- .../01_CFN_MGMT_ROLE.yml | 48 ++ .../02_CFN_DEPLOY_AHA.yml | 345 ++++++++ BETA-org-member-deployment/CODE_OF_CONDUCT.md | 4 + BETA-org-member-deployment/CONTRIBUTING.md | 59 ++ BETA-org-member-deployment/LICENSE | 15 + BETA-org-member-deployment/README.md | 170 ++++ BETA-org-member-deployment/handler.py | 763 ++++++++++++++++++ .../messagegenerator.py | 570 +++++++++++++ 8 files changed, 1974 insertions(+) create mode 100644 BETA-org-member-deployment/01_CFN_MGMT_ROLE.yml create mode 100644 BETA-org-member-deployment/02_CFN_DEPLOY_AHA.yml create mode 100644 BETA-org-member-deployment/CODE_OF_CONDUCT.md create mode 100644 BETA-org-member-deployment/CONTRIBUTING.md create mode 100644 BETA-org-member-deployment/LICENSE create mode 100644 BETA-org-member-deployment/README.md create mode 100644 BETA-org-member-deployment/handler.py create mode 100644 BETA-org-member-deployment/messagegenerator.py diff --git a/BETA-org-member-deployment/01_CFN_MGMT_ROLE.yml b/BETA-org-member-deployment/01_CFN_MGMT_ROLE.yml new file mode 100644 index 0000000..d6c172b --- /dev/null +++ b/BETA-org-member-deployment/01_CFN_MGMT_ROLE.yml @@ -0,0 +1,48 @@ +AWSTemplateFormatVersion: "2010-09-09" +Description: Deploy Cross-Account Role for PHD access +Parameters: + OrgMemberAccountId: + Type: String + AllowedPattern: '^\d{12}$' + Description: AWS Account ID of the AWS Organizations Member Account that will run AWS Health Aware +Resources: + AWSHealthAwareRoleForPHDEvents: + Type: "AWS::IAM::Role" + Properties: + Description: "Grants access to PHD events" + Path: / + AssumeRolePolicyDocument: + Version: '2012-10-17' + Statement: + - Action: + - sts:AssumeRole + Effect: Allow + Principal: + AWS: !Sub 'arn:aws:iam::${OrgMemberAccountId}:root' + Policies: + - PolicyName: AllowHealthCalls + PolicyDocument: + Statement: + - Effect: Allow + Action: + - health:DescribeAffectedAccountsForOrganization + - health:DescribeAffectedEntitiesForOrganization + - health:DescribeEventDetailsForOrganization + - health:DescribeEventsForOrganization + - health:DescribeEventDetails + - health:DescribeEvents + - health:DescribeEventTypes + - health:DescribeAffectedEntities + Resource: "*" + - PolicyName: AllowsDescribeOrg + PolicyDocument: + Statement: + - Effect: Allow + Action: + - organizations:ListAccounts + - organizations:ListAWSServiceAccessForOrganization + - organizations:DescribeAccount + Resource: "*" +Outputs: + AWSHealthAwareRoleForPHDEventsArn: + Value: !GetAtt AWSHealthAwareRoleForPHDEvents.Arn diff --git a/BETA-org-member-deployment/02_CFN_DEPLOY_AHA.yml b/BETA-org-member-deployment/02_CFN_DEPLOY_AHA.yml new file mode 100644 index 0000000..cbd3cec --- /dev/null +++ b/BETA-org-member-deployment/02_CFN_DEPLOY_AHA.yml @@ -0,0 +1,345 @@ +AWSTemplateFormatVersion: '2010-09-09' +Description: CloudFormation Template for AWS Health Aware (AHA) +Metadata: + 'AWS::CloudFormation::Interface': + ParameterGroups: + - Label: + default: Customize Alerts/Notifications + Parameters: + - AWSOrganizationsEnabled + - AWSHealthEventType + - Label: + default: Package Information + Parameters: + - S3Bucket + - S3Key + - Label: + default: >- + Communication Channels - Slack/Microsoft Teams/Amazon Chime And/or + EventBridge + Parameters: + - SlackWebhookURL + - MicrosoftTeamsWebhookURL + - AmazonChimeWebhookURL + - EventBusName + - Label: + default: Email Setup - For Alerting via Email + Parameters: + - FromEmail + - ToEmail + - Subject + - Label: + default: More Configurations - Optional + Parameters: + - EventSearchBack + - Regions + - ManagementAccountRoleArn +Conditions: + UsingSlack: !Not [!Equals [!Ref SlackWebhookURL, None]] + UsingTeams: !Not [!Equals [!Ref MicrosoftTeamsWebhookURL, None]] + UsingChime: !Not [!Equals [!Ref AmazonChimeWebhookURL, None]] + UsingEventBridge: !Not [!Equals [!Ref EventBusName, None]] + UsingSecrets: !Or [!Condition UsingSlack, !Condition UsingTeams, !Condition UsingChime, !Condition UsingEventBridge] + UsingCrossAccountRole: !Not [!Equals [!Ref ManagementAccountRoleArn, None]] +Parameters: + AWSOrganizationsEnabled: + Description: >- + You can receive both PHD and SHD alerts if you're using AWS Organizations. + If you are, make sure to enable Organizational Health View: + (https://docs.aws.amazon.com/health/latest/ug/aggregate-events.html) to + aggregate all PHD events in your AWS Organization. If not, you can still + get SHD alerts. + Default: 'No' + AllowedValues: + - 'Yes' + - 'No' + Type: String + ManagementAccountRoleArn: + Description: Arn of the IAM role in the top-level management account for collecting PHD Events. 'None' if deploying into the top-level management account. + Type: String + Default: None + AWSHealthEventType: + Description: >- + Select the event type that you want AHA to report on. Refer to + https://docs.aws.amazon.com/health/latest/APIReference/API_EventType.html for more information on EventType. + Default: 'issue | accountNotification | scheduledChange' + AllowedValues: + - 'issue | accountNotification | scheduledChange' + - 'issue' + Type: String + S3Bucket: + Description: >- + Name of your S3 Bucket where the AHA Package .zip resides. Just the name + of the bucket (e.g. my-s3-bucket) + Type: String + S3Key: + Description: >- + Name of the .zip in your S3 Bucket. Just the name of the file (e.g. + aha-v1.0.zip) + Type: String + EventBusName: + Description: >- + This is to ingest alerts into AWS EventBridge. Enter the event bus name if + you wish to send the alerts to the AWS EventBridge. Note: By ingesting + these alerts to AWS EventBridge, you can integrate with 35 SaaS vendors + such as DataDog/NewRelic/PagerDuty. If you don't prefer to use EventBridge, leave the default (None). + Type: String + Default: None + SlackWebhookURL: + Description: >- + Enter the Slack Webhook URL. If you don't prefer to use Slack, leave the default (None). + Type: String + Default: None + MicrosoftTeamsWebhookURL: + Description: >- + Enter Microsoft Teams Webhook URL. If you don't prefer to use MS Teams, + leave the default (None). + Type: String + Default: None + AmazonChimeWebhookURL: + Description: >- + Enter the Chime Webhook URL, If you don't prefer to use Amazon Chime, + leave the default (None). + Type: String + Default: None + Regions: + Description: >- + By default, AHA reports events affecting all AWS regions. + If you want to report on certain regions you can enter up to 10 in a comma separated format. + Available Regions: us-east-1,us-east-2,us-west-1,us-west-2,af-south-1,ap-east-1,ap-south-1,ap-northeast-3, + ap-northeast-2,ap-southeast-1,ap-southeast-2,ap-northeast-1,ca-central-1,eu-central-1,eu-west-1,eu-west-2, + eu-south-1,eu-south-3,eu-north-1,me-south-1,sa-east-1,global + Default: all regions + AllowedPattern: ".+" + ConstraintDescription: No regions were entered, please read the documentation about selecting all regions or filtering on some. + Type: String + EventSearchBack: + Description: How far back to search for events in hours. Default is 1 hour + Default: '1' + Type: Number + FromEmail: + Description: Enter FROM Email Address + Type: String + Default: none@domain.com + AllowedPattern: ^([\w+-.%]+@[\w-.]+\.[A-Za-z]+)(, ?[\w+-.%]+@[\w-.]+\.[A-Za-z]+)*$ + ConstraintDescription: 'FromEmail is not a valid, please verify entry. If not sending to email, leave as the default, none@domain.com.' + ToEmail: + Description: >- + Enter email addresses separated by commas (for ex: abc@amazon.com, + bcd@amazon.com) + Type: String + Default: none@domain.com + AllowedPattern: ^([\w+-.%]+@[\w-.]+\.[A-Za-z]+)(, ?[\w+-.%]+@[\w-.]+\.[A-Za-z]+)*$ + ConstraintDescription: 'ToEmail is not a valid, please verify entry. If not sending to email, leave as the default, none@domain.com.' + Subject: + Description: Enter the subject of the email address + Type: String + Default: AWS Health Alert +Resources: + LambdaExecutionRole: + Type: 'AWS::IAM::Role' + Properties: + AssumeRolePolicyDocument: + Version: '2012-10-17' + Statement: + - Effect: Allow + Principal: + Service: + - lambda.amazonaws.com + Action: + - 'sts:AssumeRole' + Path: / + Policies: + - PolicyName: AHA-LambdaPolicy + PolicyDocument: + Version: '2012-10-17' + Statement: + - Effect: Allow + Action: + - logs:CreateLogGroup + - logs:CreateLogStream + - logs:PutLogEvents + Resource: !Sub 'arn:aws:logs:${AWS::Region}:${AWS::AccountId}:*' + - !If + - UsingSecrets + - Effect: Allow + Action: + - 'secretsmanager:GetResourcePolicy' + - 'secretsmanager:DescribeSecret' + - 'secretsmanager:ListSecretVersionIds' + - 'secretsmanager:GetSecretValue' + Resource: + - !If [UsingTeams, !Sub '${MicrosoftChannelSecret}', !Ref AWS::NoValue] + - !If [UsingSlack, !Sub '${SlackChannelSecret}', !Ref AWS::NoValue] + - !If [UsingEventBridge, !Sub '${EventBusNameSecret}', !Ref AWS::NoValue] + - !If [UsingChime, !Sub '${ChimeChannelSecret}', !Ref AWS::NoValue] + - !If [UsingCrossAccountRole, !Sub '${AssumeRoleSecret}', !Ref AWS::NoValue] + - !Ref 'AWS::NoValue' + - Effect: Allow + Action: + - health:DescribeAffectedAccountsForOrganization + - health:DescribeAffectedEntitiesForOrganization + - health:DescribeEventDetailsForOrganization + - health:DescribeEventsForOrganization + - health:DescribeEventDetails + - health:DescribeEvents + - health:DescribeEventTypes + - health:DescribeAffectedEntities + - organizations:ListAccounts + Resource: "*" + - Effect: Allow + Action: + - dynamodb:ListTables + Resource: !Sub 'arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:*' + - Effect: Allow + Action: + - ses:SendEmail + Resource: !Sub 'arn:aws:ses:${AWS::Region}:${AWS::AccountId}:*' + - Effect: Allow + Action: + - dynamodb:UpdateTimeToLive + - dynamodb:PutItem + - dynamodb:DeleteItem + - dynamodb:GetItem + - dynamodb:Scan + - dynamodb:Query + - dynamodb:UpdateItem + - dynamodb:UpdateTable + - dynamodb:GetRecords + Resource: !GetAtt DynamoDBTable.Arn + - Effect: Allow + Action: + - events:PutEvents + Resource: !Sub 'arn:aws:events:${AWS::Region}:${AWS::AccountId}:event-bus/${EventBusName}' + - !If + - UsingCrossAccountRole + - Effect: Allow + Action: + - sts:AssumeRole + Resource: !Ref ManagementAccountRoleArn + - !Ref 'AWS::NoValue' + DynamoDBTable: + Type: 'AWS::DynamoDB::Table' + Properties: + AttributeDefinitions: + - AttributeName: arn + AttributeType: S + KeySchema: + - AttributeName: arn + KeyType: HASH + ProvisionedThroughput: + ReadCapacityUnits: 5 + WriteCapacityUnits: 5 + TimeToLiveSpecification: + AttributeName: ttl + Enabled: TRUE + LambdaSchedule: + Type: 'AWS::Events::Rule' + Properties: + Description: Lambda trigger Event + ScheduleExpression: rate(1 minute) + State: ENABLED + Targets: + - Arn: !GetAtt LambdaFunction.Arn + Id: LambdaSchedule + LambdaSchedulePermission: + Type: 'AWS::Lambda::Permission' + Properties: + Action: 'lambda:InvokeFunction' + FunctionName: !GetAtt LambdaFunction.Arn + Principal: events.amazonaws.com + SourceArn: !GetAtt LambdaSchedule.Arn + MicrosoftChannelSecret: + Type: 'AWS::SecretsManager::Secret' + Condition: UsingTeams + Properties: + Name: MicrosoftChannelID + Description: Microsoft Channel ID Secret + SecretString: + Ref: MicrosoftTeamsWebhookURL + Tags: + - Key: HealthCheckMicrosoft + Value: ChannelID + SlackChannelSecret: + Type: 'AWS::SecretsManager::Secret' + Condition: UsingSlack + Properties: + Name: SlackChannelID + Description: Slack Channel ID Secret + SecretString: + Ref: SlackWebhookURL + Tags: + - Key: HealthCheckSlack + Value: ChannelID + EventBusNameSecret: + Type: 'AWS::SecretsManager::Secret' + Condition: UsingEventBridge + Properties: + Name: EventBusName + Description: EventBus Name Secret + SecretString: + Ref: EventBusName + Tags: + - Key: EventBusName + Value: ChannelID + ChimeChannelSecret: + Type: 'AWS::SecretsManager::Secret' + Condition: UsingChime + Properties: + Name: ChimeChannelID + Description: Chime Channel ID Secret + SecretString: + Ref: AmazonChimeWebhookURL + Tags: + - Key: HealthCheckChime + Value: ChannelID + AssumeRoleSecret: + Type: 'AWS::SecretsManager::Secret' + Condition: UsingCrossAccountRole + Properties: + Name: AssumeRoleArn + Description: Management account role for AHA to assume + SecretString: + Ref: ManagementAccountRoleArn + Tags: + - Key: AssumeRoleArn + Value: ChannelID + LambdaFunction: + Type: 'AWS::Lambda::Function' + Properties: + Description: Lambda function that runs AHA + Code: + S3Bucket: + Ref: S3Bucket + S3Key: + Ref: S3Key + Handler: handler.main + MemorySize: 128 + Timeout: 600 + Role: + 'Fn::Sub': '${LambdaExecutionRole.Arn}' + Runtime: python3.8 + Environment: + Variables: + REGIONS: + Ref: Regions + FROM_EMAIL: + Ref: FromEmail + TO_EMAIL: + Ref: ToEmail + EMAIL_SUBJECT: + Ref: Subject + DYNAMODB_TABLE: + Ref: DynamoDBTable + EVENT_SEARCH_BACK: + Ref: EventSearchBack + ORG_STATUS: + Ref: AWSOrganizationsEnabled + HEALTH_EVENT_TYPE: + Ref: AWSHealthEventType + MANAGEMENT_ROLE_ARN: + !If + - UsingCrossAccountRole + - !Ref ManagementAccountRoleArn + - "None" + \ No newline at end of file diff --git a/BETA-org-member-deployment/CODE_OF_CONDUCT.md b/BETA-org-member-deployment/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..5b627cf --- /dev/null +++ b/BETA-org-member-deployment/CODE_OF_CONDUCT.md @@ -0,0 +1,4 @@ +## Code of Conduct +This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). +For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact +opensource-codeofconduct@amazon.com with any additional questions or comments. diff --git a/BETA-org-member-deployment/CONTRIBUTING.md b/BETA-org-member-deployment/CONTRIBUTING.md new file mode 100644 index 0000000..c4b6a1c --- /dev/null +++ b/BETA-org-member-deployment/CONTRIBUTING.md @@ -0,0 +1,59 @@ +# Contributing Guidelines + +Thank you for your interest in contributing to our project. Whether it's a bug report, new feature, correction, or additional +documentation, we greatly value feedback and contributions from our community. + +Please read through this document before submitting any issues or pull requests to ensure we have all the necessary +information to effectively respond to your bug report or contribution. + + +## Reporting Bugs/Feature Requests + +We welcome you to use the GitHub issue tracker to report bugs or suggest features. + +When filing an issue, please check existing open, or recently closed, issues to make sure somebody else hasn't already +reported the issue. Please try to include as much information as you can. Details like these are incredibly useful: + +* A reproducible test case or series of steps +* The version of our code being used +* Any modifications you've made relevant to the bug +* Anything unusual about your environment or deployment + + +## Contributing via Pull Requests +Contributions via pull requests are much appreciated. Before sending us a pull request, please ensure that: + +1. You are working against the latest source on the *main* branch. +2. You check existing open, and recently merged, pull requests to make sure someone else hasn't addressed the problem already. +3. You open an issue to discuss any significant work - we would hate for your time to be wasted. + +To send us a pull request, please: + +1. Fork the repository. +2. Modify the source; please focus on the specific change you are contributing. If you also reformat all the code, it will be hard for us to focus on your change. +3. Ensure local tests pass. +4. Commit to your fork using clear commit messages. +5. Send us a pull request, answering any default questions in the pull request interface. +6. Pay attention to any automated CI failures reported in the pull request, and stay involved in the conversation. + +GitHub provides additional document on [forking a repository](https://help.github.com/articles/fork-a-repo/) and +[creating a pull request](https://help.github.com/articles/creating-a-pull-request/). + + +## Finding contributions to work on +Looking at the existing issues is a great way to find something to contribute on. As our projects, by default, use the default GitHub issue labels (enhancement/bug/duplicate/help wanted/invalid/question/wontfix), looking at any 'help wanted' issues is a great place to start. + + +## Code of Conduct +This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). +For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact +opensource-codeofconduct@amazon.com with any additional questions or comments. + + +## Security issue notifications +If you discover a potential security issue in this project we ask that you notify AWS/Amazon Security via our [vulnerability reporting page](http://aws.amazon.com/security/vulnerability-reporting/). Please do **not** create a public github issue. + + +## Licensing + +See the [LICENSE](LICENSE) file for our project's licensing. We will ask you to confirm the licensing of your contribution. diff --git a/BETA-org-member-deployment/LICENSE b/BETA-org-member-deployment/LICENSE new file mode 100644 index 0000000..1bb4f21 --- /dev/null +++ b/BETA-org-member-deployment/LICENSE @@ -0,0 +1,15 @@ +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/BETA-org-member-deployment/README.md b/BETA-org-member-deployment/README.md new file mode 100644 index 0000000..4a871a4 --- /dev/null +++ b/BETA-org-member-deployment/README.md @@ -0,0 +1,170 @@ + +# [IN BETA] Deploying AHA in a member account within an AWS Organization + +# Introduction +The #1 feature request from customers, is the ability to deploy the AHA resources (Lambda, DynamoDB, etc.) in a member account instead of the top-level management account. This process is currently **IN BETA** so we ask that you log any issues within Github issues. + +The steps below are relatively the same as a normal deployment, if you configured an Endpoint already you can skip to [**Deployment in AWS Organization Member Account**](#deployment-in-aws-organization-member-account) + +# Configuring an Endpoint - +AHA can send to multiple endpoints (webhook URLs, Email or EventBridge). To use any of these you'll need to set it up before-hand as some of these are done on 3rd party websites. We'll go over some of the common ones here. + +## Creating a Amazon Chime Webhook URL - +**You will need to have access to create a Amazon Chime room and manage webhooks.** + +1. Create a new [chat room](https://docs.aws.amazon.com/chime/latest/ug/chime-chat-room.html) for events (i.e. aws_events). +2. In the chat room created in step 1, **click** on the gear icon and **click** *manage webhooks and bots*. +3. **Click** *Add webhook*. +4. **Type** a name for the bot (e.g. AWS Health Bot) and **click** *Create*. +5. **Click** *Copy URL*, we will need it for the deployment. + +## Creating a Slack Webhook URL - +**You will need to have access to add a new channel and app to your Slack Workspace**. + +1. Create a new [channel](https://slack.com/help/articles/201402297-Create-a-channel) for events (i.e. aws_events) +2. In your browser go to: workspace-name.slack.com/apps where workspace-name is the name of your Slack Workspace. +3. In the search bar, search for: *Incoming Webhooks* and **click** on it. +4. **Click** on *Add to Slack*. +5. From the dropdown **click** on the channel your created in step 1 and **click** *Add Incoming Webhooks integration*. +6. From this page you can change the name of the webhook (i.e. AWS Bot), the icon/emoji to use, etc. +7. For the deployment we will need the *Webhook URL*. + +## Creating a Microsoft Teams Webhook URL - +**You will need to have access to add a new channel and app to your Microsoft Teams channel**. + +1. Create a new [channel](https://docs.microsoft.com/en-us/microsoftteams/get-started-with-teams-create-your-first-teams-and-channels) for events (i.e. aws_events) +2. Within your Microsoft Team go to *Apps* +3. In the search bar, search for: *Incoming Webhook* and **click** on it. +4. **Click** on *Add to team*. +5. **Type** in the name of your on the channel your created in step 1 and **click** *Set up a connector*. +6. From this page you can change the name of the webhook (i.e. AWS Bot), the icon/emoji to use, etc. **Click** *Create* when done. +7. For the deployment we will need the webhook *URL* that is presented. + +## Configuring an Email - + +1. You'll be able to send email alerts to one or many addresses. However, you must first [verify](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/verify-email-addresses-procedure.html) the email(s) in the Simple Email Service (SES) console. +2. AHA utilizes Amazon SES so all you need is to enter in a To: address and a From: address. +3. You *may* have to allow a rule in your environment so that the emails don't get labeled as SPAM. This will be something you have to congfigure on your own. + +## Creating a Amazon EventBridge Ingestion ARN - + +1. In the AWS Console, search for **Amazon EventBridge**. +2. On the left hand side, **click** *Event buses*. +3. Under *Custom event* bus **click** *Create event bus* +4. Give your Event bus a name and **click** *Create*. +5. For the deployment we will need the *Name* of the Event bus **(not the ARN)**. + +# Setup - +There are 2 available ways to deploy AHA, both are done via the same CloudFormation template to make deployment as easy as possible. + +The 2 deployment methods for AHA are: + +1. [**AHA for users NOT using AWS Organizations**](#aha-without-aws-organizations): Users NOT using AWS Organizations will be able to get Service Health Dashboard (SHD) events ONLY. +2. [**AHA for users who ARE using AWS Organizations**](#aha-with-organizations): Users who ARE using AWS Organizations will be able to get Service Health Dashboard (SHD) events as well as aggregated Personal Health Dashboard (PHD) events for all accounts in their AWS Organization. + +## AHA Without AWS Organizations + +### Prerequisites + +1. Have at least 1 [endpoint](#configuring-an-endpoint) configured (you can have multiple) +2. Have access to deploy Cloudformation Templates with the following resources: AWS IAM policies, Amazon DynamoDB Tables, AWS Lambda, Amazon EventBridge and AWS Secrets Manager. + +### Deployment + +1. Clone the AHA package that from this repository. If you're not familiar with the process, [here](https://git-scm.com/docs/git-clone) is some documentation. The URL to clone is in the upper right-hand corner labeled `Clone uri` +2. In the root of this package you'll have two files; `handler.py` and `messagegenerator.py`. Use your tool of choice to zip them both up and name them with a unique name (e.g. aha-v1.8.zip). **Note: Putting the version number in the name will make upgrading AHA seamless.** +3. Upload the .zip you created in Step 2 to an S3 in the same region you plan to deploy this in. +4. In your AWS console go to *CloudFormation*. +5. In the *CloudFormation* console **click** *Create stack > With new resources (standard)*. +6. Under *Template Source* **click** *Upload a template file* and **click** *Choose file* and select `CFN_AHA.yml` **Click** *Next*. +7. -In *Stack name* type a stack name (i.e. AHA-Deployment). +-In *AWSOrganizationsEnabled* leave it set to default which is `No`. If you do have AWS Organizations enabled and you want to aggregate across all your accounts, you should be following the step for [AHA for users who ARE using AWS Organizations](#aha-with-aws-organizations) +-In *AWSHealthEventType* select whether you want to receive *all* event types or *only* issues. +-In *S3Bucket* type ***just*** the bucket name of the S3 bucket used in step 3 (e.g. my-aha-bucket). +-In *S3Key* type ***just*** the name of the .zip file you created in Step 2 (e.g. aha-v1.8.zip). +-In the *Communications Channels* section enter the URLs, Emails and/or ARN of the endpoints you configured previously. +-In the *Email Setup* section enter the From and To Email addresses as well as the Email subject. If you aren't configuring email, just leave it as is. +-In *EventSearchBack* enter in the amount of hours you want to search back for events. Default is 1 hour. +-In *Regions* enter in the regions you want to search for events in. Default is all regions. You can filter for up to 10, comma separated (e.g. us-east-1, us-east-2). +8. Scroll to the bottom and **click** *Next*. +9. Scroll to the bottom and **click** *Next* again. +10. Scroll to the bottom and **click** the *checkbox* and **click** *Create stack*. +11. Wait until *Status* changes to *CREATE_COMPLETE* (roughly 2-4 minutes). + +## AHA With AWS Organizations + +### Prerequisites + +1. [Enable Health Organizational View](https://docs.aws.amazon.com/health/latest/ug/enable-organizational-view-in-health-console.html) from the console, so that you can aggregate all Personal Health Dashboard (PHD) events for all accounts in your AWS Organization. +2. Have at least 1 [endpoint](#configuring-an-endpoint) configured (you can have multiple) +3. Have access to deploy Cloudformation Templates with the following resources: AWS IAM policies, Amazon DynamoDB Tables, AWS Lambda, Amazon EventBridge and AWS Secrets Manager. + +### Deployment in Top Level Management Account + +1. Clone the AHA package that from this repository. If you're not familiar with the process, [here](https://git-scm.com/docs/git-clone) is some documentation. The URL to clone is in the upper right-hand corner labeled `Clone uri` +2. In the root of this package you'll have two files; `handler.py` and `messagegenerator.py`. Use your tool of choice to zip them both up and name them with a unique name (e.g. aha-v1.8.zip). **Note: Putting the version number in the name will make upgrading AHA seamless.** +3. Upload the .zip you created in Step 2 to an S3 in the same region you plan to deploy this in. +4. In your AWS console go to *CloudFormation*. +5. In the *CloudFormation* console **click** *Create stack > With new resources (standard)*. +6. Under *Template Source* **click** *Upload a template file* and **click** *Choose file* and select `CFN_AHA.yml` **Click** *Next*. +7. -In *Stack name* type a stack name (i.e. AHA-Deployment). +-In *AWSOrganizationsEnabled* change the dropdown to `Yes`. If you do NOT have AWS Organizations enabled you should be following the steps for [AHA for users who are NOT using AWS Organizations](#aha-without-aws-organizations) +-In *AWSHealthEventType* select whether you want to receive *all* event types or *only* issues. +-In *S3Bucket* type ***just*** the bucket name of the S3 bucket used in step 3 (e.g. my-aha-bucket). +-In *S3Key* type ***just*** the name of the .zip file you created in Step 2 (e.g. aha-v1.8.zip). +-In the *Communications Channels* section enter the URLs, Emails and/or ARN of the endpoints you configured previously. +-In the *Email Setup* section enter the From and To Email addresses as well as the Email subject. If you aren't configuring email, just leave it as is. +-In *EventSearchBack* enter in the amount of hours you want to search back for events. Default is 1 hour. +-In *Regions* enter in the regions you want to search for events in. Default is all regions. You can filter for up to 10, comma separated with (e.g. us-east-1, us-east-2). +8. Scroll to the bottom and **click** *Next*. +9. Scroll to the bottom and **click** *Next* again. +10. Scroll to the bottom and **click** the *checkbox* and **click** *Create stack*. +11. Wait until *Status* changes to *CREATE_COMPLETE* (roughly 2-4 minutes). + +### Deployment in AWS Organization Member Account + +1. Clone the AHA package from the BETA-member-deployment folder. If you're not familiar with the process, [here](https://git-scm.com/docs/git-clone) is some documentation. The URL to clone is in the upper right-hand corner labeled `Clone uri` +2. In your top-level management account AWS console go to *CloudFormation* +3. In the *CloudFormation* console **click** *Create stack > With new resources (standard)*. +4. Under *Template Source* **click** *Upload a template file* and **click** *Choose file* and select `01_CFN_MGMT_ROLE.yml` **Click** *Next*. +5. -In *Stack name* type a stack name (i.e. aha-assume-role). +-In *OrgMemberAccountId* put in the account id of the member account you plan to run AHA in (e.g. 000123456789). +6. Scroll to the bottom and **click** *Next*. +7. Scroll to the bottom and **click** *Next* again. +8. Scroll to the bottom and **click** the *checkbox* and **click** *Create stack*. +9. Wait until *Status* changes to *CREATE_COMPLETE* (roughly 1-2 minutes). This will create an IAM role with the necessary AWS Organizations and AWS Health API permissions for the member account to assume. +10. In the *Outputs* tab, there will be a value for *AWSHealthAwareRoleForPHDEventsArn* (e.g. arn:aws:iam::000123456789:role/aha-org-role-AWSHealthAwareRoleForPHDEvents-ABCSDE12201), copy that down as you will need it for step 16. +11. Back In the root of the package you downloaded/cloned you'll have two files; `handler.py` and `messagegenerator.py`. Use your tool of choice to zip them both up and name them with a unique name (e.g. aha-v1.8.zip). **Note: Putting the version number in the name will make upgrading AHA seamless.** +12. Upload the .zip you created in Step 11 to an S3 in the same region you plan to deploy this in. +13. Login to the member account you plan to deploy this in and in your AWS console go to *CloudFormation*. +14. In the *CloudFormation* console **click** *Create stack > With new resources (standard)*. +15. Under *Template Source* **click** *Upload a template file* and **click** *Choose file* and select `02_CFN_DEPLOY_AHA.yml` **Click** *Next*. +16. -In *Stack name* type a stack name (i.e. AHA-Deployment). +-In *AWSOrganizationsEnabled* change the dropdown to `Yes`. If you do NOT have AWS Organizations enabled you should be following the steps for [AHA for users who are NOT using AWS Organizations](#aha-without-aws-organizations) +-In *AWSHealthEventType* select whether you want to receive *all* event types or *only* issues. +-In *S3Bucket* type ***just*** the bucket name of the S3 bucket used in step 12 (e.g. my-aha-bucket). +-In *S3Key* type ***just*** the name of the .zip file you created in Step 11 (e.g. aha-v1.8.zip). +-In the *Communications Channels* section enter the URLs, Emails and/or ARN of the endpoints you configured previously. +-In the *Email Setup* section enter the From and To Email addresses as well as the Email subject. If you aren't configuring email, just leave it as is. +-In *EventSearchBack* enter in the amount of hours you want to search back for events. Default is 1 hour. +-In *Regions* enter in the regions you want to search for events in. Default is all regions. You can filter for up to 10, comma separated with (e.g. us-east-1, us-east-2). +-In *ManagementAccountRoleArn* enter in the full IAM arn from step 10 (e.g. arn:aws:iam::000123456789:role/aha-org-role-AWSHealthAwareRoleForPHDEvents-ABCSDE12201) +17. Scroll to the bottom and **click** *Next*. +18. Scroll to the bottom and **click** *Next* again. +19. Scroll to the bottom and **click** the *checkbox* and **click** *Create stack*. +12. Wait until *Status* changes to *CREATE_COMPLETE* (roughly 2-4 minutes). + +# Updating +**Until this project is migrated to the AWS Serverless Application Model (SAM), updates will have to be done as described below:** +1. Download the updated CloudFormation Template .yml file and 2 `.py` files. +2. Zip up the 2 `.py` files and name the .zip with a different version number than before (e.g. if the .zip you originally uploaded is aha-v1.8.zip the new one should be aha-v1.9.zip) +3. In the AWS CloudFormation console **click** on the name of your stack, then **click** *Update*. +4. In the *Prepare template* section **click** *Replace current template*, **click** *Upload a template file*, **click** *Choose file*, select the newer `CFN_AHA.yml` file you downloaded and finally **click** *Next*. +5. In the *S3Key* text box change the version number in the name of the .zip to match name of the .zip you uploaded in Step 2 (The name of the .zip has to be different for CloudFormation to recognize a change). **Click** *Next*. +6. At the next screen **click** *Next* and finally **click** *Update stack*. This will now upgrade your environment to the latest version you downloaded. + +**If for some reason, you still have issues after updating, you can easily just delete the stack and redeploy. The infrastructure can be destroyed and rebuilt within minutes through CloudFormation.** + +# Troubleshooting +* If for whatever reason you need to update the Webhook URL; just update the CloudFormation Template with the new Webhook URL. +* If you are expecting an event and it did not show up it may be an oddly formed event. Take a look at *CloudWatch > Log groups* and search for the name of your Cloudformation stack and Lambda function. See what the error is and reach out to us [email](mailto:aha-builders@amazon.com) for help. diff --git a/BETA-org-member-deployment/handler.py b/BETA-org-member-deployment/handler.py new file mode 100644 index 0000000..cc20deb --- /dev/null +++ b/BETA-org-member-deployment/handler.py @@ -0,0 +1,763 @@ +import json +import boto3 +import os +import re +import time +import decimal +import uuid +import socket +import configparser +from dateutil import parser +from datetime import datetime, timedelta +from urllib.parse import urlencode +from urllib.request import Request, urlopen, URLError, HTTPError +from botocore.config import Config +from botocore.exceptions import ClientError +from boto3.dynamodb.conditions import Key, Attr +from messagegenerator import get_message_for_slack, get_org_message_for_slack, get_message_for_chime, \ + get_org_message_for_chime, \ + get_message_for_teams, get_org_message_for_teams, get_message_for_email, get_org_message_for_email, \ + get_org_message_for_eventbridge, get_message_for_eventbridge + +# query active health API endpoint +health_dns = socket.gethostbyname_ex('global.health.amazonaws.com') +(current_endpoint, global_endpoint, ip_endpoint) = health_dns +health_active_list = current_endpoint.split('.') +health_active_region = health_active_list[1] +print("current health region: ", health_active_region) +management_role_arn = os.environ["MANAGEMENT_ROLE_ARN"] + +# create a boto3 health client w/ backoff/retry +config = Config( + region_name=health_active_region, + retries=dict( + max_attempts=10 # org view apis have a lower tps than the single + # account apis so we need to use larger + # backoff/retry values than than the boto defaults + ) +) + +# Send alert module +def send_alert(event_details, event_type): + slack_url = get_secrets()["slack"] + teams_url = get_secrets()["teams"] + chime_url = get_secrets()["chime"] + SENDER = os.environ['FROM_EMAIL'] + RECIPIENT = os.environ['TO_EMAIL'] + event_bus_name = get_secrets()["eventbusname"] + + if "None" not in event_bus_name: + try: + print("Sending the alert to Event Bridge") + send_to_eventbridge(get_message_for_eventbridge(event_details, event_type), event_type, event_bus_name) + except HTTPError as e: + print("Got an error while sending message to EventBridge: ", e.code, e.reason) + except URLError as e: + print("Server connection failed: ", e.reason) + pass + if "hooks.slack.com/services" in slack_url: + try: + print("Sending the alert to Slack Channel") + send_to_slack(get_message_for_slack(event_details, event_type), slack_url) + except HTTPError as e: + print("Got an error while sending message to Slack: ", e.code, e.reason) + except URLError as e: + print("Server connection failed: ", e.reason) + pass + if "office.com/webhook" in teams_url: + try: + print("Sending the alert to Teams") + send_to_teams(get_message_for_teams(event_details, event_type), teams_url) + except HTTPError as e: + print("Got an error while sending message to Teams: ", e.code, e.reason) + except URLError as e: + print("Server connection failed: ", e.reason) + pass + # validate sender and recipient's email addresses + if "none@domain.com" not in SENDER and RECIPIENT: + try: + print("Sending the alert to the emails") + send_email(event_details, event_type) + except HTTPError as e: + print("Got an error while sending message to Email: ", e.code, e.reason) + except URLError as e: + print("Server connection failed: ", e.reason) + pass + if "hooks.chime.aws/incomingwebhooks" in chime_url: + try: + print("Sending the alert to Chime channel") + send_to_chime(get_message_for_chime(event_details, event_type), chime_url) + except HTTPError as e: + print("Got an error while sending message to Chime: ", e.code, e.reason) + except URLError as e: + print("Server connection failed: ", e.reason) + pass + +def send_org_alert(event_details, affected_org_accounts, affected_org_entities, event_type): + slack_url = get_secrets()["slack"] + teams_url = get_secrets()["teams"] + chime_url = get_secrets()["chime"] + SENDER = os.environ['FROM_EMAIL'] + RECIPIENT = os.environ['TO_EMAIL'] + event_bus_name = get_secrets()["eventbusname"] + + if "None" not in event_bus_name: + try: + print("Sending the org alert to Event Bridge") + send_to_eventbridge( + get_org_message_for_eventbridge(event_details, event_type, affected_org_accounts, + affected_org_entities), + event_type, event_bus_name) + except HTTPError as e: + print("Got an error while sending message to EventBridge: ", e.code, e.reason) + except URLError as e: + print("Server connection failed: ", e.reason) + pass + if "hooks.slack.com/services" in slack_url: + try: + print("Sending the alert to Slack Channel") + send_to_slack( + get_org_message_for_slack(event_details, event_type, affected_org_accounts, affected_org_entities), + slack_url) + except HTTPError as e: + print("Got an error while sending message to Slack: ", e.code, e.reason) + except URLError as e: + print("Server connection failed: ", e.reason) + pass + if "office.com/webhook" in teams_url: + try: + print("Sending the alert to Teams") + send_to_teams( + get_org_message_for_teams(event_details, event_type, affected_org_accounts, affected_org_entities), + teams_url) + except HTTPError as e: + print("Got an error while sending message to Teams: ", e.code, e.reason) + except URLError as e: + print("Server connection failed: ", e.reason) + pass + # validate sender and recipient's email addresses + if "none@domain.com" not in SENDER and RECIPIENT: + try: + print("Sending the alert to the emails") + send_org_email(event_details, event_type, affected_org_accounts, affected_org_entities) + except HTTPError as e: + print("Got an error while sending message to Email: ", e.code, e.reason) + except URLError as e: + print("Server connection failed: ", e.reason) + pass + if "hooks.chime.aws/incomingwebhooks" in chime_url: + try: + print("Sending the alert to Chime channel") + send_to_chime( + get_org_message_for_chime(event_details, event_type, affected_org_accounts, affected_org_entities), + chime_url) + except HTTPError as e: + print("Got an error while sending message to Chime: ", e.code, e.reason) + except URLError as e: + print("Server connection failed: ", e.reason) + pass + + +def send_to_slack(message, webhookurl): + slack_message = message + req = Request(webhookurl, data=json.dumps(slack_message).encode("utf-8"), + headers={'content-type': 'application/json'}) + try: + response = urlopen(req) + response.read() + except HTTPError as e: + print("Request failed : ", e.code, e.reason) + except URLError as e: + print("Server connection failed: ", e.reason, e.reason) + + +def send_to_chime(message, webhookurl): + chime_message = {'Content': message} + req = Request(webhookurl, data=json.dumps(chime_message).encode("utf-8"), + headers={"content-Type": "application/json"}) + try: + response = urlopen(req) + response.read() + except HTTPError as e: + print("Request failed : ", e.code, e.reason) + except URLError as e: + print("Server connection failed: ", e.reason, e.reason) + + +def send_to_teams(message, webhookurl): + teams_message = message + req = Request(webhookurl, data=json.dumps(teams_message).encode("utf-8"), + headers={"content-type": "application/json"}) + try: + response = urlopen(req) + response.read() + except HTTPError as e: + print("Request failed : ", e.code, e.reason) + except URLError as e: + print("Server connection failed: ", e.reason, e.reason) + + +def send_email(event_details, eventType): + SENDER = os.environ['FROM_EMAIL'] + RECIPIENT = os.environ['TO_EMAIL'].split(",") + #AWS_REGIONS = "us-east-1" + AWS_REGION = os.environ['AWS_REGION'] + SUBJECT = os.environ['EMAIL_SUBJECT'] + BODY_HTML = get_message_for_email(event_details, eventType) + client = boto3.client('ses', region_name=AWS_REGION) + response = client.send_email( + Source=SENDER, + Destination={ + 'ToAddresses': RECIPIENT + }, + Message={ + 'Body': { + 'Html': { + 'Data': BODY_HTML + }, + }, + 'Subject': { + 'Charset': 'UTF-8', + 'Data': SUBJECT, + }, + }, + ) + + +def send_org_email(event_details, eventType, affected_org_accounts, affected_org_entities): + SENDER = os.environ['FROM_EMAIL'] + RECIPIENT = os.environ['TO_EMAIL'].split(",") + #AWS_REGION = "us-east-1" + AWS_REGION = os.environ['AWS_REGION'] + SUBJECT = os.environ['EMAIL_SUBJECT'] + BODY_HTML = get_org_message_for_email(event_details, eventType, affected_org_accounts, affected_org_entities) + client = boto3.client('ses', region_name=AWS_REGION) + response = client.send_email( + Source=SENDER, + Destination={ + 'ToAddresses': RECIPIENT + }, + Message={ + 'Body': { + 'Html': { + 'Data': BODY_HTML + }, + }, + 'Subject': { + 'Charset': 'UTF-8', + 'Data': SUBJECT, + }, + }, + ) + + +# organization view affected accounts +def get_health_org_accounts(health_client, event, event_arn): + affected_org_accounts = [] + event_accounts_paginator = health_client.get_paginator('describe_affected_accounts_for_organization') + event_accounts_page_iterator = event_accounts_paginator.paginate( + eventArn=event_arn + ) + for event_accounts_page in event_accounts_page_iterator: + json_event_accounts = json.dumps(event_accounts_page, default=myconverter) + parsed_event_accounts = json.loads(json_event_accounts) + affected_org_accounts = affected_org_accounts + (parsed_event_accounts['affectedAccounts']) + return affected_org_accounts + + +# organization view affected entities (aka resources) +def get_health_org_entities(health_client, event, event_arn, affected_org_accounts): + if len(affected_org_accounts) >= 1: + affected_org_accounts = affected_org_accounts[0] + event_entities_paginator = health_client.get_paginator('describe_affected_entities_for_organization') + event_entities_page_iterator = event_entities_paginator.paginate( + organizationEntityFilters=[ + { + 'awsAccountId': affected_org_accounts, + 'eventArn': event_arn + } + ] + ) + affected_org_entities = [] + for event_entities_page in event_entities_page_iterator: + json_event_entities = json.dumps(event_entities_page, default=myconverter) + parsed_event_entities = json.loads(json_event_entities) + for entity in parsed_event_entities['entities']: + affected_org_entities.append(entity['entityValue']) + return affected_org_entities + else: + affected_entities = "" + return affected_entities + + +# For Customers using AWS Organizations +def update_org_ddb(event_arn, str_update, status_code, event_details, affected_org_accounts, affected_org_entities): + # open dynamoDB + dynamodb = boto3.resource("dynamodb") + ddb_table = os.environ['DYNAMODB_TABLE'] + aha_ddb_table = dynamodb.Table(ddb_table) + event_latestDescription = event_details['successfulSet'][0]['eventDescription']['latestDescription'] + # set time parameters + delta_hours = os.environ['EVENT_SEARCH_BACK'] + delta_hours = int(delta_hours) + delta_hours_sec = delta_hours * 3600 + + # formatting time in seconds + srt_ddb_format_full = "%Y-%m-%d %H:%M:%S" + str_ddb_format_sec = '%s' + sec_now = datetime.strftime(datetime.now(), str_ddb_format_sec) + + # check if event arn already exists + try: + response = aha_ddb_table.get_item( + Key={ + 'arn': event_arn + } + ) + except ClientError as e: + print(e.response['Error']['Message']) + else: + is_item_response = response.get('Item') + if is_item_response == None: + print(datetime.now().strftime(srt_ddb_format_full) + ": record not found") + # write to dynamodb + response = aha_ddb_table.put_item( + Item={ + 'arn': event_arn, + 'lastUpdatedTime': str_update, + 'added': sec_now, + 'ttl': int(sec_now) + delta_hours_sec + 86400, + 'statusCode': status_code, + 'affectedAccountIDs': affected_org_accounts, + 'latestDescription': event_latestDescription + # Cleanup: DynamoDB entry deleted 24 hours after last update + } + ) + # send to configured endpoints + if status_code != "closed": + send_org_alert(event_details, affected_org_accounts, affected_org_entities, event_type="create") + else: + send_org_alert(event_details, affected_org_accounts, affected_org_entities, event_type="resolve") + + else: + item = response['Item'] + if item['lastUpdatedTime'] != str_update and (item['statusCode'] != status_code or + item['latestDescription'] != event_latestDescription or + item['affectedAccountIDs'] != affected_org_accounts): + print(datetime.now().strftime(srt_ddb_format_full) + ": last Update is different") + # write to dynamodb + response = aha_ddb_table.put_item( + Item={ + 'arn': event_arn, + 'lastUpdatedTime': str_update, + 'added': sec_now, + 'ttl': int(sec_now) + delta_hours_sec + 86400, + 'statusCode': status_code, + 'affectedAccountIDs': affected_org_accounts, + 'latestDescription': event_latestDescription + # Cleanup: DynamoDB entry deleted 24 hours after last update + } + ) + # send to configured endpoints + if status_code != "closed": + send_org_alert(event_details, affected_org_accounts, affected_org_entities, event_type="create") + else: + send_org_alert(event_details, affected_org_accounts, affected_org_entities, event_type="resolve") + else: + print("No new updates found, checking again in 1 minute.") + + +# For Customers not using AWS Organizations +def update_ddb(event_arn, str_update, status_code, event_details): + # open dynamoDB + dynamodb = boto3.resource("dynamodb") + ddb_table = os.environ['DYNAMODB_TABLE'] + aha_ddb_table = dynamodb.Table(ddb_table) + + # set time parameters + delta_hours = os.environ['EVENT_SEARCH_BACK'] + delta_hours = int(delta_hours) + delta_hours_sec = delta_hours * 3600 + + # formatting time in seconds + srt_ddb_format_full = "%Y-%m-%d %H:%M:%S" + str_ddb_format_sec = '%s' + sec_now = datetime.strftime(datetime.now(), str_ddb_format_sec) + + # check if event arn already exists + try: + response = aha_ddb_table.get_item( + Key={ + 'arn': event_arn + } + ) + except ClientError as e: + print(e.response['Error']['Message']) + else: + is_item_response = response.get('Item') + if is_item_response == None: + print(datetime.now().strftime(srt_ddb_format_full) + ": record not found") + # write to dynamodb + response = aha_ddb_table.put_item( + Item={ + 'arn': event_arn, + 'lastUpdatedTime': str_update, + 'added': sec_now, + 'ttl': int(sec_now) + delta_hours_sec + 86400 + # Cleanup: DynamoDB entry deleted 24 hours after last update + } + ) + # send to configured endpoints + if status_code != "closed": + send_alert(event_details, event_type="create") + else: + send_alert(event_details, event_type="resolve") + + else: + item = response['Item'] + if item['lastUpdatedTime'] != str_update: + print(datetime.now().strftime(srt_ddb_format_full) + ": last Update is different") + # write to dynamodb + response = aha_ddb_table.put_item( + Item={ + 'arn': event_arn, + 'lastUpdatedTime': str_update, + 'added': sec_now, + 'ttl': int(sec_now) + delta_hours_sec + 86400 + # Cleanup: DynamoDB entry deleted 24 hours after last update + } + ) + # send to configured endpoints + if status_code != "closed": + send_alert(event_details, event_type="create") + else: + send_alert(event_details, event_type="resolve") + else: + print("No new updates found, checking again in 1 minute.") + +def get_secrets(): + secret_teams_name = "MicrosoftChannelID" + secret_slack_name = "SlackChannelID" + secret_chime_name = "ChimeChannelID" + region_name = os.environ['AWS_REGION'] + get_secret_value_response_assumerole = "" + get_secret_value_response_eventbus = "" + get_secret_value_response_chime = "" + get_secret_value_response_teams = "" + get_secret_value_response_slack = "" + event_bus_name = "EventBusName" + secret_assumerole_name = "AssumeRoleArn" + + # create a Secrets Manager client + session = boto3.session.Session() + client = session.client( + service_name='secretsmanager', + region_name=region_name + ) + # Iteration through the configured AWS Secrets + try: + get_secret_value_response_teams = client.get_secret_value( + SecretId=secret_teams_name + ) + except ClientError as e: + if e.response['Error']['Code'] == 'AccessDeniedException': + print("No AWS Secret configured for Teams, skipping") + teams_channel_id = "None" + else: + print("There was an error with the Teams secret: ",e.response) + teams_channel_id = "None" + finally: + if 'SecretString' in get_secret_value_response_teams: + teams_channel_id = get_secret_value_response_teams['SecretString'] + else: + teams_channel_id = "None" + try: + get_secret_value_response_slack = client.get_secret_value( + SecretId=secret_slack_name + ) + except ClientError as e: + if e.response['Error']['Code'] == 'AccessDeniedException': + print("No AWS Secret configured for Slack, skipping") + slack_channel_id = "None" + else: + print("There was an error with the Slack secret: ",e.response) + slack_channel_id = "None" + finally: + if 'SecretString' in get_secret_value_response_slack: + slack_channel_id = get_secret_value_response_slack['SecretString'] + else: + slack_channel_id = "None" + try: + get_secret_value_response_chime = client.get_secret_value( + SecretId=secret_chime_name + ) + except ClientError as e: + if e.response['Error']['Code'] == 'AccessDeniedException': + print("No AWS Secret configured for Chime, skipping") + chime_channel_id = "None" + else: + print("There was an error with the Chime secret: ",e.response) + chime_channel_id = "None" + finally: + if 'SecretString' in get_secret_value_response_chime: + chime_channel_id = get_secret_value_response_chime['SecretString'] + else: + chime_channel_id = "None" + + try: + get_secret_value_response_assumerole = client.get_secret_value( + SecretId=secret_assumerole_name + ) + except ClientError as e: + if e.response['Error']['Code'] == 'AccessDeniedException': + print("No AWS Secret configured for Assume Role, skipping") + assumerole_channel_id = "None" + else: + print("There was an error with the Assume Role secret: ",e.response) + assumerole_channel_id = "None" + finally: + if 'SecretString' in get_secret_value_response_assumerole: + assumerole_channel_id = get_secret_value_response_assumerole['SecretString'] + else: + assumerole_channel_id = "None" + + try: + get_secret_value_response_eventbus = client.get_secret_value( + SecretId=event_bus_name + ) + except ClientError as e: + if e.response['Error']['Code'] == 'AccessDeniedException': + print("No AWS Secret configured for EventBridge, skipping") + eventbus_channel_id = "None" + else: + print("There was an error with the EventBridge secret: ",e.response) + eventbus_channel_id = "None" + finally: + if 'SecretString' in get_secret_value_response_eventbus: + eventbus_channel_id = get_secret_value_response_eventbus['SecretString'] + else: + eventbus_channel_id = "None" + secrets = { + "teams": teams_channel_id, + "slack": slack_channel_id, + "chime": chime_channel_id, + "eventbusname": eventbus_channel_id, + "ahaassumerole": assumerole_channel_id, + } + # uncomment below to verify secrets values + #print("Secrets: ",secrets) + return secrets + +def describe_events(health_client): + # set hours to search back in time for events + delta_hours = os.environ['EVENT_SEARCH_BACK'] + health_event_type = os.environ['HEALTH_EVENT_TYPE'] + delta_hours = int(delta_hours) + time_delta = (datetime.now() - timedelta(hours=delta_hours)) + print("Searching for events and updates made after: ", time_delta) + dict_regions = os.environ['REGIONS'] + + str_filter = { + 'lastUpdatedTimes': [ + { + 'from': time_delta + } + ] + } + + if health_event_type == "issue": + event_type_filter = {'eventTypeCategories': ["issue"]} + print("AHA will be monitoring events with event type categories as 'issue' only!") + str_filter.update(event_type_filter) + + if dict_regions != "all regions": + dict_regions = [region.strip() for region in dict_regions.split(',')] + print("AHA will monitor for events only in the selected regions: ", dict_regions) + region_filter = {'regions': dict_regions} + str_filter.update(region_filter) + + event_paginator = health_client.get_paginator('describe_events') + event_page_iterator = event_paginator.paginate(filter=str_filter) + for response in event_page_iterator: + events = response.get('events', []) + return events + + +def describe_org_events(health_client): + # set hours to search back in time for events + delta_hours = os.environ['EVENT_SEARCH_BACK'] + health_event_type = os.environ['HEALTH_EVENT_TYPE'] + dict_regions = os.environ['REGIONS'] + delta_hours = int(delta_hours) + time_delta = (datetime.now() - timedelta(hours=delta_hours)) + print("Searching for events and updates made after: ", time_delta) + + str_filter = { + 'lastUpdatedTime': { + 'from': time_delta + } + } + + if health_event_type == "issue": + event_type_filter = {'eventTypeCategories': ["issue"]} + print("AHA will be monitoring events with event type categories as 'issue' only!") + str_filter.update(event_type_filter) + + if dict_regions != "all regions": + dict_regions = [region.strip() for region in dict_regions.split(',')] + print("AHA will monitor for events only in the selected regions: ", dict_regions) + region_filter = {'regions': dict_regions} + str_filter.update(region_filter) + + org_event_paginator = health_client.get_paginator('describe_events_for_organization') + org_event_page_iterator = org_event_paginator.paginate(filter=str_filter) + for response in org_event_page_iterator: + events = response.get('events', []) + return events + +def myconverter(json_object): + if isinstance(json_object, datetime): + return json_object.__str__() + + +def describe_event_details(health_client, event_arn): + response = health_client.describe_event_details( + eventArns=[event_arn], + ) + return response + +def describe_org_event_details(health_client, event_arn, affected_org_accounts): + if len(affected_org_accounts) >= 1: + affected_account_ids = affected_org_accounts[0] + response = health_client.describe_event_details_for_organization( + organizationEventDetailFilters=[ + { + 'awsAccountId': affected_account_ids, + 'eventArn': event_arn + } + ] + ) + return response + else: + response = describe_event_details(health_client, event_arn) + return response + + +def send_to_eventbridge(message, event_type, event_bus): + print("Sending response to Eventbridge - event_type, event_bus", event_type, event_bus) + client = boto3.client('events') + response = client.put_events(Entries=[ + {'Source': 'aha', 'DetailType': event_type, 'Detail': '{ "mydata": ' + json.dumps(message) + ' }', + 'EventBusName': event_bus}, ]) + print("Response is:", response) + +def get_sts_token(): + assumeRoleArn = get_secrets()["ahaassumerole"] + health_client = None + + if "arn:aws:iam::" in assumeRoleArn: + ACCESS_KEY = [] + SECRET_KEY = [] + SESSION_TOKEN = [] + + sts_connection = boto3.client('sts') + + ct = datetime.now() + role_session_name = "cross_acct_aha_session" + + acct_b = sts_connection.assume_role( + RoleArn=assumeRoleArn, + RoleSessionName=role_session_name, + DurationSeconds=900, + ) + + ACCESS_KEY = acct_b['Credentials']['AccessKeyId'] + SECRET_KEY = acct_b['Credentials']['SecretAccessKey'] + SESSION_TOKEN = acct_b['Credentials']['SessionToken'] + + # create service client using the assumed role credentials, e.g. S3 + health_client = boto3.client( + 'health', + config=config, + aws_access_key_id=ACCESS_KEY, + aws_secret_access_key=SECRET_KEY, + aws_session_token=SESSION_TOKEN, + ) + print("Running in member account deployment mode") + else: + health_client = boto3.client('health', config=config) + print("Running in management account demployment mode") + + return health_client + +def main(event, context): + print("THANK YOU FOR CHOOSING AWS HEALTH AWARE!") + health_client = get_sts_token() + org_status = os.environ['ORG_STATUS'] + str_ddb_format_sec = '%s' + + # check for AWS Organizations Status + if org_status == "No": + print("AWS Organizations is not enabled. Only Service Health Dashboard messages will be alerted.") + aws_events = describe_events(health_client) + aws_events = json.dumps(aws_events, default=myconverter) + aws_events = json.loads(aws_events) + print('Event(s) Received: ', json.dumps(aws_events)) + if len(aws_events) > 0: # if there are new event(s) from AWS + for event in aws_events: + event_arn = event['arn'] + status_code = event['statusCode'] + str_update = parser.parse((event['lastUpdatedTime'])) + str_update = str_update.strftime(str_ddb_format_sec) + + # get event details + event_details = json.dumps(describe_event_details(event_arn), default=myconverter) + event_details = json.loads(event_details) + print("Event Details: ", event_details) + if event_details['successfulSet'] == []: + print("An error occured with account:", event_details['failedSet'][0]['awsAccountId'], "due to:", + event_details['failedSet'][0]['errorName'], ":", + event_details['failedSet'][0]['errorMessage']) + continue + else: + # write to dynamoDB for persistence + update_ddb(event_arn, str_update, status_code, event_details) + else: + print("No events found in time frame, checking again in 1 minute.") + else: + print( + "AWS Organizations is enabled. Personal Health Dashboard and Service Health Dashboard messages will be alerted.") + aws_events = describe_org_events(health_client) + aws_events = json.dumps(aws_events, default=myconverter) + aws_events = json.loads(aws_events) + print('Event(s) Received: ', json.dumps(aws_events)) + if len(aws_events) > 0: + for event in aws_events: + event_arn = event['arn'] + status_code = event['statusCode'] + str_update = parser.parse((event['lastUpdatedTime'])) + str_update = str_update.strftime(str_ddb_format_sec) + + # get organizational view requirements + affected_org_accounts = get_health_org_accounts(health_client, event, event_arn) + affected_org_entities = get_health_org_entities(health_client, event, event_arn, affected_org_accounts) + + # get event details + event_details = json.dumps(describe_org_event_details(health_client, event_arn, affected_org_accounts), + default=myconverter) + event_details = json.loads(event_details) + print("Event Details: ", event_details) + if event_details['successfulSet'] == []: + print("An error occured with account:", event_details['failedSet'][0]['awsAccountId'], "due to:", + event_details['failedSet'][0]['errorName'], ":", + event_details['failedSet'][0]['errorMessage']) + continue + else: + # write to dynamoDB for persistence + update_org_ddb(event_arn, str_update, status_code, event_details, affected_org_accounts, + affected_org_entities) + else: + print("No events found in time frame, checking again in 1 minute.") + +if __name__ == "__main__": + main('', '') \ No newline at end of file diff --git a/BETA-org-member-deployment/messagegenerator.py b/BETA-org-member-deployment/messagegenerator.py new file mode 100644 index 0000000..e455b65 --- /dev/null +++ b/BETA-org-member-deployment/messagegenerator.py @@ -0,0 +1,570 @@ +import json +import boto3 +from datetime import datetime, timedelta +from botocore.exceptions import ClientError +import os +import time + + +def get_message_for_slack(event_details, event_type): + message = "" + summary = "" + if event_type == "create": + summary += ( + f":rotating_light:*[NEW] AWS Health reported an issue with the {event_details['successfulSet'][0]['event']['service'].upper()} service in " + f"the {event_details['successfulSet'][0]['event']['region'].upper()} region.*" + ) + message = { + "text": summary, + "attachments": [ + { + "color": "danger", + "fields": [ + { "title": "Account(s)", "value": "All accounts\nin region", "short": True }, + { "title": "Resource(s)", "value": "All resources\nin region", "short": True }, + { "title": "Service", "value": event_details['successfulSet'][0]['event']['service'], "short": True }, + { "title": "Region", "value": event_details['successfulSet'][0]['event']['region'], "short": True }, + { "title": "Start Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event']['startTime']), "short": True }, + { "title": "Status", "value": event_details['successfulSet'][0]['event']['statusCode'], "short": True }, + { "title": "Event ARN", "value": event_details['successfulSet'][0]['event']['arn'], "short": False }, + { "title": "Updates", "value": get_last_aws_update(event_details), "short": False } + ], + } + ] + } + + elif event_type == "resolve": + summary += ( + f":heavy_check_mark:*[RESOLVED] The AWS Health issue with the {event_details['successfulSet'][0]['event']['service'].upper()} service in " + f"the {event_details['successfulSet'][0]['event']['region'].upper()} region is now resolved.*" + ) + message = { + "text": summary, + "attachments": [ + { + "color": "00ff00", + "fields": [ + { "title": "Account(s)", "value": "All accounts\nin region", "short": True }, + { "title": "Resource(s)", "value": "All resources\nin region", "short": True }, + { "title": "Service", "value": event_details['successfulSet'][0]['event']['service'], "short": True }, + { "title": "Region", "value": event_details['successfulSet'][0]['event']['region'], "short": True }, + { "title": "Start Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event']['startTime']), "short": True }, + { "title": "End Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event']['endTime']), "short": True }, + { "title": "Status", "value": event_details['successfulSet'][0]['event']['statusCode'], "short": True }, + { "title": "Event ARN", "value": event_details['successfulSet'][0]['event']['arn'], "short": False }, + { "title": "Updates", "value": get_last_aws_update(event_details), "short": False } + ], + } + ] + } + print("Message sent to Slack: ", message) + return message + +def get_message_for_eventbridge(event_details, event_type): + message = "" + if event_type == "create": + message = { + "attachments": [ + { + "fields": [ + { "title": "Account(s)", "value": "All accounts\nin region", "short": True }, + { "title": "Resource(s)", "value": "All resources\nin region", "short": True }, + { "title": "Service", "value": event_details['successfulSet'][0]['event']['service'], "short": True }, + { "title": "Region", "value": event_details['successfulSet'][0]['event']['region'], "short": True }, + { "title": "Start Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event']['startTime']), "short": True }, + { "title": "Status", "value": event_details['successfulSet'][0]['event']['statusCode'], "short": True }, + { "title": "Event ARN", "value": event_details['successfulSet'][0]['event']['arn'], "short": False }, + { "title": "Updates", "value": get_last_aws_update(event_details), "short": False } + ], + } + ] + } + + elif event_type == "resolve": + message = { + "attachments": [ + { + "fields": [ + { "title": "Account(s)", "value": "All accounts\nin region", "short": True }, + { "title": "Resource(s)", "value": "All resources\nin region", "short": True }, + { "title": "Service", "value": event_details['successfulSet'][0]['event']['service'], "short": True }, + { "title": "Region", "value": event_details['successfulSet'][0]['event']['region'], "short": True }, + { "title": "Start Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event']['startTime']), "short": True }, + { "title": "End Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event']['endTime']), "short": True }, + { "title": "Status", "value": event_details['successfulSet'][0]['event']['statusCode'], "short": True }, + { "title": "Event ARN", "value": event_details['successfulSet'][0]['event']['arn'], "short": False }, + { "title": "Updates", "value": get_last_aws_update(event_details), "short": False } + ], + } + ] + } + print("SHD Message generated for EventBridge : ", message) + return message + +def get_org_message_for_eventbridge(event_details, event_type, affected_org_accounts, affected_org_entities): + message = "" + if len(affected_org_entities) >= 1: + affected_org_entities = "\n".join(affected_org_entities) + else: + affected_org_entities = "All resources\nin region" + if len(affected_org_accounts) >= 1: + affected_org_accounts = "\n".join(affected_org_accounts) + else: + affected_org_accounts = "All accounts\nin region" + if event_type == "create": + message = { + "attachments": [ + { + "fields": [ + { "title": "Account(s)", "value": affected_org_accounts, "short": True }, + { "title": "Resource(s)", "value": affected_org_entities, "short": True }, + { "title": "Service", "value": event_details['successfulSet'][0]['event']['service'], "short": True }, + { "title": "Region", "value": event_details['successfulSet'][0]['event']['region'], "short": True }, + { "title": "Start Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event']['startTime']), "short": True }, + { "title": "Status", "value": event_details['successfulSet'][0]['event']['statusCode'], "short": True }, + { "title": "Event ARN", "value": event_details['successfulSet'][0]['event']['arn'], "short": False }, + { "title": "Updates", "value": get_last_aws_update(event_details), "short": False } + ], + } + ] + } + + elif event_type == "resolve": + message = { + "attachments": [ + { + "fields": [ + { "title": "Account(s)", "value": affected_org_accounts, "short": True }, + { "title": "Resource(s)", "value": affected_org_entities, "short": True }, + { "title": "Service", "value": event_details['successfulSet'][0]['event']['service'], "short": True }, + { "title": "Region", "value": event_details['successfulSet'][0]['event']['region'], "short": True }, + { "title": "Start Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event']['startTime']), "short": True }, + { "title": "End Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event']['endTime']), "short": True }, + { "title": "Status", "value": event_details['successfulSet'][0]['event']['statusCode'], "short": True }, + { "title": "Event ARN", "value": event_details['successfulSet'][0]['event']['arn'], "short": False }, + { "title": "Updates", "value": get_last_aws_update(event_details), "short": False } + ], + } + ] + } + json.dumps(message) + print("PHD/SHD Message generated for Event Bridge: ", message) + return message + + +def get_org_message_for_slack(event_details, event_type, affected_org_accounts, affected_org_entities): + message = "" + summary = "" + if len(affected_org_entities) >= 1: + affected_org_entities = "\n".join(affected_org_entities) + else: + affected_org_entities = "All resources\nin region" + if len(affected_org_accounts) >= 1: + affected_org_accounts = "\n".join(affected_org_accounts) + else: + affected_org_accounts = "All accounts\nin region" + if event_type == "create": + summary += ( + f":rotating_light:*[NEW] AWS Health reported an issue with the {event_details['successfulSet'][0]['event']['service'].upper()} service in " + f"the {event_details['successfulSet'][0]['event']['region'].upper()} region.*" + ) + message = { + "text": summary, + "attachments": [ + { + "color": "danger", + "fields": [ + { "title": "Account(s)", "value": affected_org_accounts, "short": True }, + { "title": "Resource(s)", "value": affected_org_entities, "short": True }, + { "title": "Service", "value": event_details['successfulSet'][0]['event']['service'], "short": True }, + { "title": "Region", "value": event_details['successfulSet'][0]['event']['region'], "short": True }, + { "title": "Start Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event']['startTime']), "short": True }, + { "title": "Status", "value": event_details['successfulSet'][0]['event']['statusCode'], "short": True }, + { "title": "Event ARN", "value": event_details['successfulSet'][0]['event']['arn'], "short": False }, + { "title": "Updates", "value": get_last_aws_update(event_details), "short": False } + ], + } + ] + } + + elif event_type == "resolve": + summary += ( + f":heavy_check_mark:*[RESOLVED] The AWS Health issue with the {event_details['successfulSet'][0]['event']['service'].upper()} service in " + f"the {event_details['successfulSet'][0]['event']['region'].upper()} region is now resolved.*" + ) + message = { + "text": summary, + "attachments": [ + { + "color": "00ff00", + "fields": [ + { "title": "Account(s)", "value": affected_org_accounts, "short": True }, + { "title": "Resource(s)", "value": affected_org_entities, "short": True }, + { "title": "Service", "value": event_details['successfulSet'][0]['event']['service'], "short": True }, + { "title": "Region", "value": event_details['successfulSet'][0]['event']['region'], "short": True }, + { "title": "Start Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event']['startTime']), "short": True }, + { "title": "End Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event']['endTime']), "short": True }, + { "title": "Status", "value": event_details['successfulSet'][0]['event']['statusCode'], "short": True }, + { "title": "Event ARN", "value": event_details['successfulSet'][0]['event']['arn'], "short": False }, + { "title": "Updates", "value": get_last_aws_update(event_details), "short": False } + ], + } + ] + } + json.dumps(message) + print("Message sent to Slack: ", message) + return message + + +def get_message_for_chime(event_details, event_type): + message = "" + summary = "" + if event_type == "create": + + message = str("/md" + "\n" + "**:rotating_light:\[NEW\] AWS Health reported an issue with the " + event_details['successfulSet'][0]['event']['service'].upper() + " service in " + event_details['successfulSet'][0]['event']['region'].upper() + " region.**" + "\n" + "---" + "\n" + "**Account(s)**: " + "All accounts in region" + "\n" + "**Resource(s)**: " + "All resources in region" + "\n" + "**Service**: " + event_details['successfulSet'][0]['event']['service'] + "\n" + "**Region**: " + event_details['successfulSet'][0]['event']['region'] + "\n" + "**Start Time (UTC)**: " + cleanup_time(event_details['successfulSet'][0]['event']['startTime']) + "\n" + "**Status**: " + event_details['successfulSet'][0]['event']['statusCode'] + "\n" + "**Event ARN**: " + event_details['successfulSet'][0]['event']['arn'] + "\n" + "**Updates:**" + "\n" + get_last_aws_update(event_details) + ) + + elif event_type == "resolve": + + message = str("/md" + "\n" + "**:heavy_check_mark:\[RESOLVED\] The AWS Health issue with the " + event_details['successfulSet'][0]['event']['service'].upper() + " service in " + event_details['successfulSet'][0]['event']['region'].upper() + " region is now resolved.**" + "\n" + "---" + "\n" + "**Account(s)**: " + "All accounts in region" + "\n" + "**Resource(s)**: " + "All resources in region" + "\n" + "**Service**: " + event_details['successfulSet'][0]['event']['service'] + "\n" + "**Region**: " + event_details['successfulSet'][0]['event']['region'] + "\n" + "**Start Time (UTC)**: " + cleanup_time(event_details['successfulSet'][0]['event']['startTime']) + "\n" + "**End Time (UTC)**: " + cleanup_time(event_details['successfulSet'][0]['event']['endTime']) + "\n" + "**Status**: " + event_details['successfulSet'][0]['event']['statusCode'] + "\n" + "**Event ARN**: " + event_details['successfulSet'][0]['event']['arn'] + "\n" + "**Updates:**" + "\n" + get_last_aws_update(event_details) + ) + json.dumps(message) + print("Message sent to Chime: ", message) + return message + + +def get_org_message_for_chime(event_details, event_type, affected_org_accounts, affected_org_entities): + message = "" + summary = "" + if len(affected_org_entities) >= 1: + affected_org_entities = "\n".join(affected_org_entities) + else: + affected_org_entities = "All resources in region" + if len(affected_org_accounts) >= 1: + affected_org_accounts = "\n".join(affected_org_accounts) + else: + affected_org_accounts = "All accounts in region" + if event_type == "create": + + message = str("/md" + "\n" + "**:rotating_light:\[NEW\] AWS Health reported an issue with the " + event_details['successfulSet'][0]['event']['service'].upper()) + " service in " + str(event_details['successfulSet'][0]['event']['region'].upper() + " region**" + "\n" + "---" + "\n" + "**Account(s)**: " + affected_org_accounts + "\n" + "**Resource(s)**: " + affected_org_entities + "\n" + "**Service**: " + event_details['successfulSet'][0]['event']['service'] + "\n" + "**Region**: " + event_details['successfulSet'][0]['event']['region'] + "\n" + "**Start Time (UTC)**: " + cleanup_time(event_details['successfulSet'][0]['event']['startTime']) + "\n" + "**Status**: " + event_details['successfulSet'][0]['event']['statusCode'] + "\n" + "**Event ARN**: " + event_details['successfulSet'][0]['event']['arn'] + "\n" + "**Updates:**" + "\n" + get_last_aws_update(event_details) + ) + + elif event_type == "resolve": + + message = str("/md" + "\n" + "**:heavy_check_mark:\[RESOLVED\] The AWS Health issue with the " + event_details['successfulSet'][0]['event']['service'].upper()) + " service in " + str(event_details['successfulSet'][0]['event']['region'].upper() + " region is now resolved.**" + "\n" + "---" + "\n" + "**Account(s)**: " + affected_org_accounts + "\n" + "**Resource(s)**: " + affected_org_entities + "\n" + "**Service**: " + event_details['successfulSet'][0]['event']['service'] + "\n" + "**Region**: " + event_details['successfulSet'][0]['event']['region'] + "\n" + "**Start Time (UTC)**: " + cleanup_time(event_details['successfulSet'][0]['event']['startTime']) + "\n" + "**End Time (UTC)**: " + cleanup_time(event_details['successfulSet'][0]['event']['endTime']) + "\n" + "**Status**: " + event_details['successfulSet'][0]['event']['statusCode'] + "\n" + "**Event ARN**: " + event_details['successfulSet'][0]['event']['arn'] + "\n" + "**Updates:**" + "\n" + get_last_aws_update(event_details) + ) + print("Message sent to Chime: ", message) + return message + + + +def get_message_for_teams(event_details, event_type): + message = "" + summary = "" + if event_type == "create": + title = "🚨 [NEW] AWS Health reported an issue with the " + event_details['successfulSet'][0]['event'][ + 'service'].upper() + " service in the " + event_details['successfulSet'][0]['event'][ + 'region'].upper() + " region." + message = { + "@type": "MessageCard", + "@context": "http://schema.org/extensions", + "themeColor": "FF0000", + "summary": "AWS Health Aware Alert", + "sections": [ + { + "activityTitle": str(title), + "markdown": False, + "facts": [ + {"name": "Account(s)", "value": "All accounts\nin region"}, + {"name": "Resource(s)", "value": "All resources\nin region"}, + {"name": "Service", "value": event_details['successfulSet'][0]['event']['service']}, + {"name": "Region", "value": event_details['successfulSet'][0]['event']['region']}, + {"name": "Start Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event']['startTime'])}, + {"name": "Status", "value": event_details['successfulSet'][0]['event']['statusCode']}, + {"name": "Event ARN", "value": event_details['successfulSet'][0]['event']['arn']}, + {"name": "Updates", "value": get_last_aws_update(event_details)} + ], + } + ] + } + + elif event_type == "resolve": + title = "✅ [RESOLVED] The AWS Health issue with the " + event_details['successfulSet'][0]['event'][ + 'service'].upper() + " service in the " + event_details['successfulSet'][0]['event'][ + 'region'].upper() + " region is now resolved." + message = { + "@type": "MessageCard", + "@context": "http://schema.org/extensions", + "themeColor": "00ff00", + "summary": "AWS Health Aware Alert", + "sections": [ + { + "activityTitle": str(title), + "markdown": False, + "facts": [ + {"name": "Account(s)", "value": "All accounts\nin region"}, + {"name": "Resource(s)", "value": "All resources\nin region"}, + {"name": "Service", "value": event_details['successfulSet'][0]['event']['service']}, + {"name": "Region", "value": event_details['successfulSet'][0]['event']['region']}, + {"name": "Start Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event']['startTime'])}, + {"name": "End Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event']['endTime'])}, + {"name": "Status", "value": event_details['successfulSet'][0]['event']['statusCode']}, + {"name": "Event ARN", "value": event_details['successfulSet'][0]['event']['arn']}, + {"name": "Updates", "value": get_last_aws_update(event_details)} + ], + } + ] + } + print("Message sent to Teams: ", message) + return message + + +def get_org_message_for_teams(event_details, event_type, affected_org_accounts, affected_org_entities): + message = "" + summary = "" + if len(affected_org_entities) >= 1: + affected_org_entities = "\n".join(affected_org_entities) + else: + affected_org_entities = "All resources in region" + if len(affected_org_accounts) >= 1: + affected_org_accounts = "\n".join(affected_org_accounts) + else: + affected_org_accounts = "All accounts in region" + if event_type == "create": + title = "🚨 [NEW] AWS Health reported an issue with the " + event_details['successfulSet'][0]['event'][ + 'service'].upper() + " service in the " + event_details['successfulSet'][0]['event'][ + 'region'].upper() + " region." + message = { + "@type": "MessageCard", + "@context": "http://schema.org/extensions", + "themeColor": "FF0000", + "summary": "AWS Health Aware Alert", + "sections": [ + { + "activityTitle": title, + "markdown": False, + "facts": [ + {"name": "Account(s)", "value": affected_org_accounts}, + {"name": "Resource(s)", "value": affected_org_entities}, + {"name": "Service", "value": event_details['successfulSet'][0]['event']['service']}, + {"name": "Region", "value": event_details['successfulSet'][0]['event']['region']}, + {"name": "Start Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event']['startTime'])}, + {"name": "Status", "value": event_details['successfulSet'][0]['event']['statusCode']}, + {"name": "Event ARN", "value": event_details['successfulSet'][0]['event']['arn']}, + {"name": "Updates", "value": event_details['successfulSet'][0]['eventDescription']['latestDescription']} + ], + } + ] + } + + elif event_type == "resolve": + title = "✅ [RESOLVED] The AWS Health issue with the " + event_details['successfulSet'][0]['event'][ + 'service'].upper() + " service in the " + event_details['successfulSet'][0]['event'][ + 'region'].upper() + " region is now resolved." + message = { + "@type": "MessageCard", + "@context": "http://schema.org/extensions", + "themeColor": "00ff00", + "summary": "AWS Health Aware Alert", + "sections": [ + { + "activityTitle": title, + "markdown": False, + "facts": [ + {"name": "Account(s)", "value": affected_org_accounts}, + {"name": "Resource(s)", "value": affected_org_entities}, + {"name": "Service", "value": event_details['successfulSet'][0]['event']['service']}, + {"name": "Region", "value": event_details['successfulSet'][0]['event']['region']}, + {"name": "Start Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event']['startTime'])}, + {"name": "End Time (UTC)", "value": cleanup_time(event_details['successfulSet'][0]['event']['endTime'])}, + {"name": "Status", "value": event_details['successfulSet'][0]['event']['statusCode']}, + {"name": "Event ARN", "value": event_details['successfulSet'][0]['event']['arn']}, + {"name": "Updates", "value": event_details['successfulSet'][0]['eventDescription']['latestDescription']} + ], + } + ] + } + return message + print("Message sent to Teams: ", message) + + +def get_message_for_email(event_details, event_type): + if event_type == "create": + BODY_HTML = f""" + + + Greetings from AWS Health Aware,
+

There is an AWS incident that is in effect which may likely impact your resources. Here are the details:

+ Account(s): All accounts in region
+ Resource(s): All service related resources in region
+ Service: {event_details['successfulSet'][0]['event']['service']}
+ Region: {event_details['successfulSet'][0]['event']['region']}
+ Start Time (UTC): {cleanup_time(event_details['successfulSet'][0]['event']['startTime'])}
+ Status: {event_details['successfulSet'][0]['event']['statusCode']}
+ Event ARN: {event_details['successfulSet'][0]['event']['arn']}
+ Updates: {event_details['successfulSet'][0]['eventDescription']['latestDescription']}

+ For updates, please visit the AWS Service Health Dashboard
+ If you are experiencing issues related to this event, please open an AWS Support case within your account.

+ Thanks,

AHA: AWS Health Aware +

+ + + """ + else: + BODY_HTML = f""" + + + Greetings again from AWS Health Aware,
+

Good news! The AWS Health incident from earlier has now been marked as resolved.

+ Account(s): All accounts in region
+ Resource(s): All service related resources in region
+ Service: {event_details['successfulSet'][0]['event']['service']}
+ Region: {event_details['successfulSet'][0]['event']['region']}
+ Start Time (UTC): {cleanup_time(event_details['successfulSet'][0]['event']['startTime'])}
+ End Time (UTC): {cleanup_time(event_details['successfulSet'][0]['event']['endTime'])}
+ Status: {event_details['successfulSet'][0]['event']['statusCode']}
+ Event ARN: {event_details['successfulSet'][0]['event']['arn']}
+ Updates: {event_details['successfulSet'][0]['eventDescription']['latestDescription']}

+ If you are still experiencing issues related to this event, please open an AWS Support case within your account.

+

+ Thanks,

AHA: AWS Health Aware +

+ + + """ + print("Message sent to Email: ", BODY_HTML) + return BODY_HTML + + +def get_org_message_for_email(event_details, event_type, affected_org_accounts, affected_org_entities): + if len(affected_org_entities) >= 1: + affected_org_entities = "\n".join(affected_org_entities) + else: + affected_org_entities = "All servicess related resources in region" + if len(affected_org_accounts) >= 1: + affected_org_accounts = "\n".join(affected_org_accounts) + else: + affected_org_accounts = "All accounts in region" + if event_type == "create": + BODY_HTML = f""" + + + Greetings from AWS Health Aware,
+

There is an AWS incident that is in effect which may likely impact your resources. Here are the details:

+ Account(s): {affected_org_accounts}
+ Resource(s): {affected_org_entities}
+ Service: {event_details['successfulSet'][0]['event']['service']}
+ Region: {event_details['successfulSet'][0]['event']['region']}
+ Start Time (UTC): {cleanup_time(event_details['successfulSet'][0]['event']['startTime'])}
+ Status: {event_details['successfulSet'][0]['event']['statusCode']}
+ Event ARN: {event_details['successfulSet'][0]['event']['arn']}
+ Updates: {event_details['successfulSet'][0]['eventDescription']['latestDescription']}

+ For updates, please visit the AWS Service Health Dashboard
+ If you are experiencing issues related to this event, please open an AWS Support case within your account.

+ Thanks,

AHA: AWS Health Aware +

+ + + """ + else: + BODY_HTML = f""" + + + Greetings again from AWS Health Aware,
+

Good news! The AWS Health incident from earlier has now been marked as resolved.

+ Account(s): {affected_org_accounts}
+ Resource(s): {affected_org_entities}
+ Service: {event_details['successfulSet'][0]['event']['service']}
+ Region: {event_details['successfulSet'][0]['event']['region']}
+ Start Time (UTC): {cleanup_time(event_details['successfulSet'][0]['event']['startTime'])}
+ End Time (UTC): {cleanup_time(event_details['successfulSet'][0]['event']['endTime'])}
+ Status: {event_details['successfulSet'][0]['event']['statusCode']}
+ Event ARN: {event_details['successfulSet'][0]['event']['arn']}
+ Updates: {event_details['successfulSet'][0]['eventDescription']['latestDescription']}

+ If you are still experiencing issues related to this event, please open an AWS Support case within your account.

+ Thanks,

AHA: AWS Health Aware +

+ + + """ + print("Message sent to Email: ", BODY_HTML) + return BODY_HTML + + +def cleanup_time(event_time): + """ + Takes as input a datetime string as received from The AWS Health event_detail call. It converts this string to a + datetime object, changes the timezone to EST and then formats it into a readable string to display in Slack. + + :param event_time: datetime string + :type event_time: str + :return: A formatted string that includes the month, date, year and 12-hour time. + :rtype: str + """ + event_time = datetime.strptime(event_time[:16], '%Y-%m-%d %H:%M') + return event_time.strftime("%Y-%m-%d %H:%M:%S") + + +def get_last_aws_update(event_details): + """ + Takes as input the event_details and returns the last update from AWS (instead of the entire timeline) + + :param event_details: Detailed information about a specific AWS health event. + :type event_details: dict + :return: the last update message from AWS + :rtype: str + """ + aws_message = event_details['successfulSet'][0]['eventDescription']['latestDescription'] + return aws_message + + +def format_date(event_time): + """ + Takes as input a datetime string as received from The AWS Health event_detail call. It converts this string to a + datetime object, changes the timezone to EST and then formats it into a readable string to display in Slack. + + :param event_time: datetime string + :type event_time: str + :return: A formatted string that includes the month, date, year and 12-hour time. + :rtype: str + """ + event_time = datetime.strptime(event_time[:16], '%Y-%m-%d %H:%M') + return event_time.strftime('%B %d, %Y at %I:%M %p')