From c9edecd4ad06d510429c72c6e8ce7f271b65dffa Mon Sep 17 00:00:00 2001 From: Patrick de Ruiter Date: Wed, 24 Jun 2020 11:31:27 +0200 Subject: [PATCH] Added files --- examples/bastion/.terraform-version | 1 + examples/bastion/README.md | 31 +++++++++ examples/bastion/init.sh | 4 ++ examples/bastion/main.tf | 58 ++++++++++++++++ examples/bastion/outputs.tf | 16 +++++ examples/bastion/terraform.tfvars | 7 ++ examples/bastion/variables.tf | 30 ++++++++ examples/bastion/versions.tf | 4 ++ main.tf | 103 ++++++++++++++++++++++++++++ outputs.tf | 10 +++ template/user_data.sh | 24 +++++++ variables.tf | 72 +++++++++++++++++++ versions.tf | 4 ++ 13 files changed, 364 insertions(+) create mode 100644 examples/bastion/.terraform-version create mode 100644 examples/bastion/README.md create mode 100755 examples/bastion/init.sh create mode 100644 examples/bastion/main.tf create mode 100644 examples/bastion/outputs.tf create mode 100644 examples/bastion/terraform.tfvars create mode 100644 examples/bastion/variables.tf create mode 100644 examples/bastion/versions.tf create mode 100644 main.tf create mode 100644 outputs.tf create mode 100644 template/user_data.sh create mode 100644 variables.tf create mode 100644 versions.tf diff --git a/examples/bastion/.terraform-version b/examples/bastion/.terraform-version new file mode 100644 index 0000000..dabff2f --- /dev/null +++ b/examples/bastion/.terraform-version @@ -0,0 +1 @@ +0.12.6 diff --git a/examples/bastion/README.md b/examples/bastion/README.md new file mode 100644 index 0000000..6efbc79 --- /dev/null +++ b/examples/bastion/README.md @@ -0,0 +1,31 @@ +# Terraform bastion test + +### Generate ssh keys +``` +source ./init.sh +``` + +### Init terraform +``` +terrafrom init +``` + +### Plan changes +``` +terrafrom plan +``` + +### Apply changes +``` +terrafrom apply +``` + +### Test bastion is up +``` +ssh -i ec2-user@ +``` + +### Destroy +``` +terrafrom destroy +``` diff --git a/examples/bastion/init.sh b/examples/bastion/init.sh new file mode 100755 index 0000000..2e89d61 --- /dev/null +++ b/examples/bastion/init.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +mkdir -p generated +ssh-keygen -t rsa -C "demo" -P '' -f generated/id_rsa diff --git a/examples/bastion/main.tf b/examples/bastion/main.tf new file mode 100644 index 0000000..47ce09d --- /dev/null +++ b/examples/bastion/main.tf @@ -0,0 +1,58 @@ +# required to due to https://github.com/hashicorp/terraform/issues/21330 +# otherwise the terraform operation will fail with +# -> The argument "region" is required, but was not set. +provider "aws" { + version = "~> 2.0" + region = var.aws_region +} + +module "vpc" { + source = "git::https://github.com/philips-software/terraform-aws-vpc.git?ref=2.0.0" + + environment = var.environment + aws_region = var.aws_region +} + +resource "aws_key_pair" "bastion_key" { + count = var.enable_bastion ? 1 : 0 + key_name = var.key_name + public_key = file(var.ssh_key_file_bastion) +} + +# Default bastion +module "bastion" { + source = "../.." + enable_bastion = true + + environment = var.environment + project = var.project + + aws_region = var.aws_region + key_name = aws_key_pair.bastion_key[0].key_name + subnet_id = element(module.vpc.public_subnets, 0) + vpc_id = module.vpc.vpc_id +} + +# test custom AMI +module "bastion_custom" { + source = "../.." + enable_bastion = true + + environment = var.environment + project = var.project + + aws_region = var.aws_region + key_name = aws_key_pair.bastion_key[0].key_name + subnet_id = element(module.vpc.public_subnets, 0) + vpc_id = module.vpc.vpc_id + + amazon_optimized_amis = { + us-east-1 = "ami-a4c7edb2" # N. Virginia + eu-west-1 = "ami-d7b9a2b1" # Ireland + } + + tags = { + my-tag = "my-new-tag" + } +} + diff --git a/examples/bastion/outputs.tf b/examples/bastion/outputs.tf new file mode 100644 index 0000000..d535d27 --- /dev/null +++ b/examples/bastion/outputs.tf @@ -0,0 +1,16 @@ +output "default_instance_id" { + value = module.bastion.instance_id +} + +output "default_public_ip" { + value = module.bastion.public_ip +} + +output "custom_instance_id" { + value = module.bastion_custom.instance_id +} + +output "custom_public_ip" { + value = module.bastion_custom.public_ip +} + diff --git a/examples/bastion/terraform.tfvars b/examples/bastion/terraform.tfvars new file mode 100644 index 0000000..2a4d7d8 --- /dev/null +++ b/examples/bastion/terraform.tfvars @@ -0,0 +1,7 @@ +key_name = "bastion-test" + +environment = "bastion-test" + +project = "Forest" + +aws_region = "eu-west-1" diff --git a/examples/bastion/variables.tf b/examples/bastion/variables.tf new file mode 100644 index 0000000..a3378d7 --- /dev/null +++ b/examples/bastion/variables.tf @@ -0,0 +1,30 @@ +variable "aws_region" { + description = "The Amazon region" + type = string +} + +variable "project" { + description = "Name of the project" + type = string +} + +variable "environment" { + description = "Logical name of the environment" + type = string +} + +variable "key_name" { + description = "SSH key name for the environment" + type = string +} + +variable "ssh_key_file_bastion" { + description = "SSH key file for the bastion host" + default = "generated/id_rsa.pub" +} + +variable "enable_bastion" { + description = "Enable a bastion host" + default = "true" +} + diff --git a/examples/bastion/versions.tf b/examples/bastion/versions.tf new file mode 100644 index 0000000..ac97c6a --- /dev/null +++ b/examples/bastion/versions.tf @@ -0,0 +1,4 @@ + +terraform { + required_version = ">= 0.12" +} diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..57f03fd --- /dev/null +++ b/main.tf @@ -0,0 +1,103 @@ +resource "aws_security_group" "ami" { + count = var.enable_bastion ? 1 : 0 + name_prefix = "${var.environment}-security-group" + vpc_id = var.vpc_id + + ingress { + protocol = "tcp" + from_port = 22 + to_port = 22 + + cidr_blocks = [var.admin_cidr] + } + + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + tags = merge( + { + "Name" = format("%s-bastion-sg", var.environment) + }, + { + "Environment" = format("%s", var.environment) + }, + { + "Project" = format("%s", var.project) + }, + var.tags, + ) +} + +data "aws_ami" "aws_optimized_ami" { + most_recent = true + + filter { + name = "name" + values = ["amzn-ami-hvm*"] + } + + filter { + name = "architecture" + values = ["x86_64"] + } + + filter { + name = "root-device-type" + values = ["ebs"] + } + + owners = ["137112412989"] # AWS +} + +locals { + aws_ami_userdefined = lookup(var.amazon_optimized_amis, var.aws_region, "") + aws_ami = local.aws_ami_userdefined == "" ? data.aws_ami.aws_optimized_ami.id : local.aws_ami_userdefined +} + +data "template_file" "user_data" { + template = file("${path.module}/template/user_data.sh") +} + +resource "aws_instance" "instance" { + count = var.enable_bastion ? 1 : 0 + + ami = local.aws_ami + instance_type = var.instance_type + associate_public_ip_address = true + ebs_optimized = var.ebs_optimized + subnet_id = var.subnet_id + vpc_security_group_ids = [aws_security_group.ami[0].id] + key_name = var.key_name + user_data = var.user_data == "" ? data.template_file.user_data.rendered : var.user_data + + tags = merge( + { + "Name" = format("%s-bastion", var.environment) + }, + { + "Environment" = format("%s", var.environment) + }, + { + "Project" = format("%s", var.project) + }, + var.tags, + ) + + volume_tags = merge( + { + "Name" = format("%s-bastion", var.environment) + }, + { + "Environment" = format("%s", var.environment) + }, + { + "Project" = format("%s", var.project) + }, + var.tags, + ) +} + diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..e606b89 --- /dev/null +++ b/outputs.tf @@ -0,0 +1,10 @@ +output "instance_id" { + description = "Id of the created instance." + value = element(concat(aws_instance.instance.*.id, [""]), 0) +} + +output "public_ip" { + description = "Public ip of the created instance." + value = element(concat(aws_instance.instance.*.public_ip, [""]), 0) +} + diff --git a/template/user_data.sh b/template/user_data.sh new file mode 100644 index 0000000..5a508f8 --- /dev/null +++ b/template/user_data.sh @@ -0,0 +1,24 @@ +#!/bin/bash -ex +exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1 +yum -y update + +# Add current hostname to hosts file +tee /etc/hosts <