Set Up a Continuous Deployment Pipeline Using AWS CodePipeline

TUTORIAL

Overview

In this tutorial, you will learn how to create an automated software release pipeline that deploys a live sample app. You will create the pipeline using AWS CodePipeline, a service that builds, tests, and deploys your code every time there is a code change. You will use your GitHub account, an Amazon Simple Storage Service (Amazon S3) bucket, or an AWS CodeCommit repository as the source location for the sample app’s code. You will also use AWS Elastic Beanstalk as the deployment target for the sample app. Your completed pipeline will be able to detect changes made to the source repository containing the sample app and then automatically update your live sample app.

Continuous deployment allows you to deploy revisions to a production environment automatically without explicit approval from a developer, making the entire software release process automated.

Everything done in this tutorial is Free Tier eligible.

 

What you will accomplish

In this tutorial, you will:

  • create an automated software release pipeline that deploys a live sample app
  • create the pipeline using AWS CodePipeline
  • use AWS Elastic Beanstalk as the deployment target for the sample app
 

Prerequisites

Before starting this tutorial, you will need an AWS account. If you don't already have one, follow the Setting Up Your AWS Environment getting started guide for a quick overview.

 AWS experience

Beginner

 Minimum time to complete

30 minutes

 Cost to complete

Free Tier eligible

 Requires

  • AWS account*
  • Recommended browser: The latest version of Chrome or Firefox

[*]Accounts created within the past 24 hours might not yet have access to the services required for this tutorial.

 Last updated

February 14, 2023

Step 1: Create a deployment environment

Your continuous deployment pipeline will need a target environment containing virtual servers, or Amazon EC2 instances, where it will deploy sample code. You will prepare this environment before creating the pipeline.

a. To simplify the process of setting up and configuring EC2 instances for this tutorial, you will spin up a sample environment using AWS Elastic Beanstalk. With Elastic Beanstalk you can easily host web applications without needing to launch, configure, or operate virtual servers on your own. It automatically provisions and operates the infrastructure (such as virtual servers and load balancers) and provides the application stack (such as OS, language and framework, and web and application server) for you.

b. For Application name, enter Deployment Tutorial. Select PHP from the dropdown menu under Platform, and choose Create application.

Note: If you have created an Elastic Beanstalk application before, choose Create New Application on the upper-right corner. Name your application and create a new web server environment. Select PHP as your Platform and Single Instance as your Environment type. If you are planning to remote login to your instances, select a key pair. Otherwise, leave default values for the remaining options and create the environment for your continuous deployment pipeline.

c. Elastic Beanstalk will begin creating a sample environment for you to deploy your application to. It will create an Amazon EC2 instance, a security group, an Auto Scaling group, an Amazon S3 bucket, Amazon CloudWatch alarms, and a domain name for your application.

Note: This will take several minutes to complete.

Step 2: Get a copy of the sample code

In this step, you will retrieve a copy of the sample app’s code and choose a source to host the code. The pipeline takes code from the source and then performs actions on it.

You can use one of three options as your source: a GitHub repository, an Amazon S3 bucket, or an AWS CodeCommit repository. Select your preference and follow the steps below:

  • GitHub
  • a. If you would like to use your GitHub account:

  • Amazon S3
  • a. If you plan to use Amazon S3 as your source, you will retrieve the sample code from the AWS GitHub repository, save it to your computer, and upload it to an Amazon S3 bucket.

    b. Save the source files to your computer:

    • Select the file named aws-codepipeline-s3-aws-codedeploy_linux.zip.
    • Choose View raw.
    • Save the sample file to your local computer.

    c. Open the Amazon S3 console and choose Create bucket.

    d. Bucket name: Enter a unique name for your bucket, such as awscodepipeline-demobucket-variables. All bucket names in Amazon S3 must be unique, so use one of your own, not one with the name shown in the example.

    • Region: In the dropdown, select the Region where you will create your pipeline, such as US East (N. Virginia).
    • Choose Create bucket.

    e. The console displays the newly created bucket, which is empty.

    f. You will now upload the sample code to the Amazon S3 bucket. Select the Amazon S3 bucket.

    g. Select Upload.

    h. Select Add files to upload the downloaded zip file from Step 2b or drag and drop the file. Then select Upload.

  • AWS CodeCommit
  • a. If you plan to use AWS CodeCommit as your source, you will retrieve the sample code from the AWS GitHub repository, save it to your computer, and upload it to an AWS CodeCommit repository.

    b. Save the source files to your computer:

    • Select the file named aws-codepipeline-s3-aws-codedeploy_linux.zip 
    • Choose View raw.
    • Save the sample file to your local computer.

    d. Open the AWS CodeCommit console and choose Create repository.

    e. On the Create repository page:

    • Enter PipelineRepo for Repository name.
    • Choose Create.

    f. Once the repository is successfully created, scroll down to the PipelineRepo section and select Add file, then choose Upload file.

    g. On the Upload a file page, choose the Choose file button and select the downloaded aws-codepipeline-s3-aws-codedeploy_linux.zip file.

    h. Enter an Author name and Email address, then choose Commit changes.

Step 3: Create your pipeline

In this step, you will create and configure a simple pipeline with two actions: source and deploy. You will provide CodePipeline with the locations of your source repository and deployment environment.

a. Open the AWS CodePipeline console.

  • On the Welcome page, choose Create pipeline. 

b. On the Step 1: Choose pipeline settings page:

  • Pipeline name: Enter the name for your pipeline, DemoPipeline
  • Choose Next.

Note: After you create a pipeline, you cannot change its name.

c. On the Step 2: Add source stage page, select the location of the source you selected and follow the steps below:

  • GitHub
    • Select GitHub (Version 2) for the Source provider.
    • Choose Connect to GitHub.
    • Enter Deployment Tutorial for Connection name and choose Connect to GitHub.
    • Select Authorize AWS Connector for GitHub.
    • When redirected back to the connection screen, choose Install a new app. 
    • On the Install AWS Connector for GitHub page, choose Only select repositories and select the aws-codepipeline-s3-codedeploy-linux repository forked in the previous step.
    • Choose Install.
    • Once redirected back to the Connect to GitHub page, choose Connect.

    The Add source page will be updated to reflect GitHub is ready to connect. Specify the repository and branch:

    • Repository name: In the dropdown list, select the GitHub repository you want to use as the source location for your pipeline. Select the forked repository in your GitHub account named aws-codepipeline-s3-codedeploy-linux.
    • Branch name: In the dropdown list, select the branch you want to use, master.
    • Output artifact format: Select CodePipeline default.
    • Choose Next.
  • Amazon S3
    • Select Amazon S3 for the Source provider, select the Amazon S3 bucket you created, and then enter the S3 object key for the file uploaded, for example: aws-codepipeline-s3-aws-codedeploy_linux.zip.
    • Choose Next.
  • AWS CodeCommit
    • Select AWS CodeCommit for the Source provider.
    • Repository name: In the dropdown list, choose the PipelineRepo repository you created to use as the source location for your pipeline. 
    • Branch name: In the dropdown list, choose the branch you want to use, main.
    • Output artifact format: Choose CodePipeline default.
    • Choose Next.

    d. A true continuous deployment pipeline requires a build stage, where code is compiled and unit tested. CodePipeline lets you plug your preferred build provider into your pipeline. However, in this tutorial you will skip the build stage.

    • In Step 3: Add build stage, choose Skip build stage.

    e. In the confirmation dialog, select Skip.

     

    f. On the Step 4: Add deploy stage page:

    • Deploy provider: Select AWS Elastic Beanstalk.
    • Region: Retain the default Region.
    • Application name: Select Deployment Tutorial.
    • Environment name: Select Deploymenttutorial-env.
    • Choose Next. 

d. A true continuous deployment pipeline requires a build stage, where code is compiled and unit tested. CodePipeline lets you plug your preferred build provider into your pipeline. However, in this tutorial you will skip the build stage.

  • In Step 3: Add build stage, choose Skip build stage.

e. In the confirmation dialog, select Skip.

f. In the Step 4: Add deploy stage page:

  • Deploy provider: Select AWS Elastic Beanstalk. 
  • Region: Retain the default region.
  • Application name: Select Deployment Tutorial. 
  • Environment name: Select Deploymenttutorial-env.
  • Click Next.

Step 4: Activate your pipeline to deploy your code

In this step, you will launch your pipeline. Once your pipeline has been created, it will start to run automatically. First, it detects the sample app code in your source location, bundles up the files, and then moves them to the second stage that you defined. During this stage, it passes the code to Elastic Beanstalk, which contains the EC2 instance that will host your code. Elastic Beanstalk handles deploying the code to the EC2 instance.

a. In the Step 5: Review page, review the information and choose Create pipeline.

b. After your pipeline is created, the pipeline status page appears and the pipeline automatically starts to run. You can view progress as well as success and failure messages as the pipeline performs each action.

To verify your pipeline ran successfully, monitor the progress of the pipeline as it moves through each stage. The status of each stage will change from No executions yet to In progress, and then to either Succeeded or Failed. The pipeline should complete the first run within a few minutes.

c. In the status area for the Beta stage, select AWS Elastic Beanstalk. 

d. The AWS Elastic Beanstalk console opens with the details of the deployment.

  • Select the environment you created earlier, called Default-Environment Deploymenttutorial-env. 

e. Select the URL that appears in the upper-right part of the page to view the sample website you deployed.

Step 5: Commit a change and then update your app

In this step, you will revise the sample code and commit the change to your repository. CodePipeline will detect your updated sample code and then automatically initiate deploying it to your EC2 instance by way of Elastic Beanstalk. 

Note that the sample web page you deployed refers to AWS CodeDeploy, a service that automates code deployments. In CodePipeline, CodeDeploy is an alternative to using Elastic Beanstalk for deployment actions. Let’s update the sample code so that it correctly states that you deployed the sample using Elastic Beanstalk.

  • GitHub
  • a. Visit your own copy of the repository that you forked in GitHub.

    • Open index.html.
    • Select the Edit icon.

    b. Update the webpage by copying and pasting the following text on line 30: 

    You have successfully created a pipeline that retrieved this source application from GitHub and deployed it to one Amazon EC2 instance using AWS Elastic Beanstalk. You’re one step closer to practicing continuous deployment!

    c. Commit the change to your repository.

  • Amazon S3
  • a. On your desktop, visit the zip file you downloaded called aws-codepipeline-s3-aws-codedeploy_linux.zip.

    b. Edit the sample web app code:

    • Extract index.html from the zip file and open it using your preferred text editor. 
    • Update the header text that comes after Congratulations! so that it reads:

    You have successfully created a pipeline that retrieved this source application from Amazon S3 and deployed it to one Amazon EC2 instance using AWS Elastic Beanstalk. You’re one step closer to practicing continuous deployment!

    • Copy the updated index.html file back into aws-codepipeline-s3-aws-codedeploy_linux.zip and replace the older version of index.html.

    c. Reupload the edited file to your Amazon S3 bucket:

    • Return to the S3 bucket that you created earlier and select Upload.

    d. Select Add files to upload the updated aws-codepipeline-s3-aws-codedeploy_linux.zip file or drag and drop the file. Then choose Upload.

    Note: Because you enabled versioning when you first created the S3 bucket, S3 will save a copy of every version of your files.

  • AWS CodeCommit
  • a. On your desktop, visit the zip file you downloaded called aws-codepipeline-s3-aws-codedeploy_linux.zip.

    b. Edit the sample web app code:

    • Extract index.html from the zip file and open it using your preferred text editor. 
    • Update the header text that comes after Congratulations! so that it reads:

    You have successfully created a pipeline that retrieved this source application from AWS CodeCommit and deployed it to one Amazon EC2 instance using AWS Elastic Beanstalk. You’re one step closer to practicing continuous deployment!

    c. From the CodeCommit PipelineRepo page, choose Add file and select Upload file.

    d. On the Upload a file page, choose the Choose file button and select the updated aws-codepipeline-s3-aws-codedeploy_linux.zip file.

    d. Enter an Author name and Email address, then choose Commit changes.

    e. Return to your pipeline in the CodePipeline console. In a few minutes, you should see the Source change to blue, indicating that the pipeline has detected the changes you made to your source repository. Once this occurs, it will automatically move the updated code to Elastic Beanstalk.

    • After the pipeline status displays Succeeded, in the status area for the Beta stage, choose AWS Elastic Beanstalk.

    f. The AWS Elastic Beanstalk console opens with the details of the deployment. Select the environment you created earlier, called Deploymenttutorial-env.

    g. Select the URL that appears in the upper-left part of the page to view the sample website again.

    h. Your text has been updated automatically through the continuous deployment pipeline!

  • GitHub
  • Amazon S3
  • AWS CodeCommit

Step 6: Clean up your resources

To avoid future charges, you will delete all the resources you launched throughout this tutorial, which includes the pipeline, the Elastic Beanstalk application, and the source you set up to host the code.  

a. First, you will delete your pipeline. In the Pipelines view, select the pipeline radio button and select Delete pipeline.

b. To confirm deletion, enter delete in the field and choose Delete.

c. Second, delete your Elastic Beanstalk application. Visit the Elastic Beanstalk Applications page. Select the radio button for the Deployment Tutorial. Select Actions and Delete application.

d. In the Confirm Application Deletion window, enter the name of the application to be deleted and choose Delete.

  • Amazon S3
  • If you created an S3 bucket for this tutorial, delete the bucket you created:

    e. Visit the S3 console. First, we will empty the S3 bucket. Select the radio button next to the awscodepipeline bucket and choose Empty. 

    f. When a confirmation message appears, enter permanently delete in the text input field and choose Empty.

    g. Now we will delete the bucket. Select the radio button next to the awscodepipeline bucket and choose Delete.

    h. When a confirmation message appears, enter the bucket name and then choose Delete bucket.

  • AWS CodeCommit
  • If you created an AWS CodeCommit repository for this tutorial, visit the CodeCommit console and delete the repository that you created:

    e. Open the AWS CodeCommit repository. Select the radio button next to the repository you created and choose Delete repository.

    f. A confirmation window will pop up. Enter delete and choose Delete

Conclusion

Congratulations! You have successfully created an automated software release pipeline using AWS CodePipeline. Using CodePipeline, you created a pipeline that uses GitHub, Amazon S3, or AWS CodeCommit as the source location for application code and then deploys the code to an Amazon EC2 instance managed by AWS Elastic Beanstalk. Your pipeline will automatically deploy your code every time there is a code change. You are one step closer to practicing continuous deployment!

Was this page helpful?

Next steps

Now that you have learned to create a simple pipeline using AWS CodePipeline, you can learn more by visiting the following resources.

Create a four-stage pipeline

Create a more advanced, four-stage pipeline by following this guide. This pipeline uses a GitHub repository for your source, a Jenkins build server to build and test the project, and an AWS CodeDeploy application to deploy the built code to a staging server.

Next »