Set Up a Continuous Deployment Pipeline Using AWS CodePipeline
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.
Implementation
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.
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.
-
Create an application
To start, open the
Elastic Beanstalk console and choose Create
Application.
-
Configure the application
For Application name, enter
Deployment
Tutorial. Select PHP
from the dropdown menu under
Platform, and choose
Create application.
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.
-
Create a sample environment
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.
This will take several
minutes to complete.
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.
- GitHub
-
Use this procedure if you would like to use your GitHub account as your source.
- Amazon S3
-
Use this procedure if you would like to use Amazon S3 as your source.
-
Navigate to the sample code
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.
-
Download the files
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.
-
Create a bucket
Open the Amazon S3 console and choose
Create bucket.
-
Configure bucket details
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.
-
View the created bucket
The console displays the newly created bucket, which is empty.
-
Open the bucket
You will now upload the sample code to the Amazon S3 bucket. Select the Amazon S3
bucket.
-
Upload the sample code
Select Upload.
-
Add files
Select Add files to upload the zip file you
downloaded earlier or drag and drop the file. Then select Upload.
Then, go to Create your pipeline.
- AWS CodeCommit
-
Use this procedure if you would like to use AWS CodeCommit as your source.
-
Navigate to the sample code
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.
-
Download the files
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.
-
Create a repository
Open the AWS CodeCommit console and choose Create
repository.
-
Configure repository settings
On the Create repository page:
Enter PipelineRepo for Repository name.
Choose Create.
-
Upload sample code
Once the repository is successfully created, scroll down to the PipelineRepo section and select Add file, then choose Upload
file.
-
Select the sample code file
On the Upload a file page, choose the
Choose file button and select the downloaded
aws-codepipeline-s3-aws-codedeploy_linux.zip file.
-
Commit changes to main
Enter an Author name and Email address, then choose Commit
changes.
Then, go to 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.
-
Create a pipeline
Open the AWS CodePipeline console.
On the Welcome page, choose Create pipeline.
-
Configure pipeline settings
On the Step 1: Choose pipeline settings page:
After you create a pipeline, you cannot change its name.
-
Choose your source
On the Step 2: Add source stage page, select the
location of the source you selected using the following instructions.
- GitHub
-
-
Add source
Select GitHub (Version 2) for the Source provider.
Choose Connect to GitHub.
-
Enter a connection name
Enter Deployment Tutorial for Connection name and choose Connect to GitHub.
-
Grant permissions
Select Authorize AWS Connector for
GitHub.
-
Install a new app
When redirected back to the connection screen, choose Install a new app.
-
Install the forked repository
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.
-
Connect to GitHub
Once redirected back to the Connect to
GitHub page, choose Connect.
-
Specify a repository and branch
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.
-
Skip build stage
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.
-
Choose Skip
In the confirmation dialog, select Skip.
-
Configure deploy stage
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.
Continue to Activate your pipeline to deploy your
code.
- Amazon S3
-
-
Add source
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.
-
Skip build stage
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.
-
Choose Skip
In the confirmation dialog, select Skip.
-
Configure deploy stage
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.
Continue to Activate your pipeline to deploy your
code.
- AWS CodeCommit
-
-
Add source
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.
-
Skip build stage
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.
-
Choose Skip
In the confirmation dialog, select Skip.
-
Configure deploy stage
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.
Continue to 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.
-
Review configuration and create pipeline
In the Step 5: Review page,
review the information and choose Create
pipeline.
-
Monitor the pipeline status
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.
-
Select Elastic Beanstalk
In the status area for the Beta stage, select
AWS Elastic Beanstalk.
-
Select the environment
The AWS Elastic Beanstalk console opens with the details of the
deployment.
Select the environment you created earlier, called
Default-Environment
Deploymenttutorial-env.
-
Select the URL of the sample website
Select the URL to view the sample website you deployed.
A webpage with a congratulations message indicating you
successfully created a pipeline from your source to Amazon EC2
will open.
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.
Choose the appropriate tab based on the code source you used.
- GitHub
-
-
Edit the code
Visit your own copy of the repository that you forked in GitHub.
Open index.html.
Select the Edit icon.
-
Insert text
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!
-
Commit the change
Commit the change to your repository.
Then, go to View the page you updated with GitHub.
- Amazon S3
-
-
Edit the code
On your desktop, visit the zip file you downloaded called aws-codepipeline-s3-aws-codedeploy_linux.zip.
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.
-
Upload the file to your bucket
Return to the S3 bucket that you created earlier and select Upload.
-
Upload the file to your bucket
Select Add files to upload the updated
aws-codepipeline-s3-aws-codedeploy_linux.zip
file or drag and drop the file. Then choose Upload.
Because you enabled versioning when you first created the S3 bucket, S3
will save a copy of every version of your files.
Then, go to View the page you updated in Amazon S3.
- AWS CodeCommit
-
-
Edit the code
On your desktop, visit the zip file you downloaded called aws-codepipeline-s3-aws-codedeploy_linux.zip.
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!
-
Upload the file
From the CodeCommit PipelineRepo page, choose
Add file and select Upload file.
-
Upload the file
On the Upload a file page, choose the
Choose file button and select the updated
aws-codepipeline-s3-aws-codedeploy_linux.zip
file.
-
Commit changes
Enter an Author name and Email address, then choose Commit changes.
In this step, you will view the page you updated.
Choose the appropriate tab based on the code source you used.
- GitHub
-
-
Choose Elastic Beanstalk
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.
-
Select the environment
The AWS Elastic Beanstalk console opens with the details of the deployment. Select the
environment you created earlier, called Deploymenttutorial-env.
-
Select the URL
Select the URL to view the sample website again.
-
View the page
Confirm that the updated text appears on the webpage.
- Amazon S3
-
-
Choose Elastic Beanstalk
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.
-
Select the environment
The AWS Elastic Beanstalk console opens with the details of the deployment. Select the
environment you created earlier, called Deploymenttutorial-env.
-
Select the URL
Select the URL to view the sample website again.
-
View the page
Confirm that the updated text appears on the webpage.
- AWS CodeCommit
-
-
Choose Elastic Beanstalk
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.
-
Select the environment
The AWS Elastic Beanstalk console opens with the details of the deployment. Select the
environment you created earlier, called Deploymenttutorial-env.
-
Select the URL
Select the URL to view the sample website again.
-
View the page
Confirm that the updated text appears on the webpage.
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.
-
Delete the pipeline
First, you will delete your pipeline. In the
Pipelines view, select the
pipeline radio button and select Delete
pipeline.
-
Confirm deletion
To confirm deletion, enter delete in the
field and choose Delete.
-
Delete the Beanstalk application
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.
-
Confirm deletion
In the Confirm Application
Deletion window, enter the name of the application to
be deleted and choose Delete.
If you used Amazon S3 as your source, you can delete the resources to avoid future charges.
-
Empty the bucket contents
Visit the S3 console. First, we will empty the S3 bucket. Select
the radio button next to the
awscodepipeline bucket and
choose Empty.
-
Confirm deletion
When a confirmation message appears, enter permanently
delete in the text input field and choose
Empty.
-
Delete the bucket
Now we will delete the bucket. Select the radio button next to the
awscodepipeline bucket and
choose Delete.
-
Confirm deletion
When a confirmation message appears, enter the bucket name and
then choose Delete bucket.
If you used AWS CodeCommit as your source, you can delete the resources to avoid future charges.
-
Delete the repository
Open the
AWS CodeCommit repository. Select the radio button next to the
repository you created and choose Delete
repository.
-
Confirm deletion
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!