Skip to content

Commit 208aef5

Browse files
fix S3 bucket policy SourceArn typo, ignore placeholder zip
1 parent e86573d commit 208aef5

4 files changed

Lines changed: 110 additions & 0 deletions

File tree

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,5 @@ terraform/*.tfstate.backup
4141

4242
*.peminfrastructure/*.tfstate
4343
infrastructure/*.tfstate.backup
44+
45+
infrastructure/lambda_placeholder.zip

‎infrastructure/cloudfront.tf‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
resource "aws_acm_certificate" "portfolio" {
2+
provider = aws.us_east_1
3+
domain_name = "seturaman.me"
4+
validation_method = "DNS"
5+
6+
subject_alternative_names = ["www.seturaman.me"]
7+
8+
lifecycle {
9+
create_before_destroy = true
10+
}
11+
}
12+
13+
resource "aws_cloudfront_origin_access_control" "frontend" {
14+
name = "portfolio-s3-oac"
15+
origin_access_control_origin_type = "s3"
16+
signing_behavior = "always"
17+
signing_protocol = "sigv4"
18+
}
19+
20+
resource "aws_cloudfront_distribution" "frontend" {
21+
enabled = true
22+
default_root_object = "index.html"
23+
aliases = ["seturaman.me", "www.seturaman.me"]
24+
price_class = "PriceClass_All"
25+
26+
origin {
27+
domain_name = aws_s3_bucket.frontend.bucket_regional_domain_name
28+
origin_id = "S3-portfolio-frontend"
29+
origin_access_control_id = aws_cloudfront_origin_access_control.frontend.id
30+
}
31+
32+
default_cache_behavior {
33+
allowed_methods = ["GET", "HEAD", "OPTIONS"]
34+
cached_methods = ["GET", "HEAD"]
35+
target_origin_id = "S3-portfolio-frontend"
36+
viewer_protocol_policy = "redirect-to-https"
37+
compress = true
38+
39+
forwarded_values {
40+
query_string = false
41+
cookies {
42+
forward = "none"
43+
}
44+
}
45+
46+
min_ttl = 0
47+
default_ttl = 3600
48+
max_ttl = 86400
49+
}
50+
51+
custom_error_response {
52+
error_code = 403
53+
response_code = 200
54+
response_page_path = "/index.html"
55+
}
56+
57+
custom_error_response {
58+
error_code = 404
59+
response_code = 200
60+
response_page_path = "/index.html"
61+
}
62+
63+
restrictions {
64+
geo_restriction {
65+
restriction_type = "none"
66+
}
67+
}
68+
69+
viewer_certificate {
70+
acm_certificate_arn = aws_acm_certificate.portfolio.arn
71+
ssl_support_method = "sni-only"
72+
minimum_protocol_version = "TLSv1.2_2021"
73+
}
74+
75+
depends_on = [aws_acm_certificate.portfolio]
76+
}
77+
78+
resource "aws_s3_bucket_policy" "frontend" {
79+
bucket = aws_s3_bucket.frontend.id
80+
81+
policy = jsonencode({
82+
Version = "2012-10-17"
83+
Statement = [{
84+
Sid = "AllowCloudFrontAccess"
85+
Effect = "Allow"
86+
Principal = {
87+
Service = "cloudfront.amazonaws.com"
88+
}
89+
Action = "s3:GetObject"
90+
Resource = "${aws_s3_bucket.frontend.arn}/*"
91+
Condition = {
92+
StringEquals = {
93+
"AWS:SourceArn" = aws_cloudfront_distribution.frontend.arn
94+
}
95+
}
96+
}]
97+
})
98+
}

‎infrastructure/main.tf‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ terraform {
1717
provider "aws" {
1818
region = "ap-south-1"
1919
}
20+
21+
provider "aws" {
22+
alias = "us_ease_1"
23+
region = "us-east-1"
24+
}

‎infrastructure/outputs.tf‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ output "lambda_function_name" {
1212
description = "Lambda function name for Github Actions code deployment."
1313
value = aws_lambda_function.contact.function_name
1414
}
15+
16+
output "cloudfront_domain" {
17+
description = "CloudFront domain name. Add as CNAME in Namecheap."
18+
value = aws_cloudfront_distribution.frontend.domain_name
19+
}

0 commit comments

Comments
 (0)