This blog is created by Shruti Bhosekar and Tanaya Yalrute.
Learning Objectives:
Learn to create CodePipeline to deploy an app from CodeCommit repository.
Learn to create trigger CodePipeline when application changes are committed to code repository.
Learn to use CodeDeploy as a deployment service to deploy application on an EC2 instance.
Prerequisite:
Download the zip file name my-app.zip from here.
Step 1: Create Repository
In AWS console, in search bar, type CodeCommit and go to console.
In the left side panel, go to repository, click on Create repository.
In Create repository, under repository name, enter the name of the repository as
my-repo keep the rest as default and click on create.
After creating repository, select Clone URL on the top right of the page, and then choose Clone HTTPS. This copied URL is used to clone your repository to your local computer.
Step 2: Generate Credentials
Now, go to IAM console in a new tab, on the left side panel, click on Users.
Then click on the admin user.
Scroll down and go to security credentials.
In Security credentials, scroll down further and go to HTTPS Git credentials for AWS CodeCommit.
Click on Generate credentials.
Note: After we generate the credentials, click on download credentials for further use.
Step 3: Clone the repository
Now, go to CloudShell terminal, at the bottom left corner of your console.
In CloudShell terminal, enter the Clone URL address which you copied before to clone the repository and run it.
Note: After executing git clone command, configure it by entering your CodeCommit ‘Username’ and ‘Password’ that is available in CodeCommit Credentials file that you downloaded in previous step.
Enter the following command in CloudShell to clone to your repository as:
Step 4: Download the my-app.zip
You should download the zip file name my-app.zip from here (It's a same file that you downloaded in Prerequisite) and extract the file contents to the folder. Upload all these files in the my-repo created just now.
To do that, go to Cloudshell, in the CloudShell, go to Actions, click on Upload file to upload the files one at a time.
First upload the appspec.yml, index.html and LICENSE files to home directory.
Check whether the files are uploaded successfully by entering ls command.
Then move the files into your repository by running the following command as:
mv appspec.yml index.html LICENSE.txt my-repo
Go back to my-repo using cd my-repo command.
Check whether the files are uploaded in my-repo directory by entering ls command.
Now, create folder in my-repo directory and name it as scripts by entering the following command as:
mkdir scripts
Now, upload the following files in ‘scripts’ directory by entering the command as:
cd scripts
And move the files in my-repo/scripts like we did in previous step.
Also check whether the files are uploaded successfully by entering ls command.
These below files are need to be uploaded in scripts.
Now move the files in my-repo/scripts using command as:
mv install_dependencies start_server stop_server my-repo/scripts
Then run this command to go back from scripts folder- cd ..
Run the following command to stage all your files at once:
git add -A
After this we will configure the repository using our GitHub user name and email Id.
Replace the user.name by your USERNAME that you used while signing in into your GitHub:
git config --global user.name "USERNAME"
Similarly, replace user.email with your EMAIL ID that you used while signing in to your GitHub and run the following command:
git config --global user.email "EMAIL ID"
Then to commit all files enter the following command:
git commit -m "Add my application files"
In order to push the files from your local repo to your CodeCommit repository, use the following command:
git push
Note: Here configure with the USERNAME and PASSWORD that you created in step 2 from admin CodeCommit credentials.
Step 5: Create a role.
In AWS console, go to IAM, go to Roles and choose Create role.
In Select trusted entity, under Trusted entity type, select AWS service.
Under Use case, open the dropdown under of Service or use case, choose EC2.
Choose Next: Permissions.
Search and select the policy name as AmazonEC2RoleforAWSCodeDeploy.
Search and select the policy name as AmazonSSMManagedInstanceCore.
Choose Next.
Choose Next: Name, review, and create.
Enter a name for the role (for example, EC2-CodeDeploy-SSM).
Choose Create role.
Verify whether role is successfully created.
Step 6: Create a security group
Open EC2 console in new tab, in left side pane, select Security Groups.
In Create security group, under Basic details enter Security group name as my-app-instance-SG.
In Inbound rule, select Type as SSH and in Source type choose My IP.
Then click on Add rule, and again select the Type as HTTP, select Source type as My IP.
Keep the rest as default and click on Create security group.
Step 7: Create a EC2 instance
Go to instances, and choose Launch instances.
To create an EC2 instance you can refer to our blog (follow the tutorial up to step 6)
Under Name and tags, give name as my-app-instance
In Network settings, open dropdown under Subnet, and select availability zone as 1a.
Under Firewall, choose Select existing security group. And under Common security groups open dropdown and select my-app-instance-SG.
In Advanced details, under IAM instance profile, attach EC2 instance role that you created previously (EC2-CodeDeploy-SSM).
Keep rest as default and click on Launch instance.
Step 7: Create another Role
In AWS console, go to IAM, choose Roles.
Choose Create role.
Under Select trusted entity, choose AWS service.
Under Use case, open drop down and select CodeDeploy option.
After that, choose Next.
In Add permissions, keep everything default and click on Next.
Choose Next.
Give name for the role as CodeDeployRole, and then choose Create role.
Choose Create role.
Step 8: Create an application in CodeDeploy
Open AWS console in a new tab and go to CodeDeploy console.
In CodeDeploy console, in the left side pane, choose Applications.
Click on Create application.
In Create application, under Application configuration, specify the Application name as
my-app.
For Compute platform, Choose compute platform as EC2/On-premises.
Once the application is created successfully, on the same page, select Create deployment group.
In Create deployment group, under Deployment group, name enter my-deployment.
In Service role, enter a service role that you created previously (click on ARN of CodeDeployRole which will come automatically once you click on the box).
Scroll down to Deployment type, select In-place.
Under Environment configuration, check Amazon EC2 Instances and for Key enter Name and for Value enter your EC2 instance name(my-app-instance) that you created previously.
In Agent configuration with AWS Systems Manager, select Now and schedule updates.
In Deployment settings, under Deployment configuration, choose option CodeDeployDefault.OneAtATime from the drop down.
Uncheck the Enable load balancing.
Keep the rest as default and click on Create deployment group.
Step 9:
In AWS console, search and go to CodePipeline console in new tab.
Click on Create pipeline.
In Choose pipeline settings, under Pipeline settings, choose enter Pipeline name as
my-pipeline.
In Pipeline type, select V1.
In Service role, check New service role which creates a service role in IAM.
Keep everything rest as default, and click on Next.
Then in Add source stage, under Source provider, select AWS CodeCommit option.
In Repository name(my-repo), choose the name of the repository that you have already created, where you have pushed your source code (in CodeCommit).
In Branch name, choose master.
Then in Change detection options, choose a detection mode as Amazon CloudWatch Events (recommended).
Then choose Next.
In Add build stage, click on Skip build stage.
Then it will display a message of alert, click on skip.
In Add deploy stage, in Deploy provider choose the provider as AWS CodeDeploy option.
Select Application name as my-app.
In Deployment group, select my-deployment.
Click on Next.
Then finally Review the pipeline and all details and click on Create pipeline.
After pipeline is successfully created, you will see the status of the pipeline in Source as in progress as shown below:
In few minutes you will be able to see the status of the Source changes as Succeeded. This means the files added to the Source have successfully committed changes.
After few minutes, you can also see that the Deploy status has also Succeeded.
So, this means the files added to the Deploy have successfully committed the changes.
Step 10: Deploy application
In AWS console, go back to EC2 console where you have created the instance for pipeline.
Click on the instance ID, in summary, copy the Public IPv4 DNS and paste it in address bar of your web browser.
After you paste the address in address bar, a web page displays a sample application you downloaded and pushed to your CodeCommit repository.
Step 10:
Let us modify our application and re-deploy as version 2.To do that, download the new index file(index-2.html) from here.
Note: Replace and save it as index.html.
Now, go in Cloudshell terminal and remove the index.html file from my-repo using the following command:
rm index.html
Then check the index.html file is removed by running following command-
ls
Now, click on Actions and upload the updated downloaded index.html file.
Then run cd .. command to go back from my-repo folder to the local directory.
Again run the ls command to see whether index.html file is uploaded in local directory.
Move the file to my-repo folder by running following command-
mv index.html my-repo
Now go back to your repository that in my-repo by running the command as:
cd my-repo
And then run ls command to check whether the index.html file is uploaded in demo-repo successfully.
Run the Commit command that commits your changes to your CodeCommit repository by running the following commands:
git commit -am "Updated my application files"
To push your changes to your CodeCommit repository run the following command and enter the Username and Password got from your downloaded codecommit_credentials file.
Run the following command first:
git push
Go back to your CodePipeline,check the status of the Source and Deploy.
It should be appeared as Succeeded.
After Succeeded is displayed, refresh the page of your web browser and see whether the modified webpage is displayed.
Note: If you do not need to use the pipeline created further, do not forget to delete the repository, application, pipeline, roles and instance to avoid any AWS charges.
Was this document helpful? How can we make this document better. Please provide your insights. You can download PDF version for reference.
For your aws certification needs or for aws learning contact us.
Very easy to understand the concept of Code Pipeline & deploy an app from Code Commit
Easy to understand the concept of Code Pipeline & deploy an app from Code Commit
concept of Code Pipeline & deploy an app from Code Commit was understood
This blog shows how to create a CodePipeline to deploy an app from a CodeCommit repository. Easy to follow and very useful.
concept of Code Pipeline & deploy an app from Code Commit was understood