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

Amazon Elastic File System Sample for 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 a 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.

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 a 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. For more information, see the AWS CloudFormation User Guide.


    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

  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

  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 a CodeBuild Project to Use with Amazon EFS

Create a AWS CodeBuild project that uses the Amazon VPC you created earlier in this sample. This 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

  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 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 Amazon Linux 2.

  8. From Runtime(s), choose Standard.

  9. From Runtime version choose aws/codebuild/amazonlinux2-x86_64-standard:1.0.

  10. Select Privileged.

  11. Under Service role, choose New service role. In Role name, enter a name for the role 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: "" phases: install: runtime-versions: java: openjdk11 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.

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