* AWS Cloud Quest: Cloud Practitioner 과정 Terraform으로 해보기 중입니다.
오늘 할 퀘스트~!
https://juyeonee826.tistory.com/163
[AWS Cloud Quest] Cloud Computing Essentials
퀘스트 제목 Cloud Computing Essentials 퀘스트 내용 도시의 웹 포털은 신뢰성을 높이기 위해, 해변의 파도 크기를 예측하는 페이지를 AWS로 이전해야 합니다. 이는 기존에 다른 환경에서 호스팅되던 페
juyeonee826.tistory.com
목차
- S3 버킷 생성
- 파일 업로드
- 정적 웹 호스팅 활성화
- 버킷 정책 추가
- html, css의 content-type 변경
- 업로드 된 파일 이름 변경
- 결과 페이지
S3 버킷 생성
아래 세가지를 충족시킬 것입니다.
- choose ACLs enabled.
- choose Object writer.
- clear the check box to deselect Block all public access
resource "aws_s3_bucket" "static_website_bucket" {
bucket = "aws-s3-quest"
}
resource "aws_s3_bucket_ownership_controls" "static_website_bucket" {
bucket = aws_s3_bucket.static_website_bucket.id
rule {
object_ownership = "ObjectWriter"
}
}
resource "aws_s3_bucket_acl" "static_website_bucket" {
depends_on = [aws_s3_bucket_ownership_controls.static_website_bucket]
bucket = aws_s3_bucket.static_website_bucket.id
acl = "private"
}
resource "aws_s3_bucket_public_access_block" "static_website_bucket" {
bucket = aws_s3_bucket.static_website_bucket.id
block_public_acls = false
block_public_policy = false
ignore_public_acls = false
restrict_public_buckets = false
}
파일 업로드
resource "aws_s3_object" "static_website_bucket" {
for_each = fileset("./static/", "**")
bucket = aws_s3_bucket.static_website_bucket.id
key = each.value
source = "./static/${each.value}"
etag = filemd5("./static/${each.value}")
}
업로드할 파일을 static 폴더에 넣어두었습니다.
정적 웹 호스팅 활성화
resource "aws_s3_bucket_website_configuration" "static_website_bucket" {
bucket = aws_s3_bucket.static_website_bucket.id
index_document {
suffix = "index.html"
}
}
버킷 정책 추가
resource "aws_s3_bucket_policy" "static_website_bucket" {
bucket = aws_s3_bucket.static_website_bucket.id
policy = data.aws_iam_policy_document.static_website_bucket.json
}
data "aws_iam_policy_document" "static_website_bucket" {
version = "2012-10-17"
statement {
sid = "S3GetObjectAllow"
actions = [
"s3:GetObject"
]
effect = "Allow"
resources = [
"${aws_s3_bucket.static_website_bucket.arn}/*",
]
principals {
type = "AWS"
identifiers = ["*"]
}
}
}
여기까지 했는데 웹호스팅 url로 접속 시 파일이 다운로드 되는 현상 발생,,,
html, css의 content-type 변경
resource "aws_s3_object" "index_html" {
bucket = aws_s3_bucket.static_website_bucket.id
key = "index.html"
source = "./static/index.html"
content_type = "text/html"
}
resource "aws_s3_object" "styles_css" {
bucket = aws_s3_bucket.static_website_bucket.id
key = "styles.css"
source = "./static/styles.css"
content_type = "text/css"
}
변경이라고 썼지만 새로 업로드 되는 것 같다.
content-type 변경해도 css가 적용이 안 될 때는 브라우저 캐시 삭제 후 다시 접속하기.
업로드 된 파일 이름 변경
마지막 미션으로 업로드 된 파일 이름을 변경하라는 미션이 있는데 이것은 아무리 검색해도 나오지 않는다.
지선생한테 물어보니 그건 terraform이 관여하는 부분이 아니라고 지원하지 않는다고 한다.
결과 페이지
참고
https://dev.to/ankursheel/how-to-upload-multiple-files-to-aws-s3-using-terraform-24bl
How to Upload Multiple Files to AWS S3 using Terraform
A snippet to upload multiples files from a specific folder (recursively) to AWS S3 using terraform
dev.to
https://stackoverflow.com/questions/18296875/amazon-s3-downloads-index-html-instead-of-serving
Amazon S3 downloads index.html instead of serving
I've set up Amazon S3 to serve my static site, speakeasylinguistics.com. All of the DNS stuff seems to be working okay, because dig +recurse +trace www.speakeasylinguistics.com outputs the correct ...
stackoverflow.com
'DevOps > Terraform' 카테고리의 다른 글
[Terraform] AWS EC2 인스턴스 유형 변경(user data 재실행) (0) | 2023.07.15 |
---|---|
[Terraform] AWS EC2 Linux 인스턴스 생성(user data 작성) (0) | 2023.06.24 |
[Terraform] 테라폼 약간 심화 (0) | 2023.06.16 |
[Terraform] 테라폼 완전 기초 (0) | 2023.05.29 |
[Terraform] 테라폼 시작하기 (0) | 2023.05.27 |