Created
January 25, 2017 21:21
-
-
Save pgporada/4f44f0e4f5448d81d620606c1ad9513c to your computer and use it in GitHub Desktop.
terraform creating user_data for a server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -x | |
# Ensure dependencies are installed | |
yum install -y epel-release | |
yum update -y epel-release | |
yum install -y python-pip python-devel git openssl-devel libffi-devel | |
pip install --upgrade pip | |
pip install --upgrade setuptools | |
yum install -y awscli python-six ansible | |
# If you don't unset these, then the aws cli commands will fail with a 'partial credentials have been found' error | |
unset AWS_ACCESS_KEY_ID | |
unset AWS_SECRET_KEY | |
# Get the MACHINE USER key | |
# FETCH YOUR MACHINE USER KEY HERE | |
chmod 0600 /root/.ssh/YOURMACHINEUSERNAME | |
ssh-keyscan -t rsa bitbucket.org >> ~/.ssh/known_hosts | |
# Allow Ansible to SSH in from Jenkins | |
mkdir -p /home/${TERRAFORM_user}/.ssh | |
chmod 0700 /home/${TERRAFORM_user}/.ssh | |
chown ${TERRAFORM_user}:${TERRAFORM_user} /home/${TERRAFORM_user}/.ssh | |
# FETCH YOUR CI KEY(S) HERE | |
# SET UP YOUR AUTHORIZED KEY(S) HERE | |
# Get the ansible playbook from our repo | |
ssh-agent bash -c "ssh-add ~/.ssh/MYKEY ; cd /root ; git clone [email protected]:MYCOMPANY/ansible-playbook-bastion.git" | |
# Install Ansible playbook dependencies | |
ssh-agent bash -c "ssh-add ~/.ssh/MYKEY ; cd /root/ansible-playbook-bastion ; make install-python-requirements" | |
ssh-agent bash -c "ssh-add ~/.ssh/MYKEY ; cd /root/ansible-playbook-bastion ; make install-ansible-modules" | |
ssh-agent bash -c "ssh-add ~/.ssh/MYKEY ; cd /root/ansible-playbook-bastion ; ENVIRONMENT=${TERRAFORM_env} TIER=${TERRAFORM_tier} make install-s3-secrets" | |
ssh-agent bash -c "ssh-add ~/.ssh/MYKEY ; cd /root/ansible-playbook-bastion ; cd ansible ; ansible-playbook playbooks/deploy-aws-${TERRAFORM_role}.yml -i environments/aws -e cli_myhosts='localhost' -e cli_role=${TERRAFORM_role} -e cli_tier=${TERRAFORM_tier} -e cli_env=${TERRAFORM_env} --connection=local" | |
# Cleanup | |
rm -rf /root/ansible-playbook-bastion | |
rm -f /root/.ssh/MACHINEUSERKEY |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data "template_file" "bastion" { | |
template = "${file("${path.module}/init.sh")}" | |
vars { | |
TERRAFORM_env = "${var.env}" | |
TERRAFORM_tier = "${var.tier}" | |
TERRAFORM_role = "bastion" | |
TERRAFORM_user = "${var.shell_username}" | |
TERRAFORM_hosts = "localhost" | |
} | |
} | |
resource "aws_instance" "bastion" { | |
ami = "${data.aws_ami.bastion.id}" | |
instance_type = "${var.instance_type}" | |
key_name = "${var.bastion_key_name}" | |
subnet_id = "${element(split(",", var.subnet_ids), count.index)}" | |
vpc_security_group_ids = ["${aws_security_group.bastion.id}"] | |
user_data = "${data.template_file.bastion.rendered}" | |
iam_instance_profile = "${aws_iam_instance_profile.bastion.name}" | |
tags { | |
Name = "${var.env}_${var.tier}_${replace(var.region,"-","")}_bastion" | |
TYPE = "bastion" | |
ROLES = "bastion" | |
ENV = "${var.env}" | |
TERRAFORM = "true" | |
TIER = "${var.tier}" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment