├── LICENSE ├── README.md ├── files ├── index.html └── logo.png ├── main.tf ├── outputs.tf ├── terraform.tfvars.example └── variables.tf /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | terraform-aws-docker 2 | ==================== 3 | 4 | A simple PoC using Terraform to create two instances of Amazon AWS EC2 servers running Docker with containerized Nginx daemon. It uses default Amazon AMI PV image for us-east-1 datacenter. 5 | 6 | To run, not forget to copy terraform.tfvars.example to terraform.tfvars and fill your personal information. 7 | 8 | Feel free to clone and modify. :) 9 | 10 | To check what recurses will be created: 11 | 12 | $ terraform plan 13 | 14 | To create EC2 instances and their dependencies: 15 | 16 | $ terraform apply 17 | 18 | To destroy all: 19 | 20 | $ terraform destroy 21 | 22 | Marcelo Pinheiro 2014 23 | -------------------------------------------------------------------------------- /files/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Cloud & DevOps: Let's talk about Infrastructure as Code? 5 | 13 | 14 | 15 | 16 |

Welcome to nginx!

17 | 18 |

This HTTP server is running as a Docker container named {{ container_name }} on server {{ hostname }}! :D 19 | 20 |

21 |
22 |

The Developer's Conference Porto Alegre

23 |

24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /files/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/salizzar/terraform-aws-docker/03af7cdb691235c22e0c97dbb7aac3a9f4c5d621/files/logo.png -------------------------------------------------------------------------------- /main.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | region = "${var.aws_region}" 3 | } 4 | 5 | resource "aws_security_group" "default" { 6 | count = "${var.aws_security_group.sg_count}" 7 | 8 | name = "terraform_security_group_${lookup(var.aws_security_group, concat("sg_", count.index, "_name"))}" 9 | description = "AWS security group for terraform example" 10 | 11 | ingress { 12 | from_port = "${lookup(var.aws_security_group, concat("sg_", count.index, "_from_port"))}" 13 | to_port = "${lookup(var.aws_security_group, concat("sg_", count.index, "_to_port"))}" 14 | protocol = "${lookup(var.aws_security_group, concat("sg_", count.index, "_protocol"))}" 15 | cidr_blocks = [ "0.0.0.0/0" ] 16 | } 17 | 18 | tags { 19 | Name = "Terraform AWS security group" 20 | } 21 | } 22 | 23 | resource "aws_elb" "web" { 24 | name = "terraform" 25 | 26 | listener { 27 | instance_port = 80 28 | instance_protocol = "http" 29 | lb_port = 80 30 | lb_protocol = "http" 31 | } 32 | 33 | availability_zones = [ 34 | "${aws_instance.web.*.availability_zone}" 35 | ] 36 | 37 | instances = [ 38 | "${aws_instance.web.*.id}", 39 | ] 40 | } 41 | 42 | resource "aws_instance" "web" { 43 | count = 3 44 | 45 | instance_type = "${var.aws_instance_type}" 46 | ami = "${lookup(var.aws_amis, var.aws_region)}" 47 | availability_zone = "${lookup(var.aws_availability_zones, count.index)}" 48 | 49 | key_name = "${var.aws_key_name}" 50 | security_groups = [ "${aws_security_group.default.*.name}" ] 51 | associate_public_ip_address = true 52 | 53 | connection { 54 | user = "${var.aws_instance_user}" 55 | key_file = "${var.aws_key_path}" 56 | } 57 | 58 | provisioner "file" { 59 | source = "files/" 60 | destination = "/tmp/" 61 | } 62 | 63 | provisioner "remote-exec" { 64 | inline = [ 65 | "sudo yum install -y docker", 66 | "sudo service docker start", 67 | "sudo docker pull nginx", 68 | "sudo docker run -d -p 80:80 -v /tmp:/usr/share/nginx/html --name nginx_${count.index} nginx", 69 | "sudo sed -iE \"s/{{ hostname }}/`hostname`/g\" /tmp/index.html", 70 | "sudo sed -iE \"s/{{ container_name }}/nginx_${count.index}/g\" /tmp/index.html" 71 | ] 72 | } 73 | 74 | tags { 75 | Name = "Terraform web ${count.index}" 76 | } 77 | } 78 | 79 | -------------------------------------------------------------------------------- /outputs.tf: -------------------------------------------------------------------------------- 1 | output "security_group" { 2 | value = "${join(", ", aws_security_group.default.*.id)}" 3 | } 4 | 5 | output "web_ip" { 6 | value = "${join(", ", aws_instance.web.*.public_ip)}" 7 | } 8 | 9 | output "elb_address" { 10 | value = "${aws_elb.web.dns_name}" 11 | } 12 | 13 | -------------------------------------------------------------------------------- /terraform.tfvars.example: -------------------------------------------------------------------------------- 1 | aws_region = "your_region" 2 | 3 | aws_key_name = "your_key_name" 4 | aws_key_path = "your_key_path" 5 | 6 | aws_instance_user = "your_aws_user" 7 | aws_instance_type = "your_ami_instance_type" 8 | 9 | aws_security_group.sg_count = "2" 10 | 11 | aws_security_group.sg_0_name = "ssh" 12 | aws_security_group.sg_0_from_port = "22" 13 | aws_security_group.sg_0_to_port = "22" 14 | aws_security_group.sg_0_protocol = "tcp" 15 | 16 | aws_security_group.sg_1_name = "http" 17 | aws_security_group.sg_1_from_port = "80" 18 | aws_security_group.sg_1_to_port = "80" 19 | aws_security_group.sg_1_protocol = "tcp" 20 | 21 | -------------------------------------------------------------------------------- /variables.tf: -------------------------------------------------------------------------------- 1 | variable "aws_region" {} 2 | 3 | variable "aws_key_name" {} 4 | variable "aws_key_path" {} 5 | 6 | variable "aws_instance_type" {} 7 | variable "aws_instance_user" {} 8 | 9 | variable "aws_amis" { 10 | default = { 11 | us-east-1 = "ami-246ed34c" # north virginia 12 | } 13 | } 14 | 15 | variable "aws_availability_zones" { 16 | default = { 17 | "0" = "us-east-1a" 18 | "1" = "us-east-1b" 19 | "2" = "us-east-1c" 20 | } 21 | } 22 | 23 | variable "aws_security_group" { 24 | default = { 25 | sg_count = "" 26 | 27 | sg_0_name = "" 28 | sg_0_ingress_from_port = "" 29 | sg_0_ingress_to_port = "" 30 | sg_0_protocol = "" 31 | 32 | sg_1_name = "" 33 | sg_1_ingress_from_port = "" 34 | sg_1_ingress_to_port = "" 35 | sg_1_protocol = "" 36 | 37 | sg_2_name = "" 38 | sg_2_ingress_from_port = "" 39 | sg_2_ingress_to_port = "" 40 | sg_2_protocol = "" 41 | } 42 | } 43 | 44 | --------------------------------------------------------------------------------