Skip to content

Pakoke/learn-terraform-circleci

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

93 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LinkedIn CircleCI


Logo

Blue Green Deployment

This project is going to build our entire infrastructure to deploy an ECS Cluster with a dotnetapi container on it
Explore the docs »

View Demo · Report Bug · Request Feature

Table of Contents

  1. About The Project
  2. Getting Started
  3. Usage
  4. Roadmap
  5. Contributing
  6. License
  7. Contact
  8. Acknowledgements

About The Project

This project is about showing how to fully an AWS insfrastructure usingd a Circle pipeline through Terraform, Docker, Dotnet and CircleCI Orbs. On this project, we are going to build diferents AWS components which it will end up having an ECS cluster with a Dotnet api service on it. In addition to that and thanks to CircleCI, we are going to do a CodeDeploy deployment which it is going to help us deploy the latest version of the code without interrupting the any service of our apps. At the of this Blue Green deployment, we are going destroy all of our resource in our AWS to save all the cost.

Here's a blank template to get started: To avoid retyping too much info. Do a search and replace with your text editor for the following: Pakoke, learn-terraform-circleci, twitter_handle, email, project_title, project_description

Built With

Getting Started

This project and all the resources will help you to build and deploy the infrastructure. The component deployed are going to be the enough ones to have a fully ECS cluster using EC2 instances using an Autoscaling group.

Prerequisites

This is what you need to initialize to set your pipeline on CircleCI.

  • Git

    Windows

    choco install git -y

    Ubuntu

    sudo apt-get update
    sudo apt-get install git -y
  • AWS Account

  • CircleCI Account

  • AWS Client

    Windows

    choco install awscli -y

    Ubuntu

    sudo apt-get update
    sudo apt-get install awscli -y
  • Terraform

    Windows

    choco install terraform -y

    Ubuntu

    sudo wget https://releases.hashicorp.com/terraform/0.14.7/terraform_0.14.7_linux_amd64.zip
    sudo unzip terraform_0.14.7_linux_amd64.zip
    sudo mv terraform /usr/local/bin/

Installation

  1. Clone the repo
    git clone https://github.com/Pakoke/learn-terraform-circleci.git
    
  2. Go to the folder infrastructure.init
    cd /learn-terraform-circleci/infrastructure.init
    
  3. Configure your AWS credentials. For the sake of this example, I recommend to create an user with Admin permissions.
    aws configure
    
  4. Initialize the Terraform project and apply the plan to get all the information that we will need for set up our pipeline.
    terraform init
    terraform apply --auto-approve
    
  5. Get all the outputs that it shows Terraform at the end of the apply. You should see something like this:
    AWS_USER_NAME = "xxxx"
    DEFAULT_ACCOUNT_ID = "xxxxxx"
    DEFAULT_AWS_REGION = "xx-xxxx-x"
    DEFAULT_ECR_ACCOUNT_URL = "xxxxx.dkr.ecr.xx-xxxx-x.amazonaws.com"
    S3_TERRAFORM_STATE = "xxxxxxxxxxx"
    
  6. Open your CircleCI account and import the config.yml from the folder .circleci. This file is our pipeline on CircleCI, the one that it is going to be on charge of creating all of our resources.
  7. As soon as you finish to import the CircleCI pipeline, go to Project Settings and go to the section Environment Variables. On this section, we need to configure the ones that we have on the image below. The value that we need to put are the ones obtained on the step 5

project_settings

  1. As you can see, there are two variables that Terraform is not getting for us. Those two variables are sensitive information so in order to obtain those we need to go to AWS Management Console and go the IAM service. On that service we need to go to User and click on the name that Terraform show us on the step 5 AWS_USER_NAME = "xxxx". After that, you just need generate a secret key and access key to finish configuring your CircleCI environments variables.

  2. Now there is only one step more to finish the set up of our pipeline. This set up is to configure our Terraform backend and save our state accross steps on our pipeline. To do this, we need to search the files version.tf and replace the string put your s3 bucket with the one obtained on the terraform output from the step 5 S3_TERRAFORM_STATE = "xxxxxxxxxxx". As soon as you finish to replace it, commit your changes and push it. This last action it will automatically push the first pipeline.

Usage

At this point, you will have your pipeline set up and running without any issue. To see that it is working and you have your entire infrastructure created, we need to understand each set of steps to really see that we have everything built in our AWS account.

On our pipeline we are going to have four sections

  1. Apply steps: This section will consist on building the main infrastructure which it is going to consist in a ECS cluster, a VPC and an Application Load Balancer. You can test this part going to the terraform step and get the output there. You will see an URL which it is going to return a HEALTHY message. Note: CircleCI ofuscate environment variables that's why we see the string .*********.. In this case, that string correspond to the region itself.

aws_loadbalancer_endpoint

aws_loadbalancer

  1. Build and Push image steps: This section will handle the creation of our docker image. To do this, we will use an Orb provided by CircleCI to help us build a docker image. This docker image is located inside of our apps/dotnetapi/src folder. This image is going to update automatically to ECR, our repository of images which later on will be used by our cluster to deploy the app.

buildandpushimage

  1. Apply Apps setps: This part of our deployment will handle the installation of our Target Group and task and services for our ECS Cluster. This will built all the components to connect our app to the load balancer. In addition to that, it will build a CodeDeploy deploy configuration which it is going to managed our BlueGreen deployment for our Apps. In this section, we need to take into account one variable which we will use every time that we push some code. This variable will let Terraform know what of the Target Group of our Balancer is the active. aws-deploy-status: type: string default: "blue" This variable can be change on our circleci pipeline configuration so everytime that we are going to do a change on our application we need to change this variable to let Terraform knows on what status it is.

  2. Blue Green Deployment steps: This step is going execute our Blue Green deployment using CodeDeploy. CodeDeploy is another service of AWS which itis a fully managed deployment service that automates software deployments to a variety of compute services such as Amazon EC2, AWS Fargate, AWS Lambda, and your on-premises servers.

codedeploybluegreen

As soon as it finish, we will see something like the image below. The process is simple, using the two target group and the deployment configuration CodeDeploy itself is going to switch the traffic between those not after deploy our new service into our ECS cluster. After that we will end up having as primary the other Target Group and the other one as a secondary Target Group.

codedeploybluegreen_finish

To test that we are deployment the latest version, we only need to do the next changes. First, we need to wait until we finish to deploy everything. To know when it finish, we are only going to check the url and the swagger page. If you see the swagger page, we are good to proceed. Then, we need to do the next changes to our code and our pipeline.

On our config.yml, change this variable. This variable is going to help Terraform knows in what status we are and which of the two Target Group is the active one.

  aws-deploy-status:
    type: string
    default: "blue" -> "green"

On our Dotnet api application, change the next line on the file WeatherForecastController.cs to the next above.

        private static readonly string[] Summaries = new[]
        {
            // Comment the next line and uncomment the next one below            
            // This will help to see how the blue green deployment install the latest code on our ecs cluster
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
            // "Nevado","Caluroso","Nublado","Tormentoso"
        };

As soon as you do those two changes, push it into our pipeline and approved the steps. When the step blue-green-deployment finish, you will see the changes on our API. You can check specifically the changes, you only need to go to the url http://urlelb/WeatherForecast

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Francisco Javier Ruiz - @twitter_handle - jrdt1991@gmail.com

Project Link: https://github.com/Pakoke/learn-terraform-circleci

About

Supplemental repository for Learn content on CircleCI

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • HCL 84.9%
  • C# 13.7%
  • Dockerfile 1.4%