Skip to content

Commit 118f08f

Browse files
authored
Added user data (#2)
* Added user data * Added dns redirect tier * Bind ansible module
1 parent 258325e commit 118f08f

File tree

3 files changed

+56
-11
lines changed

3 files changed

+56
-11
lines changed

main.tf

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,6 @@ module "label" {
2525
name = "${var.name}"
2626
}
2727

28-
# Apply the tf_github_authorized_keys module for this resource
29-
module "github_authorized_keys" {
30-
source = "git::https://github.com/cloudposse/tf_github_authorized_keys.git?ref=tags/0.1.0"
31-
github_api_token = "${var.github_api_token}"
32-
github_organization = "${var.github_organization}"
33-
github_team = "${var.github_team}"
34-
}
35-
3628
resource "aws_iam_instance_profile" "default" {
3729
name = "${module.label.id}"
3830
role = "${aws_iam_role.default.name}"
@@ -71,11 +63,29 @@ resource "aws_security_group" "default" {
7163
}
7264
}
7365

66+
# Apply the tf_github_authorized_keys module for this resource
67+
module "github_authorized_keys" {
68+
source = "git::https://github.com/cloudposse/tf_github_authorized_keys.git?ref=tags/0.1.0"
69+
github_api_token = "${var.github_api_token}"
70+
github_organization = "${var.github_organization}"
71+
github_team = "${var.github_team}"
72+
}
73+
74+
data "template_file" "user_data" {
75+
template = "${file("${path.module}/user_data.sh")}"
76+
77+
vars {
78+
user_data = "${join("\n", compact(concat(var.user_data, list(module.github_authorized_keys.user_data))))}"
79+
welcome_message = "${var.welcome_message}"
80+
ssh_user = "${var.ssh_user}"
81+
}
82+
}
83+
7484
resource "aws_instance" "default" {
7585
ami = "${var.ec2_ami}"
7686
instance_type = "${var.instance_type}"
7787

78-
user_data = "${module.github_authorized_keys.user_data}"
88+
user_data = "${data.template_file.user_data.rendered}"
7989

8090
vpc_security_group_ids = [
8191
"${compact(concat(list(aws_security_group.default.id), var.security_groups))}"
@@ -103,7 +113,7 @@ resource "aws_eip" "default" {
103113

104114
# Apply the provisioner module for this resource
105115
module "ansible" {
106-
source = "git::https://github.com/cloudposse/tf_ansible.git?ref=tags/0.2.0"
116+
source = "git::https://github.com/cloudposse/tf_ansible.git?ref=tags/0.3.0"
107117
arguments = "${var.ansible_arguments}"
108118
envs = ["host=${aws_eip.default.public_ip}"]
109119
playbook = "${var.ansible_playbook}"

user_data.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
3+
apt-get update
4+
apt-get -y install figlet
5+
6+
# Generate system banner
7+
figlet "${welcome_message}" > /etc/motd
8+
9+
##
10+
## Setup SSH Config
11+
##
12+
cat <<"__EOF__" > /home/${ssh_user}/.ssh/config
13+
Host *
14+
StrictHostKeyChecking no
15+
__EOF__
16+
chmod 600 /home/${ssh_user}/.ssh/config
17+
chown ${ssh_user}:${ssh_user} /home/${ssh_user}/.ssh/config
18+
19+
${user_data}

variables.tf

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ variable "github_organization" {}
66

77
variable "github_team" {}
88

9-
variable "ansible_playbook" {}
9+
variable "ansible_playbook" {
10+
default = ""
11+
}
1012

1113
variable "associate_public_ip_address" {
1214
default = "true"
1315
}
1416

1517
variable "ansible_arguments" {
1618
type = "list"
19+
default = []
1720
}
1821

1922
variable "instance_type" {
@@ -45,3 +48,16 @@ variable "name" {
4548
variable "ec2_ami" {
4649
default = "ami-cd0f5cb6"
4750
}
51+
52+
variable "user_data" {
53+
type = "list"
54+
default = []
55+
}
56+
57+
variable "ssh_user" {
58+
default = "ubuntu"
59+
}
60+
61+
variable "welcome_message" {
62+
default = ""
63+
}

0 commit comments

Comments
 (0)