[AWS/CodeDeploy] 설계 오류 대책 비망록(주로 AppSpec)

개요


CodeDeploy에서 depro에서 발생한 오류와 대책에 대한 비망록
주로 AppSpec의 쓰기 방법입니다.

전제 조건


S3에 원본 파일 그룹을 zip 파일로 배치하고 이 파일을 사용하여CodeDeploy로 EC2를 처리합니다
디렉토리 구성은 다음과 같습니다.
  • 모든 디렉터리/파일의 소유자, 그룹의fuga
  • 644www/tmp의 권한
  • 777
  • 위임ww/tmp/request
  • 644ww/tmp/response 권한 수여
  • 디렉토리 구조
    hoge
      |-- hoge1.php
      |-- hoge2.php
      |-- hoge3.php
      |-- www
           |-- hoge.php
           |-- tmp
                | -- request
    	    | -- response
    
    ※ 현장 내용을 샘플 명칭으로 변경.

    보충 사항


    발생한 순서에 따라 오류를 해결하는 형식
    최종 버전만 필요하면 완성형 부분을 참조하십시오

    사례 및 대책


    박스 1


    CodeDeploy에서 오류가 발생하지 않지만 예상 권한에 도달하지 못한 경우
    이 시점의 AppSpec 내용은 다음과 같습니다.
    appspec.yml
    version: 0.0
    os: linux
    files:
      - source: /
        destination: /hoge
    permissions:
      - object: /hoge
        pattern: "**"
        owner: fuga
        group: fuga
    permissions:
      - object: /hoge/www/tmp
       pattern: "**"
        owner: fuga
        group: fuga
        mode: 644
        type:
          - directory
    permissions:
      - object: /hoge/www/tmp/request
        owner: fuga
        group: fuga
        mode: 777
        type:
          - directory
    permissions:
      - object: /hoge/www/tmp/response
        owner: fuga
        group: fuga
        mode: 644
        type:
          - directory
    

    원인과 대책


    왜냐하면 모든 Object가 permissions를 발표했어요.
    두 번째 행 이후의 "perissions:"삭제

    박스 2


    디버그 후 콘솔에 다음과 같은 오류가 발생했습니다
    잘못된 내용
    The overall deployment failed because too many individual instances failed deployment, too few healthy instances are available for deployment, or some instances in your deployment group are experiencing problems.
    
    이 시점의 AppSpec 내용은 다음과 같습니다.
    appspec.yml
    version: 0.0
    os: linux
    files:
      - source: /
        destination: /hoge
    permissions:
      - object: /hoge
        pattern: "**"
        owner: fuga
        group: fuga
      - object: /hoge/www/tmp
       pattern: "**"
        owner: fuga
        group: fuga
        mode: 644
        type:
          - directory
      - object: /hoge/www/tmp/request
        owner: fuga
        group: fuga
        mode: 777
        type:
          - directory
      - object: /hoge/www/tmp/response
        owner: fuga
        group: fuga
        mode: 644
        type:
          - directory
    

    원인과 대책


    한 객체에 대해 여러 번 권한을 설정하기 때문입니다.
    (hoge 부하의 모든 대상에 권한을 설정하면 부하의 www/tmp/request, www/tmp/response에 권한을 설정합니다)
    특정 대상의 권한만 변경하려면 except 중복을 사용하지 마십시오

    상자


    디버그 후 컨트롤 데스크톱에서/var/log/aws/codeploy-agent/codeploy-agent.log에서 다음 오류가 발생했습니다
    ※ 후자는 오류가 발생한 EC2 사례에서 확인 가능
    오류 내용(콘솔)
    The overall deployment failed because too many individual instances failed deployment, too few healthy instances are available for deployment, or some instances in your deployment group are experiencing problems.
    
    오류 내용(codeplay-agent.log)
    The deployment failed because the permissions setting for (/hoge/www/tmp/request) is specified more than once in the application specification file.
    
    이 시점의 AppSpec 내용은 다음과 같습니다.
    appspec.yml
    version: 0.0
    os: linux
    files:
      - source: /
        destination: /hoge
    permissions:
      - object: /hoge
        pattern: "**"
        except: [hoge/www/tmp]
        owner: fuga
        group: fuga
      - object: /hoge/www/tmp
       pattern: "**"
       except: [hoge/www/tmp/request,hoge/www/tmp/response]
        owner: fuga
        group: fuga
        mode: 644
        type:
          - directory
      - object: /hoge/www/tmp/request
        owner: fuga
        group: fuga
        mode: 777
        type:
          - directory
      - object: /hoge/www/tmp/response
        owner: fuga
        group: fuga
        mode: 644
        type:
          - directory
    

    원인과 대책


    의외로!CodeDeploy 자체의 결함이 원인입니다.
    except의 중첩은 NG입니다.
    (이곳에서 except에서 hoge/www/tmp/request, hoge/www/tmp/response를 사용한 후 except 부하의 hoge/ww/tmp/response를 시도합니다.)
    상세한 상황은 아래와 같다.
    https://github.com/aws/aws-codedeploy-agent/issues/151
    부득이하게 프로그램이 시작될 때 발로 대응합니다

    완성


    AppSpec의 완성형입니다.
    굉장히 쉬워 보여요.
    appspec.yml
    version: 0.0
    os: linux
    files:
      - source: /
        destination: /hoge
    permissions:
      - object: /hoge
        pattern: "**"
        owner: fuga
        group: fuga
    hooks:
      ApplicationStart:
        - location: scripts/changePermission.sh
    
    scripts/changePermission.sh의 내용은 다음과 같다
    ※ hoge 카탈로그 바로 아래, scripts 카탈로그, change Permission 커팅.sh 추가 중
    scripts/changePermission.sh
    #!bin/bash
    
    # change permissions
    chmod 644 /hoge/www/tmp
    chmod 777 /hoge/www/tmp/request
    chmod 644 /hoge/www/tmp/response
    
    hoge/www/tmp에만 except를 진행하고 다른 권한을 AppSpec에 기술합니다./hoge/www/tmp/request에만 조개 스크립트에서 권한을 변경하는 방법도 있지만, 부하가 여분의 파일 (.gitkeep) 이 있으면 오류가 발생할 수 있기 때문에 이 쓰기 방법이 됩니다.

    기타


    또한 원본 파일을 Git 관리하고 직접 디버깅하면 오류가 발생합니다.
    git를 디렉터리와 함께 삭제한 다음 zip화→디버깅을 진행합니다.
    (가급적 gitignore, README.md 등 운행 환경에서 사용하지 마라)

    끝맺다


    연이은 실수로 가슴이 찢어질 것 같았지만 해결할 수 있어 안심이 되었다.
    하지만 마지막 실수는 CodeDeploy 측의 실수였다

    참고 자료

  • CodeDeploy Spec 파일 참조 | aws
  • AppSpec의'파일'구역(EC2/예매권 시작만)|aws
  • 권한 시스템

  • AppSpec의 "접근 허가" 부분 (EC2/만 개봉) |aws
  • CodeDeploy를 통해 디렉터리 권한 설정
  • 좋은 웹페이지 즐겨찾기