6887a27c by Volodymyr Tsap

Add sample4 with template

Adding README.md files
1 parent ab47c46e
# Sample1. Creating ubuntu instance using one file
```
# setup your AMS access parameters in ~/.aws
# Init terraform
terraform init
# Create instance
terraform apply
```
# Terraform would produce the state file terraform.tfstate
# Sample2. Creating ubuntu instance using set of files
# AMI selection moved to separate ami.tf
# Instance settings parametized by terraform variables
# Created variable files: terraform.tfvars, variables.tf
#
```
# setup your AMS access parameters in ~/.aws
# Init terraform
terraform init
# Create instance
terraform apply
```
## Get instance AMI
data "aws_ami" "xpdays-ami" {
most_recent = true
filter {
name = "name"
values = ["xpdays-ami*"]
}
}
#!/bin/bash
hostname ${instancehostname} && hostname > /etc/hostname
echo "127.0.0.1 localhost `hostname`" > /etc/hosts
cd /home/ubuntu
git clone https://bitbucket.org/bohdaq/wisehands.me.git
cd /home/ubuntu/wisehands.me/ && play deps
mkdir -p /home/ubuntu/wisehands.me/modules/guice-1.2
cd /home/ubuntu/wisehands.me/modules/guice-1.2
wget https://www.playframework.com/modules/guice-1.2.zip
unzip guice-1.2.zip
sed -i 's/mysql-database-endpoint/${dbendpoint}/g' /home/ubuntu/wisehands.me/conf/application.conf
cd /home/ubuntu/wisehands.me/ && play run
## Define provider
provider "aws" {
region = "${var.region}"
}
# Template for initial configuration bash script
data "template_file" "init" {
template = "${file("init.tpl")}"
count = "${length(var.instance_suffix)}"
vars {
dbendpoint="${aws_db_instance.db-instance.username}:${aws_db_instance.db-instance.password}@${aws_db_instance.db-instance.endpoint}\\/${aws_db_instance.db-instance.name}"
instancehostname="xpdays-${var.instance_suffix[count.index]}-${count.index}"
}
}
# Define the instance
resource "aws_instance" "xpdays-instance" {
ami = "${data.aws_ami.xpdays-ami.id}"
vpc_security_group_ids = [ "${var.vpc_security_group_ids}" ]
instance_type = "${lookup(var.instance_type, var.environment)}"
user_data = "${data.template_file.init.*.rendered[count.index]}"
tags {
Name = "xpdays${count.index + 1}"
}
count = "${length(var.instance_suffix)}"
}
## Print Output
output "xpdays_instance_public_ip" {
value = "${join(",",aws_instance.xpdays-instance.*.public_ip)}"
}
resource "aws_db_subnet_group" "default_db_subnet_group" {
name = "main"
subnet_ids = ["${var.default_db_subnet_group_subnet_ids[var.region]}"]
tags {
Name = "Default DB subnet group"
}
}
resource "aws_db_instance" "db-instance" {
allocated_storage = 10
storage_type = "gp2"
engine = "mysql"
engine_version = "5.7.17"
instance_class = "db.t2.micro"
name = "wisehandsdb"
username = "root"
password = "53N4CsNmQrxh2"
db_subnet_group_name = "${aws_db_subnet_group.default_db_subnet_group.id}"
final_snapshot_identifier = "snapshot-defaultdbinstance${count.index + 1}"
skip_final_snapshot = true
publicly_accessible = true
tags {
key = "Name"
value = "default-db-instance${count.index + 1}-${var.environment}"
}
}
output "database_endpoint" {
value = "${aws_db_instance.db-instance.username}:${aws_db_instance.db-instance.password}@${aws_db_instance.db-instance.endpoint}/${aws_db_instance.db-instance.name}"
}
# String
region = "eu-central-1"
# List
vpc_security_group_ids = [ "sg-84e649ed", "sg-90ea45fa" ]
# Map
instance_type = {
production = "t2.micro"
development = "m3.medium"
}
# Map of Lists
default_db_subnet_group_subnet_ids = {
eu-central-1 = [ "subnet-f1e92d8a", "subnet-304b7f7a" ]
eu-west-1 = [ "subnet-f1e92d8a", "subnet-304b7f7a" ]
}
#
instance_suffix = ["a","b"]
variable "region" {
type = "string"
default = "eu-central-1"
description = "The AWS region"
}
variable "environment" {
description = "The Environment Type"
default = "production"
}
variable "default_db_subnet_group_subnet_ids" {
type = "map"
default = {}
}
variable "vpc_security_group_ids" {
type = "list"
}
variable "instance_type" {
type = "map"
default = {}
}
variable "instance_suffix" {
type = "list"
description = "Add instance suffix"
}