Boommeister
Boommeister

Reputation: 2107

AWS CodeBuild error: Expected Phases to be non-empty at line 2

I'm trying to deploy my Angular 8 App on AWS CodeBuild. But I get this error: DOWNLOAD_SOURCE Failed YAML_FILE_ERROR: Expected Phases to be non-empty at line 2

And also an ACCESS DENIED error in the build log events, although I granted all public access when I created the S3 bucket. COMMAND_EXECUTION_ERROR: Error while executing command: aws s3 cp dist s3://${S3_BUCKET} --recursive. Reason: exit status 1

I added a access policy to my S3 bucket like described here, but that didn't help.

My buildspec.yml file looks like this and the phases list is not empty:

version: 0.1
phases:
  install:
    commands:
      - npm install -g @angular/cli
  build:
    commands:
      - npm install
      - ng build --prod --configuration=${BUILD_ENV}
  post_build:
    commands:
      - aws s3 cp dist s3://${S3_BUCKET} --recursive
      - aws configure set preview.cloudfront true
      - echo ${S3_BUCKET}
artifacts:
  files:
    - '**/*'
base-directory: 'dist*'
discard-paths: yes

I never worked with YAML before, what am I doing wrong here?

Here are the build log events:

2020-06-13T10:12:47.354+02:00
[Container] 2020/06/13 08:12:45 Entering phase PRE_BUILD

2020-06-13T10:12:47.354+02:00
[Container] 2020/06/13 08:12:45 Phase complete: PRE_BUILD State: SUCCEEDED

2020-06-13T10:12:47.354+02:00
[Container] 2020/06/13 08:12:45 Phase context status code: Message:

2020-06-13T10:12:47.354+02:00
[Container] 2020/06/13 08:12:45 Entering phase BUILD

2020-06-13T10:12:47.354+02:00
[Container] 2020/06/13 08:12:45 Running command npm install

2020-06-13T10:13:07.379+02:00

2020-06-13T10:13:07.379+02:00
> [email protected] postinstall /codebuild/output/src970973936/src/node_modules/core-js

2020-06-13T10:13:07.379+02:00
> node -e "try{require('./postinstall')}catch(e){}"

2020-06-13T10:13:07.379+02:00

2020-06-13T10:13:07.379+02:00
[96mThank you for using core-js ([94m https://github.com/zloirock/core-js [96m) for polyfilling     
JavaScript standard library![0m

2020-06-13T10:13:07.379+02:00

2020-06-13T10:13:07.379+02:00
[96mThe project needs your help! Please consider supporting of core-js on Open Collective or 
Patreon: [0m

2020-06-13T10:13:07.379+02:00
[96m>[94m https://opencollective.com/core-js [0m

2020-06-13T10:13:07.379+02:00
[96m>[94m https://www.patreon.com/zloirock [0m

2020-06-13T10:13:07.379+02:00

2020-06-13T10:13:07.379+02:00 
[96mAlso, the author of core-js ([94m https://github.com/zloirock [96m) is looking for a good 
job -)[0m

2020-06-13T10:13:07.379+02:00

2020-06-13T10:13:07.379+02:00

2020-06-13T10:13:07.379+02:00
> @angular/[email protected] postinstall /codebuild/output/src970973936/src/node_modules/@angular/cli

2020-06-13T10:13:07.379+02:00
> node ./bin/postinstall/script.js

2020-06-13T10:13:07.379+02:00

2020-06-13T10:13:09.415+02:00
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/webpack-dev-server
/node_modules/fsevents):

2020-06-13T10:13:09.415+02:00
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted 
{"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

2020-06-13T10:13:09.415+02:00
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/watchpack/node_modules
/fsevents):

2020-06-13T10:13:09.415+02:00
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted     
{"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

2020-06-13T10:13:09.415+02:00
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/fsevents):

2020-06-13T10:13:09.415+02:00
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted    
{"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

2020-06-13T10:13:09.415+02:00

2020-06-13T10:13:09.415+02:00
 added 1413 packages from 1124 contributors and audited 1418 packages in 22.639s

2020-06-13T10:13:09.415+02:00

2020-06-13T10:13:09.415+02:00
run `npm fund` for details

2020-06-13T10:13:09.415+02:00

2020-06-13T10:13:09.415+02:00
found 5 vulnerabilities (3 low, 2 high)

2020-06-13T10:13:09.415+02:00
run `npm audit fix` to fix them, or `npm audit` for details

2020-06-13T10:13:09.415+02:00

2020-06-13T10:13:09.415+02:00
[Container] 2020/06/13 08:13:09 Running command ng build --prod --configuration=${BUILD_ENV}

2020-06-13T10:13:15.474+02:00
Compiling @angular/animations : es2015 as esm2015

2020-06-13T10:13:15.474+02:00
Compiling @angular/core : es2015 as esm2015

2020-06-13T10:13:19.504+02:00
Compiling @angular/animations/browser : es2015 as esm2015

2020-06-13T10:13:21.526+02:00
Compiling @angular/animations/browser/testing : es2015 as esm2015

2020-06-13T10:13:21.526+02:00
Compiling @angular/common : es2015 as esm2015

2020-06-13T10:13:23.553+02:00
Compiling @angular/common/http : es2015 as esm2015

2020-06-13T10:13:23.553+02:00
Compiling @angular/common/http/testing : es2015 as esm2015

2020-06-13T10:13:25.579+02:00
Compiling @angular/forms : es2015 as esm2015

2020-06-13T10:13:27.607+02:00
Compiling @angular/platform-browser : es2015 as esm2015

2020-06-13T10:13:27.607+02:00
Compiling @angular/platform-browser/animations : es2015 as esm2015

2020-06-13T10:13:29.635+02:00
Compiling @angular/core/testing : es2015 as esm2015

2020-06-13T10:13:29.635+02:00
Compiling @angular/platform-browser-dynamic : es2015 as esm2015

2020-06-13T10:13:31.660+02:00
Compiling @angular/platform-browser/testing : es2015 as esm2015

2020-06-13T10:13:31.660+02:00
Compiling @angular/compiler/testing : es2015 as esm2015

2020-06-13T10:13:31.660+02:00
Compiling @angular/platform-browser-dynamic/testing : es2015 as esm2015

2020-06-13T10:13:33.685+02:00
Compiling @angular/common/testing : es2015 as esm2015

2020-06-13T10:13:33.685+02:00
Compiling @angular/router : es2015 as esm2015

2020-06-13T10:13:35.712+02:00
Compiling @angular/router/testing : es2015 as esm2015

2020-06-13T10:14:09.752+02:00
Generating ES5 bundles for differential loading...

2020-06-13T10:14:23.800+02:00
ES5 bundle generation complete.

2020-06-13T10:14:23.800+02:00

2020-06-13T10:14:23.800+02:00
chunk {0} runtime-es2015.1eba213af0b233498d9d.js (runtime) 1.45 kB [entry] [rendered]

2020-06-13T10:14:23.800+02:00
chunk {0} runtime-es5.1eba213af0b233498d9d.js (runtime) 1.45 kB [entry] [rendered]

2020-06-13T10:14:23.800+02:00
chunk {1} main-es2015.cfbcf44215fba926de2a.js (main) 299 kB [initial] [rendered]

2020-06-13T10:14:23.800+02:00
chunk {1} main-es5.cfbcf44215fba926de2a.js (main) 362 kB [initial] [rendered]

2020-06-13T10:14:23.800+02:00
chunk {2} polyfills-es2015.690002c25ea8557bb4b0.js (polyfills) 36.1 kB [initial] [rendered]

2020-06-13T10:14:23.800+02:00
chunk {3} polyfills-es5.9e286f6d9247438cbb02.js (polyfills-es5) 129 kB [initial] [rendered]

2020-06-13T10:14:23.800+02:00
chunk {4} styles.9235c00a82b231f12fd9.css (styles) 586 bytes [initial] [rendered]

2020-06-13T10:14:23.800+02:00
Date: 2020-06-13T08:14:22.612Z - Hash: a55ad60a4042b04ff781 - Time: 70971ms

2020-06-13T10:14:23.800+02:00

2020-06-13T10:14:23.800+02:00

2020-06-13T10:14:23.800+02:00

2020-06-13T10:14:23.800+02:00
**[Container] 2020/06/13 08:14:22 Phase complete: BUILD State: SUCCEEDED**

2020-06-13T10:14:23.800+02:00
[Container] 2020/06/13 08:14:22 Phase context status code: Message:

2020-06-13T10:14:23.800+02:00
**[Container] 2020/06/13 08:14:22 Entering phase POST_BUILD**

2020-06-13T10:14:23.800+02:00
[Container] 2020/06/13 08:14:22 Running command aws s3 cp dist s3://${S3_BUCKET} --recursive

2020-06-13T10:14:29.834+02:00
**upload failed: dist/studileih/styles.9235c00a82b231f12fd9.css to s3://studileih-cicd-env.com
/studileih/styles.9235c00a82b231f12fd9.css An error occurred (AccessDenied) when calling the 
PutObject operation: Access Denied**

2020-06-13T10:14:29.834+02:00
**Completed 586 Bytes/847.7 KiB (0 Bytes/s) with 9 file(s) remaining upload failed: dist/studileih  
/runtime-es5.1eba213af0b233498d9d.js to s3://studileih-cicd-env.com/studileih/runtime-
es5.1eba213af0b233498d9d.js An error occurred (AccessDenied) when calling the PutObject operation: 
Access Denied**

2020-06-13T10:14:29.834+02:00
**Completed 2.0 KiB/847.7 KiB (0 Bytes/s) with 8 file(s) remaining upload failed: dist/studileih
/index.html to s3://studileih-cicd-env.com/studileih/index.html An error occurred (AccessDenied) 
when calling the PutObject operation: Access Denied**

2020-06-13T10:14:29.834+02:00
**Completed 3.0 KiB/847.7 KiB (0 Bytes/s) with 7 file(s) remaining upload failed: dist/studileih
/runtime-es2015.1eba213af0b233498d9d.js to s3://studileih-cicd-env.com/studileih/runtime-
es2015.1eba213af0b233498d9d.js An error occurred (AccessDenied) when calling the PutObject 
operation: Access Denied**

[...followed by some more similar ACCESS DENIED LOGS]

2020-06-13T10:14:29.834+02:00
**[Container] 2020/06/13 08:14:28 Command did not exit successfully aws s3 cp dist s3://${S3_BUCKET}    
--recursive exit status 1**

2020-06-13T10:14:29.834+02:00
**[Container] 2020/06/13 08:14:28 Phase complete: POST_BUILD State: FAILED**

2020-06-13T10:14:29.834+02:00
**[Container] 2020/06/13 08:14:28 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error   
while executing command: aws s3 cp dist s3://${S3_BUCKET} --recursive. Reason: exit status 1**

2020-06-13T10:14:29.834+02:00
[Container] 2020/06/13 08:14:28 Expanding base directory path: .

2020-06-13T10:14:29.834+02:00
[Container] 2020/06/13 08:14:28 Assembling file list

2020-06-13T10:14:29.834+02:00
[Container] 2020/06/13 08:14:28 Expanding .

2020-06-13T10:14:29.834+02:00
[Container] 2020/06/13 08:14:28 Expanding file paths for base directory .

2020-06-13T10:14:29.834+02:00
[Container] 2020/06/13 08:14:28 Assembling file list

2020-06-13T10:14:29.834+02:00
[Container] 2020/06/13 08:14:28 Expanding **/*

2020-06-13T10:14:29.834+02:00
[Container] 2020/06/13 08:14:28 Found 57833 file(s)

2020-06-13T10:14:43.865+02:00
[Container] 2020/06/13 08:14:42 Phase complete: UPLOAD_ARTIFACTS State: SUCCEEDED

2020-06-13T10:14:43.865+02:00
[Container] 2020/06/13 08:14:42 Phase context status code: Message:

SOLVED: The service role I attached to my Build project in CodeBuild after Marcin's Answer down below - The build succeeded now!:

the s3 bucket policy

Upvotes: 2

Views: 1914

Answers (1)

Marcin
Marcin

Reputation: 238131

The command aws s3 cp dist s3://${S3_BUCKET} --recursive is failing because role associated with your code build project does not have s3:PutObject permissions to the ${S3_BUCKET}.

The link you provided to the S3 bucket policy is the wrong policy, as it should allow access the IAM role that CodeBuild is using, not the IAM user.

To rectify the issue, you have two options:

  1. Add needed permissions to the CodeBuild role that is used to build your project. The link also shows an example CodeBuild role with S3 permissions.
  2. Add the role to the bucket policy.

You haven't posted the Bucket Policy that you use, so I just provide an example:

{
    "Id": "MyPolicyForCodeBuild",
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AllowCodeBuild",
            "Action": "s3:*",
            "Effect": "Allow",
            "Resource": [
                "arn:aws:s3:::<your-bucket>/*",
                "arn:aws:s3:::<your-bucket>"
            ],
            "Principal": {
                "AWS": [
                    "<arn-of-codebuild-role>"
                ]
            }
        }
    ]
}

Upvotes: 2

Related Questions