In this guide, CodePipeline is utilized to deploy code stored in a CodeCommit repository to a solitary Amazon EC2 instance. The pipeline activates upon pushing changes to the CodeCommit repository, facilitating the deployment of modifications to an Amazon EC2 instance through CodeDeploy as the designated deployment service.
The pipeline has two stages:
• A source stage (Source) for your CodeCommit source action.
• A deployment stage (Deploy) for your CodeDeploy deployment action.
Step 1: Create a CodeCommit repository.
• Navigate to the CodeCommit console and click on create repository.
• Repository name-enter a name for your repository- MySampleRepo
• Description – Add a description for the repository or leave blank.
• Choose Create.
Step 2: Set up your local repository.
In this step, you will set up a local repository to connect to the remote CodeCommit repository.
• Git Bash in the location that you want to set up as your local repository. (Ensure you have git installed and already created Git credentials for your IAM user -
https://docs.aws.amazon.com/console/codecommit/connect-gc-np)
• With your new repository open in the console, choose Clone URL on the top right of the page, and then choose Clone HTTPS or Clone SSH to copy the command based on the connection type.
• Run the command on your local machine (same location you Git bash earlier) to clone the repository.
• Copy the Sample application code to the local repository and run the following Git commands.
git status - To display the status of the files
git add . – To stage the files for tracking
git commit – To commit the files
git push – To push files to remote repository
• Confirm that the codes have been pushed to the remote repository, Check the CodeCommit repository.
Step 3: Create Roles.
In this step we will create 2 roles.
1. Instance Role: This role allows the installation and management of the code deploy agent on the instance and also allow the instance to be managed by SSM.
To create an instance role
- Navigate to IAM console and choose Roles.
- Choose Create role.
- Under Select type of trusted entity, select AWS service.
- Under Choose a use case, select EC2.
- Choose Next: Permissions.
- Search for and select the policy named- AmazonEC2RoleforAWSCodeDeploy.
- Search for and select the policy named-AmazonSSMManagedInstanceCore.
- Choose Next: Tags.
- Choose Next: Review. Enter a name for the role- EC2instanceprofilerole.
- Choose Create role.
2. CodeDeploy Service Role: This role allows CodeDeploy to perform deployments.
To create CodeDeploy Service role
- Navigate to IAM console and choose Roles.
- Choose Create role.
- Under Select type of trusted entity, select AWS service.
- Under Choose a use case, select CodeDeploy.
- Choose Next
- Enter a name for the role- CodedeployRole
- Choose Create role.
Step 4: Launch Amazon EC2 Linux Instance.
To launch an instance
- Navigate to Amazon EC2 console, choose Instances, and select Launch instances.
- Name- enter MySamplePipelineServer. This assigns the instance a tag Key of Name and a tag Value of MySamplePipelineServer.
- Application and OS Images (Amazon Machine Image)- Select Amazon Linux 2023 AMI -Free tier eligible.
- Instance type- Choose t2. micro - Free tier eligible.
Key pair (login)- Choose a key pair or create one. You can also choose Proceed without a key pair.
Network settings- do the following.
Auto-assign Public IP, make sure the status is Enable.
Security group, choose Create a new security group.
In the row for SSH, under Source type, choose My IP.
Choose Add security group, choose HTTP, and then under Source type, choose Anywhere.
- Advanced details- In IAM instance profile, choose the IAM role you created in the previous procedure -EC2Instanceprofilerole
- Summary- under Number of instances, enter 1.
Choose Launch instance.
Step 5: Create and Application and Deployment Group in CodeDeploy
To create an application group
- Navigate to CodeDeploy console and choose Applications.
- Choose Create application.
- Application name- enter MySampleApplication.
- Compute Platform- Choose EC2/On-premises.
- Choose Create application
To create a deployment group in CodeDeploy
A deployment group is a resource that defines deployment-related settings like which instances to deploy to and how fast to deploy them.
- Choose Create deployment group.
- Deployment group name- enter MySampleDeploymentGroup.
- Service role- choose the service role you created earlier- Codedeployrole
- Deployment type- choose In-place.
- 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- MySamplePipelineServer.
- Agent configuration with AWS Systems Manager- choose Now and schedule updates.
- Deployment configuration- choose CodeDeployDefault.OneAtaTime.
- Load Balancer- make sure Enable load balancing is not selected. We do not need a load balancer or a target group for this project.
- Choose Create deployment group.
Step 6: Create a CodePipeline Pipeline
In this step, you create a pipeline that runs automatically when code is pushed to your CodeCommit repository.
To create a CodePipeline pipeline
- Navigate to CodePipeline console and choose Create pipeline.
- Choose pipeline settings, in Pipeline name- enter MySamplePipeline.
- Service role- choose New service role to allow CodePipeline create a service role in IAM.
- Leave the settings under Advanced settings at their defaults, and then choose Next.
- Add source stage, in Source provider- choose CodeCommit. In Repository name- choose the name of the CodeCommit repository you created in Step 1- MySampleRepo. In Branch name- choose master, and then choose Next step.
- Change detection options- leave the defaults. This allows CodePipeline to use Amazon CloudWatch Events to detect changes in your source repository.
- Choose Next.
- Add build stage, choose Skip build stage, and then accept the warning message by choosing Ok. Choose Next.
- Add deploy stage, in Deploy provider- choose CodeDeploy. In Application name- choose MySampleApplication. In Deployment group- choose MySampleDeploymentGroup, and then choose Next step.
- 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
To verify that your pipeline ran successfully
- Navigate to the Instance console, copy and paste the public DNS of the instance into the address bar of your web browser.
- The web page displays for the sample application you downloaded and pushed to your CodeCommit repository.
Congratulations! You just created a simple pipeline in CodePipeline.
Step 6: Modify the code in your CodeCommit Repository
Your pipeline is configured to run whenever code changes are made to your CodeCommit repository. In this step, you make changes to the HTML file that is part of the sample CodeDeploy application in the CodeCommit repository. When you push these changes, your pipeline runs again, and the changes you make are visible at the web address you accessed earlier.
- Navigate to the CodeCommit Repository
- Open the Index.html file and click Edit (Change the background colour to #FF0000 “red” and “Congratulations” to MySampleCodePipeline.
- Commit changes to Master by adding Name, email and commit message
- Click commit changes This will automatically trigger CodePipeline to deploy the new version of the application.
Refresh your browser to see the new changes made to your application.
Step 7: Clean Up
First, delete the pipeline, then the CodeDeploy application and its associated Amazon EC2 instance, and finally, the CodeCommit repository.
Thanks for Reading!!!
Top comments (0)