6887a27c by Volodymyr Tsap

Add sample4 with template

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