Skip to content

Build and Deploy Spring Petclinic Application to AWS App Runner using AWS CDK.

License

Notifications You must be signed in to change notification settings

aws-samples/aws-apprunner-cdk

Build and Deploy Spring Petclinic Application to AWS App Runner using AWS CDK.

Overview

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.

Architecture

Architecture

Repository Structure

  • 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

Configure Development Environment

Environment Setup and Local Testing

  1. 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"
  2. Bootstrap AWS CDK:

    cdk bootstrap aws://$CDK_DEFAULT_ACCOUNT/$CDK_DEFAULT_REGION
  3. Clone the source code repository from aws-samples:

    cd ~/environment
    git clone https://github.com/aws-samples/aws-apprunner-cdk.git
  4. Setup Git User:

    git config --global user.name "workshop-admin"
    git config --global user.email "[email protected]"
    
  5. Init AWS CDK environment:

    cd ~/environment/aws-apprunner-cdk/cdk
    npm install @types/node
    
  6. Package the application using Apache Maven Wrapper

    cd ~/environment/aws-apprunner-cdk/petclinic
    ./mvnw package -Dmaven.test.skip=true
  7. Run Petclinic application locally and run the following inside the Cloud9 terminal:

    ./mvnw spring-boot:run
  8. Validate the launched application and preview the application running locally: Petclinic Launch

    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.

    Petclinic Preview

    Press Ctrl+C to stop the running application.

Build the infrastructure and pipeline using AWS CDK and validate the application and pipeline.

Note: This workshop will create chargeable resources in your account. When finished, please make sure you clean up resources as instructed at the end.

  1. 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
  2. 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. CDK Deploy
    • 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.
  3. 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.
  4. 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
  5. Review the deployed stack.

  6. 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 to Welcome 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.
  7. 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 the workshop-admin IAM role.
    • Delete the dockerhub_credentials from AWS Secrets Manager.

Security

See CONTRIBUTING for more information.

License

This library is licensed under the MIT-0 License. See the LICENSE file.