Skip to content

Commit df3bf2d

Browse files
committed
✨(terraform) add terraform configuration
In a near future, we want to store ashley medias and static files on AWS. We want to use Terraform to setup the required AWS resources for each environment.
1 parent f5338ec commit df3bf2d

20 files changed

Lines changed: 387 additions & 1 deletion

‎.gitignore‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ ENV/
4242
env.bak/
4343
venv.bak/
4444
env.d/development/crowdin
45+
env.d/terraform
4546

4647
# Logs
4748
*.log
@@ -65,3 +66,8 @@ node_modules
6566
# SASS
6667
.sass-cache
6768

69+
# Terraform
70+
.terraform
71+
*.tfstate
72+
*.tfstate.backup
73+
/terraform/state.tf

‎Makefile‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ default: help
6161

6262
bootstrap: ## Prepare Docker images for the project
6363
bootstrap: \
64-
env.d/development/crowdin \
64+
env.d/development/crowdin \
65+
env.d/terraform \
6566
build \
6667
build-front \
6768
migrate
@@ -276,6 +277,11 @@ i18n-generate-and-upload: \
276277
crowdin-upload
277278
.PHONY: i18n-generate-and-upload
278279

280+
# -- Terraform
281+
282+
env.d/terraform:
283+
cp env.d/terraform.dist env.d/terraform
284+
279285
# -- Misc
280286
help:
281287
@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

‎bin/state‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
set -eo pipefail
3+
4+
# Run Terraform commands on the create_state_bucket project in the Hashicorp
5+
# docker container, passing our environment variables
6+
7+
8+
# Run Terraform commands in the Hashicorp docker container via docker compose
9+
DOCKER_USER="$(id -u):$(id -g)" \
10+
docker-compose run --rm terraform-state "$@"

‎bin/terraform‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
set -eo pipefail
3+
4+
# Run Terraform commands in the Hashicorp docker container via docker compose
5+
# shellcheck disable=SC2068
6+
DOCKER_USER="$(id -u):$(id -g)" \
7+
docker-compose run --rm terraform "$@"

‎docker-compose.yml‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,19 @@ services:
5252
user: "${DOCKER_USER:-1000}"
5353
volumes:
5454
- .:/app
55+
56+
terraform:
57+
image: hashicorp/terraform:0.15.3
58+
env_file: env.d/terraform
59+
user: ${DOCKER_USER:-1000}
60+
working_dir: /app
61+
volumes:
62+
- ./terraform:/app
63+
64+
terraform-state:
65+
image: hashicorp/terraform:0.15.3
66+
env_file: env.d/terraform
67+
user: ${DOCKER_USER:-1000}
68+
working_dir: /app
69+
volumes:
70+
- ./terraform/create_state_bucket:/app

‎env.d/terraform.dist‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# AWS admin credentials
2+
AWS_ACCESS_KEY_ID=yourAwsAccesKeyId
3+
AWS_SECRET_ACCESS_KEY=YourAwsSecretAccessKey
4+
AWS_DEFAULT_REGION=eu-west-1
5+
6+
# In this map, you should set the domain name of your ashley application,
7+
# per environment (key = terraform workspace, value = domain name)
8+
TF_VAR_app_domain={ production = "ashley.education" }

‎terraform/.terraform.lock.hcl‎

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎terraform/cloudfront.tf‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
2+
locals {
3+
static_origin_id = "ashley-static-origin"
4+
s3_media_origin_id = "ashley-media-origin"
5+
}
6+
7+
# Create an origin access identity that will allow CloudFront to access S3
8+
# See bucket policies in s3.tf or documentation for more details:
9+
# https://www.terraform.io/docs/providers/aws/r/cloudfront_origin_access_identity.html
10+
resource "aws_cloudfront_origin_access_identity" "ashley_oai" {
11+
comment = "ashley origin for the ${terraform.workspace} environment"
12+
}
13+
14+
resource "aws_cloudfront_distribution" "ashley_cloudfront_distribution" {
15+
# Origin for static distribution
16+
origin {
17+
domain_name = lookup(var.app_domain, terraform.workspace)
18+
origin_id = local.static_origin_id
19+
20+
custom_origin_config {
21+
http_port = 80
22+
https_port = 443
23+
origin_protocol_policy = "https-only"
24+
origin_ssl_protocols = ["SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2"]
25+
}
26+
}
27+
# Origin for the media S3 bucket
28+
origin {
29+
domain_name = aws_s3_bucket.ashley_media.bucket_domain_name
30+
origin_id = local.s3_media_origin_id
31+
32+
s3_origin_config {
33+
origin_access_identity = aws_cloudfront_origin_access_identity.ashley_oai.cloudfront_access_identity_path
34+
}
35+
}
36+
37+
enabled = true
38+
is_ipv6_enabled = true
39+
wait_for_deployment = false
40+
41+
# Allow public access by default, served by static bucket
42+
default_cache_behavior {
43+
allowed_methods = ["GET", "HEAD", "OPTIONS"]
44+
cached_methods = ["GET", "HEAD", "OPTIONS"]
45+
target_origin_id = local.static_origin_id
46+
47+
forwarded_values {
48+
query_string = true
49+
headers = ["Access-Control-Request-Headers", "Access-Control-Request-Method", "Authorization", "Origin"]
50+
51+
cookies {
52+
forward = "none"
53+
}
54+
}
55+
56+
min_ttl = 0
57+
default_ttl = 3600
58+
max_ttl = 86400
59+
compress = true
60+
viewer_protocol_policy = "redirect-to-https"
61+
}
62+
63+
# Media bucket: allow public access
64+
ordered_cache_behavior {
65+
path_pattern = "/media/*"
66+
allowed_methods = ["GET", "HEAD", "OPTIONS"]
67+
cached_methods = ["GET", "HEAD", "OPTIONS"]
68+
target_origin_id = local.s3_media_origin_id
69+
70+
forwarded_values {
71+
query_string = false
72+
headers = ["Access-Control-Request-Headers", "Access-Control-Request-Method", "Authorization", "Origin"]
73+
74+
cookies {
75+
forward = "none"
76+
}
77+
}
78+
79+
min_ttl = 0
80+
default_ttl = 3600
81+
max_ttl = 86400
82+
compress = true
83+
viewer_protocol_policy = "redirect-to-https"
84+
}
85+
86+
price_class = lookup(var.cloudfront_price_class, terraform.workspace, "PriceClass_100")
87+
88+
restrictions {
89+
geo_restriction {
90+
restriction_type = "none"
91+
}
92+
}
93+
94+
tags = {
95+
Environment = terraform.workspace
96+
}
97+
98+
viewer_certificate {
99+
cloudfront_default_certificate = true
100+
}
101+
}

‎terraform/create_state_bucket/.terraform.lock.hcl‎

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
resource "aws_dynamodb_table" "terraform-state-locks" {
2+
name = "ashley_terraform_state_locks"
3+
read_capacity = 1
4+
write_capacity = 1
5+
hash_key = "LockID"
6+
7+
attribute {
8+
name = "LockID"
9+
type = "S"
10+
}
11+
}

0 commit comments

Comments
 (0)