In this guide, I will be showing you the steps to setup a Continuous Deployment Pipeline with AWS CodePipeline I will be using the Free Tier to set up an automated release pipeline that you can use to deploy your applications to the testing or production environment. We will be using AWS CodePipeline to create the pipeline. AWS CodePipeline is used to build, test, and deploy codes each time there is a code change. We can make use of the GitHub Repository, Amazon Simple Storage Service (Amazon S3) bucket, or an AWS CodeCommit repository where we are going to store our source code. We shall also make use of AWS Elastic Beanstalk as the deployment location where you can access the application.
By the time you finish the configuration anytime you make a code change to the source code the pipeline will always detect the changes made and update the application on the testing or production environment. Please see How To Use Azure Key Vault secrets in Azure Pipelines, how to build your first CI/CD Pipeline in Azure DevOps using ASP.Net Core Application, and How to automate Infrastructure Deployments in the Cloud with Ansible and Azure Pipelines.
Prerequisites to setup a Continuous Deployment Pipeline with AWS CodePipeline
You need an AWS account, and you need a code repository account e.g. GitHub, AWS CodeCommit or Amazon S3.
1: Setup a Deployment Environment
Every deployment pipeline environment needs a virtual server also known as Amazon EC2 instances, where you can deploy your source code. Set up the environment before creating any deployment pipeline.
You can avoid the process of configuring an EC2 instance by just spinning up an AWS Elastic Beanstalk (EBS). With an Elastic Beanstalk, you can easily and quickly host your application without configuring a virtual server or EC2 instance. It will automatically provision and operates the infrastructure for you, setting up the servers and configuring the load balancers on your behalf. EBS will also provide the application stack like the programming language and framework on your behalf.
Open the Elastic Beanstalk Console as shown below
Click on Create Application
Give the Application a Name
Enter a name for the Application and Select PHP from the Platform dropdown as the technology stack. And on the Application code, select Sample application. We are only setting up the Deployment environment where the application code will be deployed. After this click Create Application.
Please note that If you are planning to have access to your instances then you will need to select a key pair, if not then leave the default values as it is and move on with the creation of the environment for the CICD Pipeline.
Select the platform
When the platform is selected. Click on Create Application
After the application is created, whether it is a sample or from your code repo. You will need to click on the Application name
Click on Create Environment button to start creating the environment you can deploy your code.
Part of what an EBS will create for you automatically are an Amazon EC2 instance, a security group, an Auto Scaling group, an Amazon S3 bucket, Amazon CloudWatch alarms, and a domain name to access your application.
Select either the sample application or you can upload your code which will serve as the initial deployment to the new environment.
Leave the default value on the Configure service access section, while steps 3-5 are optional.
After the submission of the configurations, the environment will be created.
2. Store your sample code in the repository
The next step will be to store your sample code in the repository which will be used as the source to host our code. The deployment Pipeline we will create will take code from the repository and perform some deployment actions on it.
You can use any of these options as your code repository: a GitHub repository, an Amazon S3 bucket, or an AWS CodeCommit repository.
Using GitHub Repository
Create a new GitHub repository and push your code to GitHub from your local or from any location.
Using Amazon S3 as Repository
You can use Amazon S3 as your code repository, you can either move your code from the AWS GitHub repository or from your local system and upload it to an Amazon S3 bucket.
Using AWS CodeCommit as Repository
Move your code from the AWS GitHub repository or from your local system and push it to AWS CodeCommit. If you want to know more on how to work with AWS CodeCommit, read How to Setup HTTPS users using Git credentials and Pushing Code to AWS CodeCommit
Open the AWS CodeCommit console and choose Create Repository. Enter the repository name and click Create.
As you can see, the Repository has been created.
Once the Repository is created then you can Add your file or your sample code. Click on Add file, then choose Upload file.
On the Upload a file page, click on the Choose file button and select your zipped code file. Enter the Author name and Email address, then click on Commit changes.
3. Steps to Creating your Pipeline
The next step now is to create and configure a simple pipeline with two actions: source and deploy. We are going to provide the CodePipeline with the locations of the source code repository and deployment environment we earlier created.
Open the AWS CodePipeline console and click Create pipeline.
Enter the name for your pipeline, leave other settings on default and click Next.
Note: After creating a pipeline, you cannot change its name.
Add source stage
On the next page Add Source stage, you will need to select the location of the source code we created earlier. You can select AWS CodeCommit, Amazon S3 or GitHub.
But in this guide, I will be showing you how to connect with AWS CodeCommit as the Code Repository. If you want to know how to connect with GitHub read Using GitHub as Source Provider to AWS CodePipeline
this repo is where you stored your input artifacts for your pipeline. Choose the provider and then provide the connection details.
Select AWS CodeCommit for the Source provider and follow these steps.
- Select the repository you created earlier from the drop-down list to serve as a source provider for your pipeline.
- Select main as the branch.
- Amazon Cloudwatch Events is recommended for change detection options.
- Choose CodePipeline default as the output artifact format.
- Click Next
AWS CodePipeline will allow you to plug your desired build provider into your pipeline because a standard continuous deployment pipeline will always require a build stage, where code is compiled, and unit tested. But in this guide that aspect will not be covered we will skip the build stage.
Under the Add build stage section, click on Skip build stage.
Under the deploy section make these selections:
- Deploy provider: AWS Elastic Beanstalk.
- Region: Leave default Region.
- Application name: The application you created earlier.
- Environment name: Deployment Environment you created earlier.
- Click Next.
Review all the information and choose Create pipeline.
4. Next Step is activating the pipeline and deploying your code
Once everything is set you can launch your pipeline and when it’s created, it will start to run automatically.
The first stage is that it will detect the application code in your source location, bundles up the files, and moves it to the second stage.
In the next stage, it will pass the code to Elastic Beanstalk which will deploy the code to the EC2 instance. Remember that EBS contains the EC2 instance that will host and run your code.
After the Deployment Pipeline is created click on AWS Elastic Beanstalk.
Inside the Elastic beanstalk click on the domain which will launch the application deployed.
If all goes well the application gets launched successfully.
Whenever you make a change to the code, and you push back to the AWS CodeCommit, GitHub or Amazon S3 it will automatically be deployed via the Pipeline created and the changes on the code will reflect.
I hope you found this blog post on how to setup a Continuous Deployment Pipeline with AWS CodePipeline interesting and helpful. In case you have any questions do not hesitate to ask in the comment section.