To create our first deployment pipeline with CodePipeline, we are going to use the AWS console, which offers a very intuitive web interface:
- Open the following link in your browser: https://console.aws.amazon.com/cod epipeline.
- If prompted, click on Get started.
- On the next screen, give your pipeline the name helloworld and click on Next Step.
- For the source location, select GitHub as a Source provider and click on Connect to Github. If requested, sign into your GitHub account.
- This will bring you back to the AWS CodePipeline screen. We can now select a Repository and branch. We will select the helloworld project and the master branch. Click on Next step.
- This brings us to stage three of our pipeline where we can select our Build provider. Our application is being written in Node.js so we don't need to build anything. Select No build and click on Next step.
- The next step is called Beta. This is essentially our staging deployment step. Under Deployment provider, select AWS CodeDeploy. Under Application name, select helloworld. Finally, select staging for the Deployment group. Click on Next step.
- This brings us to a step in which we have to choose our Role Name. Conveniently, AWS have also added a Create Role button. Click on this.
- On the next screen, select Create a new IAM Role and give it the name AWS- CodePipeline-Service. Use the policy proposed and click on Allow.
- Go back to the CodePipeline step and make sure that role name says AWS- CodePipeline-Service. Click on Next step.
- On the review screen, make sure everything is correct. Finally, click on Create Pipeline.
The pipeline will be created in a matter of seconds and run for the first time.
Once both steps have run, you can verify that the code has been deployed by opening in your browser http://<instanceip>:3000. The instance IP can be found in the CloudFormation template or the EC2 console. You can even verify the success with the following one-liner:
$ aws cloudformation describe-stacks
--stack-name helloworld-staging
--query 'Stacks[0].Outputs[0].OutputValue'
| xargs -I {} curl {}:3000
Hello World
We have finished our basic pipeline. By taking advantage of CodePipeline, CodeDeploy, GitHub, and S3, we have built a very elegant solution to handle the deployment our web application. Every time a pull request is merged to the master, our pipeline will pick up the change, automatically create a new package with the new code, store it on S3, and then deploy it to staging. Thanks to CodeDeploy we can have a basic test in place to verify that the version is working. If needed, we can also roll back to any revisions that were built previously.
Our pipeline doesn't have to be limited to staging; we can actually do a lot more. As we mentioned previously, CodePipeline can integrate with Jenkins. We can use CodePipeline to build artifacts, but also to run some extra series of tests. Let's add it to our pipeline before deploying to staging.