Skip to content
This repository was archived by the owner on May 19, 2026. It is now read-only.

Commit f11edc1

Browse files
committed
TF Config for s3 hosting
1 parent 65e5064 commit f11edc1

5 files changed

Lines changed: 248 additions & 0 deletions

File tree

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,10 @@ doc/
2525

2626
# Vagrant artifacts
2727
ubuntu-*-console.log
28+
29+
# OpenTofu / Terraform
30+
infra/.terraform/
31+
infra/.terraform.lock.hcl
32+
infra/terraform.tfstate
33+
infra/terraform.tfstate.backup
34+
infra/terraform.tfvars

infra/buildspec.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
version: 0.2
2+
3+
phases:
4+
install:
5+
runtime-versions:
6+
ruby: 3.2
7+
commands:
8+
- gem install bundler --no-document
9+
- bundle install --jobs 4 --retry 3
10+
build:
11+
commands:
12+
- bundle exec middleman build --clean --watcher-disable
13+
post_build:
14+
commands:
15+
- aws s3 sync build/ s3://$S3_BUCKET/ --delete

infra/main.tf

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "~> 5.0"
6+
}
7+
}
8+
}
9+
10+
provider "aws" {
11+
region = "us-east-1"
12+
}
13+
14+
# S3 bucket for the static site
15+
resource "aws_s3_bucket" "site" {
16+
bucket = var.domain
17+
}
18+
19+
resource "aws_s3_bucket_website_configuration" "site" {
20+
bucket = aws_s3_bucket.site.id
21+
22+
index_document {
23+
suffix = "index.html"
24+
}
25+
}
26+
27+
resource "aws_s3_bucket_public_access_block" "site" {
28+
bucket = aws_s3_bucket.site.id
29+
30+
block_public_acls = false
31+
block_public_policy = false
32+
ignore_public_acls = false
33+
restrict_public_buckets = false
34+
}
35+
36+
resource "aws_s3_bucket_policy" "site" {
37+
bucket = aws_s3_bucket.site.id
38+
depends_on = [aws_s3_bucket_public_access_block.site]
39+
40+
policy = jsonencode({
41+
Version = "2012-10-17"
42+
Statement = [{
43+
Effect = "Allow"
44+
Principal = "*"
45+
Action = "s3:GetObject"
46+
Resource = "${aws_s3_bucket.site.arn}/*"
47+
}]
48+
})
49+
}
50+
51+
# Artifact bucket for CodePipeline (private)
52+
resource "aws_s3_bucket" "artifacts" {
53+
bucket = "${var.domain}-pipeline-artifacts"
54+
}
55+
56+
resource "aws_s3_bucket_lifecycle_configuration" "artifacts" {
57+
bucket = aws_s3_bucket.artifacts.id
58+
59+
rule {
60+
id = "expire"
61+
status = "Enabled"
62+
filter {}
63+
expiration {
64+
days = 7
65+
}
66+
}
67+
}
68+
69+
# CodeBuild
70+
resource "aws_iam_role" "codebuild" {
71+
name = "api-docs-codebuild"
72+
73+
assume_role_policy = jsonencode({
74+
Version = "2012-10-17"
75+
Statement = [{
76+
Effect = "Allow"
77+
Principal = { Service = "codebuild.amazonaws.com" }
78+
Action = "sts:AssumeRole"
79+
}]
80+
})
81+
}
82+
83+
resource "aws_iam_role_policy" "codebuild" {
84+
role = aws_iam_role.codebuild.id
85+
86+
policy = jsonencode({
87+
Version = "2012-10-17"
88+
Statement = [
89+
{
90+
Effect = "Allow"
91+
Action = ["logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents"]
92+
Resource = "*"
93+
},
94+
{
95+
Effect = "Allow"
96+
Action = ["s3:PutObject", "s3:DeleteObject", "s3:ListBucket"]
97+
Resource = [aws_s3_bucket.site.arn, "${aws_s3_bucket.site.arn}/*"]
98+
},
99+
{
100+
Effect = "Allow"
101+
Action = ["s3:GetObject", "s3:GetObjectVersion", "s3:PutObject"]
102+
Resource = "${aws_s3_bucket.artifacts.arn}/*"
103+
}
104+
]
105+
})
106+
}
107+
108+
resource "aws_codebuild_project" "site" {
109+
name = "api-docs"
110+
service_role = aws_iam_role.codebuild.arn
111+
112+
artifacts {
113+
type = "CODEPIPELINE"
114+
}
115+
116+
environment {
117+
compute_type = "BUILD_GENERAL1_SMALL"
118+
image = "aws/codebuild/standard:7.0"
119+
type = "LINUX_CONTAINER"
120+
121+
environment_variable {
122+
name = "S3_BUCKET"
123+
value = aws_s3_bucket.site.bucket
124+
}
125+
}
126+
127+
source {
128+
type = "CODEPIPELINE"
129+
buildspec = "infra/buildspec.yml"
130+
}
131+
}
132+
133+
# CodePipeline
134+
resource "aws_iam_role" "codepipeline" {
135+
name = "api-docs-codepipeline"
136+
137+
assume_role_policy = jsonencode({
138+
Version = "2012-10-17"
139+
Statement = [{
140+
Effect = "Allow"
141+
Principal = { Service = "codepipeline.amazonaws.com" }
142+
Action = "sts:AssumeRole"
143+
}]
144+
})
145+
}
146+
147+
resource "aws_iam_role_policy" "codepipeline" {
148+
role = aws_iam_role.codepipeline.id
149+
150+
policy = jsonencode({
151+
Version = "2012-10-17"
152+
Statement = [
153+
{
154+
Effect = "Allow"
155+
Action = ["s3:GetObject", "s3:GetObjectVersion", "s3:PutObject", "s3:ListBucket"]
156+
Resource = [aws_s3_bucket.artifacts.arn, "${aws_s3_bucket.artifacts.arn}/*"]
157+
},
158+
{
159+
Effect = "Allow"
160+
Action = ["codebuild:BatchGetBuilds", "codebuild:StartBuild"]
161+
Resource = aws_codebuild_project.site.arn
162+
},
163+
{
164+
Effect = "Allow"
165+
Action = "codestar-connections:UseConnection"
166+
Resource = var.github_connection_arn
167+
}
168+
]
169+
})
170+
}
171+
172+
resource "aws_codepipeline" "site" {
173+
name = "api-docs"
174+
role_arn = aws_iam_role.codepipeline.arn
175+
176+
artifact_store {
177+
location = aws_s3_bucket.artifacts.bucket
178+
type = "S3"
179+
}
180+
181+
stage {
182+
name = "Source"
183+
action {
184+
name = "Source"
185+
category = "Source"
186+
owner = "AWS"
187+
provider = "CodeStarSourceConnection"
188+
version = "1"
189+
output_artifacts = ["source"]
190+
configuration = {
191+
ConnectionArn = var.github_connection_arn
192+
FullRepositoryId = "jetbuilt/jetbuilt-api-docs"
193+
BranchName = "main"
194+
OutputArtifactFormat = "CODEBUILD_CLONE_REF"
195+
DetectChanges = "true"
196+
}
197+
}
198+
}
199+
200+
stage {
201+
name = "Build"
202+
action {
203+
name = "Build"
204+
category = "Build"
205+
owner = "AWS"
206+
provider = "CodeBuild"
207+
version = "1"
208+
input_artifacts = ["source"]
209+
configuration = {
210+
ProjectName = aws_codebuild_project.site.name
211+
}
212+
}
213+
}
214+
}

infra/outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "site_endpoint" {
2+
description = "S3 website endpoint — use this as the Cloudflare origin"
3+
value = aws_s3_bucket_website_configuration.site.website_endpoint
4+
}

infra/variables.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
variable "domain" {
2+
default = "api.jetbuilt.com"
3+
}
4+
5+
variable "github_connection_arn" {
6+
description = "ARN of the existing CodeStar connection for GitHub"
7+
default = "arn:aws:codeconnections:us-east-1:476548171748:connection/14ed4999-877c-4ca9-b791-d265cc159a6e"
8+
}

0 commit comments

Comments
 (0)