In today’s agile development landscape, continuous integration and continuous deployment (CI/CD) have become fundamental practices for modern software deployments. CTO.ai stands as one of the industry leaders in this arena, offering seamless integration and deployment pipelines. Integrating CTO.ai with AWS CloudWatch for alerting can enhance your pipeline by notifying you about the build status and any anomalies that may occur during the build process.

In this blog post, we will walk you through the process of setting up AWS CloudWatch alerts for CTO.ai builds step by step.

Prerequisites

Before we begin, ensure that you have the following:

  • An AWS account is set up and configured with the necessary access rights.
  • A CTO.ai account.
  • AWS CLI was installed and configured with the necessary access credentials for your AWS account.

Setting Up AWS CloudWatch

The first step involves setting up AWS CloudWatch in your AWS console. Go to the AWS CloudWatch service and create a new dashboard. In the AWS dashboard,  you can configure various metrics and alarms.

Setting Up an IAM Role for CTO.ai Workflows

To allow CTO.ai to send data to CloudWatch, create an IAM role with the necessary permissions. Grant permissions like cloudwatch:PutMetricData to allow CTO.ai to push metrics to CloudWatch.

Configuring CTO.ai Environment

Next, configure your CTO.ai environment variables to include AWS access credentials. You would typically do this in the project settings under secrets. When you are done, you can deploy any of our AWS Workflow stacks that suit your needs. We currently have the AWS ECS fargate workflow and the AWS EKS ASG workflow. Once you have deployed your infrastructure, create a Dockerfile and ops.yml file in your project repository, using our public sample repository as guidance for defining your own application and services.

Book-a-Consultation_v2

Integrating CTO.ai and AWS CloudWatch

In your ops.yml file, replace the event values with your own values and define your jobs. Be sure to specify the Docker, AWS, and bash commands and workflow commands necessary to build and deploy your application container image.

version: "1"
pipelines:
  - name: sample-expressjs-pipeline-aws-ecs-fargate:0.1.1
    description: build a release for deployment
    env:
      static:
        - DEBIAN_FRONTEND=noninteractive
        - ORG=workflows-sh
        - REPO=sample-expressjs-aws-ecs-fargate
        - AWS_REGION=us-west-1
        - STACK_TYPE=aws-ecs-fargate
      secrets:
        - GITHUB_TOKEN
        - AWS_ACCESS_KEY_ID
        - AWS_SECRET_ACCESS_KEY
        - AWS_ACCOUNT_NUMBER
    events:
      - "github:workflows-sh/sample-expressjs-aws-ecs-fargate:pull_request.merged"
      - "github:workflows-sh/sample-expressjs-aws-ecs-fargate:pull_request.opened"
      - "github:workflows-sh/sample-expressjs-aws-ecs-fargate:pull_request.synchronize"
    jobs:
      - name: sample-expressjs-build-job-aws-ecs-fargate
        description: sample-expressjs build step
        packages:
          - git
          - unzip
          - python
        steps:
          - curl https://s3.amazonaws.com/aws-cli/awscli-bundle-1.18.200.zip -o awscli-bundle.zip
          - unzip awscli-bundle.zip && ./awscli-bundle/install -b ~/bin/aws
          - export PATH=~/bin:$PATH
          - aws --version
          - git clone https://$GITHUB_TOKEN:[email protected]/$ORG/$REPO
          - cd $REPO && ls -asl
          - git fetch && git checkout $REF
          - aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_NUMBER.dkr.ecr.$AWS_REGION.amazonaws.com/$REPO
          - docker build -f Dockerfile -t $AWS_ACCOUNT_NUMBER.dkr.ecr.$AWS_REGION.amazonaws.com/$REPO-$STACK_TYPE:$REF .
          - docker push $AWS_ACCOUNT_NUMBER.dkr.ecr.$AWS_REGION.amazonaws.com/$REPO-$STACK_TYPE:$REF
          # Adding CloudWatch metric and alarm commands
          - aws cloudwatch put-metric-data --metric-name BuildSuccess --namespace "CTO.ai" --value 1
          - aws cloudwatch put-metric-alarm --alarm-name build_failure_alarm --alarm-description "Alarm for build failures" --metric-name BuildFailure --namespace "CTO.ai" --statistic SampleCount --period 300 --threshold 1 --comparison-operator GreaterThanOrEqualToThreshold --dimensions Name=Build,Value=Failed --evaluation-periods 1 --alarm-actions arn:aws:sns:us-west-1:$AWS_ACCOUNT_NUMBER:MyTopic
services:
  - name: sample-expressjs-service-aws-ecs-fargate:0.1.1
    description: A sample expressjs service
    run: node /ops/index.js
    port: [ '8080:8080' ]
    sdk: off
    domain: ""
    env:
      static:
        - PORT=8080
    events:
      - "github:workflows-sh/sample-expressjs-aws-ecs-fargate:pull_request.merged"
      - "github:workflows-sh/sample-expressjs-aws-ecs-fargate:pull_request.opened"
      - "github:workflows-sh/sample-expressjs-aws-ecs-fargate:pull_request.synchronize"
    trigger:
     - build
     - publish
     - start

Creating CloudWatch Alarms

Next, create CloudWatch Alarms to receive notifications. Navigate to the CloudWatch dashboard, create a new alarm, and define the conditions based on the CTO.ai metrics you have set up. Add the dashboard widget you want for your logs and metrics tooling.

Configuring Notification

Configure a notification to get alerted when the alarm state is triggered. AWS SNS (Simple Notification Service) can be used to set up email or SMS notifications.

Testing Your Setup

Finally, test your setup by triggering a build in CTO.ai using the ops build . You can also trigger your workflows when the event triggers you configured are met. Once the build completes, it should send the metrics to CloudWatch, and if the conditions are met, the alarm will trigger, sending a notification through the SNS topic you configured.


Conclusion

Setting up AWS CloudWatch alerts for CTO.ai can be a potent combination, ensuring that you are promptly notified of any issues in your CI/CD pipeline, thus maintaining a robust and reliable deployment process. By following the above steps, you can set up a monitoring and alerting system that will aid in proactive issue detection and resolution, bringing efficiency and stability to your deployment pipeline. Happy coding!

Ready to unlock the power of CTO.ai for your team? Schedule your consultation now with one of our experts today!