This code repository is part of the Amazon EKS Cluster Setup tutorial: "Building a CI/CD Pipeline for Amazon EKS Workloads with Native AWS Services". It includes a sample application, a dockerfile, configuration files for AWS CodeBuild and AWS CodePipeline, and Hashicorp Terraform Infrastructure as Code to set up the CI/CD pipeline.
This repository is dedicated to setting up an end-to-end CI/CD pipeline for hosting and testing application code, building a container image from the code, pushing and storing the container image to Elastic Container Registry, and deploying this container on EKS as a deployment. It includes the following components:
- Preparing the dedicated VPC for the cluster: As a basis, we created a dedicated Amazon VPC to host the EKS cluster. The VPC uses the CIDR
10.0.0.0/16
, 3 private, and 3 public subnets. We create also an Internet Gateway and a NAT Gateway. You can find the VPC configuration in the ekc_cluster.tf file. - Creating the EKS Cluster: We built an EKS Cluster with a managed node group with a minimum size 1 and maximum size 5, and one instance type of
t3.small
. - Storing the Application Code to AWS CodeCommit: Utilize the AWS CodeCommit service to create a Git repository for the application code. A new code push on a specific branch of this git repository triggers CodePipeline, initiating the CI/CD flow. We use AWS EventBridge and create an event rule that detects new commits to the repository and triggers the pipeline.
- Testing the Application Code with AWS CodeBuild: As a second stage of the CI/CD pipeline, we leverage CodeBuild to lint and test our application code. The configurations for linting and testing are provided as YAML files to AWS CodeBuild. CodeBuild expects a build specification (buildspec) file with the collection of commands and related sesttings that CodeBuild uses to run a specific stage in the pipeline.
- Building a Container Image with AWS CodeBuild and storing it in Elastic Container Registry: As a third stage of the CI/CD pipeline, we build the container image based on the provided Dockerfile and the application source code with CodeBuild. The configuration file for building the Docker container image is proved as YAML file to CodeBuild. After successfully building the container image, we push it on the Elastic Container Registry repository.
- Deploying the Container Image on EKS: Finally, as the last step of the CI/CD pipeline, we trigger an action with CodeBuild to deploy the newly created container image to EKS as a deployment. For this deployment, we use the helm Kubernetes package manager in the CodeBuild environment and provide the YAML deployment charts for our application.
- First, open a terminal and execute the command below to clone the demo code repository locally:
git clone https://github.com/build-on-aws/cicd-pipeline-for-containers.git
- Next, navigate to the top directory of the demo code repository:
cd cicd-pipeline-for-containers
- Before applying the Terraform manifests, we have to initialize the project. Run the terraform init command, which performs several different initialization steps in order to prepare the current working directory for use with Terraform:
terraform init
- After the successful project initialization, run this command to create the necessary AWS components and infrastructure:
terraform apply
-
Navigate via the AWS CodeCommit Console to your newly created AWS CodeCommit repository
demo-codecommit-repo
and clone the empty code repository locally. -
Then copy these 3 directories from the first code repository you cloned to the new one and push the code changes.
cp -r cicd-pipeline-for-containers-demo/helm_charts demo-codecommit-repo/helm_charts/
cp -r cicd-pipeline-for-containers-demo/pipeline_files demo-codecommit-repo/pipeline_files/
cp -r cicd-pipeline-for-containers-demo/sample-cluster-app demo-codecommit-repo/sample-cluster-app/
- Then, navigate to the demo-codecommit-repo directory, add, commit, and push the changes:
cd demo-codecommit-repo
git add helm_charts/ pipeline_files/ sample-cluster-app/
git commit -m "Add demo application code, CodeBuild config files, and helm charts"
git push origin main
- After successfully pushing the new code, navigate via the AWS Console to AWS CodePipeline to see it in action. Your new code, should trigger a new release change and kickstart the CI/CD process.
To clean up, run on the initial cloned repository:
terraform destroy
The Sample Cluster Application used for the purposes of this demo was taken from this GitHub Repository: eks-container-pipeline-cdk-template/sample-cluster-app.
See CONTRIBUTING for more information.
This library is licensed under the MIT-0 License. See the LICENSE file.