s3 + cloudfront : github action deploy

hoho kim·2022년 1월 11일
0

front end static 파일들을 s3 + cloudfront를 통해서

서버리스로 구축하기위해서 진행한 설정을 정리해 본다.

셋팅을 위해 aws에서 사용한 서비스들

s3, cloudfront, acm, iam

프로세스

  1. main branch에 커밋
  2. github action을 통해서 s3에 파일 업로드
  3. cloudfront invalidation (캐시 삭제)

github action

ci는 생략. 배포를 위한 테스트 이다.
public 폴더에 있는 파일을 s3버킷의 루트에 옮겨지도록 설정했다.
파일 전송 후 cloud front 캐시를 날려주는 순서로 진행된다.

name: CD

on:

  push:
    branches: [ main ]

jobs:

  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2

      - uses: jakejarvis/s3-sync-action@master
        with:
          args: --acl public-read --follow-symlinks --delete
        env:
          AWS_S3_BUCKET: ${{ secrets.AWS_S3_BUCKET }}
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_REGION: 'ap-southeast-1'
          SOURCE_DIR: './public'
      - uses: awact/cloudfront-action@master
        env:
          SOURCE_PATH: '/*'
          AWS_REGION: 'ap-southeast-1'
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          DISTRIBUTION_ID: ${{ secrets.DISTRIBUTION_ID }}

s3 설정

https://docs.aws.amazon.com/ko_kr/AmazonS3/latest/userguide/HostingWebsiteOnS3Setup.html

위 링크로 설명은 생략.

주위 사항은 해당 버킷의 퍼블릭 액세스, 버킷 정책, 객체 소유권 설정을 이상없이 해야한다.
위 github action에서 사용된 aws iam유저는 s3, cloud front에 대한 권한을 소유해야한다.

#### 객체 소유권
편집 버튼을 통해서 ACL을 활성화 시켜야한다. (이걸 안해주면 github action step에서 오류 발생)
#### 버킷 정책
```
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": [
                "s3:PutObject",
                "s3:PutObjectAcl",
                "s3:GetObject",
                "s3:GetObjectAcl",
                "s3:DeleteObject"
            ],
            "Resource": [
                "arn:aws:s3:::s3test.fruttidino.com",
                "arn:aws:s3:::s3test.fruttidino.com/*"
            ]
        },
        {
            "Sid": "S3PolicyStmt-DO-NOT-MODIFY-1641877660400",
            "Effect": "Allow",
            "Principal": {
                "Service": "logging.s3.amazonaws.com"
            },
            "Action": [
                "s3:PutObject",
                "s3:PutObjectAcl"
            ],
            "Resource": "arn:aws:s3:::s3test.fruttidino.com/*"
        }
    ]
}
```

cloud front

참고했던 블로그 링크로 대체.

https://victorydntmd.tistory.com/335

주의 사항

옵션중 recdirect http to https를 선택해준다.

ssl 인증서는 acm을 통해서 발급 받는다.

route53에서 cloud front url을 cname으로 연결한다.

이렇게 하면 도메인을 ssl만으로 연결되게 할 수 있다.

은근 손이 많이 간다... 쉬운건 없음.. 권한에서 삽질을 엄청나게해서 시간을 많이 날렸다..

실제 frontend 프로젝트에서는 build된 폴더를 s3로 옮기면 될것이고

ci코드를 추가해서 build까지 진행해서 cd를 진행하면 될듯하다.

profile
재밌는 개발

0개의 댓글