AWS Codebuild puting all artifacts in root of S3 bucket

I think you're missing a piece - it's not clear but you need to specify the path in your Codebuild template with the following artifacts:

artifacts:
  files:
    - '**/*'
  base-directory: 'dist'

And then ensure that in the "Artifacts" section of your codebuild project, you specify the S3 bucket normally, but add the "optional" name parameter to be /

This sets the output to the root of the S3 directory - see https://docs.aws.amazon.com/codebuild/latest/APIReference/API_ProjectArtifacts.html#CodeBuild-Type-ProjectArtifacts-name

If type is set to S3, this is the name of the output artifact object. If you set the name to be a forward slash ("/"), the artifact is stored in the root of the output bucket.


I only recently figured this out myself, so experts please forgive me if I am incorrect, but:

The "discard-paths: yes" is what throws away your directory structure. It essentially flattens your artifact set. If (like me) you only want to throw away paths up to your artifact folder, put that full path in the "base-directory" field.

Again, this part I'm not sure about, but it seemed to me that you can't tell CodeBuild not to apply server side encryption to your artifacts, meaning if its a website you intend to allow people to browse to, that's not going to work, at least out of the box. What I ended up doing was instead of using the artifact section of codebuild at all, I added the following post_build command:

commands: - aws s3 sync my/artifact/path/ s3://my-bucket-name/

edit: I am building using a docker image I created and added to Amazon's ECR, I had to install the AWS CLI in my image to be able to run that command.


Adhering to the paradigma of CodePipeline, I'd suggest you produce an artifact containing only the folder using code build, and then deploy that artifact to whereever, e.g. to S3 using AWS Code Deploy.

But if if you really want to use Code Build then the following is a solution:

In the Codebuild template use:

artifacts:
    files:
        - "dist/**/*"

Then in the AWS Console edit the "Artifacts" section of your codebuild project, use the following inputs:

  • "Name": use the forward slash ("/")
  • "Path - optional": leave empty
  • "Namespace type - optional": choose "None"

Other Examples taken from aws documentation:

  • If path is set to MyArtifacts, namespaceType is set to BUILD_ID, and name is set to MyArtifact.zip, then the output artifact is stored in MyArtifacts//MyArtifact.zip.

  • If path is empty, namespaceType is set to NONE, and name is set to "/", the output artifact is stored in the root of the output bucket.

  • If path is set to MyArtifacts, namespaceType is set to BUILD_ID, and name is set to "/", the output artifact is stored in MyArtifacts/.