Table of contents
- Step 1: Setting Up the Source Repository
- Step 2: Setting Up with the AWS System Manager
- Step 3: Setting Up AWS CodeBuild
- 1. Create a CodeBuild Project Go to the AWS Management Console and navigate to CodeBuild. Click Create Build Project. Enter a name for your project (e.g., MyApp-Build).
- 2. Configure Source Select GitHub as the source provider and choose the repository you created earlier.
- 3. Buildspec File Define a buildspec.yml version of the build spec that can be used in your CodeBuild project:
- 4. IAM Role Ensure that your CodeBuild project has an IAM role with permissions to access the required services (CodeCommit, S3, CodeDeploy, etc.).
- Step 4: Attach IAM policies with SSM
- Step 5: Enable privileged
- Step 6: Configure Deployment with AWS CodePipeline
- Step 7: Automating Deployment with AWS CodeDeploy
- 1. Set Up EC2 Instances Launch EC2 instances and install the CodeDeploy agent:
- Now go to the terminal to create an agent using CLI follow this link: https://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-install-ubuntu.html
- 2. Create a CodeDeploy Application In the AWS Management Console, navigate to CodeDeploy and click Create Application. Name the application and select EC2/On-Premises as the compute platform.
- 3. create a role To grant permissions to any service to access another service in AWS, we use IAM roles. Roles are used for services. Create a role and select code deploy then click next and then next
- Select the role name and create the role.
- Now go to the modified IAM roles.
- After updating the IAM role we need to restart the service and check the status.
- Now go to the code deploy and create a deployment group.
- Now we have to create the deployment, go to the application, and create a deployment.
- Copy the latest commit ID and paste it into the commit ID
- Click on Create Deployment now
- Build is successful
- Now go to the code pipeline and edit tab Go to the build stage and add a stage. Set the name as code-deploy then add an action group
- After done click on the save button
- Finally, the application is running :)
- Conclusion
In modern DevOps workflows, Continuous Integration and Continuous Deployment (CI/CD) pipelines are essential for automating software build, test, and deployment processes. AWS offers several fully managed services that streamline CI/CD workflows: AWS CodePipeline for orchestrating the process, AWS CodeBuild for building and testing code, and AWS CodeDeploy for automating the deployment across various compute services like EC2, ECS, and Lambda.
This blog post will create a complete CI/CD pipeline using AWS CodePipeline, CodeBuild, and CodeDeploy. This tutorial will help you automate the process of building, testing, and deploying an application to an EC2 instance.
Step 1: Setting Up the Source Repository
To begin, let’s set up a source repository on GitHub.
1. Create a Repository:
Go to GitHub and create a new repository for your application. Add the sample application code "https://github.com/MaryamMairaj123/AWS-DevOps/aws-devops-main/day-14" 2. Create a Personal Access Token: In GitHub, navigate to Settings > Developer Settings > Personal Access Tokens and generate a token with the necessary permissions. This will allow AWS to connect to your GitHub repository. 3. Configure GitHub in AWS CodePipeline: Next, you’ll authorize AWS CodePipeline to connect to your GitHub repository.
Step 2: Setting Up with the AWS System Manager
Go to the systems manager to secure sensitive information for docker registering. In the systems manager go to the parameters stores and click on Create parameters.
I have created three parameters.
Step 3: Setting Up AWS CodeBuild
AWS CodeBuild compiles your code, runs unit tests, and produces deployable artifacts.
1. Create a CodeBuild Project Go to the AWS Management Console and navigate to CodeBuild. Click Create Build Project. Enter a name for your project (e.g., MyApp-Build).
2. Configure Source Select GitHub as the source provider and choose the repository you created earlier.
3. Buildspec File Define a buildspec.yml version of the build spec that can be used in your CodeBuild project:
version: 0.2
env:
parameter-store:
DOCKER_REGISTRY_USERNAME: "/myapp/docker-credentials/username"
DOCKER_REGISTRY_PASSWORD: "/myapp/docker-credentials/password"
DOCKER_REGISTRY_URL: "/myapp/docker-registry/url"
phases:
install:
runtime-versions:
python: 3.11
commands:
- pip install -r aws-devops-main/day-14/simple-python-app/requirements.txt
pre_build:
commands:
- echo "Logging in to Docker registry..."
- echo "$DOCKER_REGISTRY_PASSWORD" | docker login "$DOCKER_REGISTRY_URL" --username "$DOCKER_REGISTRY_USERNAME" --password-stdin
build:
commands:
- cd aws-devops-main/day-14/simple-python-app
- echo "Building Docker Image"
- docker build -t "$DOCKER_REGISTRY_URL/$DOCKER_REGISTRY_USERNAME/python-flask-app-service:latest" .
- echo "Pushing Docker Image to Registry"
- docker push "$DOCKER_REGISTRY_URL/$DOCKER_REGISTRY_USERNAME/python-flask-app-service:latest"
post_build:
commands:
- echo "Build is Successful"
4. IAM Role Ensure that your CodeBuild project has an IAM role with permissions to access the required services (CodeCommit, S3, CodeDeploy, etc.).
Step 4: Attach IAM policies with SSM
Now go to the IAM Roles and attach the policies of SSM
Step 5: Enable privileged
Go to the project edit and enable privileged and update project.
Step 6: Configure Deployment with AWS CodePipeline
1. Go to the AWS code pipeline and create a new code pipeline
2. Configure Source
3. Add Build Stage
4. Skip the deployment stage for now and create the pipeline.
5. Here is the Docker image
The CI part is done here.
Now Deploying CD part.
Step 7: Automating Deployment with AWS CodeDeploy
AWS CodeDeploy automates the deployment of your code to EC2 instances.
1. Set Up EC2 Instances Launch EC2 instances and install the CodeDeploy agent:
Add Tags
Now go to the terminal to create an agent using CLI follow this link: https://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-install-ubuntu.html
sudo yum install ruby
sudo yum install wget
wget https://aws-codedeploy-us-east-1.s3.amazonaws.com/latest/install
chmod +x ./install
sudo ./install auto
sudo service codedeploy-agent start
systemctl status codedeploy-agent
2. Create a CodeDeploy Application In the AWS Management Console, navigate to CodeDeploy and click Create Application. Name the application and select EC2/On-Premises as the compute platform.
3. create a role To grant permissions to any service to access another service in AWS, we use IAM roles. Roles are used for services. Create a role and select code deploy then click next and then next
Select the role name and create the role.
Now go to the modified IAM roles.
After updating the IAM role we need to restart the service and check the status.
Now go to the code deploy and create a deployment group.
Now we have to create the deployment, go to the application, and create a deployment.
Copy the latest commit ID and paste it into the commit ID
Click on Create Deployment now
Build is successful
Now go to the code pipeline and edit tab Go to the build stage and add a stage. Set the name as code-deploy then add an action group
After done click on the save button
Finally, the application is running :)
Conclusion
Following this guide, you've successfully set up a CI/CD pipeline using AWS CodePipeline, CodeBuild, and CodeDeploy. This automated pipeline allows you to integrate, build, and deploy your application efficiently. As you refine your skills, consider adding additional stages for testing or integrating security checks.
Happy automating!