• Products
  • Documentation
  • Resources

Deploy AWS CloudWatch alarms with Gitlab

To demonstrate how to develop, deploy, and manage applications using Jira Software and various connected tools, our team created ImageLabeller, a simple demo application built on AWS that uses machine learning to apply labels to images.

This page covers how to monitor ImageLabeller with Opsgenie. Before you begin, we recommend reading the ImageLabeller architecture and Deploy ImageLabeller with Gitlab pages for context.

Add an alarms repository for Opsgenie integration

Go to Jira and create a new Jira issue for adding AWS CloudWatch alarms repository to GitLab. In this example the Jira issue ID is IM-6.

screenshot of jira issue to add repo "AWS cloudwatch alarms" in gitlab

Go to GitLab and create a new project. Update the Project URL to use the group you linked to Jira. Click Create project.

screenshot of creating project "cloudwatchalarms" in gitlab

In your terminal go to your CloudWatchAlarms repository, and run the following to push your AWS CloudFormation template.yml file to GitLab.

1 2 3 4 5 git add --all git commit -m "IM-6 add CloudWatchAlarms repository to gitlab" git remote add origin git@gitlab.com:pmmquickstartguides/cloudwatchalarms.git git branch -m mainline git push -u origin mainline

As with previous repositories you must add AWS access keys, configure protected branches, and setup deployment environments.

Alarms template.yml

 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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

Set the SNS topic subscription endpoint to the endpoint URL you copied from Opsgenie. The SubmitImageLambdaAlarm monitors a single metric emitted by the SubmitImage AWS Lambda. If there are more than 10 invocations, a minute the alarm is raised.

.gitlab-ci.yml for deploying to AWS

Go to your CloudWatchAlarms repository in your terminal, and create a branch named after your Jira issue ID.

1 git checkout -b IM-6

Create .gitlab-ci.yml file with the following yaml. This defines a deployment workflow for your Test, Staging, and Production environments.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 stages: - merge-request - test-us-west-1 - test-us-east-2 - production-us-west-2 - production-us-east-1 - production-ca-central-1 merge-request-pipeline-job: stage: merge-request rules: - if: $CI_PIPELINE_SOURCE == "merge_request_event" script: - echo "This pipeline always succeeds and enables merge." - echo true deploy-test-us-west-1: stage: test-us-west-1 environment: test-us-west-1 rules: - if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH && $CI_PIPELINE_SOURCE != "merge_request_event" image: registry.gitlab.com/gitlab-org/cloud-deploy/aws-base:latest script: - export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID - export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY - aws cloudformation deploy --region us-west-1 --template-file template.yml --stack-name OpenDevOpsAlarms deploy-test-us-east-2: stage: test-us-east-2 environment: test-us-east-2 rules: - if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH && $CI_PIPELINE_SOURCE != "merge_request_event" image: registry.gitlab.com/gitlab-org/cloud-deploy/aws-base:latest script: - export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID - export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY - aws cloudformation deploy --region us-east-2 --template-file template.yml --stack-name OpenDevOpsAlarms deploy-production-us-west-2: stage: production-us-west-2 environment: production-us-west-2 rules: - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_PIPELINE_SOURCE != "merge_request_event" image: registry.gitlab.com/gitlab-org/cloud-deploy/aws-base:latest script: - export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID - export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY - aws cloudformation deploy --region us-west-2 --template-file template.yml --stack-name OpenDevOpsAlarms deploy-production-us-east-1: stage: production-us-east-1 environment: production-us-east-1 rules: - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_PIPELINE_SOURCE != "merge_request_event" image: registry.gitlab.com/gitlab-org/cloud-deploy/aws-base:latest script: - export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID - export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY - aws cloudformation deploy --region us-east-1 --template-file template.yml --stack-name OpenDevOpsAlarms deploy-production-ca-central-1: stage: production-ca-central-1 environment: production-ca-central-1 rules: - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_PIPELINE_SOURCE != "merge_request_event" image: registry.gitlab.com/gitlab-org/cloud-deploy/aws-base:latest script: - export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID - export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY - aws cloudformation deploy --region us-east-2 --template-file template.yml --stack-name OpenDevOpsAlarms

Pushing to a feature branch

Run the following from the command line to push your changes to the IM-6 branch of your CloudWatchAlarms repository. Include the Jira issue ID in commit messages, and branch names to enable the Jira GitLab integration to keep track of what is happening in your project.

1 2 3 git add --all git commit -m "IM-6 .gitlab-ci.yml to CloudWatchAlarms repo" git push -u origin IM-6

Click CI/CD, then Pipelines to see the pipeline run.

Create a merge request

Create a merge request to deploy to your production environments after GitLab deploys to your test environments.

screenshot of merge request in gitlab

Merge the changes to mainline after the merge request pipeline completes. Click CI/CD, then Pipelines to see the running production pipeline.

screenshot of running production pipeline in gitlab

Testing the Alarm

Generate an alert by triggering the AWS CloudWatch alarm you just set up, or by clicking Create alert.

screenshot of creating alert in ospgenie

Check Slack to see that the notification appeared.

screenshot of slack notification

Bravo! You did it. ImageLabeller is now up and running.

 

Last modified on Sep 24, 2021
Cached at 1:41 AM on Oct 16, 2021 |

Additional Help