Created
June 18, 2018 13:40
-
-
Save codfather/91be108f4f9ae5278604dceebbf19d86 to your computer and use it in GitHub Desktop.
Create a simple test server for spike using Terraform - MIND - can have slashes in gist file names - so have used - instead.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
provider "aws" { | |
region = "${var.region}" | |
version = "~> 1.9" | |
} | |
provider "template" { | |
version = "~> 1.0" | |
} | |
resource "aws_default_vpc" "default" { | |
tags { | |
Name = "Default VPC" | |
} | |
} | |
module "security_groups" { | |
vpc-id = "${aws_default_vpc.default.id}" | |
my_ip_address = "${var.current_ip}" | |
secgrp_des = "${var.secgrp_des}" | |
source = "modules/security_groups" | |
} | |
module "ubuntu-spike" { | |
# source = "${var.var}" | |
source = "modules/instance" | |
vpc_subnet = "${aws_default_vpc}" | |
poc-vpc-id = "${aws_default_vpc.default.id}" | |
# Pass variables down from the security group module to the instance | |
vpc-sec-grp-id = "${module.security_groups.spike_security_group_id}" | |
# Specify the version of Ubuntu to use | |
distro = "bionic" | |
version = "18.04" | |
eip_allocation = "${var.eip_allocation}" | |
public_key = "${var.public_key}" | |
} | |
output "pub_ip_address" { | |
value = "${module.ubuntu-spike.public_ip}" | |
} | |
output "security_group_id" { | |
value = "${module.security_groups.spike_security_group_id}" | |
} | |
output "pub_dns" { | |
value = "${module.ubuntu-spike.public_dns}" | |
} | |
output "vpc_id" { | |
value = "${aws_default_vpc.default.id}" | |
} | |
output "subnet_id" { | |
value = "${module.ubuntu-spike.instance_default_vpc_subnet}" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data "aws_ami" "ubuntu" { | |
most_recent = true | |
filter { name = "name" | |
values = ["ubuntu/images/hvm-ssd/ubuntu-${var.distro}-18.04-amd64-server-*"] | |
} | |
filter { | |
name = "virtualization-type" | |
values = ["hvm"] | |
} | |
owners = ["099720109477"] # Canonical | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create a new instance of the latest Ubuntu AMI | |
# t2.micro node | |
# Associate an existing IP address if required | |
resource "aws_eip_association" "staticip" { | |
allocation_id = "${var.eip_allocation}" | |
instance_id = "${aws_instance.spike.id}" | |
} | |
resource "aws_default_subnet" "default_az1" { | |
availability_zone = "eu-west-2c" | |
tags { | |
Name = "Default subnet" | |
} | |
} | |
resource "aws_instance" "spike" { | |
ami = "${data.aws_ami.ubuntu.id}" | |
user_data = "${data.template_file.userdata.rendered}" | |
instance_type = "t2.micro" | |
key_name = "${var.public_key}" | |
vpc_security_group_ids = ["${var.vpc-sec-grp-id}"] | |
subnet_id = "${aws_default_subnet.default_az1.id}" | |
root_block_device { | |
volume_size = "10" | |
delete_on_termination = "true" | |
} | |
tags { | |
Name = "spike-instance" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
output "public_ip" { | |
value = "${aws_eip_association.staticip.public_ip}" | |
} | |
# Using splat here to allow for multiple servers if needed | |
output "public_dns" { | |
value = "${aws_instance.spike.*.public_dns}" | |
} | |
output "instance_default_vpc_subnet" { | |
value = "${aws_default_subnet.default_az1.id}" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
variable "eip_allocation" { | |
default = "" | |
} | |
variable "public_key" { | |
type = "string" | |
} | |
variable "vpc_subnet" { | |
type = "string" | |
default = "" | |
} | |
variable "distro" {} | |
variable "poc-vpc-id" { | |
type = "string" | |
} | |
variable "vpc-sec-grp-id" { | |
type = "string" | |
} | |
variable "vpc-sec-id" { | |
default = "" | |
} | |
# sets up a template to be used by the resources to be used by cloudinit | |
data "template_file" "userdata" { | |
template = "${file("templates/userdata.tpl")}" | |
vars { | |
amiused = "${data.aws_ami.ubuntu.id}" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resource "aws_security_group" "spike" { | |
vpc_id = "${var.vpc-id}" | |
name = "spike-sg" | |
tags { | |
Name = "spike-sg" | |
} | |
} | |
resource "aws_security_group_rule" "allow-tcp-22" { | |
security_group_id = "${aws_security_group.spike.id}" | |
type = "ingress" | |
protocol = "tcp" | |
from_port = 22 | |
to_port = 22 | |
cidr_blocks = ["${var.my_ip_address}/32"] | |
description = "${var.secgrp_des}" | |
} | |
resource "aws_security_group_rule" "allow-all-outbound" { | |
security_group_id = "${aws_security_group.spike.id}" | |
type = "egress" | |
protocol = "-1" | |
from_port = 0 | |
to_port = 0 | |
cidr_blocks = ["0.0.0.0/0"] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
output "spike_security_group_id" { | |
value = "${aws_security_group.spike.id}" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
variable "vpc-id" { | |
type = "string" | |
} | |
variable "my_ip_address" { | |
description = "Enter you current IP address" | |
} | |
variable "secgrp_des" {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This is where you put the private variables specific to your environment. | |
variable "eip_allocation" { | |
default = "eipalloc-<number>" | |
} | |
variable "public_key" { | |
default = "<key-name>" | |
} | |
variable "current_ip" { | |
default = "<your-current-local-ip-address>" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#cloud-config | |
# Turned off package upgrade to speed up build process | |
package_upgrade: true | |
hostname: spiketest | |
manage_etc_hosts: true | |
packages: | |
- facter | |
- python-minimal | |
runcmd: | |
- echo "Ami used for this instance build - ${amiused}" > /home/ubuntu/current-ami-used.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
variable "secgrp_des" { | |
description = "Description for this security group" | |
default = "spiketest" | |
} | |
variable "region" { | |
default = "eu-west-2" | |
} | |
variable "distro" { | |
description = "Variable to specify which distro to spin up" | |
default = "" | |
} | |
variable "version" { | |
description = "VAriable to set the distro version" | |
default = "" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment