Skip to content

Instantly share code, notes, and snippets.

@bitglue
Created January 15, 2015 18:23
Show Gist options
  • Save bitglue/a18e8329f0f17a9dbd60 to your computer and use it in GitHub Desktop.
Save bitglue/a18e8329f0f17a9dbd60 to your computer and use it in GitHub Desktop.
Deploy a CoreOS cluster with Terraform
variable "cluster-size" {
description = "Number of CoreOS machines to deploy"
}
variable "region" {
description = "AWS region in which to deploy"
default = "us-east-1"
}
variable "coreos-ami" {
description = "AMI for CoreOS, mapped by region"
default = {
us-east-1 = "ami-705d3d18"
}
}
variable "subnet-azs" {
description = "Availability zones for each subnet"
default = {
"0" = "a"
"1" = "b"
"2" = "d"
}
}
variable "subnet-blocks" {
description = "CIDR blocks for each subnet"
default = {
"0" = "10.2.1.0/24"
"1" = "10.2.2.0/24"
"2" = "10.2.3.0/24"
}
}
provider "aws" {
region = "${var.region}"
}
resource "aws_vpc" "vpc" {
cidr_block = "10.2.0.0/16"
tags {
Name = "terraform test"
}
}
resource "aws_internet_gateway" "gateway" {
vpc_id = "${aws_vpc.vpc.id}"
}
resource "aws_route_table" "public" {
vpc_id = "${aws_vpc.vpc.id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.gateway.id}"
}
}
resource "aws_security_group" "http" {
name = "http"
description = "allow http"
vpc_id = "${aws_vpc.vpc.id}"
ingress {
from_port = "0"
to_port = "80"
cidr_blocks = ["0.0.0.0/0"]
protocol = "tcp"
}
}
resource "aws_security_group" "ssh" {
name = "ssh"
description = "allow ssh"
vpc_id = "${aws_vpc.vpc.id}"
ingress {
from_port = "0"
to_port = "22"
cidr_blocks = ["0.0.0.0/0"]
protocol = "tcp"
}
}
resource "aws_route_table_association" "public" {
subnet_id = "${element(aws_subnet.core.*.id, count.index)}"
route_table_id = "${aws_route_table.public.id}"
count = 3
}
resource "aws_subnet" "core" {
availability_zone = "${var.region}${lookup(var.subnet-azs, count.index)}"
cidr_block = "${lookup(var.subnet-blocks, count.index)}"
vpc_id = "${aws_vpc.vpc.id}"
count = 3
tags {
Name = "terraform test"
}
}
resource "aws_instance" "coreos" {
ami = "${lookup(var.coreos-ami, var.region)}"
instance_type = "t2.micro"
count = "${var.cluster-size}"
subnet_id = "${element(aws_subnet.core.*.id, count.index)}"
key_name = "phil"
associate_public_ip_address = true
user_data = "${file("${path.module}")}"
security_groups = ["${aws_security_group.ssh.id}", "${aws_security_group.http.id}"]
# Dependency necessary for destroy to work: the instances can actually be
# created without the gateway, but the gateway can't be destroyed as long
# as the instances still have public IPs.
depends_on = ["aws_internet_gateway.gateway"]
tags {
Name = "terraform test"
}
}
output "ip" {
value = "${join(\", \", aws_instance.coreos.*.public_ip)}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment