-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
93 lines (74 loc) · 3.32 KB
/
main.tf
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
provider "null" {
version = "~> 2.1"
}
data "aws_caller_identity" "current" {}
data "aws_region" "current" {}
data "terraform_remote_state" "datomic" {
backend = "s3"
config = {
# TODO: Insert remote state backend for Datomic cluster
}
}
# Datomic documentation (https://docs.datomic.com/cloud/operation/query-groups.html#details) recommends keeping the stack name at fewer than 24 characters, so trying to inject the full suffix will be basically useless. Instead, let's just use the rev portion of the suffix.
locals {
base_stack_name = "http-direct-example-qg"
split_suffix = split("-", var.suffix)
rev = local.split_suffix[0]
stack_name = "${local.base_stack_name}${local.rev == "" ? "" : "-${local.rev}"}"
}
data "aws_iam_policy_document" "ions" {
# TODO: Insert Ion policies here
}
resource "aws_iam_policy" "ions" {
name = "${local.stack_name}-ions"
policy = data.aws_iam_policy_document.ions.json
}
resource "aws_cloudformation_stack" "query_group" {
name = local.stack_name
capabilities = ["CAPABILITY_NAMED_IAM"]
parameters = {
ApplicationName = "http-direct-example"
EnvironmentMap = "{:env \"${var.env}\" :query-group \"${local.stack_name}\" :system \"${data.terraform_remote_state.datomic.outputs.system_name[var.env]}\" :region \"${var.region}\" :database-name \"${var.database_name}\" :allow-origin \"${var.allow_origin}\"}"
InstanceType = var.instance_type
KeyName = data.terraform_remote_state.datomic.outputs.key_name[var.env]
NodePolicyArn = aws_iam_policy.ions.arn
SystemName = data.terraform_remote_state.datomic.outputs.system_name[var.env]
}
template_url = var.query_group_cfs_url
}
locals {
ion_dependency_hashes = [
md5(file("${path.module}/ions/deps.edn")),
md5(file("${path.module}/ions/resources/datomic/ion-config.edn")),
md5(file("${path.module}/ions/src/http_direct_example/ion/not_found.clj")),
md5(file("${path.module}/ions/src/http_direct_example/ion/proxy.clj")),
md5(file("${path.module}/ions/src/http_direct_example/ion/resource.clj")),
md5(file("${path.module}/ions/src/http_direct_example/ion/utilities.clj")),
]
ion_dependency_hash = md5(join("\n", local.ion_dependency_hashes))
}
resource "null_resource" "ions" {
depends_on = [aws_cloudformation_stack.query_group]
triggers = {
ion_dependency_hash = local.ion_dependency_hash
query_group_stack_id = aws_cloudformation_stack.query_group.id
}
provisioner "local-exec" {
command = var.env == "dev" ? "cd \"${path.module}/ions\" && DEPLOYMENT_GROUP=${aws_cloudformation_stack.query_group.outputs["CodeDeployDeploymentGroup"]} bash bin/dev-release.sh" : "cd \"${path.module}/ions\" && ASSUME_ROLE_ARN=${var.assume_role_arn} DEPLOYMENT_GROUP=${aws_cloudformation_stack.query_group.outputs["CodeDeployDeploymentGroup"]} UNAME=${var.rev} REGION=${var.region} bash bin/codebuild-release.sh"
}
}
data "aws_autoscaling_groups" "query_group" {
depends_on = [aws_cloudformation_stack.query_group]
filter {
name = "Key"
values = ["aws:cloudformation:stack-name"]
}
filter {
name = "Value"
values = [aws_cloudformation_stack.query_group.name]
}
}
resource "aws_api_gateway_vpc_link" "query_group" {
name = local.stack_name
target_arns = [aws_cloudformation_stack.query_group.outputs["LoadBalancer"]]
}