40
loading...
This website collects cookies to deliver better user experience
Have an AWS account with proper access.
And, most important this blog needs your time. Before implementing it, I request you to read it completely.
For the experiment, I am using a convention of using the name “BLOG-CICD”.
Don’t forget to delete all the resources once you are done with the experiment otherwise, it will incur charges.
Go to the IAM console.
Click on the user name to whom you want to give permissions.
Click on Security Credentials
Scroll down and go to HTTPS Git credentials for AWS CodeCommit
section.
Click on Generate Credentials
and it will create code commit credentials for that IAM user.
Go to the IAM console.
Click on the user for whom you created code commit credentials earlier.
Under the Permissions tab, go to add policy.
Search for AWSCodeCommitPowerUser
policy and add it.
In the next step, we can configure our instance. No need to go into more details here. Keep everything default and click on the next
Add Tag, this is a very important step. With the help of this tag, CodeBuild will identify the deployment instance. We will use it later. I have used name tag only to keep it simple as shown in the diagram below and click next
.
For now, keep it as it is for making it simple and click on next
.
Next, create or use the existing key pair for your instance and launch instance
.
buildspec.yaml
appspec.yaml
buildspec.yaml
appspec.yaml
Click on Getting started
under Code build.
Click on create a project.
Give a project name.
Select Source provider
as AWS CodeCommit
under the source section.
Select your repository.
Select Reference type
as a branch.
Select Environment image under Environment section
Select your Operating system.
Amazon Linux 2
.Select Runtime as Standard.
Select Image. In my case, I am using aws/codebuild/amazonlinux2-x86_64-standard:3.0
Click on the radio button of New Service Role, it will create a new role for you with the required permissions.
buildspec.yaml
file:# Do not change version. This is the version of aws buildspec, not the version of your buildspec file.
version: 0.2
phases:
pre_build:
commands:
#installs dependencies
- "Commands for Installing Dependencies" ## E.g. npm install / pip install -r requirements.txt / etc.
build:
commands:
- echo Production Build started
- "Build Commands here" ## E.g. npm run build / python setup.py / etc.
post_build:
commands:
- echo Build completed
# Include build files required for your application to run.
artifacts:
files:
- "file names here" ## E.g. dist/*, appspec.yaml (make sure to include this one), build/*, etc.
# This is an appspec.yml template file for use with an EC2/On-Premises deployment in CodeDeploy.
version: 0.0
# Specify "os: linux" if this revision targets Amazon Linux
os: linux
# Place artifacts file inside EC2 instance
files:
- source: /
destination: /home/ec2-user/server
permissions:
- object: /
pattern: "**"
owner: ec2-user
group: ec2-user
hooks:
# During the BeforeInstall deployment lifecycle event, run the commands
# in the script specified in "location" which is under destination path.
BeforeInstall:
- location: relative_path_inside_your_project_folder/before_install.sh
timeout: 1000
runas: root
# During the AfterInstall deployment lifecycle event, run the commands
# in the script specified in "location".
AfterInstall:
- location: relative_path_inside_your_project_folder/after_install.sh
timeout: 1600
runas: root
# During the ApplicationStart deployment lifecycle event, run the commands
# in the script specified in "location".
ApplicationStart:
- location: relative_path_inside_your_project_folder/app_start.sh
timeout: 300
runas: root
In this deployment, I am going with the buildspec file. So, for this, I have already saved my buildspec.yaml file in my source code. If you are moving with the insert build commands option
, just insert the necessary command here and click on Continue to pipeline.
Leave the Batch configuration and Artifacts section as it is.
You may enable logs, it will be very helpful in resolving the bugs if something wrong happens. But this is optional.
Click on Getting Started
of CodeDeploy.
Click on Create application
.
Give application name.
Select Compute platform for your pipeline. I am deploying the application in EC2. So, I am going with EC2/On-premises
option. If you want to deploy your application on another platform select accordingly.
Click on Create application
.
Now we need to create a deployment group.
For the deployment group, we will use the EC2 instance created in the earlier step.
Click on the Deployment group.
Give Deployment Name.
Select the service role you created earlier.
Select Deployment type as In-place.
In the environment configuration, select Amazon EC2 instances
.
Here, we need to search for the instance/instances based on the tag on which you want to deploy the application. In my case, I am using a name tag for searching. Once you give your tag you can see the number of matched instances.
Coming to section Agent configuration with AWS Systems Manager
. We need a code deploy agent to be configured in your EC2 machine. Either we can configure it manually. But why should we do it manually? AWS is there to help us. So, under the section Agent configuration with AWS Systems Manage
, select Now and schedule updates
.
Select the period for the code deploy agent to update. So, after the given period CodeDeploy agent will be automatically updated.
Select CodeDeployDefault.AllAtOnce
in the deployment setting.
Skip load balancer for this pipeline.
Finally, click on the Create Deployment Group
Click on Getting Started
under CodePipeline.
Click on Create pipeline
.
Give pipeline name.
It is better to create a new service role. Select a new service role. AWS will create a role with the required permissions.
In the next step add source provider as AWS CodeCommit.
Select your Repository name from the drop-down menu.
Select branch name as well from the drop-down menu.
Go with the recommended Amazon Cloudwatch Events as change detection options.
Select CodePipeline default as the output artifact format.
Choose AWS CodeBuild as a build provider.
Cross-check your region here.
Select your Project created earlier.
Select build type as Single build.
Now come to Add deploy stage.
Select Deploy provider as AWS CodeDeploy.
Verify your region as well.
Select your application from the drop-down menu.
Select the deployment group created earlier.
Finally, Review your Pipeline and click on Create pipeline
.
Configure Code deploy agent manually to your EC2 instance. Check the steps below how to do?
Run the below command one by one.
REMEMBER: I am using a Linux machine. If you are using a different OS, please follow this:install code deploy agent
sudo yum update
sudo yum install ruby
sudo yum install wget
cd /home/ec2-user
Download code deploy agent with the below command.
wget https://<bucket-name>.s3.<region>-identifier.amazonaws.com/latest/install
Run the installation script by the below command
chmod +x ./install
Check status of Code deploy agent. You will get status as running.
sudo service codedeploy-agent status
If not running, make it running by the below command.
sudo service codedeploy-agent status
Trigger the pipeline again and wait for the job to be complete.
ssh into the server and check the logs with the below command and take action properly.
tail -f /var/log/aws/codedeploy-agent/codedeploy-agent.log
40