Skip to content

Instantly share code, notes, and snippets.

@seanknox
Last active March 9, 2016 17:36
Show Gist options
  • Save seanknox/119377bae88917484d7f to your computer and use it in GitHub Desktop.
Save seanknox/119377bae88917484d7f to your computer and use it in GitHub Desktop.
# Create a VPC to launch our instances into
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
tags {
Name = "Terraform VPC"
}
}
resource "aws_vpc_endpoint" "private_s3" {
vpc_id = "${aws_vpc.main.id}"
service_name = "com.amazonaws.us-west-2.s3"
route_table_ids = [ "${aws_route_table.public.id}" ]
}
# Create an internet gateway to give our subnet access to the outside world
resource "aws_internet_gateway" "default" {
vpc_id = "${aws_vpc.main.id}"
}
# Create a subnet to launch our instances into
resource "aws_subnet" "public" {
vpc_id = "${aws_vpc.main.id}"
cidr_block = ""10.0.1.0/24""
availability_zone = "us-west-1b,us-west-1c"
map_public_ip_on_launch = true
depends_on = ["aws_internet_gateway.default"]
tags {
Name = "public"
}
}
# Grant the VPC internet access on its separate route table
resource "aws_route_table" "public" {
vpc_id = "${aws_vpc.main.id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.default.id}"
}
tags {
Name = "public"
}
}
resource "aws_route_table_association" "public" {
subnet_id = "${aws_subnet.public.id}"
route_table_id = "${aws_route_table.public.id}"
}
resource "aws_elb" "web-elb" {
name = "terraform-elb"
subnets = ["${aws_subnet.public.id}"]
security_groups = ["${aws_security_group.elb.id}"]
idle_timeout = 300
connection_draining = true
connection_draining_timeout = 300
listener {
instance_port = 80
instance_protocol = "http"
lb_port = 80
lb_protocol = "http"
}
health_check {
healthy_threshold = 2
unhealthy_threshold = 2
timeout = 30
target = "HTTP:80/"
interval = 120
}
}
resource "aws_autoscaling_group" "web-asg" {
name = "terraform-asg"
availability_zones = ["us-west-1b,us-west-1c"]
max_size = 3
min_size = 2
desired_capacity = 2
force_delete = false
launch_configuration = "${aws_launch_configuration.web-lc.name}"
load_balancers = ["${aws_elb.web-elb.name}"]
health_check_grace_period = 60
health_check_type = "EC2"
tag {
key = "Name"
value = "web-asg"
propagate_at_launch = "true"
}
}
resource "aws_launch_configuration" "web-lc" {
name_prefix = "terraform-lc-"
# ubuntu-trusty-14.04 (x64)
image_id = "ami-7f675e4f"
instance_type = "t2.micro"
key_name = "${aws_key_pair.auth.id}"
security_groups = ["${aws_security_group.web_tier_access.id}"]
user_data = "${file("install_nginx.sh")}"
lifecycle {
create_before_destroy = true
}
}
# A security group for the ELB so it is accessible via the web
resource "aws_security_group" "elb" {
name = "terraform_elb"
description = "Used in the terraform"
vpc_id = "${aws_vpc.main.id}"
# HTTP access from anywhere
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# outbound internet access
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 8
to_port = 0
protocol = "icmp"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_security_group" "vpc_http" {
name = "VPC HTTP"
description = "Terraform HTTP access from VPC"
vpc_id = "${aws_vpc.main.id}"
# HTTP access from the VPC
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["10.0.0.0/16"]
}
}
resource "aws_security_group" "web_tier_access" {
name = "web_tier_access"
description = "Allow inbound admin ssh, http to the web tier"
vpc_id = "${aws_vpc.main.id}"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# RDP
ingress {
from_port = 3389
to_port = 3389
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# WinRM access
ingress {
from_port = 5985
to_port = 5985
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 8
to_port = 0
protocol = "icmp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
aws_key_pair.auth: Creating...
fingerprint: "" => "<computed>"
key_name: "" => "terraform_sean"
public_key: "" => "ssh-rsa ...== [email protected]"
aws_vpc.main: Creating...
cidr_block: "" => "10.0.0.0/16"
default_network_acl_id: "" => "<computed>"
default_security_group_id: "" => "<computed>"
dhcp_options_id: "" => "<computed>"
enable_classiclink: "" => "<computed>"
enable_dns_hostnames: "" => "<computed>"
enable_dns_support: "" => "<computed>"
main_route_table_id: "" => "<computed>"
tags.#: "" => "1"
tags.Name: "" => "Terraform VPC"
aws_key_pair.auth: Creation complete
aws_vpc.main: Creation complete
aws_internet_gateway.default: Creating...
vpc_id: "" => "vpc-17807373"
aws_security_group.vpc_http: Creating...
description: "" => "Terraform HTTP access from VPC"
egress.#: "" => "<computed>"
ingress.#: "" => "1"
ingress.2165049311.cidr_blocks.#: "" => "1"
ingress.2165049311.cidr_blocks.0: "" => "10.0.0.0/16"
ingress.2165049311.from_port: "" => "80"
ingress.2165049311.protocol: "" => "tcp"
ingress.2165049311.security_groups.#: "" => "0"
ingress.2165049311.self: "" => "0"
ingress.2165049311.to_port: "" => "80"
name: "" => "VPC HTTP"
owner_id: "" => "<computed>"
vpc_id: "" => "vpc-17807373"
aws_security_group.elb: Creating...
description: "" => "Used in the terraform"
egress.#: "" => "1"
egress.482069346.cidr_blocks.#: "" => "1"
egress.482069346.cidr_blocks.0: "" => "0.0.0.0/0"
egress.482069346.from_port: "" => "0"
egress.482069346.protocol: "" => "-1"
egress.482069346.security_groups.#: "" => "0"
egress.482069346.self: "" => "0"
egress.482069346.to_port: "" => "0"
ingress.#: "" => "2"
ingress.2214680975.cidr_blocks.#: "" => "1"
ingress.2214680975.cidr_blocks.0: "" => "0.0.0.0/0"
ingress.2214680975.from_port: "" => "80"
ingress.2214680975.protocol: "" => "tcp"
ingress.2214680975.security_groups.#: "" => "0"
ingress.2214680975.self: "" => "0"
ingress.2214680975.to_port: "" => "80"
ingress.3068409405.cidr_blocks.#: "" => "1"
ingress.3068409405.cidr_blocks.0: "" => "0.0.0.0/0"
ingress.3068409405.from_port: "" => "8"
ingress.3068409405.protocol: "" => "icmp"
ingress.3068409405.security_groups.#: "" => "0"
ingress.3068409405.self: "" => "0"
ingress.3068409405.to_port: "" => "0"
name: "" => "terraform_elb"
owner_id: "" => "<computed>"
vpc_id: "" => "vpc-17807373"
aws_security_group.web_tier_access: Creating...
description: "" => "Allow inbound admin ssh, http to the web tier"
egress.#: "" => "1"
egress.482069346.cidr_blocks.#: "" => "1"
egress.482069346.cidr_blocks.0: "" => "0.0.0.0/0"
egress.482069346.from_port: "" => "0"
egress.482069346.protocol: "" => "-1"
egress.482069346.security_groups.#: "" => "0"
egress.482069346.self: "" => "0"
egress.482069346.to_port: "" => "0"
ingress.#: "" => "5"
ingress.1560369949.cidr_blocks.#: "" => "1"
ingress.1560369949.cidr_blocks.0: "" => "0.0.0.0/0"
ingress.1560369949.from_port: "" => "22"
ingress.1560369949.protocol: "" => "tcp"
ingress.1560369949.security_groups.#: "" => "0"
ingress.1560369949.self: "" => "0"
ingress.1560369949.to_port: "" => "22"
ingress.2120593537.cidr_blocks.#: "" => "1"
ingress.2120593537.cidr_blocks.0: "" => "0.0.0.0/0"
ingress.2120593537.from_port: "" => "5985"
ingress.2120593537.protocol: "" => "tcp"
ingress.2120593537.security_groups.#: "" => "0"
ingress.2120593537.self: "" => "0"
ingress.2120593537.to_port: "" => "5985"
ingress.2214680975.cidr_blocks.#: "" => "1"
ingress.2214680975.cidr_blocks.0: "" => "0.0.0.0/0"
ingress.2214680975.from_port: "" => "80"
ingress.2214680975.protocol: "" => "tcp"
ingress.2214680975.security_groups.#: "" => "0"
ingress.2214680975.self: "" => "0"
ingress.2214680975.to_port: "" => "80"
ingress.3068409405.cidr_blocks.#: "" => "1"
ingress.3068409405.cidr_blocks.0: "" => "0.0.0.0/0"
ingress.3068409405.from_port: "" => "8"
ingress.3068409405.protocol: "" => "icmp"
ingress.3068409405.security_groups.#: "" => "0"
ingress.3068409405.self: "" => "0"
ingress.3068409405.to_port: "" => "0"
ingress.4243602507.cidr_blocks.#: "" => "1"
ingress.4243602507.cidr_blocks.0: "" => "0.0.0.0/0"
ingress.4243602507.from_port: "" => "3389"
ingress.4243602507.protocol: "" => "tcp"
ingress.4243602507.security_groups.#: "" => "0"
ingress.4243602507.self: "" => "0"
ingress.4243602507.to_port: "" => "3389"
name: "" => "web_tier_access"
owner_id: "" => "<computed>"
vpc_id: "" => "vpc-17807373"
aws_internet_gateway.default: Creation complete
aws_subnet.public: Creating...
availability_zone: "" => "us-west-2a"
cidr_block: "" => "10.0.1.0/24"
map_public_ip_on_launch: "" => "1"
tags.#: "" => "1"
tags.Name: "" => "public"
vpc_id: "" => "vpc-17807373"
aws_route_table.public: Creating...
route.#: "" => "1"
route.3930981082.cidr_block: "" => "0.0.0.0/0"
route.3930981082.gateway_id: "" => "igw-875408e2"
route.3930981082.instance_id: "" => ""
route.3930981082.nat_gateway_id: "" => ""
route.3930981082.network_interface_id: "" => ""
route.3930981082.vpc_peering_connection_id: "" => ""
tags.#: "" => "1"
tags.Name: "" => "public"
vpc_id: "" => "vpc-17807373"
aws_security_group.vpc_http: Creation complete
aws_security_group.web_tier_access: Creation complete
aws_launch_configuration.web-lc: Creating...
associate_public_ip_address: "" => "0"
ebs_block_device.#: "" => "<computed>"
ebs_optimized: "" => "<computed>"
enable_monitoring: "" => "1"
image_id: "" => "ami-7f675e4f"
instance_type: "" => "t2.micro"
key_name: "" => "terraform_sean"
name: "" => "<computed>"
name_prefix: "" => "terraform-lc-nysug-"
root_block_device.#: "" => "<computed>"
security_groups.#: "" => "1"
security_groups.1729858570: "" => "sg-0de06e6a"
user_data: "" => "ece17379fa1f3dcb0a45201df85936c8e9118445"
aws_security_group.elb: Creation complete
aws_subnet.public: Creation complete
aws_elb.web-elb: Creating...
availability_zones.#: "" => "<computed>"
connection_draining: "" => "1"
connection_draining_timeout: "" => "300"
dns_name: "" => "<computed>"
health_check.#: "" => "1"
health_check.0.healthy_threshold: "" => "2"
health_check.0.interval: "" => "120"
health_check.0.target: "" => "HTTP:80/"
health_check.0.timeout: "" => "30"
health_check.0.unhealthy_threshold: "" => "2"
idle_timeout: "" => "300"
instances.#: "" => "<computed>"
internal: "" => "<computed>"
listener.#: "" => "1"
listener.3057123346.instance_port: "" => "80"
listener.3057123346.instance_protocol: "" => "http"
listener.3057123346.lb_port: "" => "80"
listener.3057123346.lb_protocol: "" => "http"
listener.3057123346.ssl_certificate_id: "" => ""
name: "" => "terraform-elb"
security_groups.#: "" => "1"
security_groups.1001775934: "" => "sg-0ce06e6b"
source_security_group: "" => "<computed>"
source_security_group_id: "" => "<computed>"
subnets.#: "" => "1"
subnets.923991259: "" => "subnet-614a5416"
zone_id: "" => "<computed>"
aws_route_table.public: Creation complete
aws_route_table_association.public: Creating...
route_table_id: "" => "rtb-3177a155"
subnet_id: "" => "subnet-614a5416"
aws_vpc_endpoint.private_s3: Creating...
policy: "" => "<computed>"
route_table_ids.#: "" => "1"
route_table_ids.107673955: "" => "rtb-3177a155"
service_name: "" => "com.amazonaws.us-west-2.s3"
vpc_id: "" => "vpc-17807373"
aws_route_table_association.public: Creation complete
aws_vpc_endpoint.private_s3: Creation complete
aws_launch_configuration.web-lc: Creation complete
aws_elb.web-elb: Creation complete
aws_autoscaling_group.web-asg: Creating...
availability_zones.#: "" => "1"
availability_zones.2487133097: "" => "us-west-2a"
default_cooldown: "" => "<computed>"
desired_capacity: "" => "2"
force_delete: "" => "0"
health_check_grace_period: "" => "60"
health_check_type: "" => "EC2"
launch_configuration: "" => "terraform-lc-nysug-inpqi3ty3jbb3c5ksr4lcwukbu"
load_balancers.#: "" => "1"
load_balancers.3768627209: "" => "terraform-elb"
max_size: "" => "3"
min_size: "" => "2"
name: "" => "terraform-asg-nysug"
tag.#: "" => "1"
tag.2421615522.key: "" => "Name"
tag.2421615522.propagate_at_launch: "" => "1"
tag.2421615522.value: "" => "web-asg"
vpc_zone_identifier.#: "" => "<computed>"
wait_for_capacity_timeout: "" => "10m"
Error applying plan:
1 error(s) occurred:
* aws_autoscaling_group.web-asg: timeout while waiting for state to become '[success]'
Terraform does not automatically rollback in the face of errors.
Instead, your Terraform state file has been partially updated with
any resources that successfully completed. Please address the error
above and apply again to incrementally change your infrastructure.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment