I am trying to : 1- create a bucket 2- upload static site files to it 3- configure the bucket as a static website 4- configure route53 to forward subdomain to the bucket website instead of using the ugly url from s3.
the terraform files are created and I can access the web site. however, I need to run the terraform apply twice: 1- first run will create the bucket, upload the site files and do the needed bucket configurations for static site. but the route53 record resource will fail with error
Error: Missing required argument
│
│ with module.ui_site.aws_route53_record.www-a,
│ on modules\UI\route53.tf line 14, in resource "aws_route53_record" "www-a":
│ 14: name = aws_s3_bucket.site.website_domain
│
│ The argument "alias.0.name" is required, but no definition was found.
2- second run will create the route53 record.
terraform file:
resource "aws_s3_bucket" "site" {
bucket = "${var.ui_bucket_name}.${var.root_domain}"
}
resource "aws_s3_bucket_public_access_block" "site" {
bucket = aws_s3_bucket.site.id
block_public_acls = false
block_public_policy = false
ignore_public_acls = false
restrict_public_buckets = false
}
resource "aws_s3_bucket_website_configuration" "site" {
bucket = aws_s3_bucket.site.id
index_document {
suffix = "index.html"
}
error_document {
key = "index.html"
}
}
resource "aws_s3_bucket_ownership_controls" "site" {
bucket = aws_s3_bucket.site.id
rule {
object_ownership = "BucketOwnerPreferred"
}
}
resource "aws_s3_bucket_acl" "site" {
bucket = aws_s3_bucket.site.id
acl = "public-read"
depends_on = [
aws_s3_bucket_ownership_controls.site,
aws_s3_bucket_public_access_block.site
]
}
resource "aws_s3_bucket_policy" "site" {
bucket = aws_s3_bucket.site.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Sid = "PublicReadGetObject"
Effect = "Allow"
Principal = "*"
Action = "s3:GetObject"
Resource = [
aws_s3_bucket.site.arn,
"${aws_s3_bucket.site.arn}/*",
]
},
]
})
depends_on = [
aws_s3_bucket_public_access_block.site
]
}
module "template_files" {
source = "hashicorp/dir/template"
base_dir = "${path.module}/../../../client_ui/build"
}
resource "aws_s3_object" "site" {
bucket = aws_s3_bucket.site.bucket
for_each = module.template_files.files
key = each.key
content_type = each.value.content_type
# The template_files module guarantees that only one of these two attributes
# will be set for each file, depending on whether it is an in-memory template
# rendering result or a static file on disk.
source = each.value.source_path
content = each.value.content
# Unless the bucket has encryption enabled, the ETag of each object is an
# MD5 hash of that object.
etag = each.value.digests.md5
}
#######################################################
#######################################################
#######################################################
## Route53
data "aws_route53_zone" "zone" {
name = var.root_domain
private_zone = false
}
resource "aws_route53_record" "www-a" {
zone_id = data.aws_route53_zone.zone.zone_id
name = aws_s3_bucket.site.bucket
type = "A"
alias {
name = aws_s3_bucket.site.website_domain
zone_id = aws_s3_bucket.site.hosted_zone_id
evaluate_target_health = true
}
depends_on = [
aws_s3_bucket_website_configuration.site
]
}
I tried with different resources in the depends on section of the route53 record: bucketresource , bucket_website resource.
but it is still failing
Instead of
name = aws_s3_bucket.site.website_domain
you should be using
name = aws_s3_bucket_website_configuration.site.website_domain