The Road from Code to Cloud (Part 1)

Introduction

In the realm of modern software development, the journey from writing code to deployment has undergone a profound transformation. It's no longer about manual steps and ad-hoc practices but rather a systematic, automated, and efficient process. This article will guide you through the steps of setting up a code deployment pipeline, empowering you to move your code seamlessly from development to production. So, let's explore the world of deployment pipelines and how they can enhance our software development workflow.

For this article, we will be creating a code deployment pipeline for a server developed using the Node.js framework and will primarily utilize various AWS services, including CodeCommit, IAM, CodeDeploy, CodePipeline, EC2, S3, Load Balancers, CloudFront, Route 53, and Certificate Manager, to achieve this.


Section 1: Setting up CodeCommit repository

AWS CodeCommit is a fully managed source control service. It is compatible with Git and be easily integrated with other AWS Services.

To create a repository in CodeCommit, follow the following steps:

  • Open the CodeCommit console at https://ap-south-1.console.aws.amazon.com/codesuite/codecommit/
  • In the region selector, select the AWS Region where you want to create the repository and pipeline.
  • On the repositories page, click Create Repository.
  • On the Create Repository Page, under Repository Name, enter a name for your repository.
  • Then, click Create.

Creating CodeCommit repository


Now, the next step is to set up local repository to connect to your remote CodeCommit repository. Follow these steps:

  • The first step is to install Git on your local machine.
  • The next step is to clone your remote CodeCommit Repository into your local machine. To do that, you first need to generate Git credentials for AWS CodeCommit.
  • Navigate to IAM console. In the navigation pane, choose Users, and from the list of users, select your IAM user (the user for which credentials need to be generated). (Note: In case, if no IAM user exists, create a user with the AWS PowerUserAccess Policy attached).

Generating Git credentials for AWS CodeCommit


  • Now, on the user details page, select the Security Credentials tab, and under HTTPS Git Credentials for AWS CodeCommit, click Generate. Save the credentials generated by IAM.
  • Open the CodeCommit console and navigate to your newly created repository. Choose Clone URL and copy the HTTPS URL (https://git-codecommit.ap-south-1.amazonaws.com/v1/repos/SampleRepo).
  • Open your terminal, run the git clone command with the HTTPS clone URL you copied to clone the repository. You will be prompted for the username and password for the repository. Enter the credentials generated in the above step.
  • You are now successfully connected to your remote CodeCommit repository from your local machine. (Note: your pipeline will fetch source code from this repository when it runs.)

Successfully connected to CodeCommit repository from local machine

Section 2: Adding sample code to our CodeCommit Repository

In this section, we will upload sample Node.js code along with the required files needed for deploying our code with CodeDeploy in our remote CodeCommit Repository. You can download the sample application code from this link (https://github.com/dhruvpithwa/sampleLinuxCode).?

The sample application code contains the following files for deploying with CodeDeploy

  • appspec.yml: An application specification file in YAML format used by CodeDeploy to manage a deployment (https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file.html)
  • scripts folder: This folder contains the following shell scripts for installing dependencies and starting/stopping the sample application for automated deployment, such as install_dependencies, start_server, stop_server, etc.
  • index.js: It contains sample Node.js code (Hello World).
  • package.json: This file contains Node dependencies.


Now, we will unzip the downloaded files (sample application code) into our local repository. Be sure to place the files directly into the local repository) (Note: The directory and file hierarchy should resemble the following.)


Directory structure of local repository


We will use Git commands to upload our files to the remote repository.

git add .
git commit -m "adding sample application code"
git push        
Uploading files from local repository to remote CodeCommit repository


Now, the files which were downloaded and added to the local repository have now been added to the main branch of our CodeCommit SampleRepo repository and are ready to be included in the pipeline.


Section 3: Setting up AWS EC2 Linux Instance

In this section, we will create an AWS EC2 instance to deploy our sample application. However, before proceeding with the creation, we need to establish an instance role that allows for the installation and management of the CodeDeploy Agent on the instance. The CodeDeploy agent is a software package that enables an instance to be used in CodeDeploy deployments. Additionally, you will attach policies that permit the instance to fetch files used by the CodeDeploy agent for deploying your application and allow the instance to be managed by SSM.


To create an instance role, follow these steps:

  • Open the IAM console at?https://console.aws.amazon.com/iam/
  • From the console dashboard, select Roles.
  • Click Create role.
  • Under?Select type of trusted entity, choose AWS service. Under?Select your use case, choose?EC2. Then, click?Next: Permissions.
  • Search for and select the policy named?AmazonEC2RoleforAWSCodeDeploy.
  • Search for and select the policy named?AmazonSSMManagedInstanceCore. Click Next: Tags.
  • Proceed to Next Review and enter a name for the role , such as?EC2InstanceRole.
  • Finally click Create role.

EC2 instance role


Once the instance role is created, we will proceed with launching our EC2 instance.

  • Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
  • In the side navigation, select Instances, and then choose Launch instances from the top of the page.
  • In?the Name field, enter?CodePipelineDemo. This assigns the instance a tag?with a key?of?Name?and a value?of?CodePipelineDemo. Later, we will create a CodeDeploy application that deploys the sample application to this instance. CodeDeploy selects instances for deployment based on their tags.
  • Under?Application and OS Images (Amazon Machine Image), locate the?Amazon Linux?AMI option with the AWS logo, and ensure it is selected. (This AMI is described as the Amazon Linux 2 AMI (HVM) and is labelled "Free tier eligible".)
  • Under Instance type, select the t2.micro type, which is eligible for the free tier, as the hardware configuration for your instance.
  • Under?Key pair (login), choose a key pair or create one.
  • Under Network Settings, do the following. In Auto-assign Public IP, make sure the status is Enable.
  • Next to?Assign a security group, choose?Create a new security group.
  • In the row for?SSH, under?Source type, choose?My IP.
  • Expand?Advanced details. In?IAM instance profile, choose the IAM role we created in the previous procedure (for example,?EC2InstanceRole).
  • Under?Summary, in the Number of instances field, enter?1.
  • Click Launch instance.
  • You can monitor the launch status on the 'Instances' page. When an instance is launched, its initial state is 'pending.' After the instance starts, its state changes to 'running,' and it is assigned a public DNS name. (If the 'Public DNS' column is not displayed, click the 'Show/Hide' icon and then select 'Public DNS.')

EC2 instance launched

Section 4: Setting up CodeDeploy

In CodeDeploy, an application is a resource that contains the software application you want to deploy. Later, we can use this application with CodePipeline to automate deployments of the sample application to our EC2 instance.

?Before proceeding with CodeDeploy application creation, we will create a role that will allow CodeDeploy to perform deployments.

?

To create a CodeDeploy service role:

  • Open the IAM console at?https://console.aws.amazon.com/iam/).
  • From the console dashboard, select Roles.
  • Click Create role.
  • Under?Select trusted entity, choose?AWS service. Under?Use case, choose?CodeDeploy. Choose?Next. The?AWSCodeDeployRole?managed policy is already attached to the role.
  • Click Next.
  • Enter a name for the role (for example,?CodeDeployRole), and then choose?Create role.

CodeDeploy service role


Once our service role is successfully created, we can proceed with creating an application in CodeDeploy.

  • Open the CodeDeploy console at?https://console.aws.amazon.com/codedeploy.
  • If the?Applications?page does not appear, go to the menu and select?Applications.
  • Click Create application.
  • In?Application name, enter?CodeDeployDemo.
  • In?Compute Platform, choose?EC2/On-premises.
  • Click Create application.?


Now, for configuring deployment related settings, such as which instances to deploy and how fast to deploy, we need a deployment group. Follow these steps to create deployment a group in CodeDeploy:?

  • On the page displaying your application, click Create deployment group.
  • In?Deployment group name, enter?CodeDeploymentDemoGroup.
  • In?Service role, select the service role you created earlier (for example,?CodeDeployRole).
  • Under?Deployment type, choose?In-place.
  • In?Environment configuration, choose?Amazon EC2 Instances. In the?Key?field, enter?Name. In the?Value?field, enter the name you used to tag the instance (for example,?CodePipelineDemo).
  • In Agent configuration with AWS Systems Manager, select Now and schedule updates. This will install the agent on the instance. The Linux instance is already configured with the SSM agent and will now be updated with the CodeDeploy agent.
  • Under?Deployment configuration, choose?CodeDeployDefault.OneAtaTime.
  • Uncheck Enable Load Balancing option for now.
  • Click Create deployment group.

Creating CodeDeploy deployment group

Section 5: Setting up CodePipeline

In this section, we will create an AWS CodePipeline pipeline that runs automatically when code is pushed to the CodeCommit repository.


To create a CodePipeline pipeline, follow following steps:

  • Open the CodePipeline console at?https://console.aws.amazon.com/codepipeline
  • Click Create pipeline.??
  • In?Step 1: Choose pipeline settings, in?Pipeline name, enter?SamplePipeline. In?Service role, choose?New service role?to allow CodePipeline to create a service role in IAM. Leave the settings under?Advanced settings?at their defaults, and then choose?Next.
  • In?Step 2: Add source stage, in?Source provider, choose?CodeCommit. In?Repository name, choose the name of the CodeCommit repository you created in Step 1: Create a CodeCommit repository. In?Branch name, choose?main, and then choose?Next step.?After you select the repository name and branch, a message displays the Amazon CloudWatch Events rule to be created for this pipeline.?Under?Change detection options, leave the defaults. This allows CodePipeline to use Amazon CloudWatch Events to detect changes in your source repository.?Choose?Next.
  • In?Step 3: Add build stage, choose?Skip build stage, and then accept the warning message by choosing?Skip?again. Choose?Next.??
  • In?Step 4: Add deploy stage, in?Deploy provider, choose?CodeDeploy. In?Application name, choose?CodeDeployDemo. In?Deployment group, choose?CodeDeploymentDemoGroup, and then choose?Next step.
  • In?Step 5: Review, review the information, and then choose?Create pipeline.?


The pipeline starts running after it is created. It downloads the code from your CodeCommit repository and creates a CodeDeploy deployment to your EC2 instance. You can monitor the progress and review success and failure messages as the CodePipeline sample deploys the application to the Amazon EC2 instance in the CodeDeploy deployment.

CodePipeline running successfully
CodeDeploy deployment stages successful


At this point, we have successfully created a pipeline, that automatically run when new code changes are pushed to the CodeCommit repository and deploys the changes to the EC2 instance.


Section 6: Verifying Code Deployment Pipeline

In this section, we will verify whether our EC2 instance with the sample application code is now accessible over the internet or not. Our sample application code consists of Node.js server. When accessed over the internet we should receive a “Hello World” response.


So, lets verify the changes:

  • Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
  • Select the instance (CodePipelineDemo)
  • Copy the Public IPv4 DNS and paste it into your browser. (You will encounter an error such as "This site can't be reached." The reason is that our server is running on port 3000, but we don't have inbound rules configured for that port.)

Accessing sample application over browser

??????

  • To fix this issue, navigate to the Security tab and open the Security Group and Edit Inbound rules.
  • Add new rule, select Type Custom TCP, Port range 3000 and then select 0.0.0.0/0 (IPv4 addresses) (Note: another approach to solve this problem is to configure port forwarding in the EC2 instance)
  • Save the rules.


Now, refresh the browser tab with the IPv4 public DNS, and you will see "Hello World!" as the response. Hurrah! We have successfully made our code live!

Sample application is live and deployed successfully


So far, we have configured AWS Code Deployment Pipeline which will automatically run (when there will be any code changes in the remote repository for the main branch) and deploy our application using CodeDeploy on EC2.

In the next part, we will have a look on how we can configure our domain (Route53, Certificate Manager), enable load balancing (LoadBalancers, Target Group), create cloud front distribution (CloudFront) and set up SSL (HTTPS) for the application currently deployed on EC2 instance.? So, stay tuned!!!




要查看或添加评论,请登录

社区洞察

其他会员也浏览了