Trying to add a CloudFront invalidation command in buildspec.yml throws 254 error
Asked Answered
D

0

6

I'm trying to invalidate the Cloudfront cache after a build has done and what I get is the following error in Codebuild : [Container] 2022/05/16 15:46:11 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: aws cloudfront create-invalidation --distribution-id myid --paths '/*'. Reason: exit status 254

Here's my BuildSpec definition

version: 0.2
env:
  variables:
    APP_NAME: "managerui"
phases:
  install:
    runtime-versions:
      nodejs: 14.x
    commands:
      - echo install process started
      - cd src/UI/managerui/
      - ls 
      - npm install && npm install -g @angular/cli
  build:
    commands:
      - echo build process started now
      - ls 
      - ng build --configuration=production
  post_build:
    commands:
      - echo build process finished, we should uplload to S3 now
      - ls 
      - cd dist/
      - ls -la
      - aws s3 sync . s3://ett-manager-ui --delete
      - aws cloudfront create-invalidation --distribution-id=myid--paths '/*

Do you see anything that's wrong?? I've tried running the create-invalidation instruction on my running laptop and it works..

Thanks in advance

#UPDATE

I've resolved... it was a missing permission issue... I've added

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": [
                "cloudfront:UpdateDistribution",
                "cloudfront:DeleteDistribution",
                "cloudfront:CreateInvalidation"
            ],
            "Resource": "arn:aws:cloudfront::<account_id>:distribution/<distribution_id>"
        }
    ]
}

and it works fine.

This can be closed

Duffer answered 16/5, 2022 at 15:53 Comment(3)
where did you add that permission?Harlequin
I just tried and it worked by adding them in the service role used by CodePipeline ;)Hybridize
In case someone is trying this and it is not working. Consider checking that the distribution_id in the buildspec.yml file is the correct oneLafreniere

© 2022 - 2024 — McMap. All rights reserved.