Skip to main content
Welcome — this guide walks through creating an AWS CodePipeline that builds and pushes a container image for a Flask-based login microservice. You’ll learn how to:
  • Verify the Flask application routes are present.
  • Add a Dockerfile to containerize the app.
  • Add a buildspec.yml so CodeBuild can build and push the image to ECR.
  • Create the ECR repository and configure a CodePipeline that pulls from CodeCommit and invokes CodeBuild.
This workflow assumes your source is hosted in AWS CodeCommit and that you will use AWS CodeBuild to build and push the Docker image to Amazon ECR. Prepare the Flask application routes (app.py) Confirm these route handlers exist in your application (for example, in app.py). They are the minimal routes used by this microservice:
This example checks passwords directly in the database and may use plaintext passwords. For production, always store passwords hashed (e.g., bcrypt) and use secure authentication flows. Also validate and sanitize inputs to prevent SQL injection — use parameterized queries and an ORM when possible.
Files to add to the repository
  • Dockerfile — containerize the Flask app.
  • buildspec.yml — instructs CodeBuild how to build the image, authenticate to ECR, push the image, and emit imagedefinitions.json used by deployment stages.
  • Application code (app.py, templates, requirements.txt, etc.) — already present in your repo.
Create the Dockerfile Add a Dockerfile at the repository root to build the container image. A minimal example:
This image shows an AWS Cloud9 development environment with a file directory on the left, a code editor in the center displaying a Dockerfile, and a terminal at the bottom.
What the Dockerfile does
  • Uses an official Python slim base image.
  • Establishes /usr/src/app as the working directory.
  • Copies your source into the image.
  • Installs Python dependencies from requirements.txt.
  • Exposes port 5000 and configures Flask environment variables.
  • Launches the Flask app using flask run.
Create the buildspec.yml Create buildspec.yml at the repo root. This file controls CodeBuild phases: authenticate to ECR, build and tag the image, push tags, and produce imagedefinitions.json for downstream deploy steps. Note: Replace the example ECR repository URI and account ID with your own. In this example, 666234783044.dkr.ecr.eu-central-1.amazonaws.com/kodekloud-login-page is used as a placeholder.
Use aws ecr get-login-password (AWS CLI v2) instead of the deprecated aws ecr get-login. Ensure environment variables such as AWS_DEFAULT_REGION and REPOSITORY_URI are provided in the CodeBuild project settings or as build environment variables.
Environment variables used by the build Create the ECR repository If you don’t have an ECR repo yet, create one in the ECR console:
  1. Open the Amazon ECR service in the AWS Console.
  2. Click “Create repository”.
  3. Enter the repository name (for example, kodekloud-login-page) and create it.
  4. Copy the repository URI (for example, 666234783044.dkr.ecr.eu-central-1.amazonaws.com/kodekloud-login-page) and set it as the REPOSITORY_URI environment variable for your CodeBuild project.
Commit and push your changes to CodeCommit From your Cloud9 environment or a local terminal, add the new files and push to your CodeCommit repository:
Go to CodeCommit to verify the files Open the AWS CodeCommit console and confirm Dockerfile and buildspec.yml appear in the repository.
The image shows an AWS CodeCommit console displaying a list of repositories, including details like name, last modified date, and cloning options. The interface has navigation options for different AWS developer tools like CodeArtifact, CodeBuild, and CodePipeline on the left.
Create the CodePipeline Steps to create a simple pipeline that builds and pushes your image:
  1. Open AWS CodePipeline.
  2. Click “Create pipeline”.
  3. Provide a pipeline name such as login-page-microservice.
  4. For Source provider choose CodeCommit, select your repository (e.g., login-page-microservice) and branch (e.g., master).
  5. For Build provider choose AWS CodeBuild and select (or create) a CodeBuild project. Ensure the project references the repository and has REPOSITORY_URI and AWS_DEFAULT_REGION configured as environment variables.
  6. (Optional) Skip the Deploy stage for now — you can add an ECS/EKS/CloudFormation/CodeDeploy stage later.
  7. Create the pipeline.
When configuring the source stage, select the repository and branch. The UI appears like this:
The image shows an AWS CodePipeline interface where the "Add source stage" step is being set up, allowing the user to select a source provider, repository name, branch name, and set change detection options.
When creating or selecting the build stage, verify the build project and configure environment variables and the buildspec if necessary:
The image shows an AWS CodePipeline interface with a form to add a build stage, allowing users to select a build provider and configure options for a build project. It includes the option to add environment variables and choose between a single or batch build.
Pipeline execution and build After pipeline creation, CodePipeline will detect the source change and trigger the pipeline automatically. The sequence:
  • Source stage: CodeCommit triggers when you push changes.
  • Build stage: CodeBuild runs buildspec.yml to build, tag, and push Docker images to ECR.
  • (Optional) Deploy stage: consume imagedefinitions.json to update tasks or manifests.
You can view pipeline progress in the console and stream CodeBuild logs to see build output and Docker push progress. The pipeline UI shows the status for each stage:
The image shows an AWS CodePipeline interface with a project named "login-page-microservice." The pipeline has successfully completed the source stage and is currently in progress on the build stage.
Validate the image in ECR When the build completes successfully:
  1. Open the Amazon ECR console.
  2. Navigate to your repository and verify that the pushed image tags exist (for example, latest and a commit-based tag like abc1234-latest).
  3. Use the image URI when deploying to ECS, EKS, or EC2.
Next steps and deployment options With images available in ECR you can deploy via:
  • Amazon ECS (Fargate or EC2 launch types) using a task definition and service.
  • Amazon EKS by updating your Kubernetes Deployment image.
  • EC2 / Docker by pulling the image on an instance and running it.
Refer to the documentation for detailed deployment steps for your chosen platform. Links and References

Watch Video