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:

pipeline {
    agent any

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)
}
stages {
    stage('Starting --> ') {
        steps {
            // Pipeline steps go here
        }
    }
}

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"

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())

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!

Explore ITGix services and learn more about our expertise.

Leave a Reply