5.1. Notes
5.1. Notes
CodePipeline Overview
Overview
Amazon CodePipeline is a visual workflow tool that helps orchestrate Continuous Integration and
Continuous Deployment (CI/CD) processes within AWS. It allows you to automate the entire release
process, enabling frequent and reliable updates.
1. Sources
o Supported Sources:
2. Build Phase
o Tools:
AWS: CodeBuild.
3. Test Phase
o Tools:
4. Deployment
o Supported Services:
5. Invoke
o Use: Invoke Lambda functions or Step Functions as part of the pipeline for additional
automation or processing.
o Actions: Specific tasks within each stage like building, testing, or deploying.
o Example Workflow:
7. Manual Approval
o Use Case: Add a manual approval step before critical stages like deploying to
production.
Artifact Creation:
o CodePipeline generates artifacts at each stage, which are stored in Amazon S3.
o These artifacts are passed between stages, enabling seamless workflow progression.
Example Workflow:
2. Artifact Creation: CodePipeline extracts the code, creates an artifact, and stores it in
S3.
3. CodeBuild: CodePipeline passes the artifact to CodeBuild, which builds the code.
4. Deployment: The build artifacts are passed to CodeDeploy, which handles the
deployment.
Troubleshooting CodePipeline
2. Permissions Issues
o IAM Role: Ensure CodePipeline has the correct IAM permissions to interact with
other AWS services (e.g., CodeCommit, CodeBuild).
3. Auditing
o CloudTrail: Use CloudTrail to audit AWS API calls made by CodePipeline, especially if
you encounter permission-related issues.
Before diving into the hands-on with CodePipeline, we need to set up two AWS Elastic Beanstalk
environments. These environments will be used to deploy updates during the CodePipeline
exercises.
Steps to Create Elastic Beanstalk Environments
o Key Pair: No key pair required (you can skip this option)
After a few moments, you should see a "Congratulations" message indicating the environment has
been successfully created.
This will create a second environment for production, which we will use in the CodePipeline hands-
on session.
Once you're done with the hands-on exercise, don't forget to delete the environments to avoid
unnecessary charges for running EC2 instances.
In this lecture, we'll create our first CI/CD pipeline using AWS CodePipeline. The pipeline will
automate the process of deploying code from a GitHub repository to two AWS Elastic Beanstalk
environments: one for development and one for production.
Step-by-Step Guide
1. Create the Pipeline
o Name: MyFirstPipeline
o Service Role: Create a new service role, which allows CodePipeline to perform
necessary actions.
2. Connect to GitHub
o Trigger: Set a trigger to start the pipeline on any push to the main branch.
o Build Provider: Skip this step for now (you can explore it later).
o After the initial deployment, edit the pipeline to add a new stage for production
deployment.
2. Add a Stage
1. Trigger a Deployment
o Make a change to the index.html file in your GitHub repository, e.g., change the
background color from blue to red.
o The pipeline will automatically start, deploying the new version to the development
environment.
o Review the changes, and if satisfied, approve the deployment to the production
environment.
o Verify that the changes, like the new background color, are reflected in production.
CodeBuild Overview
Purpose: CodeBuild is a fully managed continuous integration service that compiles source
code, runs tests, and produces deployable software packages.
Key Components
1. Source Providers:
o The buildspec.yml file is crucial. It must be located at the root of the source code
repository. This file contains build instructions.
o You can also manually insert build instructions into the console, but using
buildspec.yml is best practice and is important for the exam.
2. Build Process:
o Output Logs: Stored in Amazon S3 and CloudWatch Logs for later analysis.
3. Supported Languages:
o Pre-built images are available for languages like Java, Ruby, Python, Go, Node.js,
Android, .NET Core, and PHP.
o For other environments, you can extend a Docker image to support the language you
need.
o Container Environment: CodeBuild uses a container (Java, Go, etc.) to run the build
instructions from buildspec.yml.
o Artifacts: The output files from the build process are stored in an S3 bucket.
Buildspec.yml File
Environment Variables: Can be plaintext or pulled from SSM Parameter Store or Secrets
Manager.
Phases:
Exam Tips
Environment and Phases: Understand how phases in buildspec.yml relate to the build
process.
Caching and Artifacts: Be familiar with how caching and artifacts work.
Integration: Recognize how CodeBuild integrates with other AWS services like CloudWatch
and S3.
Objective: Create a CodeBuild project to test that the "Congratulations" message appears when
deploying an application.
2. Source Configuration:
o Set up the project to rebuild every time a code change is pushed to the repository
(PUSH).
3. Build Configuration:
Select Standard runtime with the latest image (e.g., standard 7.0).
4. Additional Configuration:
5. Buildspec Configuration:
o Use Buildspec.yaml file located in the root directory of the source code repository.
o No artifacts are required since this build is only for testing, not building anything.
6. Logging:
o The build process will begin by pulling the code from the GitHub repository.
8. Expected Outcome:
o The build will fail initially because the Buildspec.yaml file is not yet present in the
repository.
o This is expected, and the file will be added in the next step.
Objective: Fix the error in CodeBuild by creating a buildspec.yaml file and integrate CodeBuild with
CodePipeline for automated testing and deployment.
yaml
Copy code
version: 0.2
phases:
install:
commands:
- echo Installing Node.js latest
pre_build:
commands:
build:
commands:
post_build:
commands:
o The new build will automatically start because of the webhook between GitHub and
CodeBuild.
o Check the build status in CodeBuild. If the build is successful, it indicates that the
buildspec.yaml file is correctly configured.
o Remove the Primary Source Webhook Events to prevent automatic builds from
GitHub pushes.
2. Modify CodePipeline:
o Set the input artifact to SourceArtifact and select MyFirstBuild as the project.
o The pipeline should automatically trigger a build, and the build should fail because
the test (grep command) will not find "Congratulations" in the index.html.
o Edit the index.html file again, restoring "Congratulations" and adding some text (e.g.,
"Congratulations CodeBuild").
o The pipeline should pass this time, and the changes will be deployed to the
development environment.
5. Final Verification:
Summary:
The error in CodeBuild was fixed by creating and configuring the buildspec.yaml file.