End-to-end project deployment of an application using CodeCommit, CodeArtifact, CodeBuild, CodeDeploy, CodePipeline.
Steps:
-
Create a IAM user with "CodecommitPowerUser" and "IAMUserChangePassword" permission
-
Create a user and download the .csv file
-
Open your created user and go to security tab and scrool down and generate HTTPS Git credentials for AWS CodeCommit and save the file.
-
Create a new instance
-
Open terminal
-
$sudo su
-
$apt-get update
-
$git clone
-
Go inside that directory
-
Enter your username and password from the .csv file which u downloaded (codecommit_credentials)
-
enter git commands: git status git add . git commit -m "first commit" git push origin master
-
go to CodeCommit and check the file pushed
-
Create a branch using "git checkout -b dev"
-
create a new file and push the file in dev branch
-
check CodeCommit and then click on Create pull request
-
Complete the pull request and merge the branches. This Completes the CodeCommit Part
-
Click on CodeBuild-> Create a build project
-
Select the repo under sources
-
For BuildSpec file, create a file in EC2 terminal. Find the file code below:
vi buildspec.yml version: 0.2
phases: install: commands: - echo Installing NGINX - sudo apt-get update - sudo apt-get install nginx -y
build: commands: - echo Build Started on $(date) - cp index.html /var/www/html/
post_build: commands: - echo Configuration NGINX
artifacts: files: - /var/www/html/index.html
git branch --> to know the working branch git checkout master--> to change the branch Note: If you get git error, type this commands,
-
git pull origin master
-
git config --global pull.rebase true
-
git pull origin master
-
git push origin master
-
Create CodeBuild
-
Click on Start Build
-
After your project is built successful , then go to build projects
-
select the project--> edit--> artifact
Follow these steps and go further.
Here completes your CodeBuild part.
Now go further for CodeDeploy part.
- Click on CodeDeploy
- Create a new application
- And select cloud platform as EC2
- next create a deployment group
- Create an IAM role with below mentioned permissions:
AmazonS3FullAccess AmazonEC2RoleforAWSCodeDeployLimited AWSCodeDeployRole AWSCodeDeployFullAccess AmazonEC2RoleforAWSCodeDeploy AmazonEC2FullAccess
after creating the role, go to trust relationship tab and edit the policy and add this new policy :
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": [ "codedeploy.amazonaws.com", "ec2.amazonaws.com" ] }, "Action": "sts:AssumeRole" } ] }
and save the role
-
Copy the ARN of the iam role and paste it under service role in deployment group
-
Select amazon EC2 instance under environment configuration
-
launch an ec2 instance , and then come back to deployment group and add that ec2 instance name
-
Uncheck the load balancer checkbox.
Finally deployment group is succesfully created.
Now you can go further.
Now connect to your old instance and ls buildspec.yml index.html sample.txt
- Create a new file vi appspec.yml version: 0.0 os: linux files:
- source: /
destination: /var/www/html
hooks:
AfterInstall:
- location: script/install_nginx.sh timeout: 300 runas: root ApplicationStart:
- location: script/start_nginx.sh timeout: 300 runas: root
- Create a directory mkdir scripts
vi install_nginx.sh
#!/bin/bash
sudo apt-get update sudo apt-get install -y nginx
vi start_nginx.sh
#!/bin/bash
sudo service nginx start
Save both the files and come back with cd ..
git status 3. and push all the files to CodeCommit
Now build your project again
- Go to CodeDeploy -> Create a deployment
- give the path of s3 zip file, and select zip
- create deployment
- Deployment will be in pending state
Create a new role ec2deployment with permissions: AmazonEC2FullAccess CodeDeployFullAccess S3FullAccess
- Now go back to your instance cat install.sh $sudo service codedeploy-agent restart