Blog

Jenkins Pipeline – A Tale of Automating ECS and RDS operations using Groovy

Picture of Iveta Paneva
Iveta Paneva
DevOps and Cloud Engineer
18.01.2024
Reading time: 3 mins.
Last Updated: 12.02.2024

Table of Contents

 This blog post will guide you through the creation of a Jenkins pipeline to automate AWS ECS (Elastic Container Service) and RDS (Relational Database Service) operations. The pipeline script is written in Groovy, the language commonly used in Jenkins.

Today, we’ll demonstrate one of the best practices for automating the process of initiating an ECS container and an RDS instance, complete with  an email notification in case of job failure. We will explore the steps one by one and conclude with the best approach.

The pipeline ensures that your ECS service updates and RDS instance checks are executed consistently, avoiding manual errors and promoting a seamless deployment process.

Here is the complete pipeline code and the explanation of the steps you will follow:

pipeline {
    agent any
    environment {
       AWSRegion='ca-central-1'
    }
    stages {
        stage('Starting --> ') {
            steps{
                script {
                        sh "aws ecs update-service --cluster my-ecs-cluster-name --service my-ecs-service-name --profile my-aws-profile-name --region ${AWSRegion} --desired-count 1 | grep desiredCount"                        
                        sh(script: '''#!/bin/bash
                            aws rds describe-db-instances --profile my-aws-profile-name --db-instance-identifier my-db-name --region ${AWSRegion} | grep available
                            if [ $? == 0 ];then
                                echo "RDS instance has already started!"
                            else
                                aws rds start-db-instance --profile my-aws-profile-name --db-instance-identifier my-db-name --region ${AWSRegion} | grep DBInstanceStatus
                            fi
                            '''.stripIndent())

                    }
                }
            }
        }
    }
    post{
        failure{
            mail to: 'my-email-address@email.com',
            subject: "Jenkins build:${currentBuild.currentResult}: ${env.JOB_NAME}",
            body: "${currentBuild.currentResult}: Job ${env.JOB_NAME}\nMore info can be found here: ${env.BUILD_URL}"
        }
    }
}

Tips: If you would like to stop the ECS and RDS, just replace “ –desired-count 1” instead of 1 to be set to  0, and for the RDS  instead of “start-db-instance” replace it to  “stop-db-instance

Now let’s break down the steps of the pipeline one by one:

  • The pipeline is configured to run on any available agent.
pipeline {
    agent any
  • Use of environment variables:

The AWS region is set as an environment variable for easy configuration.

environment {9
   AWSRegion='ca-central-1'  (you can replace it with the region you want)
}
  • The pipeline has a single stage named “Starting” with corresponding steps.
stages {
    stage('Starting --> ') {
        steps {
            // Pipeline steps go here
        }
    }
}
  • Update the ECS service count:

The pipeline begins with updating an ECS service, by adjusting the desired task count to 1 (means will start the container).

AWS CLI commands are used to achieve this:

sh "aws ecs update-service --cluster my-ecs-cluster-name --service my-ecs-service-name --profile my-aws-profile-name --region ${AWSRegion} --desired-count 1 | grep desiredCount"
  • Start the RDS instance:

Next, the pipeline checks the status of an RDS instance. If the instance is already available, it prints a message. Otherwise, it starts the RDS instance using the AWS CLI.

sh(script: '''
    aws rds describe-db-instances --profile my-aws-profile-name --db-instance-identifier my-db-name --region ${AWSRegion} | grep available
    if [ $? == 0 ];then
        echo "RDS instance has already started!"
    else
        aws rds start-db-instance --profile my-aws-profile-name --db-instance-identifier my-db-name --region ${AWSRegion} | grep DBInstanceStatus
    fi
'''.stripIndent())
  • Post-Build actions:

In case of a build failure, an email notification is sent to the specified email address:

post {

    failure {

        mail to: 'my-email-address@email.com',

        subject: "Jenkins build:${currentBuild.currentResult}: ${env.JOB_NAME}",

        body: "${currentBuild.currentResult}: Job ${env.JOB_NAME}\nMore info can be found here: ${env.BUILD_URL}"

    }

}

Example of the email:

Subject: Jenkins build:FAILURE: OPERATIONS/Multi-Instance-Stop

FAILURE: Job OPERATIONS/Multi-Instance-Stop

More info can be found here: https://X.X.X.X/job/PATH/TO/THE/JOB/Start-my-instances

Tips: One of the best aspects of doing such a job is that you can manipulate the execution date/time and what services can be started or stopped. By setting the cron in Jenkins you can minimize the workflow, so as to not increase your monthly expenses

In conclusion, the Groovy script and AWS CLI commands are easily adaptable, allowing you to tailor the pipeline to your AWS environment and specific requirements.

In essence, this pipeline is your ticket to a stress-free and festive AWS deployment experience. 

Let Jenkins handle the routine tasks of updating ECS services and checking RDS instances. 

No need to manually intervene – set it and forget it!

Leave a Reply

Your email address will not be published. Required fields are marked *

More Posts

This guide will walk you through deploying multiple AWS Lambda functions using Pulumi, an infrastructure as code tool that allows you to define and manage cloud resources using familiar programming...
Reading
Day 3 of Kubecon Day 3 of KubeCon + CloudNativeCon in Paris was a spring of fresh ideas, sustainability discussions, and ever-evolving cloud-native technologies. Here’s a deep dive into the...
Reading
Get In Touch
ITGix provides you with expert consultancy and tailored DevOps services to accelerate your business growth.
Newsletter for
Tech Experts
Join 12,000+ business leaders and engineers who receive blogs, e-Books, and case studies on emerging technology.