d18efca8 by Volodymyr Tsap

adding sample5

1 parent 8627d5eb
...@@ -3,5 +3,6 @@ ...@@ -3,5 +3,6 @@
3 *.tfstate 3 *.tfstate
4 *.tfstate.backup 4 *.tfstate.backup
5 */.* 5 */.*
6 */*/.*
6 # Module directory 7 # Module directory
7 .terraform/ 8 .terraform/
......
1 # Sample2. Creating ubuntu instance using set of files 1 # Sample3. Deploy a sample application, using RDS
2 2
3 1. Build custom image using packer. Build/redeploy workflow 3 1. Build custom image using packer. Build/redeploy workflow
4 2. Deploy a sample application using *user_data* 4 2. Deploy a sample application using *user_data*
......
1 # Sample5. Scaling our applications, adding scaling policies
2
3 1. Add IAM profile for instance
4 2. Creating launch configuration, autoscaling grops
5 3. Building module
6 4. Dealing with LoadBalancers and http
7
8 ```
9 # setup your AMS access parameters in ~/.aws
10
11 # Init terraform
12 terraform init
13 # Create instance
14 terraform apply
15 ```
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 a policy to RO
2 resource "aws_iam_policy" "ec2-ro-policy" {
3 name = "ec2-ro-policy"
4 path = "/"
5 description = "Autocreated Policy to read tags from ec2 instance"
6 policy = <<EOF
7 {
8 "Version": "2012-10-17",
9 "Statement": [
10 {
11 "Sid": "Stmt1506366968000",
12 "Effect": "Allow",
13 "Action": [
14 "ec2:DescribeInstances",
15 "ec2:DescribeTags"
16 ],
17 "Resource": [
18 "*"
19 ]
20 }
21 ]
22 }
23 EOF
24 }
25
26 ## STS AssumeRole Data
27 data "aws_iam_policy_document" "instance-assume-role-policy" {
28 statement {
29 actions = ["sts:AssumeRole"]
30 principals {
31 type = "Service"
32 identifiers = ["ec2.amazonaws.com"]
33 }
34 }
35 }
36
37 ## Add EC2 instance role
38 resource "aws_iam_role" "ec2-instance-role" {
39 name = "ec2-instance-role"
40 path = "/"
41 assume_role_policy = "${data.aws_iam_policy_document.instance-assume-role-policy.json}"
42 }
43
44 ## Attach policy to role
45 resource "aws_iam_policy_attachment" "ec2-policy-attachemnt" {
46 name = "ec2-policy-attachemnt"
47 roles = ["${aws_iam_role.ec2-instance-role.name}"]
48 policy_arn = "${aws_iam_policy.ec2-ro-policy.arn}"
49 }
50
51 ## Create instance profile and attah the role
52 resource "aws_iam_instance_profile" "ec2-instance-profile" {
53 name = "ec2-instance-profile"
54 role = "${aws_iam_role.ec2-instance-role.name}"
55 }
56
1 # Template for initial configuration bash script
2 data "template_file" "init" {
3 template = "${file("files/init.tpl")}"
4 count = "${length(var.instance_suffix)}"
5
6 vars {
7 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}"
8 instancehostname="xpdays-${var.instance_suffix[count.index]}-${count.index}"
9 }
10 }
11
12 # Define the instance
13 #resource "aws_instance" "xpdays-instance" {
14 # ami = "${data.aws_ami.xpdays-ami.id}"
15 # vpc_security_group_ids = [ "${var.vpc_security_group_ids}" ]
16 # instance_type = "${lookup(var.instance_type, var.environment)}"
17 # user_data = "${data.template_file.init.*.rendered[count.index]}"
18 #
19 # tags {
20 # Name = "xpdays${count.index + 1}"
21 # }
22 #
23 # count = "${length(var.instance_suffix)}"
24 #}
25
26 resource "aws_launch_configuration" "launch-xpdays" {
27 # name = "${var.environment}-launch-xpdays${count.index + 1}"
28 image_id = "${data.aws_ami.xpdays-ami.id}"
29 instance_type = "${lookup(var.instance_type, var.environment)}"
30 iam_instance_profile = "${aws_iam_instance_profile.ec2-instance-profile.name}"
31 associate_public_ip_address = true
32 enable_monitoring = true
33 user_data = "${data.template_file.init.*.rendered[count.index]}"
34 lifecycle {
35 create_before_destroy = true
36 }
37 count = "${length(var.instance_suffix)}"
38 }
39
1 ## Define provider
2 provider "aws" {
3 region = "${var.region}"
4 }
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 = ["blue","green"]
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