Menu
AWS CodeBuild
User Guide (API Version 2016-10-06)

AWS CodeDeploy Sample for AWS CodeBuild

This sample instructs AWS CodeBuild to use Maven to produce as build output a single JAR file named my-app-1.0-SNAPSHOT.jar. This sample then uses AWS CodeDeploy to deploy the JAR file to an Amazon Linux instance. (Alternatively, you can use AWS CodePipeline to automate the use of AWS 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 may result in charges to your AWS account. These include possible charges for AWS CodeBuild and for AWS resources and actions related to Amazon S3, AWS KMS, CloudWatch Logs, and Amazon EC2. For more information, see AWS CodeBuild Pricing, Amazon S3 Pricing, AWS 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.

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

    If successful, this directory structure and files will be created.

    Copy
    (root directory name) `-- 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 (root directory name)/my-app directory.

    Copy
    version: 0.2 phases: 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 (root directory name)/my-app directory.

    Copy
    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.

    Copy
    (root directory name) `-- 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 (root directory name)/my-app, and then upload the ZIP file to a source code repository type supported by AWS CodeBuild and AWS CodeDeploy, such as an Amazon S3 input bucket or a GitHub or Bitbucket repository.

    Important

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

    Do not add (root directory name) or (root directory name)/my-app to the ZIP file, just the directories and files inside of (root directory name)/my-app. The ZIP file should contain these directories and files:

    Copy
    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 AWS 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.)

    Copy
    { "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/java:openjdk-8", "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 AWS CodeDeploy, then follow the steps in Run a Build. Otherwise, skip this step. (This is because if you plan to deploy the build output artifact with AWS CodePipeline, then AWS CodePipeline will use AWS CodeBuild to run the build automatically.)

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

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

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

    • Create or identify an IAM instance profile to enable your instances to access the Amazon 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 AWS CodeDeploy User Guide.

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

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

  11. Deploy the build output artifact to the instance.

    To deploy with AWS CodeDeploy, see Deploying a Revision with AWS CodeDeploy in the AWS CodeDeploy User Guide.

    To deploy with AWS CodePipeline, see Use AWS CodePipeline with AWS 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