Leverage AWS services AWS App Runner, Amazon RDS, Amazon Elastic Container Registry, AWS CodePipeline, AWS CodeCommit, AWS CodeBuild, AWS CodeDeploy and AWS CDK to deploy infrastructure as code and build out a CI/CD pipeline. We will use Spring Boot Petclinic application as a sample app to demonstrate the CI/CD pipeline. Petclinic is an MVC application built using Spring framework and leverages a relational database to persist the data.
- petclinic - Spring Boot application code. This is a clone of the spring petclinic repo. Modification includes addition of the Dockerfile. For more details review the README
- cdk - AWS CDK Project for Building and Deploying Petclinic App. For more details review the README
- Follow the steps in the Cloud9 Environment Setup guide - Create-Cloud9-Environment
-
Setup and Install AWS CDK, Typescript:
npm install -g typescript export CDK_DEFAULT_ACCOUNT="AWS ACCOUNT TO DEPLOY" export CDK_DEFAULT_REGION="AWS REGION TO DEPLOY"
-
Bootstrap AWS CDK:
cdk bootstrap aws://$CDK_DEFAULT_ACCOUNT/$CDK_DEFAULT_REGION
-
Clone the source code repository from aws-samples:
cd ~/environment git clone https://github.com/aws-samples/aws-apprunner-cdk.git
-
Setup Git User:
git config --global user.name "workshop-admin" git config --global user.email "[email protected]"
-
Init AWS CDK environment:
cd ~/environment/aws-apprunner-cdk/cdk npm install @types/node
-
Package the application using Apache Maven Wrapper
cd ~/environment/aws-apprunner-cdk/petclinic ./mvnw package -Dmaven.test.skip=true
-
Run Petclinic application locally and run the following inside the Cloud9 terminal:
./mvnw spring-boot:run
-
Validate the launched application and preview the application running locally:
This will run the application on port 8080. Click Preview from the top menu and then click “Preview Running Application.” It will open a browser displaying the Spring Petclinic application.
Press Ctrl+C to stop the running application.
Note: This workshop will create chargeable resources in your account. When finished, please make sure you clean up resources as instructed at the end.
-
Set up DockerHub Credentials in Secrets Manager for Docker Login credentials. Please provide your personal docker username (not email) and password. Note this step demonstrates the use of Docker Hub and provides an example on how to leverage the private registries to be included in the pipeline using Secrets Manager.
cd ~/environment/aws-apprunner-cdk aws secretsmanager create-secret \ --name dockerhub_credentials \ --description "DockerHub Credentials" \ --secret-string "{\"username\":\"dockerhub_username\",\"password\":\"dockerhub_password\"}" \ --region=$CDK_DEFAULT_REGION
-
Deploy Step Phase 1: Create Code Commit Repo
- Setup the Code Commit Repo.
cd ~/environment/aws-apprunner-cdk/cdk cdk deploy
- When asked whether you want to proceed with the actions, enter
y
. - Wait for AWS CDK to complete the deployment before proceeding. It will take few minutes to complete “cdk deploy”. Pipeline will fail since the code is not available in code commit repo.
- Setup the Code Commit Repo.
-
Deploy Step Phase 2: Enable CI/CD Pipeline and deploy infrastructure and application.
- Commit the code and set the origin to the code commit repo that was created.
cd ~/environment/aws-apprunner-cdk git remote rename origin upstream git remote add origin "provide codecommit repo HTTPS URL created in above step" git push origin main
- Code Commit changes will invoke the Code Pipeline. It will take approximately 20 to 30 minutes to complete the deployment.
- Initial deployment will take longer since this includes building the container image for the application and setting up the infrastructure that includes Networking, RDS and other dependent components.
- Explore the deployment progress on the CloudFormation console.
- Commit the code and set the origin to the code commit repo that was created.
-
Validate the Deployment.
- Once the deploy is complete, you can review the repo in App Runner service using the AWS console.
- View the App Runner service using the AWS App Runner console
-
Review the deployed stack.
- View the RDS database using the Amazon RDS console.
- View the ECR repo using the Amazon ECR console.
- View the CodeCommit repo using the AWS CodeCommit console.
- View the CodeBuild project using the AWS CodeBuild console.
- View the CodePipeline using the AWS CodePipeline console.
-
Validate the CI/CD Pipeline.
- Make a source code change in the petclinic application. Make the change to messages.properties under
petclinic/src/main/resources/messages/messages.properties
. Change the welcome value toWelcome to CI/CD using AWS CDK
. - Commit the change and push the change to the code commit repo.
cd ~/environment/aws-apprunner-cdk git commit -am "updated the title" git push origin main
- Review that the commit started the code pipeline build. It will take approximately 10 minutes to deploy the change which includes building a new image and deploying the latest version to AWS AppRunner.
- Once the deploy is complete, you can refresh the application home page and you should see the change reflected on the home page.
- Make a source code change in the petclinic application. Make the change to messages.properties under
-
Cleaning Up.
- Delete the AWS CDK stack.
cd ~/environment/aws-apprunner-cdk/cdk cdk destroy
- When asked whether you want to proceed with the actions, enter
y
. Wait for AWS CDK to complete the destroy. - Delete the AWS CloudFormation
petclinic-deploy-*
stack from the AWS CloudFormation Console. - Delete the images in the AWS ECR
petclinic
repo. - Delete the AWS CloudFormation
petclinic-build-*
stack from the AWS CloudFormation Console. - Delete the AWS Cloud9
workshop-environment
environment including theworkshop-admin
IAM role. - Delete the
dockerhub_credentials
from AWS Secrets Manager.
- Delete the AWS CDK stack.
See CONTRIBUTING for more information.
This library is licensed under the MIT-0 License. See the LICENSE file.