The purpose of this demo is show how to use some AWS services for CI/CD. We have a Node.js application utilizing Amazon Rekognition API to identify celebrities and we are going to automatize the source, build and deploy process.
To get started you will need an IAM user with the following access:
- CloudFormation
- Cloud9
- CodeCommit
- CodePipeline
- CodeBuild
- CodeDeploy
- EC2
Note: Tested in the N. Virginia region (us-east-1).
- Open the EC2 console at https://console.aws.amazon.com/ec2
- In the navigation pane choose KeyPair, and create or import a Key Pair named 'cicd-keypair'
- Open the CloudFormation console at https://console.aws.amazon.com/cloudformation
- On the Welcome page, click on Create stack button
- On the Step 1 - Specify template: Choose Upload a template file, click on Choose file button and select the template.yaml located inside deploy directory
- On the Step 2 - Specify stack details: Enter the Stack name as 'cicd-techtalk'
- On the Step 3 - Configure stack options: Just click on Next button
- On the Step 4 - Review: Enable the checkbox I acknowledge that AWS CloudFormation might create IAM resources with custom names., and click on Create Stack button
- Wait for the stack get into status CREATE_COMPLETE
- Under the Outputs tab, take a note of ELB value
- Open the CodeCommit console at https://console.aws.amazon.com/codecommit
- Click on Create repository button, enter the Repostiory name as 'cicd-techtalk' and then click on Create button
- Open the IAM console at https://console.aws.amazon.com/iam
- In the navigation pane select Users, and then click on awsstudent username
- Under Security Credentials tab, click on Generate button of section HTTPS Git credentials for AWS CodeCommit
- Click on Download credentials button and save the generated csv file
- Open the Cloud9 console at https://console.aws.amazon.com/cloud9
- On the Step 1 - Name environment: Enter the Environment name as 'cicdtech-talk'
- On the Step 2 - Configure settings: Just click on Next button
- On the Step 3 - Review: Check the resources being created, and click on Create Environment button
- Once your envionment was provisioned, select the bash tab and execute the following commands:
git init
git add .
git commit -m "Repo Init"
git remote add origin https://git-codecommit.us-east-1.amazonaws.com/v1/repos/cicd-techtalk
git push -u origin master
Note: Please note that you'll be asked for the credentials created in the step Grant access for CodeCommit on IAM
- Open the CodeBuild console at https://console.aws.amazon.com/codebuild
- Click on Create build project button
- Enter the Project name as 'cicd-techtalk'
- On Source define AWS CodeCommit as the source provider and select cicd-techtalk for repository
- On Environment choose Ubuntu for Operational System, Standard for Runtime and aws/codebuild/standard:2.0 as the Image version. After, select Existing service role and search for CodeBuild-cicd-techtalk
- Uncheck the checkbox Allow AWS CodeBuild to modify this service role so it can be used with this build project
- Click on Create build project button
- Open the CodeDeploy console at https://console.aws.amazon.com/codedeploy
- Click on Create application button
- Enter the Application name as 'cicd-techtalk', select EC2/On-premises for Compute platform and then click on Create button
- Once your application was created, under Deployment groups tab click on Create deployment group button
- Enter the deployment group name as 'cicd-techtalk'
- Select the Service Role as CodeDeploy-cicd-techtalk
- On Environment configuration select Amazon EC2 instances, enter for Key 'Name' and for Value 'cicd-techtalk'
- On Load Balancer configuration select cicd-techtalk-tg
- Click on Create deployment group button
- Open the CodePipeline console at https://console.aws.amazon.com/codepipeline
- Click on Create pipeline button
- On the Step 1 - Choose pipeline setting: Enter the Pipeline name as 'cicd-techtalk'
- On the Step 2 - Add source stage: 3.1 Select AWS CodeCommit for Source provider 3.2 Select cicd-techtalk for Repository name 3.3 Select master for Branch name
- On the Step 3 - Add build stage: Select AWS Codebuild for Build provider, and 'cicd-techtalk' for Project name
- On the Step 4 - Add deploy stage: Select AWS CodeDeploy for Deploy provider, cicd-techtalk for Application and Deployment group
- On the Step 5 - Review: Click on Create pipeline button
Note: The first execution will fail during the build phase, because the project code is not commited yet.
- Open the Cloud9 console at https://console.aws.amazon.com/cloud9
- Select the bash tab and execute the following commands:
wget https://github.com/aws-samples/aws-braziltechtalk2019-cicd-demo/zipball/master -O project.zip
unzip project.zip
mv aws-samples-aws-braziltechtalk2019-cicd-demo-*/* .
mv aws-samples-aws-braziltechtalk2019-cicd-demo-*/.gitignore .
rm -fr aws-samples-aws-braziltechtalk2019-cicd-demo-* project.zip
git add .
git commit -m 'project code'
git push
Note: There is an intentional error on app.js. You can see the error after you launch the application on the first time and click on the button Find celebrity!.
- Access the application opening your browser at the ELB DNS obtained in the step CoudFormation
- Test it using a celebrity picture of your preference and you will receive and error because it cannot find the configuration file
- Fix the configuration file name Using Cloud9, by opening the file app/app.js and changing on the first line from PLEASE-FIX-ME to config
- Still on Cloud9, select the bash tab and execute the following commands to commit and push your new code:
git add app/app.js
git commit -m "Fix configuration file name"
git push
- The pipeline will be started and you can follow its execution on CodePipeline console. It should finishes succesffully!
- Try again the application!
- Delete the CI/CD stack in the following order: CodePipeline, CodeDeploy, CodeBuild, CodeCommit and Cloud9
- Open the CloudFormation console at https://console.aws.amazon.com/cloudformation
- Select cicd-techtalk Stack and click on Delete button
https://docs.aws.amazon.com/codebuild/latest/userguide/sample-codedeploy.html
This sample code is made available under the MIT-0 license. See the LICENSE file.