Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
221 changes: 195 additions & 26 deletions examples/ipv6/main.tf
Original file line number Diff line number Diff line change
@@ -1,42 +1,211 @@
provider "aws" {
region = local.region
resource "aws_instance" "web_host" {
# ec2 have plain text secrets in user data
ami = "${var.ami}"
instance_type = "t2.nano"

vpc_security_group_ids = [
"${aws_security_group.web-node.id}"]
subnet_id = "${aws_subnet.web_subnet.id}"
user_data = <<EOF
#! /bin/bash
sudo apt-get update
sudo apt-get install -y apache2
sudo systemctl start apache2
sudo systemctl enable apache2
export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMAAA
export AWS_SECRET_ACCESS_KEY=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
export AWS_DEFAULT_REGION=us-west-2
echo "<h1>Deployed via Terraform</h1>" | sudo tee /var/www/html/index.html
EOF
tags = {
Name = "${local.resource_prefix.value}-ec2"
}
monitoring = true
ebs_optimized = true
}

resource "aws_ebs_volume" "web_host_storage" {
# unencrypted volume
availability_zone = "${var.availability_zone}"
#encrypted = false # Setting this causes the volume to be recreated on apply
size = 1
tags = {
Name = "${local.resource_prefix.value}-ebs"
}
}

resource "aws_ebs_snapshot" "example_snapshot" {
# ebs snapshot without encryption
volume_id = "${aws_ebs_volume.web_host_storage.id}"
description = "${local.resource_prefix.value}-ebs-snapshot"
tags = {
Name = "${local.resource_prefix.value}-ebs-snapshot"
}
}

resource "aws_volume_attachment" "ebs_att" {
device_name = "/dev/sdh"
volume_id = "${aws_ebs_volume.web_host_storage.id}"
instance_id = "${aws_instance.web_host.id}"
}

resource "aws_security_group" "web-node" {
# security group is open to the world in SSH port
name = "${local.resource_prefix.value}-sg"
description = "${local.resource_prefix.value} Security Group"
vpc_id = aws_vpc.web_vpc.id

ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = [
"0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [
"0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = [
"0.0.0.0/0"]
}
depends_on = [aws_vpc.web_vpc]
}

resource "aws_vpc" "web_vpc" {
cidr_block = "172.16.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Name = "${local.resource_prefix.value}-vpc"
}
}

resource "aws_subnet" "web_subnet" {
vpc_id = aws_vpc.web_vpc.id
cidr_block = "172.16.10.0/24"
availability_zone = var.availability_zone
map_public_ip_on_launch = true

tags = {
Name = "${local.resource_prefix.value}-subnet"
}
}

resource "aws_subnet" "web_subnet2" {
vpc_id = aws_vpc.web_vpc.id
cidr_block = "172.16.11.0/24"
availability_zone = var.availability_zone2
map_public_ip_on_launch = true

tags = {
Name = "${local.resource_prefix.value}-subnet2"
}
}


resource "aws_internet_gateway" "web_igw" {
vpc_id = aws_vpc.web_vpc.id

tags = {
Name = "${local.resource_prefix.value}-igw"
}
}

resource "aws_route_table" "web_rtb" {
vpc_id = aws_vpc.web_vpc.id

tags = {
Name = "${local.resource_prefix.value}-rtb"
}
}

locals {
region = "eu-west-1"
resource "aws_route_table_association" "rtbassoc" {
subnet_id = aws_subnet.web_subnet.id
route_table_id = aws_route_table.web_rtb.id
}

################################################################################
# VPC Module
################################################################################
resource "aws_route_table_association" "rtbassoc2" {
subnet_id = aws_subnet.web_subnet2.id
route_table_id = aws_route_table.web_rtb.id
}

module "vpc" {
source = "../.."
resource "aws_route" "public_internet_gateway" {
route_table_id = aws_route_table.web_rtb.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.web_igw.id

name = "ipv6"
cidr = "10.0.0.0/16"
timeouts {
create = "5m"
}
}

azs = ["${local.region}a", "${local.region}b"]
private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
public_subnets = ["10.0.101.0/24", "10.0.102.0/24"]
database_subnets = ["10.0.103.0/24", "10.0.104.0/24"]

enable_nat_gateway = false
resource "aws_network_interface" "web-eni" {
subnet_id = aws_subnet.web_subnet.id
private_ips = ["172.16.10.100"]

create_database_subnet_route_table = true
create_database_internet_gateway_route = true
tags = {
Name = "${local.resource_prefix.value}-primary_network_interface"
}
}

enable_ipv6 = true
assign_ipv6_address_on_creation = true
# VPC Flow Logs to S3
resource "aws_flow_log" "vpcflowlogs" {
log_destination = aws_s3_bucket.flowbucket.arn
log_destination_type = "s3"
traffic_type = "ALL"
vpc_id = aws_vpc.web_vpc.id

private_subnet_assign_ipv6_address_on_creation = false
tags = {
Name = "${local.resource_prefix.value}-flowlogs"
Environment = local.resource_prefix.value
}
}

public_subnet_ipv6_prefixes = [0, 1]
private_subnet_ipv6_prefixes = [2, 3]
database_subnet_ipv6_prefixes = [4, 5]
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
}
bucket = "${local.resource_prefix.value}-flowlogs"
force_destroy = true

tags = {
Owner = "user"
Environment = "dev"
Name = "${local.resource_prefix.value}-flowlogs"
Environment = local.resource_prefix.value
}
versioning {
enabled = true
}
}

output "ec2_public_dns" {
description = "Web Host Public DNS name"
value = aws_instance.web_host.public_dns
}

output "vpc_id" {
description = "The ID of the VPC"
value = aws_vpc.web_vpc.id
}

output "public_subnet" {
description = "The ID of the Public subnet"
value = aws_subnet.web_subnet.id
}

output "public_subnet2" {
description = "The ID of the Public subnet"
value = aws_subnet.web_subnet2.id
}