From 32f7874afaffd2621237de2454dd1eaf2da319aa Mon Sep 17 00:00:00 2001 From: Daniel Mikula Date: Wed, 17 May 2023 06:37:49 +0200 Subject: [PATCH] added ec2 terraform file --- ec2.tf | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ script.sh | 4 ++ 2 files changed, 142 insertions(+) create mode 100644 ec2.tf create mode 100644 script.sh diff --git a/ec2.tf b/ec2.tf new file mode 100644 index 0000000..b09eebe --- /dev/null +++ b/ec2.tf @@ -0,0 +1,138 @@ +resource "aws_instance" "k3s_box" { + ami = "ami-0889a44b331db0194" # amazon linux + instance_type = "t3.small" + associate_public_ip_address = true + + user_data = "${file("script.sh")}" + + tags = { + Name = "k3s-box" + } +} + +resource "aws_vpc" "k3s_box_vpc" { + cidr_block = "10.0.0.0/16" + + tags = { + Name = "k3s-box-vpc" + } +} + +resource "aws_subnet" "k3s_box_public_subnet" { + vpc_id = aws_vpc.k3s_box_vpc.id + cidr_block = "10.0.1.0/24" + availability_zone = "us-east-1a" + + tags = { + Name = "k3s-box-public-subnet" + } +} + +resource "aws_subnet" "k3s_box_private_subnet" { + vpc_id = aws_vpc.k3s_box_vpc.id + cidr_block = "10.0.2.0/24" + availability_zone = "us-east-1a" + + tags = { + Name = "k3s-box-private-subnet" + } +} + +resource "aws_internet_gateway" "k3s_box_ig" { + vpc_id = aws_vpc.k3s_box_vpc.id + + tags = { + Name = "k3s-box-internet-gateway" + } +} + +resource "aws_route_table" "k3s_box_rt" { + vpc_id = aws_vpc.k3s_box_vpc.id + + route { + cidr_block = "0.0.0.0/0" + gateway_id = aws_internet_gateway.k3s_box_ig.id + } + + route { + ipv6_cidr_block = "::/0" + gateway_id = aws_internet_gateway.k3s_box_ig.id + } + + tags = { + Name = "k3s-box-route-table" + } +} + +resource "aws_route_table_association" "k3s_box_public_1_rt_a" { + subnet_id = aws_subnet.k3s_box_public_subnet.id + route_table_id = aws_route_table.k3s_box_rt.id +} + +resource "aws_security_group" "k3s_box_sg" { + name = "security group for k3s box" + description = "security group for k3s box" + vpc_id = aws_vpc.k3s_box_vpc.id + + ingress { + description = "SSH" + from_port = 22 + to_port = 22 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + ipv6_cidr_blocks = ["::/0"] + } + + # ALLOWS HTTPS and HTTP from anywhere +# ingress { +# description = "HTTPS" +# from_port = 443 +# to_port = 443 +# protocol = "tcp" +# cidr_blocks = ["0.0.0.0/0"] +# ipv6_cidr_blocks = ["::/0"] +# } + +# ingress { +# description = "HTTP" +# from_port = 80 +# to_port = 80 +# protocol = "tcp" +# cidr_blocks = ["0.0.0.0/0"] +# ipv6_cidr_blocks = ["::/0"] +# } + + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + ipv6_cidr_blocks = ["::/0"] + } + + tags = { + Name = "k3s-box-sg" + } +} + +# create key pair +resource "tls_private_key" "rsa" { + algorithm = "RSA" + rsa_bits = 4096 +} + +resource "aws_key_pair" "k3s_box_kp" { + key_name = "k3s-box-key" + public_key = tls_private_key.rsa.public_key_openssh +} + +# save key pair to machine +resource "local_file" "k3s_box_private_key" { + content = tls_private_key.rsa.private_key_pem + filename = "k3s_box_private_key" + file_permission = 0400 +} + +output "k3s_box_global_ips" { + value = ["${aws_instance.k3s_box.*.public_ip}"] +} \ No newline at end of file diff --git a/script.sh b/script.sh new file mode 100644 index 0000000..8a19255 --- /dev/null +++ b/script.sh @@ -0,0 +1,4 @@ +#! /bin/bash + +touch ~/hello.txt +echo "hello you" >> ~/hello.txt \ No newline at end of file