Skip to content

Added user data #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ module "label" {
name = "${var.name}"
}

# Apply the tf_github_authorized_keys module for this resource
module "github_authorized_keys" {
source = "git::https://github.com/cloudposse/tf_github_authorized_keys.git?ref=tags/0.1.0"
github_api_token = "${var.github_api_token}"
github_organization = "${var.github_organization}"
github_team = "${var.github_team}"
}

resource "aws_iam_instance_profile" "default" {
name = "${module.label.id}"
role = "${aws_iam_role.default.name}"
Expand Down Expand Up @@ -71,11 +63,29 @@ resource "aws_security_group" "default" {
}
}

# Apply the tf_github_authorized_keys module for this resource
module "github_authorized_keys" {
source = "git::https://github.com/cloudposse/tf_github_authorized_keys.git?ref=tags/0.1.0"
github_api_token = "${var.github_api_token}"
github_organization = "${var.github_organization}"
github_team = "${var.github_team}"
}

data "template_file" "user_data" {
template = "${file("${path.module}/user_data.sh")}"

vars {
user_data = "${join("\n", compact(concat(var.user_data, list(module.github_authorized_keys.user_data))))}"
welcome_message = "${var.welcome_message}"
ssh_user = "${var.ssh_user}"
}
}

resource "aws_instance" "default" {
ami = "${var.ec2_ami}"
instance_type = "${var.instance_type}"

user_data = "${module.github_authorized_keys.user_data}"
user_data = "${data.template_file.user_data.rendered}"

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

# Apply the provisioner module for this resource
module "ansible" {
source = "git::https://github.com/cloudposse/tf_ansible.git?ref=tags/0.2.0"
source = "git::https://github.com/cloudposse/tf_ansible.git?ref=tags/0.3.0"
arguments = "${var.ansible_arguments}"
envs = ["host=${aws_eip.default.public_ip}"]
playbook = "${var.ansible_playbook}"
Expand Down
19 changes: 19 additions & 0 deletions user_data.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash

apt-get update
apt-get -y install figlet

# Generate system banner
figlet "${welcome_message}" > /etc/motd

##
## Setup SSH Config
##
cat <<"__EOF__" > /home/${ssh_user}/.ssh/config
Host *
StrictHostKeyChecking no
__EOF__
chmod 600 /home/${ssh_user}/.ssh/config
chown ${ssh_user}:${ssh_user} /home/${ssh_user}/.ssh/config

${user_data}
18 changes: 17 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ variable "github_organization" {}

variable "github_team" {}

variable "ansible_playbook" {}
variable "ansible_playbook" {
default = ""
}

variable "associate_public_ip_address" {
default = "true"
}

variable "ansible_arguments" {
type = "list"
default = []
}

variable "instance_type" {
Expand Down Expand Up @@ -45,3 +48,16 @@ variable "name" {
variable "ec2_ami" {
default = "ami-cd0f5cb6"
}

variable "user_data" {
type = "list"
default = []
}

variable "ssh_user" {
default = "ubuntu"
}

variable "welcome_message" {
default = ""
}