使用 GitHub 部署 AWS CloudWatch 警报
![Warren Marusiak 头像](https://wac-cdn.atlassian.com/dam/jcr:7509aefb-43e8-401d-90fe-0850cbe6bb13/wmarusiak_headshot%20(1).png?cdnVersion=2022)
Warren Marusiak
高级技术传播者
为了演示如何使用 Jira Software 和各种互联工具开发、部署和管理应用,我们的团队创建了 ImageLabeller,这是一款基于 AWS 构建的简单演示应用,它使用机器学习将标签应用于图像。
本页面介绍如何设置 AWS SageMaker 预定义模型,这是 ImageLabeller 成功处理图像的先决条件。在您开始之前,我们建议阅读 ImageLabeller 架构和 使用 GitHub 部署 ImageLabeller 页面,以了解背景信息。
为 Opsgenie 集成添加警报存储库
转到 Jira 并创建一个新 Jira 事务,将 AWS CloudWatch 警报存储库添加到 GitHub。在本例中,Jira 事务 ID 为 IM-10。
![添加 github 存储库的 jira 事务的屏幕截图](https://wac-cdn.atlassian.com/dam/jcr:f5497e4c-8c42-4291-90f4-35fc3ddb13ca/screenshot_jsw_issueaddgithubalarmsrepository.png?cdnVersion=2022)
转到 GitHub 并单击“新建”。为“负责人”选择合适的组织。单击“创建存储库”以继续。
![在 github 中创建警报存储库的屏幕截图](https://wac-cdn.atlassian.com/dam/jcr:fdf36856-a64e-4b1d-baa2-65e1f49e85b7/screenshot_github_createalarmsrepository.png?cdnVersion=2022)
单击“设置”,然后单击“密钥”。将您的 AWS 访问密钥 ID 添加为 AWS_ACCESS_KEY_ID,将您的 AWS 秘密访问密钥添加为 AWS_SECRET_ACCESS_KEY。
![在 github 中添加 aws 访问密钥的屏幕屏幕](https://wac-cdn.atlassian.com/dam/jcr:862e11d1-ef4e-4e21-92e9-5916c67f115a/screenshot_github_addawsaccesskeyalarmsrepo.png?cdnVersion=2022)
在您的终端中转到您的 CloudWatchAlarms 存储库,并运行以下命令以将您的 AWS CloudFormation 代码推送到 GitHub。
git add --all
git commit -m "IM-10 add CloudWatchAlarms to github"
git remote add origin git@github.com:PmmQuickStartGuides01/CloudWatchAlarms.git
git branch -m mainline
git push -u origin mainline
警报 template.yml
Description: 'A description'
Resources:
OpsGenieSNS:
Type: AWS::SNS::Topic
Properties:
DisplayName: 'Opsgenie'
Subscription:
- Endpoint: 'https://api.opsgenie.com/v1/json/cloudwatch?apiKey=a4449509-6998-4d55-841d-2e6b363520c7'
Protocol: 'HTTPS'
TopicName: 'Opsgenie'
SubmitImageLambdaAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmActions:
- !Ref 'OpsGenieSNS'
AlarmDescription: 'SubmitImage Too Many Invocations'
ComparisonOperator: 'GreaterThanThreshold'
Dimensions:
- Name: FunctionName
Value: !ImportValue 'SubmitImageFunctionName'
EvaluationPeriods: 1
MetricName: 'Invocations'
Namespace: 'AWS/Lambda'
Period: 60
Statistic: 'Sum'
Threshold: 10
将 SNS 主题订阅端点设置为您从 Opsgenie 复制的端点 URL。SubmitImageLambdaAlarm 监控 submitImage AWS Lambda 发出的单个指标。如果一分钟内调用次数超过 10 次,则会发出警报。
用于部署到 AWS 的 GitHub 操作
在您的终端中转到 CloudWatchAlarms 存储库,创建一个以您的 Jira 事务 ID 命名的分支,并创建一个 .github/workflows 目录。
git checkout -b IM-10
mkdir -p .github/workflows && cd .github/workflows
使用以下 yaml 创建 deploy-test-staging.yml。这为您的测试和暂存环境定义了部署工作流程,该工作流程在推送到主线以外的分支时运行。
name: deploy-cloudwatchalarms-test-staging
on:
push:
branches:
- '*'
- '!mainline'
jobs:
deploy-us-west-1:
runs-on: ubuntu-latest
outputs:
env-name: ${{ steps.env-name.outputs.environment }}
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Configure AWS credentials
id: creds
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: "us-west-1"
- name: Deploy to AWS CloudFormation
uses: aws-actions/aws-cloudformation-github-deploy@v1
with:
name: OpenDevOpsAlarms
template: template.yml
no-fail-on-empty-changeset: "1"
deploy-us-east-2:
runs-on: ubuntu-latest
needs: deploy-us-west-1
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Configure AWS credentials
id: creds
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: "us-east-2"
- name: Deploy to AWS CloudFormation
uses: aws-actions/aws-cloudformation-github-deploy@v1
with:
name: OpenDevOpsAlarms
template: template.yml
no-fail-on-empty-changeset: "1"
然后使用以下 yaml 创建 deploy-prod.yml。这为您的生产环境定义了部署工作流程,该工作流程在拉取请求将变更合并到主线中时运行。
name: deploy-cloudwatchalarms-prod
on:
pull_request:
branches:
- mainline
jobs:
deploy-us-west-2:
runs-on: ubuntu-latest
outputs:
env-name: ${{ steps.env-name.outputs.environment }}
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Configure AWS credentials
id: creds
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: "us-west-2"
- name: Deploy to AWS CloudFormation
uses: aws-actions/aws-cloudformation-github-deploy@v1
with:
name: OpenDevOpsAlarms
template: template.yml
no-fail-on-empty-changeset: "1"
deploy-ca-central-1:
runs-on: ubuntu-latest
needs: deploy-us-west-2
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Configure AWS credentials
id: creds
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: "ca-central-1"
- name: Deploy to AWS CloudFormation
uses: aws-actions/aws-cloudformation-github-deploy@v1
with:
name: OpenDevOpsAlarms
template: template.yml
no-fail-on-empty-changeset: "1"
deploy-us-east-1:
runs-on: ubuntu-latest
needs: deploy-ca-central-1
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Configure AWS credentials
id: creds
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: "us-east-1"
- name: Deploy to AWS CloudFormation
uses: aws-actions/aws-cloudformation-github-deploy@v1
with:
name: OpenDevOpsAlarms
template: template.yml
no-fail-on-empty-changeset: "1"
推送到功能分支
从命令行运行以下命令,将您的代码推送到您的 CloudWatchAlarms 存储库的 IM-10 分支。
git add --all
git commit -m "IM-10 add github actions to CloudWatchAlarms"
git push -u origin IM-10
单击“操作”以查看正在运行的工作流程。
![在 github 中运行工作流程的屏幕截图](https://wac-cdn.atlassian.com/dam/jcr:ba53984a-1acd-48d1-8220-bb2d49f201bd/screenshot_github_seerunningworkflowsalarmsrepo.png?cdnVersion=2022)
创建拉取请求
单击“创建拉取请求”以合并到主线中。
![在 github 中创建拉取请求的屏幕截图](https://wac-cdn.atlassian.com/dam/jcr:82c762bf-f4a5-4361-b528-5ba687b995a6/screenshot_github_createpullrequestalarmsrepo.png?cdnVersion=2022)
单击“操作”以监视生产部署。
![github 中的生产部署的屏幕截图](https://wac-cdn.atlassian.com/dam/jcr:743455d2-8514-433e-ba57-d9ff3376fe3a/screenshot_github_monitorproductionalarmsrepo.png?cdnVersion=2022)
测试警报
通过触发您刚才设置的 AWS CloudWatch 提醒或单击“创建提醒”来生成提醒。
![在 opsgenie 中创建提醒的屏幕截图](https://wac-cdn.atlassian.com/dam/jcr:bf0b4621-78c9-4eff-8393-e60a6e84cb4e/screenshot_opsgenie_testgithubalarmsrepo.png?cdnVersion=2022)
查看 Slack 可看到通知已出现。
![opsgenie 中的 slack 通知的屏幕截图](https://wac-cdn.atlassian.com/dam/jcr:e3e5423a-bc3d-442d-b804-fcc88788e731/screenshot_opsgenie_slackintegrationnotificationgithub.png?cdnVersion=2022)
漂亮!您做到了。ImageLabeller 现已启动并运行。
分享这篇文章
下一主题
推荐阅读
将这些资源加入书签,以了解 DevOps 团队的类型,或获取 Atlassian 关于 DevOps 的持续更新。
![Devops 示意图](https://wac-cdn.atlassian.com/dam/jcr:bd9d8b2c-ca36-444f-8595-719cb1990e64/Devops-community.png?cdnVersion=2022)
DevOps 社区
![Devops 示意图](https://wac-cdn.atlassian.com/dam/jcr:297108ea-d232-4368-af51-b53af230c4fe/Simulation-workshop.png?cdnVersion=2022)
DevOps 学习路径
![地图插图](https://wac-cdn.atlassian.com/dam/jcr:25f6330a-4191-408f-a4e5-2e24bfba67b4/Maturity-model.png?cdnVersion=2022)