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

The procedures in this guide support the new console design. If you choose to use the older version of the console, you will find many of the concepts and basic procedures in this guide still apply. To access help in the new console, choose the information icon.

Amazon Elastic File System Sample for AWS CodeBuild

You might want to create your AWS CodeBuild builds on Amazon EFS. Amazon EFS is a scalable, shared file service for Amazon EC2 instances. The storage capacity with Amazon EFS is elastic, so it grows or shrinks as files are added and removed. It has a simple web services interface that you can use to create and configure file systems. It also manages all of the file storage infrastructure for you, so you do not need to worry about deploying, patching, or maintaining file system configurations. For more information, see What Is Amazon Elastic File System.

This sample shows you how to configure an AWS CodeBuild project so that it mounts and then builds a Java application to an Amazon EFS file system. Before you begin, you must have a Java application ready to build that is uploaded to an Amazon S3 input bucket or an AWS CodeCommit, GitHub, GitHub Enterprise, or Bitbucket repository. You can use the instructions in Maven in 5 Minutes Sample for AWS CodeBuild to create a Java application.

Note

If you use Maven in 5 Minutes Sample for AWS CodeBuild to create a sample Java application, you can skip the step 3 (the step that creates the file buildspec.yml). In this sample you do not need a buildspec file because you use the buildspec editor to enter build commands required to mount your Amazon EFS file system.

Amazon Elastic File System and AWS CodeBuild Sample High-Level Steps

This sample covers the three high-level steps required to use Amazon EFS with AWS CodeBuild:

  1. Create an Amazon VPC.

  2. Create an Amazon EFS that uses this Amazon VPC.

  3. Create and build an AWS CodeBuild project that uses the Amazon VPC. Instructions that include how to mount an Amazon Elastic File System file system are entered into the buildspec editor when you build the project.

Create an Amazon VPC Using AWS CloudFormation

Create your Amazon VPC with an AWS CloudFormation template.

  1. Follow the instructions here, AWS CloudFormation VPC Template, to use AWS CloudFormation to create an Amazon VPC. If you are already familiar with AWS CloudFormation, you can go directly to the AWS CloudFormation console to create a stack using the template available for download from https://s3.amazonaws.com/codebuild-cloudformation-templates-public/vpc_cloudformation_template.yml. For more information, see the AWS CloudFormation User Guide.

    Note

    The Amazon VPC created by this AWS CloudFormation template has two private subnets and two public subnets. You must only use private subnets when you use AWS CodeBuild to mount Amazon EFS. If you use one of the public subnets, the build fails.

  2. Sign in to the AWS Management Console and open the Amazon VPC console at https://console.aws.amazon.com/vpc/.

  3. Choose the Amazon VPC you created with AWS CloudFormation.

  4. Make a note of the VPC ID displayed on the Summary tab. This ID is required when you create your AWS CodeBuild project later in this sample.

Create an Amazon Elastic File System File System with Your Amazon VPC

Create a simple Amazon EFS file system for this sample using the Amazon VPC you created earlier.

  1. Sign in to the AWS Management Console and open the Amazon EFS console at https://console.aws.amazon.com/efs/.

  2. Choose Create file system.

  3. From VPC, choose the VPC ID you noted earlier in this sample.

  4. Leave the Availability Zones associated with your subnets selected.

  5. Choose Next Step.

  6. In Add tags, for the default Name key, in Value, enter the name of your Amazon EFS file system.

  7. Keep General Purpose and Bursting selected as your default performance and throughput modes, and then choose Next Step.

  8. Choose Create File System.

  9. Choose the name of the file system you created from the list. Make a note of the DNS name. You enter this in the buildspec file that is used to build your AWS CodeBuild project.

Create an AWS CodeBuild Project to Use with Amazon EFS

Create an AWS CodeBuild project that uses the Amazon VPC you created earlier in this sample. This AWS CodeBuild project does not use a source and does not create an artifact. When the build is run, it mounts the Amazon EFS file system created earlier in this sample and caches the Maven dependency to it.

  1. Open the AWS CodeBuild console at https://console.aws.amazon.com/codesuite/codebuild/home.

  2. From the navigation pane, choose Build projects, and then choose Create build project.

  3. In Project name, enter a name for your project.

  4. From Source provider, choose the repository that contains the Java application you want to build.

  5. Enter information, such as a repository URL, that AWS CodeBuild uses to locate your application. The options are different for each source provider. For more information, see Choose source provider.

  6. From Environment image, choose Managed image.

  7. From Operating system, choose Ubuntu.

  8. From Runtime, choose Java.

  9. From Runtime version choose aws/codebuild/java:openjdk-8.

  10. Select Privileged.

  11. Under Service role, choose New service role. In Role name, enter a name for the role AWS CodeBuild creates for you.

  12. For Build specification, choose Insert build commands and then choose Switch to editor.

  13. Enter the following buildspec commands into the editor. For the EFS_DNS, enter the DNS name of your file system.

    version: 0.2 env: variables: EFS_DIR: "/efs" M3_HOME: ".m2" EFS_DNS: "fs-11223344.efs.us-east-1.amazonaws.com" phases: install: commands: - mkdir -p $EFS_DIR - apt-get update && apt-get install -y nfs-common pre_build: commands: - mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 $EFS_DNS:/ $EFS_DIR - df -h - mkdir -p $EFS_DIR/$M3_HOME/ build: commands: - mvn compile -Dgpg.skip=true -Dmaven.repo.local=$EFS_DIR/$M3_HOME/
  14. Expand Additional configuration.

  15. From VPC, choose the VPC ID.

  16. From Subnets, choose one or more of the private subnets associated with your Amazon VPC. You must use private subnets in a build that mounts an Amazon EFS file system. If you use a public subnet, the build fails.

  17. From Security Groups, choose the security group that works with your Amazon VPC.

  18. Use the default values for all other settings, and then choose Create build project. When your build is complete, you are on the console page for your project.

  19. Choose Start build.

AWS CodeBuild and Amazon EFS Sample Summary

After your AWS CodeBuild project is built, you have a .jar file created by your Java application. The .jar file is built to your Amazon EFS file system in a directory called /efs/.m2. AWS CodeBuild uses the mount command, mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2, to mount the Amazon EFS file system. For more information, see Mounting File Systems.