LinuxUnity LinuxUnity

Posts

Building an Automated CI/CD Pipeline from GitHub to AWS Elastic Beanstalk

Deploying source code directly to production after every change introduces significant risk. This guide shows how to build a multi-environment CI/CD pipeline on AWS that automatically deploys to Staging and requires manual approval before releasing to Production.

11/07/2026 9 min read 9 views

Z Zedx

AWS
In this article

In a production environment, deploying source code directly to the live system immediately after every change always carries significant risk. A bug that has not been thoroughly tested can affect users, cause service disruptions, or make it difficult for the operations team to roll back.

Therefore, a standard CI/CD pipeline typically requires a Staging environment for prior testing, along with a manual approval step before release to Production.

In this article, we will build a multi-environment CI/CD pipeline on AWS, starting from GitHub, building with AWS CodeBuild, orchestrating with AWS CodePipeline, deploying automatically to Staging, and deploying to Production only after Manual Approval.

System architecture overview

The model in this article will operate according to the following end-to-end flow:

The main stages in the pipeline:

Source Stage: The engineer pushes the source code to GitHub. AWS CodePipeline receives change events through a GitHub connection/webhook.

Build Stage: AWS CodeBuild initializes the build environment and reads the buildspec.yml, installs dependencies, runs the required commands, and packages the application.

Staging Deployment: After the build, the artifact is automatically deployed to the Staging environment on Elastic Beanstalk.

Manual Approval: The pipeline pauses for QA, the Tech Lead, or the release owner to review the application in Staging. If everything is working as expected, the reviewer clicks Approve.

Production Deployment: The application is released to the Production environment without requiring SSH access to the server or manual operations on the server.

Infrastructure Preparation

Before configuring the pipeline, we need to prepare the application deployment environment and the necessary IAM Roles so that the AWS services can work together securely.

1. Granting System Permissions with IAM Roles

A very common issue when running CI/CD labs on AWS is that the pipeline build succeeds but deployment fails due to insufficient permissions. With Elastic Beanstalk, we need to prepare 2 important roles in advance.

A. Service Role for Elastic Beanstalk

This role allows the Elastic Beanstalk service to manage and interact with related AWS resources such as Auto Scaling, Load Balancer, and health monitoring.

Create the role with the following information:

  • Service: Elastic Beanstalk
  • Use case: Elastic Beanstalk – Environment
  • Role name: aws-elasticbeanstalk-service-role

Policies to attach:

  • AWSElasticBeanstalkEnhancedHealth
  • AWSElasticBeanstalkManagedUpdatesCustomerRolePolicy

B. Instance Profile for EC2

When Elastic Beanstalk launches the underlying EC2 instances, those instances also need permissions to interact with the relevant AWS services.

Create a role with the following details:

  • Service: Elastic Beanstalk
  • Use case: Elastic Beanstalk – Compute
  • Role name: aws-elasticbeanstalk-ec2-role

Policies to attach:

  • AWSElasticBeanstalkWebTier
  • AWSElasticBeanstalkWorkerTier
  • AWSElasticBeanstalkMulticontainerDocker

2. Configure Staging and Production environments in Elastic Beanstalk

AWS provides several ways to deploy applications, depending on the architecture and the desired level of control: EC2 combined with an Auto Scaling Group, ECS/Fargate for containers, EKS for Kubernetes, AWS App Runner for simple web services/containers, Lambda for serverless applications, or Elastic Beanstalk for traditional web applications that need to be deployed quickly.

In this article, we will use Elastic Beanstalk as the deployment example. The reason is that Elastic Beanstalk integrates directly with CodePipeline and automatically manages underlying components such as EC2, the Load Balancer, Auto Scaling, and Health Check, while also supporting the creation of multiple environments, such as Staging and Production, within the same Application. This allows the lab to focus on the CI/CD workflow instead of requiring the entire underlying infrastructure to be configured manually.

Instead of manually creating each environment and risking configuration inconsistencies, we will create the Staging environment first and then clone it to Production so that both environments have consistent configurations.

Initialize the Staging environment (stg-env):

Access the Elastic Beanstalk Console and create a new Application, for example:

app-express-js-sample-app

Create the first environment within this application:

  • Platform: Select the platform that matches the source code, such as Node.js running on Amazon Linux 2023.
  • Application code: You can select Sample application to initialize the environment quickly before deploying the actual code.
  • Service Access: Assign the two IAM Roles created in the previous step:
    • aws-elasticbeanstalk-service-role
    • aws-elasticbeanstalk-ec2-role

Click Create and wait approximately 3–5 minutes for AWS to create the necessary resources, such as EC2, Security Group, Load Balancer, and a temporary domain.

Step 2.2: Clone the Production environment (prod-env)

When stg-env is ready:

  1. Go to the Actions menu in the upper-right corner.
  2. Select Clone Environment.
  3. Rename the new environment to prod-env.
  4. Keep the technical parameters unchanged and click Clone.

After this step, we will have two separate environments with two different URLs:

  • stg-env: used for testing before release.
  • prod-env: used for production.

3. Create a CodeBuild Project

AWS CodeBuild allows us to build applications in a managed environment without maintaining a Jenkins server or a separate build server. The service charges based on actual build time, making it suitable for small to medium-sized lab or production pipelines.

Connect AWS to GitHub

Navigate to:

Developer Tools -> Settings -> Connections -> Create connection

Select GitHub as the provider and enter a connection name, for example: repo-connection

Click Connect to GitHub. A pop-up window will request authentication and access to the repository. Select the correct repository containing the application source code:

Then click Install & Authorize.

Step 3.2: Configure the CodeBuild Project

Go to the CodeBuild Console and select Create build project.

Configure the main sections:

  • Source: Select the GitHub connection you just created.
  • Buildspec: Select Use a buildspec file.
  • Leave the remaining options at their default values:

4. Building a pipeline with AWS CodePipeline

Now connect all the components into a complete CI/CD workflow.

Create a basic pipeline

Configure the stages:

Source Stage

  • Source provider: GitHub via GitHub App.
  • Connection: Select the connection you created.
  • Repository: Select the correct repository containing the source code.
  • Branch: Select the branch to monitor, for example master or main.

Build Stage

  • Build provider: AWS CodeBuild.
  • Project name: Select the CodeBuild project created in step 3.

Deploy Stage Staging

  • Deploy provider: AWS Elastic Beanstalk.
  • Application name: Select the Elastic Beanstalk application you created.
  • Environment name: Select stg-env.

After completion, the pipeline will automatically trigger the first run, and you can inspect the live logs.

Troubleshooting: Deployment to Elastic Beanstalk failed

During the first run, deployment to Elastic Beanstalk may fail if the default CodePipeline role does not have sufficient permissions to operate with Elastic Beanstalk.

For the lab environment, the quick workaround is:

  1. Go to IAM Console.
  2. Find the CodePipeline role, usually named AWSCodePipelineServiceRole-*.
  3. Attach policy AdministratorAccess-AWSElasticBeanstalk.
  4. Return to CodePipeline and click Retry on the failed stage.

Note: Attaching the policy above is appropriate for simplifying this lab. In a real production environment, you should restrict permissions according to the least-privilege principle and grant only the actions required by the pipeline.

Add Manual Approval and Deploy Production

After the pipeline has run through Staging, we add a review step before releasing to Production.

In the Pipeline interface:

  1. Click Edit.
  2. Below the deploy Staging stage, select Add stage.
  3. Name the new stage Review.
  4. In the Review stage, select Add action group.

Configure the action:

  • Action name: ManualApproval
  • Action provider: Manual approval
  • SNS topic: You can configure one if you want to send notification emails to QA or the release owner.

Continue adding the deploy Production stage:

  1. Select Add stage.
  2. Name the stage DeployToProd.
  3. In this stage, select Add action group.

Configure the action:

  • Action name: DeployToProd
  • Action provider: AWS Elastic Beanstalk
  • Input artifacts: BuildArtifact
  • Application name: Select the current application.
  • Environment name: Select prod-env.

Click Done and Save to save the changes.

End-to-end testing

To test the pipeline, we make a small change to the source code—for example, changing the interface background color to orange.

Make a direct code change on GitHub:

As soon as the push is complete, return to AWS CodePipeline. You will see the Source Stage change to In progress. The code is then sent to CodeBuild for packaging and automatically deployed to stg-env.

Once the Staging deployment is complete, the pipeline will stop at stage Review and display the Waiting for approval status.

Verify the result:

  • Access the Staging URL: the interface has changed to orange.
  • Access the Production URL: the interface still shows the old version.

This demonstrates that the new change has been deployed only to Staging and has not affected Production.

After completing the verification:

  1. Open the Manual Approval stage in CodePipeline.
  2. Click Review.
  3. Enter the approval message, for example:

Staging dat yeu cau, phat hanh len Production.

  1. Click Approve.

The pipeline will continue running the stage DeployToProd. After approximately 1–2 minutes, reload the Production URL to confirm that the changes have been deployed successfully.

Resource Cleanup

After completing the lab, clean up the resources to avoid incurring unexpected costs.

Recommended cleanup steps:

Elastic Beanstalk: Go to Application, select Actions, and then select Delete application. This action deletes the environments and related resources, such as EC2 instances, the load balancer, and the Auto Scaling group.

AWS CodePipeline and CodeBuild: Delete the corresponding pipeline and build project.

S3 Artifact Bucket: CodePipeline typically creates or uses an S3 bucket to store artifacts between stages. In the S3 Console, locate the bucket whose name contains codepipeline-*, empty the bucket first, and then delete it.

GitHub Connection: Go to Developer Tools -> Settings -> Connections to delete the connection if it is no longer in use.

Summary

After this lab, we have built a basic CI/CD pipeline that includes all the essential components:

  • Automatically detects changes from GitHub.
  • Build and package the application using CodeBuild.
  • Automatically deploy to the Staging environment.
  • Pause the pipeline for review using Manual Approval.
  • Release to Production only after approval has been granted.

This is a highly useful foundational model that can be extended with additional automated testing steps, security scanning, SNS/Slack notifications, or more advanced release strategies in the future.