Skip to content

Instantly share code, notes, and snippets.

@vgiotsas
Created September 19, 2020 13:05
Show Gist options
  • Save vgiotsas/ce720f6744c5f25696078c143cc3ac79 to your computer and use it in GitHub Desktop.
Save vgiotsas/ce720f6744c5f25696078c143cc3ac79 to your computer and use it in GitHub Desktop.
terraform {
required_version = ">= 0.12"
}
provider "aws" {
region = var.region
access_key = var.access_key
secret_key = var.secret_key
}
data "aws_availability_zones" "available" {
state = "available"
}
data "aws_subnet_ids" "all" {
vpc_id = data.aws_vpc.default.id
}
data "aws_ami" "default" {
most_recent = true
filter {
name = "name"
values = ["amzn2-ami-hvm-2.0.2020*"]
}
filter {
name = "architecture"
values = ["x86_64"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "root-device-type"
values = ["ebs"]
}
owners = ["amazon"]
}
resource "aws_vpc" "main" {
cidr_block = "192.168.0.0/16"
assign_generated_ipv6_cidr_block = "true"
enable_dns_support = "true"
enable_dns_hostnames = "true"
tags = {
Name = "cdn-${var.region}"
}
}
resource "aws_subnet" "public" {
count = length(data.aws_availability_zones.available.names)
vpc_id = aws_vpc.main.id
cidr_block = cidrsubnet(aws_vpc.main.cidr_block, 8, count.index)
ipv6_cidr_block = cidrsubnet(aws_vpc.main.ipv6_cidr_block, 8, count.index)
map_public_ip_on_launch = true
assign_ipv6_address_on_creation = true
availability_zone = element(data.aws_availability_zones.available.names, count.index)
tags = {
Name = "cdn-${element(data.aws_availability_zones.available.names, count.index)}-public"
}
}
resource "aws_security_group" "default" {
vpc_id = aws_vpc.main.id
ingress {
from_port = -1
to_port = -1
protocol = "icmp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = -1
to_port = -1
protocol = "icmpv6"
ipv6_cidr_blocks = ["::/0"]
}
}
resource "aws_instance" "server" {
count = length(data.aws_availability_zones.available.names) * var.servers_per_az
instance_type = var.instance_type
ami = data.aws_ami.default.id
subnet_id = element(aws_subnet.public.*.id, count.index)
ipv6_address_count = "1"
vpc_security_group_ids = [aws_security_group.default.id, aws_vpc.main.default_security_group_id]
credit_specification {
cpu_credits = "standard"
}
tags = {
Name = "cdn-server-${element(data.aws_availability_zones.available.names, count.index)}-${count.index}"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment