As a web developer, your work may include several complex tasks such as code deployment automation to production servers or if you work in a group and face issues while uploading your code to a live website, then this blog post is a solution to overcome all such issues.
When using FTP, you must have overwritten the code of your co-developer, an issue that is quite common and happens with most of us. This guide will show you the code deployment automation with AWS CodeCommit, CodeDeploy, and Pipeline services.
The Technological Overview:
- AWS CodeCommit: We will use CodeCommit to maintain our repositories (Web Content/Code Files) that will hold all project files and commit logs.
- EC2 Server: We will use EC2 as our production server, it will contain the live site and code for your website visitors.
- AWS CodeDeploy: It will hold the application to publish your code to the EC2 server. We will also create a Deployment Group that will hold the name/tag of the targeted EC2 instance where we wish to publish our code.
- Pipeline: We will select CodeCommit as our source and CodeDeploy as our deploy stage. Therefore, whenever any code changes are detected in CodeCommit, the pipeline will run and push those code changes to CodeDeploy (EC2 Instance).
Code Deployment Automation: Explained in Detail:
Creating an IAM and Git User for Code Deployment Automation
We will first need to create an IAM user and Git User that will make the code changes and publish them on the deployment server.
- Create an IAM User
- Allow Access to: AWSCodeCommitFullAccess, AWSCodeDeployFullAccess, AWSCodeCommitPowerUser. You will find this option in the “Attach existing policies directly” tab.
- Now, log in with this IAM user.
- Click on Security Credentials from the top right corner.
- Click on the AWS CodeCommit credentials tab.
- Under the “HTTPS Git credentials for AWS CodeCommit” section, click on the Generate Credential button.
- Now, copy that username and password in a safe place. This will be your Git’s main username and password to push/pull code changes.
Creating a Repository in CodeCommit
Now, let’s create a repository that will hold your exact code.
- Go to CodeCommit Dashboard from the IAM User.
- At the top right corner, select the region where you want to create your repository.
- Click on Create repository, give it a name, description, and click on create.
- Under the “Clone the repository” section, you will see a URL of your repository. Save it in a safe place. It will look like: https://git-codecommit.ap-south-1.amazonaws.com/v1/repos/HarryTest
Uploading your project file for the first time
- Open the command prompt of your local system where your original code resides.
- In the command prompt, navigate to the path where your code resides.
- Now, run the following commands to push your code to the AWS repository for the first time:
git config –local init.defaultBranch main
git config –global init.defaultBranch main
git add . ## this will commit all files of the project
git commit –m “Initial commit”
git push https://git-codecommit.ap-south-1.amazonaws.com/v1/repos/HarryTest –all
- Create EC2 Instance & Install CodeDeploy Agent
- Create an Instance role
- Open your AWS IAM Console.
- Go to the Roles tab >> Create role.
- Under Select type of trusted entity, select AWS service. Under ‘Choose a use case’, select EC2. Under ‘Select your use case’, choose EC2. Choose Next: Permissions.
- Search for and select the policy named AmazonEC2RoleforAWSCodeDeploy, and then choose Next: Tags.
- Choose Next: Review. Enter a name for the role (for example, EC2InstanceRole) and click on Create.
- Create an EC2 instance
- Go to the EC2 console. Click on Launch Instance.
- Choose an Amazon Machine Image (AMI), locate Amazon Linux 2 AMI (HVM), SSD Volume Type, and then choose Select. (This AMI is labelled “Free tier eligible” and can be found at the top of the list.)
- Choose an Instance Type page, choose the free tier eligible t2.micro type as the hardware configuration for your instance, and then choose Next: Configure Instance Details.
- Configure Instance Details page, do the following:
- In the Number of instances, enter 1.
- In Auto-assign Public IP, choose Enable.
- In the IAM role, choose the IAM role you created in the previous procedure (for example, EC2InstanceRole).
- Expand Advanced Details, and in the User data field, enter the following:
yum -y update
yum install -y ruby
yum install -y aws-cli
chmod +x ./install
- Leave the rest of the items on Step 3: Configure Instance Details page unchanged. Choose Next: Add Storage.
- Leave Step 4: Add Storage page unchanged, and then choose Next: Add Tags.
- Choose Add Tag. In Key, enter Name, and in Value, enter MyCodePipelineDemo. Choose Next: Configure Security Group. Later, you create a CodeDeploy application that deploys the sample application to this instance. CodeDeploy selects instances to deploy based on the tags that are attached to instances.
- Configure Security Group page, do the following:
- Next to Assign a security group, select Create a new security group.
- In the row for SSH, under Source, choose My IP.
- Choose Add Rule, choose HTTP, and then under Source, choose My IP
- Choose Review and Launch.
- On the Review Instance Launch page, choose Launch. When prompted for a key pair, select ‘Proceed without a key pair’.
- Choose View Instances to close the confirmation page and return to the console.
- You can view the status of the launch on the Instances page. When you launch an instance, its initial state is ‘pending’. After the instance starts, its state changes to ‘running’, and it receives a public DNS name. (If the Public DNS column is not displayed, choose the Show/Hide icon, and then select Public DNS.)
- It can take a few minutes for the instance to be ready for you to connect to it. View the information in the Status Checks column to see if your instance has passed its status checks.
Create an application in CodeDeploy
- Create a CodeDeploy service role
- Open the IAM console at https://console.aws.amazon.com/iam/
- From the console dashboard, choose Roles.
- Choose to Create role.
- Under Select type of trusted entity, select AWS service. Under Choose a use case, select CodeDeploy. Under Select your use case, choose CodeDeploy. Choose Next: Permissions. The AWSCodeDeployRole managed policy is already attached to the role.
- Choose Next: Tags, and Next: Review.
- Enter a name for the role (for example, CodeDeployRole), and then choose to Create role.
- Create an application in CodeDeploy
- Open the CodeDeploy console at https://console.aws.amazon.com/codedeploy
- If the Applications page does not appear, on the menu, choose Applications.
- Choose to Create application.
- In the Application name, enter MyDemoApplication.
- In Compute Platform, choose EC2/On-premises.
- Choose to Create an application.
- Create a deployment group in CodeDeploy
- On the page that displays your application, choose to Create deployment group.
- In the Deployment group name, enter MyDemoDeploymentGroup.
- In-Service Role, choose the service role you created earlier (for example, CodeDeployRole).
- Under Deployment type, choose In-place.
- Under 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, MyCodePipelineDemo).
- Under Deployment configuration, choose CodeDeployDefault.OneAtaTime.
- Under Load Balancer, make sure Enable load balancing is not selected. You do not need to set up a load balancer or choose a target group for this example.
- Expand the Advanced section. Under Alarms, if any alarms are listed, choose Ignore alarm configuration.
- Choose to Create deployment group.
Create a Pipeline
- Create a Pipeline
- Sign in to the AWS Management Console and open the CodePipeline console at http://console.aws.amazon.com/codesuite/codepipeline/home
- Open the CodePipeline console at https://console.aws.amazon.com/codepipeline/
- Select Create pipeline.
- In Step 1: Select pipeline settings, in the Pipeline name, enter MyFirstPipeline.
- In the Service role, choose the New service role to allow CodePipeline to create a service role in IAM.
- Leave the settings under Advanced settings at their defaults, and then select Next.
- In Step 2: Add source stage, in Source provider, select CodeCommit.
In Repository name, select the name of the CodeCommit repository you created in step 1: Create a CodeCommit repository. In Branch name, choose main, and then select 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.
- Select Next.
- In Step 3: Add build stage, select Skip build stage, and then accept the warning message by choosing Skip again. Select Next.
Note: In this tutorial, you are deploying code that requires a no-build service, so you can skip this step. However, if your source code needs to be built before it is deployed to instances, you can configure CodeBuild in this step.
- In Step 4: Add deploy stage, in Deploy provider, select CodeDeploy.
In the Application name, select MyDemoApplication. In the Deployment group, select MyDemoDeploymentGroup, and then select Next step.
- In Step 5: Review the information and then select 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 view progress, ‘success’ and ‘failure’ messages as the CodePipeline sample deploys the webpage to the Amazon EC2 instance in the CodeDeploy deployment.
You are all set now! Go to Deploy ⇒ Deployments. You will see the logs of all deployments that are successful and the ones that have failed.
To verify the changes on the site, open your EC2 Instance, search for your public DNS, which will appear as: http://ec2-3-108-40-48.ap-south-1.compute.amazonaws.com.
Once you browse it, you will see your live pages.
You have successfully completed the Code Deployment Automation. You can now push new code changes to your repository and publish them on your site automatically. If you still face any issues, feel free to comment here.