AWS Codepipeline for EC2

AWS Codepipeline for EC2

Github Link for Source Code: https://github.com/AmanPathak-dev/AWS-Codepipeline.git
I’ve used Project1 for this tutorial.

THE CONCEPT

CODE COMMIT

  1. Create a Repository in the CodeCommit.

2. Click on “Clone URL” to get the HTTP link of the repository.

But to clone the repo there is a pre-requisite that you should have the credentials of the code commit to clone the repository and other functionality regarding git(push, pull, etc).

3. So, Create the User by going into the IAM.

4. You don’t need to do anything like add a user to the group. But as you need this user’s credentials for code commit. So, there is one more thing to play with the code commit which is IAM Roles and policies which will be added in the next step.

As you can see, the user has been created. Now click on it and you will get option of “credentials”.

5. Now, click on the Generate credentials button.

6. Here you get the Credentials for the Code Commit. Also, download the .csv file for future reference because once this credentials will disappear, you can’t see it again.

7. Here, you can see the credentials from the downloaded .csv file. Keep it for future reference.

8. Now, As the AWS CodeCommit is private So, we need to clone the repo in our local and then push the changes. But to do that, you must have the Policy which name “AWSCodeCommitPowerUser”.

This will help you into git commands authorization.

9. Now, When we will clone the repo in VSCode Editor. Then, there is an authentication will be required where you need to pass the username and password which we’ll take from the .csv file or if you don’t remote Step 6 Screenshot.

Here, we cloned the repo successfully.

10. Now, I have selected a simple project from tooplate website and move it here.

11. Here, I’ve pushed the Project on Code Commit Repo.

CODE BUILD

12. Now, The Next process is to Create the Code Build.

So, to do that We will provide the requirements as mentioned below.

Here, I am creating the role for the Code Build because Code Build needs some access such as of CodeCommit, S3, and CodeDeploy.

Here, I didn’t have provided the file name of buildspec because it will search for the file name builspec.yml by default. So, you don’t need to mention that.

Here, We need to Configure the artifacts stored path. (Related to the last Screenshot).

13. Now, We will write the Code Build file named buildspec.yml and push it on the Code Commit Repository.

Here, you can see that the buildspec.yml file has been pushed to the CodeCommit Repository.

14. Now, We are ready to start the build by clicking on “Start Build”.

Here you can see the build details in “Phase Details” where the build has been successful. So, we can proceed with CodeDeploy where the code will deploy to the EC2 Instance.

CODE DEPLOY

15. Go into the AWS CodeDeploy Service and click on “Create application”.

16. Enter the application name to your application As we are deploying the Code on the EC2 Server. So we will select the EC2/On-premises option under the Compute Platform Section and click on Create application.

17. After creating the application, we have to create a deployment group by clicking on “create deployment group”.

18. But before creating a deployment group, we have to create a server where the code will be deployed.

So, for that, we will go into the AWS EC2 Service, and I’ve created Ubuntu 22.04 Server.

Here I’ve passed user_data to install the codedeploy-agent which is a must and the dependencies must be compatible with the selected server.

If you get any error at the start of your code deploy. So, the root cause of it will be your script where you didn’t have installed codedeploy-agent properly hence the codedeploy-agent is not running.

You can use this script if your server is Ubuntu22.04.

19. After Installing the codedeploy-agent, We will create the deployment group.

Here the other main thing is the service role where the Code deploy needs access to the selected service to deploy the code on the EC2 Server.

Here you can see the selected Policies name which is required to deploy the code on the Server.

20. Now, select the EC2 Sever where you’ve installed the codedeploy-agent.

Here you’ve to choose Never for “Install AWS CodeDeploy Agent” because we have already installed the codedeploy-agent manually and disabled the load balancer as well.

21. After Creating the deployment group, We will create the last thing here which is deployment only.

Here, we will select the Deployment group which we have created already and the Revision Tye will be “My application is stored in Amazon S3” because we have uploaded the artifacts in our AWS S3 Bucket.

Also, we will get the S3 URL where the artifact has been uploaded and the zip file name as well.

22. To Deploy the Code using AWS Code Deploy, We need another file named appspec.yml where we write the script to deploy the code on the server.

23. Now, We’ll push the appspec file to the Code Commit Repository.

24. But there is one more thing we have to do.

As you know that the Code Deploy needs access to the EC2 Server to deploy the code.

But we need to also add the IAM Role to the EC2 Server where EC2 will give permission to the Codedeploy to deploy the code.

So, to add the IAM role to the EC2 Instance.

Select the EC2 Instance -> Click on Security -> Manage IAM Role and add the role which we created in Screenshot 25.

25. Here, we have created the ECInstanceRole for the deployment of the code.

26. After creating the IAM role you’ve to restart the codedeploy-agent service again.

27. Now, Before starting the Code deploy deployment. We need to Code Build again because the Code has been updated for appspec file. In the previous successful build, the appspec file has not been uploaded or included in the artifacts.

Now, The new artifacts have been created in a zip file. So you can proceed with the CodeDeploy.

28. Here, When we start the deployment process. So As you can see, It is successfully completed.

29. Now, Copy the Public IP of your EC2 Server where you’ve deployed the code. You’ll see your website or content as an output.

AWS CODE PIPELINE

30. But there is an issue, what If I want to automate the entire process?

So, to do that. We’ll create AWS Codepipeline and with the help of this, We’ll Automate the entire process.

To do that, Go on to the AWS CodePipeline Service and click on “Create Pipeline”.

31. Now, Give the configuration given below for the Pipeline Settings.

32. Now Add the CodeCommit as the Source provider and provide the required configurations as given below.

33. Now, Add the Code Build as a build provider and provide the required configurations as given below.

34. Now, Add the AWS CodeDeploy as Deploy Provider and provide the required configurations as given below.

35. The Final Configuration will look like this.

36. Now, you have to just run the pipeline and here you can see the pipeline has run successfully.

Hope, You’ve Understood Everything.