-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.tf
80 lines (69 loc) · 2.39 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
##########################################
# Retrieve the latest AMI id
##########################################
module "latest-ami" {
source = "./modules/ami-latest"
owners = var.ami-owners-list
regex = var.ami-regex
}
##########################################
# Initiate the temp files
##########################################
data "template_file" "organization_list" {
template = "${path.module}/organization-list-accts.log"
}
data "local_file" "getorganizationAccts" {
filename = data.template_file.organization_list.rendered
depends_on = [null_resource.getorganizationAccts]
}
resource "null_resource" "getorganizationAccts" {
provisioner "local-exec" {
command = "./aws-cli.sh"
interpreter = ["bash"]
environment = {
FILE = data.template_file.organization_list.rendered
PROFILE = var.profile
}
}
triggers = {
lastrun = data.template_file.organization_list.rendered
}
}
############################
# Packer File
############################
data "template_file" "ami-file" {
template = "${file("${path.module}/ami-template.json")}"
vars = {
ami-name = var.ami-name
vpc_id = var.vpc-id
subnet_id = var.subnet-id
# Remove substring if only 1 security group is needed
security_groups = "${substr(local.security-groups, 1, length(local.security-groups) - 2)}"
accounts = "${substr(data.local_file.getorganizationAccts.content, 1, length(data.local_file.getorganizationAccts.content) - 2)}"
# If you only have one account use -3
# accounts = "${substr(data.local_file.getorganizationAccts.content,1,length(data.local_file.getorganizationAccts.content)-3)}"
source_ami = module.latest-ami.ami-id
region = var.region
profile = var.profile
instance_profile = var.instance-profile
instance_type = var.instance-type
ssh_username = var.ssh-username
script = "${"./amazon.sh"}"
os = var.os
}
}
resource "local_file" "ami-json" {
content = data.template_file.ami-file.rendered
filename = "ami.json"
}
############################
# String manipulation
############################
data "template_file" "security-groups" {
count = "${length(var.security-groups)}"
template = "\"${element(var.security-groups, count.index)}\""
}
locals {
security-groups = "${join(",", data.template_file.security-groups.*.rendered)}"
}