Repository navigation
Expand file tree
/
Copy pathsubscription.tf
More file actions
73 lines (71 loc) · 2.67 KB
/
Copy pathsubscription.tf
File metadata and controls
73 lines (71 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# The locked AWS provider has no PricingPlanManager resource. Manage the native
# AWS subscription through a CloudFormation stack instead of an untracked CLI create.
resource "aws_wafv2_web_acl" "cdn" {
count = var.enable_delivery ? 1 : 0
provider = aws.certificate
name = "gravity-cdn"
scope = "CLOUDFRONT"
default_action {
allow {}
}
# Downloads require no CAPTCHA/challenge. Add reviewed rules separately.
visibility_config {
cloudwatch_metrics_enabled = true
metric_name = "gravity-cdn"
sampled_requests_enabled = false
}
}
resource "aws_cloudformation_stack" "pro" {
count = var.enable_delivery ? 1 : 0
provider = aws.certificate
name = "gravity-cloudfront-pro"
template_body = jsonencode({
AWSTemplateFormatVersion = "2010-09-09"
Resources = {
Subscription = {
Type = "AWS::PricingPlanManager::Subscription"
Properties = {
PlanFamily = "CloudFront"
PlanTier = "PRO"
UsageLevel = "DEFAULT"
ResourceArns = [
aws_cloudfront_distribution.cdn[0].arn,
aws_wafv2_web_acl.cdn[0].arn,
aws_route53_zone.site["cdn.gravitylinux.org"].arn,
]
}
}
}
Outputs = { SubscriptionArn = { Value = { Ref = "Subscription" } } }
})
lifecycle {
# Cancellation is delayed to the end of the billing cycle, so teardown must
# be deliberate rather than deleting a distribution under an active plan.
prevent_destroy = true
precondition {
condition = var.approve_pro_subscription
error_message = "Delivery requires approve_pro_subscription = true: apply will activate the paid CloudFront Pro plan ($15/month at current pricing)."
}
}
}
# CloudFormation creates paid plans as PENDING_APPROVAL. Applying this module
# explicitly approves PRO ($15/month), then waits for ACTIVE before publication.
# Recheck on each changed artifact manifest as well as initial activation.
resource "terraform_data" "activate_pro" {
count = var.enable_delivery ? 1 : 0
triggers_replace = [
aws_cloudformation_stack.pro[0].outputs["SubscriptionArn"],
aws_cloudformation_stack.pro[0].template_body,
filesha256("${path.module}/activate-pro.py"),
var.artifact_manifest == null ? "" : filesha256(var.artifact_manifest),
]
provisioner "local-exec" {
command = "python3 \"$ACTIVATION_SCRIPT\""
environment = {
ACTIVATION_SCRIPT = abspath("${path.module}/activate-pro.py")
SUBSCRIPTION_ARN = aws_cloudformation_stack.pro[0].outputs["SubscriptionArn"]
DISTRIBUTION_ARN = aws_cloudfront_distribution.cdn[0].arn
WAF_ARN = aws_wafv2_web_acl.cdn[0].arn
}
}
}