2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Terraformで作ったS3オブジェクトをCloudFront経由でアクセスするとダウンロードになった。。。

2
Last updated at Posted at 2023-10-16

前提

・S3バケットを作成
・CloudFrontのOAIを作成し、作ったS3バケットをオリジンに指定(S3のバケットポリシーの自動編集にチェックを入れた)

事象

AWSマネジメントコンソールでS3バケットへhtmlをアップした場合、CloudFront経由で無事アクセスできたが、

index1.html
uploaded by AWS management console.

image.png

このように、Terraformのソースコードで、まずS3バケットをimportして、S3オブジェクトをアップすると、

import {
  to = aws_s3_bucket.bucket
  id = "bucket_name"
}
resource "aws_s3_object" "object" {
  bucket = aws_s3_bucket.bucket.id
  key = "index2.html"
  source = "index2.html"
  etag = filemd5("index2.html")
}
index2.html
uploaded by Terraform.

マネジメントコンソールで見ると、index2.htmlは無事アップロードできるように見えたが、

CloudFront経由でindex2.htmlへアクセスしようとすると、「ダウンロード」になってしまった。。。

解決方法

原因は、「content_type」の設定が漏れてしまったから!

import {
  to = aws_s3_bucket.bucket
  id = "bucket_name"
}
resource "aws_s3_object" "object" {
  bucket = aws_s3_bucket.bucket.id
  key = "index2.html"
  source = "index2.html"
  etag = filemd5("index2.html")
  content_type = "text/html"  ###ここを修正!!
}

AWSマネジメントコンソールでアップしたindex1.htmlのメタデータを見ると、
text/htmlって自動で設定されるのが分かって、

image.png

しかし、Terraformでオブジェクトをアップする場合、明示的にcontent_type = "text/html"を設定してあげないと、application/octet-streamになってしまって。。。

index2.htmlのメタデータを見ると、確かにストリームだね。。。

image.png

これだと、アクセスするときはダウンロードになってしまう。。。

終わりに

今回はAWSマネジメントコンソールとTerraformでS3オブジェクトをアップする場合の挙動を勉強できた!

参考サイト

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?