CodeDeploy sample for CodeBuild - Amazon CodeBuild
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

CodeDeploy sample for CodeBuild

This sample instructs Amazon CodeBuild to use Maven to produce as build output a single JAR file named my-app-1.0-SNAPSHOT.jar. This sample then uses CodeDeploy to deploy the JAR file to an Amazon Linux instance. You can also use Amazon CodePipeline to automate the use of CodeDeploy to deploy the JAR file to an Amazon Linux instance. This sample is based on the Maven in 5 Minutes topic on the Apache Maven website.

Important

Running this sample might result in charges to your Amazon account. These include possible charges for CodeBuild and for Amazon resources and actions related to Amazon S3, Amazon KMS, CloudWatch Logs, and Amazon EC2. For more information, see CodeBuild pricing, Amazon S3 pricing, Amazon Key Management Service pricing, Amazon CloudWatch pricing, and Amazon EC2 pricing.

Running the sample

To run this sample
  1. Download and install Maven. For more information, see Downloading Apache Maven and Installing Apache Maven on the Apache Maven website.

  2. Switch to an empty directory on your local computer or instance, and then run this Maven command.

    mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

    If successful, this directory structure and files are created.

    . └── my-app ├── pom.xml └── src ├── main │ └── java │ └── com │ └── mycompany │ └── app │ └── App.java └── test └── java └── com └── mycompany └── app └── AppTest.java
  3. Create a file with this content. Name the file buildspec.yml, and then add it to the my-app directory.

    version: 0.2 phases: install: runtime-versions: java: corretto8 build: commands: - echo Build started on `date` - mvn test post_build: commands: - echo Build completed on `date` - mvn package artifacts: files: - target/my-app-1.0-SNAPSHOT.jar - appspec.yml discard-paths: yes
  4. Create a file with this content. Name the file appspec.yml, and then add it to the my-app directory.

    version: 0.0 os: linux files: - source: ./my-app-1.0-SNAPSHOT.jar destination: /tmp

    When finished, your directory structure and file should look like this.

    . └── my-app ├── buildspec.yml ├── appspec.yml ├── pom.xml └── src ├── main │ └── java │ └── com │ └── mycompany │ └── app │ └── App.java └── test └── java └── com └── mycompany └── app └── AppTest.java
  5. Create a ZIP file that contains the directory structure and files inside of my-app, and then upload the ZIP file to a source code repository type supported by Amazon CodeBuild and CodeDeploy, such as an S3 input bucket or a GitHub or Bitbucket repository.

    Important

    If you want to use CodePipeline to deploy the resulting build output artifact, you cannot upload the source code to a Bitbucket repository.

    Do not add my-app to the ZIP file, just the directories and files inside of my-app. The ZIP file should contain these directories and files:

    . └── CodeDeploySample.zip ├── buildspec.yml ├── appspec.yml ├── pom.xml └── src ├── main │ └── java │ └── com │ └── mycompany │ └── app │ └── App.java └── test └── java └── com └── mycompany └── app └── AppTest.java
  6. Create a build project by following the steps in Create a build project.

    If you use the Amazon CLI to create the build project, the JSON-formatted input to the create-project command might look similar to this. (Replace the placeholders with your own values.)

    { "name": "sample-codedeploy-project", "source": { "type": "S3", "location": "codebuild-region-ID-account-ID-input-bucket/CodeDeploySample.zip" }, "artifacts": { "type": "S3", "location": "codebuild-region-ID-account-ID-output-bucket", "packaging": "ZIP", "name": "CodeDeployOutputArtifact.zip" }, "environment": { "type": "LINUX_CONTAINER", "image": "aws/codebuild/standard:4.0", "computeType": "BUILD_GENERAL1_SMALL" }, "serviceRole": "arn:aws:iam::account-ID:role/role-name", "encryptionKey": "arn:aws:kms:region-ID:account-ID:key/key-ID" }
  7. If you plan to deploy the build output artifact with CodeDeploy, follow the steps in Run a build. Otherwise, skip this step. (This is because if you plan to deploy the build output artifact with CodePipeline, CodePipeline uses CodeBuild to run the build automatically.)

  8. Complete the setup steps for using CodeDeploy, including:

    • Grant the user access to CodeDeploy and the Amazon services and actions CodeDeploy depends on. For more information, see Provision an user in the Amazon CodeDeploy User Guide.

    • Create or identify a service role to enable CodeDeploy to identify the instances where it deploys the build output artifact. For more information, see Creating a service role for CodeDeploy in the Amazon CodeDeploy User Guide.

    • Create or identify an IAM instance profile to enable your instances to access the S3 input bucket or GitHub repository that contains the build output artifact. For more information, see Creating an IAM instance profile for your Amazon EC2 instances in the Amazon CodeDeploy User Guide.

  9. Create or identify an Amazon Linux instance compatible with CodeDeploy where the build output artifact is deployed. For more information, see Working with instances for CodeDeploy in the Amazon CodeDeploy User Guide.

  10. Create or identify a CodeDeploy application and deployment group. For more information, see Creating an application with CodeDeploy in the Amazon CodeDeploy User Guide.

  11. Deploy the build output artifact to the instance.

    To deploy with CodeDeploy, see Deploying a revision with CodeDeploy in the Amazon CodeDeploy User Guide.

    To deploy with CodePipeline, see Use CodePipeline with CodeBuild.

  12. To find the build output artifact after the deployment is complete, sign in to the instance and look in the /tmp directory for the file named my-app-1.0-SNAPSHOT.jar.

Related resources