Skip to content

Commit 5093952

Browse files
committed
#542 Update unity terraform
1 parent 5f0ab3d commit 5093952

File tree

7 files changed

+525
-86
lines changed

7 files changed

+525
-86
lines changed

sds/unity/terraform/add-mmgis.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/bin/bash
2+
# From https://gist.github.com/jamesmishra/18ee5d7d053db9958d0e4ccbb37f8e1d
3+
set -Eeuxo pipefail
4+
# Filesystem code is adapted from:
5+
# https://github.com/GSA/devsecops-example/blob/03067f68ee2765f8477ae84235f7faa1d2f2cb70/terraform/files/attach-data-volume.sh
6+
DEVICE=${local.block_device_path}
7+
DEST=${var.persistent_volume_mount_path}
8+
devpath=$(readlink -f $DEVICE)
9+
10+
if [[ $(file -s $devpath) != *ext4* && -b $devpath ]]; then
11+
# Filesystem has not been created. Create it!
12+
mkfs -t ext4 $devpath
13+
fi
14+
# add to fstab if not present
15+
if ! egrep "^$devpath" /etc/fstab; then
16+
echo "$devpath $DEST ext4 defaults,nofail,noatime,nodiratime,barrier=0,data=writeback 0 2" | tee -a /etc/fstab > /dev/null
17+
fi
18+
mkdir -p $DEST
19+
mount $DEST
20+
chown ec2-user:ec2-user $DEST
21+
chmod 0755 $DEST
22+
23+
# Filesystem code is over
24+
# Now we install docker and docker-compose.
25+
# Adapted from:
26+
# https://gist.github.com/npearce/6f3c7826c7499587f00957fee62f8ee9
27+
yum update -y
28+
amazon-linux-extras install docker
29+
systemctl start docker.service
30+
usermod -a -G docker ec2-user
31+
chkconfig docker on
32+
yum install -y python3-pip
33+
python3 -m pip install docker-compose
34+
35+
# Put the docker-compose.yml file at the root of our persistent volume
36+
cat > $DEST/docker-compose.yml <<-TEMPLATE
37+
${var.docker_compose_str}
38+
TEMPLATE
39+
40+
# Write the systemd service that manages us bringing up the service
41+
cat > /etc/systemd/system/mmgis.service <<-TEMPLATE
42+
[Unit]
43+
Description=${var.description}
44+
After=${var.systemd_after_stage}
45+
[Service]
46+
Type=simple
47+
User=${var.user}
48+
ExecStart=/usr/local/bin/docker-compose -f $DEST/docker-compose.yml up
49+
Restart=on-failure
50+
[Install]
51+
WantedBy=multi-user.target
52+
TEMPLATE
53+
54+
# Start the service.
55+
systemctl start mmgis

sds/unity/terraform/bk.tf

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
resource "aws_iam_instance_profile" "unity_mmgis_instance_profile" {
2+
name = "unity-mmgis-instance-profile-tf"
3+
4+
role = var.role
5+
6+
tags = {
7+
Name = "unity_mmgis_instance_profile"
8+
}
9+
}
10+
11+
resource "aws_instance" "unity_mmgis_instance" {
12+
ami = var.ami
13+
instance_type = "t3.large"
14+
15+
tags = {
16+
Name = "unity-mmgis-instance-tf"
17+
}
18+
19+
#key_name = var.key_name
20+
21+
vpc_security_group_ids = [var.sg_id]
22+
23+
subnet_id = var.subnet_id
24+
25+
iam_instance_profile = aws_iam_instance_profile.unity_mmgis_instance_profile.name
26+
27+
block_device_path = "/dev/sdh"
28+
user_data = file("./add-mmgis.sh")
29+
}
30+
31+
resource "aws_ebs_volume" "persistent" {
32+
availability_zone = aws_instance.this.availability_zone
33+
size = var.persistent_volume_size_gb
34+
}
35+
36+
resource "aws_volume_attachment" "persistent" {
37+
device_name = local.block_device_path
38+
volume_id = aws_ebs_volume.persistent.id
39+
instance_id = aws_instance.this.id
40+
}
41+
42+
resource "aws_instance" "this" {
43+
ami = data.aws_ami.latest_amazon_linux.id
44+
availability_zone = var.availability_zone
45+
instance_type = var.instance_type
46+
key_name = var.key_name
47+
associate_public_ip_address = var.associate_public_ip_address
48+
vpc_security_group_ids = var.vpc_security_group_ids
49+
subnet_id = var.subnet_id
50+
iam_instance_profile = var.iam_instance_profile
51+
user_data = local.user_data
52+
tags = merge (
53+
{
54+
Name = var.name
55+
},
56+
var.tags
57+
)
58+
}

sds/unity/terraform/lb.tf

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# target group
2+
resource "aws_lb_target_group" "unity_mmgis_tg_tf" {
3+
name = "unity-mmgis-tg-tf"
4+
port = 8080
5+
protocol = "TCP"
6+
target_type = "instance"
7+
#vpc_id = data.aws_vpc.default.id
8+
vpc_id = var.vpc_id
9+
10+
health_check {
11+
enabled = true
12+
protocol = "HTTP"
13+
port = 8080
14+
path = "/unity/v0/collections/MUR25-JPL-L4-GLOB-v4.2_analysed_sst/processes"
15+
interval = 30
16+
timeout = 10
17+
matcher = 200
18+
healthy_threshold = 5
19+
unhealthy_threshold = 2
20+
}
21+
22+
tags = {
23+
Name = "unity_mmgis_tg_tf"
24+
}
25+
}
26+
27+
# attach instance
28+
resource "aws_lb_target_group_attachment" "unity_mmgis_tg_attachment_tf" {
29+
target_group_arn = aws_lb_target_group.unity_mmgis_tg_tf.arn
30+
target_id = aws_instance.unity_mmgis_instance.id
31+
port = 8080
32+
}
33+
34+
# create alb
35+
resource "aws_lb" "unity-mmgis-lb-tf" {
36+
name = "unity-mmgis-lb-tf"
37+
load_balancer_type = "network"
38+
internal = true
39+
#security_groups = [var.sg_id]
40+
#security_groups = []
41+
#subnets = [for subnet in aws_subnet.public : subnet.id]
42+
subnets = var.subnet_ids
43+
44+
enable_deletion_protection = false
45+
46+
#access_logs {
47+
# bucket = "tbd"
48+
# prefix = "mmgis/tbd/unity-mmgis-lb"
49+
# enabled = true
50+
#}
51+
52+
tags = {
53+
Name = "unity-mmgis-lb-tf"
54+
}
55+
}
56+
57+
resource "aws_lb_listener" "unity_mmgis_lb_listener" {
58+
load_balancer_arn = aws_lb.unity-mmgis-lb-tf.arn
59+
port = 80
60+
protocol = "TCP"
61+
62+
default_action {
63+
type = "forward"
64+
target_group_arn = aws_lb_target_group.unity_mmgis_tg_tf.arn
65+
}
66+
67+
tags = {
68+
Name = "unity_mmgis_lb_listener"
69+
}
70+
}

sds/unity/terraform/main.tf

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
data "aws_ssm_parameter" "vpc_id" {
2+
name = "/unity/account/network/vpc_id"
3+
}
4+
5+
data "aws_ssm_parameter" "subnet_list" {
6+
name = "/unity/account/network/subnet_list"
7+
}
8+
9+
#data "aws_ssm_parameter" "u-cs-ecs" {
10+
# name = "/unity/account/ecs/execution_role_arn"
11+
#}
12+
13+
module "base" {
14+
source = ""
15+
project = var.project
16+
venue = var.venue
17+
subsystem = var.subsystem
18+
capability = var.capability
19+
custom_url = var.custom_url
20+
groups = var.groups
21+
api = var.api
22+
component = var.component
23+
desired_count = var.desired_count
24+
app_protocol = var.app_protocol
25+
app_listening_port = var.app_listening_port
26+
environment = local.environment_vars
27+
ecr_uri = var.ecr_uri
28+
docker_image_name = var.docker_image_name
29+
docker_image_tag = var.docker_image_tag
30+
max_capacity = var.max_capacity
31+
app_one_ecs = var.app_one_ecs
32+
instance_type = var.instance_type
33+
ebs_block_device_size = var.ebs_block_device_size
34+
root_block_device_size = var.root_block_device_size
35+
ebs_mount_directory = var.ebs_mount_directory
36+
application_endpoint_url = var.application_endpoint_url
37+
terraform_app_commit = var.terraform_app_commit
38+
deployment_method = var.deployment_method
39+
secrets = local.secrets
40+
docker_volume_path = var.docker_volume_path
41+
efs_config = {
42+
efs_id = var.efs_id
43+
efs_root_directory = var.efs_root_directory
44+
}
45+
}
46+
47+
locals {
48+
subnet_map = jsondecode(data.aws_ssm_parameter.subnet_list.value)
49+
subnet_ids = nonsensitive(local.subnet_map["private"])
50+
public_subnet_ids = nonsensitive(local.subnet_map["public"])
51+
}
52+
53+
54+
# Application environment variables
55+
locals {
56+
environment_vars = {
57+
AWS_DEFAULT_REGION = module.base.aws_region
58+
DOMAIN = module.base.cname
59+
SERVER = var.server
60+
AUTH = var.auth
61+
NODE_ENV = var.node_env
62+
DB_HOST = var.db_host
63+
DB_PORT = var.db_port
64+
DB_NAME = var.db_name
65+
DB_USER = var.db_user
66+
PORT = var.app_listening_port
67+
DB_POOL_MAX = var.db_pool_max
68+
DB_POOL_TIMEOUT = var.db_pool_timeout
69+
DB_POOL_IDLE = var.db_pool_idle
70+
CSSO_GROUPS = var.csso_groups
71+
VERBOSE_LOGGING = var.verbose_logging
72+
FRAME_ANCESTORS = var.frame_ancestors
73+
FRAME_SRC = var.frame_src
74+
THIRD_PARTY_COOKIES = var.third_party_cookies
75+
ROOT_PATH = var.root_path
76+
WEBSOCKET_ROOT_PATH = var.websocket_root_path
77+
CLEARANCE_NUMBER = var.clearance_number
78+
DISABLE_LINK_SHORTENER = var.disable_link_shortener
79+
HIDE_CONFIG = var.hide_config
80+
FORCE_CONFIG_PATH = var.force_config_path
81+
LEADS = "[${join(", ", formatlist("\"%s\"", var.leads))}]"
82+
ENABLE_MMGIS_WEBSOCKETS = var.enable_mmgis_websockets
83+
ENABLE_CONFIG_WEBSOCKETS = var.enable_config_websockets
84+
ENABLE_CONFIG_OVERRIDE = var.enable_config_override
85+
MAIN_MISSION = var.main_mission
86+
SKIP_CLIENT_INITIAL_LOGIN = var.skip_client_initial_login
87+
GENERATE_SOURCEMAP = var.generate_sourcemap
88+
SPICE_SCHEDULED_KERNEL_DOWNLOAD = var.spice_scheduled_kernel_download
89+
SPICE_SCHEDULED_KERNEL_DOWNLOAD_ON_START = var.spice_scheduled_kernel_download_on_start
90+
SPICE_SCHEDULED_KERNEL_cron_expr = var.spice_scheduled_kernel_cron_expr
91+
}
92+
}
93+
94+
locals {
95+
secrets = {
96+
SECRET = var.secret
97+
DB_PASS = var.db_pass
98+
}
99+
}

sds/unity/terraform/output.tf

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# We try to match the API contract that `aws_instance` has.
2+
# Descriptions for these outputs are copied from:
3+
# https://www.terraform.io/docs/providers/aws/r/instance.html
4+
output "id" {
5+
description = "The instance ID"
6+
value = aws_instance.this.id
7+
}
8+
9+
output "arn" {
10+
description = "The ARN of the instance"
11+
value = aws_instance.this.arn
12+
}
13+
14+
output "availability_zone" {
15+
description = "The availability zone of the instance"
16+
value = aws_instance.this.availability_zone
17+
}
18+
19+
output "placement_group" {
20+
description = "The placement group of the instance"
21+
value = aws_instance.this.placement_group
22+
}
23+
24+
output "public_dns" {
25+
description = "The public DNS name assigned to the instance. For EC2-VPC, this is only available if you've enabled DNS hostnames for your VPC"
26+
value = aws_instance.this.public_dns
27+
}
28+
29+
output "public_ip" {
30+
description = "The public IP address assigned to the instance, if applicable. NOTE: If you are using an aws_eip with your instance, you should refer to the EIP's address directly and not use public_ip, as this field will change after the EIP is attached."
31+
value = aws_instance.this.public_ip
32+
}
33+
34+
output "ipv6_addresses" {
35+
description = "A list of assigned IPv6 addresses, if any"
36+
value = aws_instance.this.ipv6_addresses
37+
}
38+
39+
output "primary_network_interface_id" {
40+
description = "The ID of the instance's primary network interface"
41+
value = aws_instance.this.primary_network_interface_id
42+
}
43+
44+
output "private_dns" {
45+
description = " The private DNS name assigned to the instance. Can only be used inside the Amazon EC2, and only available if you've enabled DNS hostnames for your VPC"
46+
value = aws_instance.this.private_dns
47+
}
48+
49+
output "private_ip" {
50+
description = "The private IP address assigned to the instance"
51+
value = aws_instance.this.private_ip
52+
}
53+
54+
output "security_groups" {
55+
description = " The associated security groups."
56+
value = aws_instance.this.security_groups
57+
}
58+
59+
output "vpc_security_group_ids" {
60+
description = "The associated security groups in non-default VPC."
61+
value = aws_instance.this.vpc_security_group_ids
62+
}
63+
64+
output "subnet_id" {
65+
description = "The VPC subnet ID."
66+
value = aws_instance.this.subnet_id
67+
}
68+
69+
output "credit_specification" {
70+
description = " Credit specification of instance."
71+
value = aws_instance.this.credit_specification
72+
}
73+
74+
output "instance_state" {
75+
description = "The state of the instance. One of: pending, running, shutting-down, terminated, stopping, stopped. See Instance Lifecycle for more information."
76+
value = aws_instance.this.instance_state
77+
}
78+
79+
# TODO: This is a list with the `aws_instance` resource and we are just
80+
# returning a string. I know there is an obvious solution for this...
81+
output "ebs_block_device_id" {
82+
description = "The persistent block device that we are storing information on."
83+
value = aws_ebs_volume.persistent.id
84+
}

0 commit comments

Comments
 (0)