표준 배포 시작(AWS CLI) - Amazon CloudFront

표준 배포 시작(AWS CLI)

이 섹션에 나온 절차에서는 CloudFront에서 AWS CLI를 사용하여 다음을 수행하는 기본 구성 설정 방법을 보여줍니다.

  • 배포 오리진으로 사용할 Amazon S3 버킷 생성.

  • 객체의 원래 버전을 S3 버킷에 저장.

  • 오리진 액세스 제어(OAC)를 사용하여 Amazon S3 오리진에 인증된 요청 전송. OAC는 CloudFront를 통해 요청을 전송하여 최종 사용자가 S3 버킷에 직접 액세스하는 것을 방지합니다. OAC에 대한 자세한 내용은 Amazon S3 오리진에 대한 액세스 제한을 참조합니다.

  • 객체에 대한 URL에 CloudFront 도메인 이름(예: https://d111111abcdef8.cloudfront.net/index.html) 사용.

  • 기본 24시간(최소 시간 0초) 동안 CloudFront 엣지 로케이션에 객체 보관.

이러한 옵션은 대부분 사용자 지정이 가능합니다. CloudFront 배포 옵션을 사용자 지정하는 방법은 배포 생성 단원을 참조하세요.

사전 조건

시작하기 전에 먼저 AWS 계정 설정의 단계를 완료해야 합니다.

AWS CLI를 설치하고 자격 증명을 사용하여 구성. 자세한 내용은 AWS CLI 사용 설명서에서 AWS CLI 시작하기를 참조하세요.

Amazon S3 버킷 생성

Amazon S3 버킷은 파일(객체) 또는 폴더를 위한 컨테이너입니다. CloudFront는 S3 버킷을 소스로 사용하여 거의 모든 유형의 파일을 배포할 수 있습니다. 예를 들어 CloudFront에서는 텍스트, 이미지 및 비디오를 배포할 수 있습니다. Amazon S3에 저장할 수 있는 데이터의 양에는 최대값이 없습니다.

이 자습서에서는 S3 버킷을 생성하고 기본 웹페이지를 생성하는 데 사용할 HTML 파일을 업로드합니다.

aws s3 mb s3://amzn-s3-demo-bucket/ --region us-east-1

amzn-s3-demo-bucket을 전역적으로 고유한 버킷 이름으로 바꿉니다. AWS 리전의 경우 지리적으로 가까운 리전을 선택하는 것이 좋습니다. 이렇게 하면 지연 시간과 비용이 줄어듭니다. 물론 다른 리전을 선택하는 것도 가능합니다. 예를 들어 규제 요구 사항을 해결하기 위해 이렇게 할 수 있습니다.

버킷에 콘텐츠 업로드

이 자습서에서는 기본적인 "Hello World" 웹페이지의 샘플 콘텐츠 파일을 다운로드하고 추출합니다.

# Create a temporary directory mkdir -p ~/cloudfront-demo # Download the sample Hello World files curl -o ~/cloudfront-demo/hello-world-html.zip https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/samples/hello-world-html.zip # Extract the zip file unzip ~/cloudfront-demo/hello-world-html.zip -d ~/cloudfront-demo/hello-world

이렇게 하면 index.html 파일과 css 폴더가 있는 디렉터리가 생성됩니다. 이러한 파일을 S3 버킷에 업로드합니다.

aws s3 cp ~/cloudfront-demo/hello-world/ s3://amzn-s3-demo-bucket/ --recursive>

오리진 액세스 제어(OAC) 생성

이 자습서에서는 오리진 액세스 제어(OAC)를 생성합니다. OAC를 사용하면 Amazon S3 오리진에 인증된 요청을 안전하게 전송할 수 있습니다. OAC에 대한 자세한 내용은 Amazon S3 오리진에 대한 액세스 제한을 참조합니다.

aws cloudfront create-origin-access-control \ --origin-access-control-config Name="oac-for-s3",SigningProtocol=sigv4,SigningBehavior=always,OriginAccessControlOriginType=s3

출력의 OAC ID를 환경 변수로 저장합니다. 예시 값을 실제 OAC ID로 바꿉니다. 다음 단계에서 이 값을 사용합니다.

OAC_ID="E1ABCD2EFGHIJ"

표준 배포 생성

distribution-config.json이라는 이름으로 배포 구성 파일을 만듭니다. Id, DomainName, TargetOriginId 값을 포함해 예시 버킷 이름을 실제 버킷 이름으로 바꿉니다.

cat > distribution-config.json << EOF { "CallerReference": "cli-example-$(date +%s)", "Origins": { "Quantity": 1, "Items": [ { "Id": "S3-amzn-s3-demo-bucket", "DomainName": "amzn-s3-demo-bucket.s3.amazonaws.com", "S3OriginConfig": { "OriginAccessIdentity": "" }, "OriginAccessControlId": "$OAC_ID" } ] }, "DefaultCacheBehavior": { "TargetOriginId": "S3-amzn-s3-demo-bucket", "ViewerProtocolPolicy": "redirect-to-https", "AllowedMethods": { "Quantity": 2, "Items": ["GET", "HEAD"], "CachedMethods": { "Quantity": 2, "Items": ["GET", "HEAD"] } }, "DefaultTTL": 86400, "MinTTL": 0, "MaxTTL": 31536000, "Compress": true, "ForwardedValues": { "QueryString": false, "Cookies": { "Forward": "none" } } }, "Comment": "CloudFront distribution for S3 bucket", "Enabled": true } EOF

표준 배포를 생성합니다.

aws cloudfront create-distribution --distribution-config file://distribution-config.json

출력의 배포 ID와 도메인 이름을 환경 변수로 저장합니다. 예제 값을 사용자의 값으로 바꿉니다. 이 자습서 뒷부분에서 이 정보가 필요합니다.

DISTRIBUTION_ID="EABCD1234XMPL" DOMAIN_NAME="d111111abcdef8.cloudfront.net"

이 자습서의 배포 및 S3 버킷을 프로덕션 환경에서 사용하기 전에 먼저 특정 요구 사항에 맞게 구성해야 합니다. 프로덕션 환경에서 액세스를 구성하는 방법에 대한 자세한 내용은 보안 액세스 구성 및 콘텐츠에 대한 액세스 제한 섹션을 참조합니다.

S3 버킷 정책 업데이트

CloudFront가 객체에 액세스할 수 있도록 S3 버킷 정책을 업데이트합니다. 예시 버킷 이름을 실제 버킷 이름으로 바꿉니다.

# Get your AWS account ID ACCOUNT_ID=$(aws sts get-caller-identity --query 'Account' --output text) # Create the bucket policy cat > bucket-policy.json << EOF { "Version": "2012-10-17", "Statement": [ { "Sid": "AllowCloudFrontServicePrincipal", "Effect": "Allow", "Principal": { "Service": "cloudfront.amazonaws.com" }, "Action": "s3:GetObject", "Resource": "arn:aws:s3:::amzn-s3-demo-bucket/*", "Condition": { "StringEquals": { "AWS:SourceArn": "arn:aws:cloudfront::$ACCOUNT_ID:distribution/$DISTRIBUTION_ID" } } } ] } EOF # Apply the bucket policy aws s3api put-bucket-policy \ --bucket amzn-s3-demo-bucket \ --policy file://bucket-policy.json

배포 작업 확인

배포를 생성한 후 배포를 완료하는 데 다소 시간이 걸립니다. 배포 상태가 InProgress에서 Deployed로 변경되면 다음 단계로 진행합니다.

aws cloudfront get-distribution --id $DISTRIBUTION_ID --query 'Distribution.Status'

또는 wait 명령을 사용하여 배포 작업을 기다릴 수 있습니다.

aws cloudfront wait distribution-deployed --id $DISTRIBUTION_ID

CloudFront를 통해 콘텐츠에 액세스

CloudFront를 통해 콘텐츠에 액세스하려면 CloudFront 배포의 도메인 이름과 콘텐츠의 기본 페이지를 결합합니다. 예시 CloudFront 도메인 이름을 실제 도메인 이름으로 바꿉니다.

https://d111111abcdef8.cloudfront.net/index.html

이전 단계를 수행했고 HTML 파일을 생성했다면 Hello world!라고 써있는 웹 페이지가 표시됩니다.

이 S3 버킷에 더 많은 콘텐츠를 업로드하는 경우 CloudFront 배포 도메인 이름과 S3 버킷의 객체 경로를 결합하여 CloudFront를 통해 콘텐츠에 액세스할 수 있습니다. 예를 들어 new-page.html이라는 새 파일을 S3 버킷의 루트에 업로드하는 경우 URL은 다음과 같습니다.

https://d111111abcdef8.cloudfront.net/new-page.html.

정리

실습용으로만 배포 및 S3 버킷을 생성한 경우에는 요금이 발생하지 않도록 삭제합니다. 먼저 배포를 비활성화하고 삭제합니다.

표준 배포를 비활성화 및 삭제하려면(AWS CLI)
  1. 먼저 배포를 비활성화합니다.

    # Get the current configuration and ETag ETAG=$(aws cloudfront get-distribution-config --id $DISTRIBUTION_ID --query 'ETag' --output text) # Create a modified configuration with Enabled=false aws cloudfront get-distribution-config --id $DISTRIBUTION_ID | \ jq '.DistributionConfig.Enabled = false' > temp_disabled_config.json # Update the distribution to disable it aws cloudfront update-distribution \ --id $DISTRIBUTION_ID \ --distribution-config file://<(jq '.DistributionConfig' temp_disabled_config.json) \ --if-match $ETAG
  2. 배포가 비활성화될 때까지 기다립니다.

    aws cloudfront wait distribution-deployed --id $DISTRIBUTION_ID
  3. 배포를 삭제합니다.

    # Get the current ETag ETAG=$(aws cloudfront get-distribution-config --id $DISTRIBUTION_ID --query 'ETag' --output text) # Delete the distribution aws cloudfront delete-distribution --id $DISTRIBUTION_ID --if-match $ETAG
S3 버킷을 삭제하려면(AWS CLI)
  • S3 버킷과 해당 콘텐츠를 삭제합니다. 예시 버킷 이름을 실제 버킷 이름으로 바꿉니다.

    # Delete the bucket contents aws s3 rm s3://amzn-s3-demo-bucket --recursive # Delete the bucket aws s3 rb s3://amzn-s3-demo-bucket

이 자습서용으로 생성된 로컬 파일을 정리하려면 다음 명령을 실행합니다.

# Clean up local files rm -f distribution-config.json bucket-policy.json temp_disabled_config.json rm -rf ~/cloudfront-demo

선택적으로 이 자습서용으로 생성한 OAC를 삭제할 수 있습니다.

# Get the OAC ETag OAC_ETAG=$(aws cloudfront get-origin-access-control --id $OAC_ID --query 'ETag' --output text) # Delete the OAC aws cloudfront delete-origin-access-control --id $OAC_ID --if-match $OAC_ETAG