May 18, 2022

Blog @ Munaf Sheikh

Latest news from tech-feeds around the world.

Platform Engineering With Pulumi (Part 3): Platform and Application Deployment With GitOps Automation

Great post from our friends at Source link

In Episode 1 of this blog series, we built an AWS landing zone for our React/Node.js application, using Pulumi. In Episode 2  we built a simple React app and an Express API server. We manually deployed the app on the landing zone. In this episode, we will automate it with GitActions and CodeDeploy.

For the sample application used in this blog, I have used Multi-repo. There is a huge debate on Mono vs Multi Repos. I might blog about that separately, but for now let’s assume Multi repo is the best for this application and go ahead :-D.

Before we configure the GitActions, we have to modify the infrastructure code to include Infra code for the creation of code deploy configurations.

Modifying the Infra Code To Create CodeDeploy

In Episode 1, we did not create the CodeDeploy Configurations. Find below the modifications done in the Python code to create appropriate Roles and CodeDeploy configurations. Let’s walk through the code (please refer to my GitHub for more latest code.)

Creating appropriate Roles and CodeDeploy configurations

In the above code, We are creating a Role, to allow the CodeDeploy service to have access, and attaching appropriate access policies (AmazonEC2FullAccess, AWSCodeDeployFullAccess, AdministratorAccess, AWSCodeDeployRole).

Let’s now create the CodeDeploy application

Creating Codeploy application

In the above code, we are creating a CodeDeploy application. Let’s now create deployment groups.


Creating deployment groups

In the above code, we are creating a CodeDeploy Deployment Group, with the Role that we created. This deployment group will be used to deploy API code (Node.js/Express). The below code created a CodeDeploy Deployment Group to deploy the React application. Both the deployment groups are created under the same CodeDeploy Application

CodeDeploy Deployment Group to deploy the React application

Let’s now run the code withpulumi up.

GitOps Deploying Node.js API Application

Now that we have all the infrastructure ready.

Lets first create a CodeDeploy Configuration file (appspec.yml) in our Node.js application.

Creating CodeDeploy Configuration file in Node.js application

In the appspec.yml, we are configuring the source and destination (on EC2) folders. This configuration is used by CodeDeploy to deploy the application files in the appropriate folder (/home/ec2-user/contacts-api).

We are also configuring 2 Hooks

Before Install Hook: This is a shell script that we are asking CodeDeploy to run, before deploying the application code. In this shell script, we will set up the environment required. The following is the code for setup.sh. In this shell script, we are installing NVM and creating a directory for CodeDeploy to deploy the source files:Installing NVM and creating a directory

Application Start Hook: Which is another shell script that we are asking CodeDeploy to run to start the application after deploying the application code. The following is the code for appStart.sh, where we are setting up the nvm environment variables, running npm installto install all the dependencies, and then running our application.

Setting up NVM environment and dependencies to run the application

Let’s now build a GitOps pipeline for the NodeJS application. The following is the code for GitActions, which gets triggered on pull request and push.
Building a GitOps pipeline for the NodeJS application

The above code is very simple. We are using a ubuntu-latest VM instance, setting the AWS credentials (the AWS_IAM_ACCESS_KEY, AWS_SECRET_ACCESS_KEY and AWS_REGION configurations are already configured in GitHub under Settings->Secrets. Here is a screenshot).

Setting the AWS credentials


We are then triggering the appropriate CodeDeploy deployment group, that we have configured using Pulumi in the above section.

Now that we have all the pipeline code, let’s test it by running the GitActions Workflow. The following animated gif shows the execution of the workflow and the deployment.

Let’s now automate the deployment of the React app.

GitOps Deploying React Application

To deploy React, the steps are similar, We first configure the AWS key, region, and secret in GitHub secrets.

Deploying React application

Let’s create the GitActions workflow. The following is the code:


Creating the GitActions workflow

This code is very similar to how we deployed Node.js except that the deployment group we are using for React is different. We are using pulumi-blog-app-codedeploy-deploymentgroup.

Let’s add the appspec.yml in the React root folder. The following is the source code. Similar to what we did with the Node.js application, we have 2 hooks, one to set up the environment and the other script to start the application.

Adding the appspec.yml in the React root folder

Let’s create the startup.sh, where we set up the environment before we deploy the application.

Creating startup.sh to set up environment for application deployment

In the above code we are installing nvm, and making sure contacts-app the folder is created.

The following is the code for running the application.

Code for running the application

In the above script, we are installing pm2 and serve, building the application, and running the application. These are the exact steps we followed in Episode 2.

Let’s now execute the workflow. The following is the screen capture video of the workflow execution.

Now we have all the pipelines configured whenever there are changes to the application code, the respective GitAction workflow will deploy the application to the EC2 instance.

We need one more GitOps pipeline for the Infrastructure code. I thought of covering that in this episode..but it has already become a long one. So I will be publishing the same in the next Episode.

Until then take care and Have fun, hope this was helpful 😀 

#Platform #Engineering #Pulumi #Part #Platform #Application #Deployment #GitOps #Automation