diff options
| author | Paul Buetow <paul@buetow.org> | 2023-12-17 15:53:28 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2023-12-17 15:53:28 +0200 |
| commit | 712c77dc31fbc7fffee67c852ed6b0d5548e5906 (patch) | |
| tree | 8f7663016983d1757fa1ddd2b5bbb49d785e5e49 | |
| parent | 7a0fb4e424cdb273926286cd2acd068180e37459 (diff) | |
initial fargate
| -rw-r--r-- | playground/ec2-instance-test/main.tf | 116 | ||||
| -rw-r--r-- | playground/ec2-instance-test/network.tf | 114 | ||||
| -rw-r--r-- | playground/fargate/main.tf | 160 |
3 files changed, 274 insertions, 116 deletions
diff --git a/playground/ec2-instance-test/main.tf b/playground/ec2-instance-test/main.tf index 8147f2e..7c9d166 100644 --- a/playground/ec2-instance-test/main.tf +++ b/playground/ec2-instance-test/main.tf @@ -19,122 +19,6 @@ resource "aws_key_pair" "id_rsa_pub" { public_key = file("${path.module}/id_rsa.pub") } - -resource "aws_vpc" "my_vpc" { - cidr_block = "10.0.0.0/16" # Specify your CIDR block - enable_dns_support = true - enable_dns_hostnames = true - - tags = { - Name = "${var.environment}-my-vpc" - } -} - -resource "aws_internet_gateway" "my_igw" { - vpc_id = aws_vpc.my_vpc.id - - tags = { - Name = "${var.environment}-my-igw" - } -} - -resource "aws_subnet" "my_public_subnet" { - vpc_id = aws_vpc.my_vpc.id # Referencing the VPC - cidr_block = "10.0.1.0/24" # Specify your CIDR block for the subnet - availability_zone = "eu-central-1a" # Change to your desired AZ - map_public_ip_on_launch = true - - tags = { - Name = "${var.environment}-my-subnet" - } -} - -resource "aws_route_table" "my_route_table" { - vpc_id = aws_vpc.my_vpc.id - - route { - cidr_block = "0.0.0.0/0" - gateway_id = aws_internet_gateway.my_igw.id - } - - tags = { - Name = "${var.environment}-my-route-table" - } -} - -resource "aws_route_table_association" "a" { - subnet_id = aws_subnet.my_public_subnet.id - route_table_id = aws_route_table.my_route_table.id -} - -resource "aws_security_group" "allow_ssh" { - name = "allow_ssh" - description = "Allow SSH inbound traffic" - vpc_id = aws_vpc.my_vpc.id - - ingress { - from_port = 22 - to_port = 22 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - } - - tags = { - Name = "${var.environment}-allow-ssh" - } -} - -resource "aws_security_group" "allow_http" { - name = "allow_http" - description = "Allow HTTP inbound traffic" - vpc_id = aws_vpc.my_vpc.id - - ingress { - from_port = 80 - to_port = 80 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - } - - tags = { - Name = "${var.environment}-allow_http" - } -} - -resource "aws_security_group" "allow_https" { - name = "allow_https" - description = "Allow HTTPS inbound traffic" - vpc_id = aws_vpc.my_vpc.id - - ingress { - from_port = 443 - to_port = 443 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - } - - tags = { - Name = "${var.environment}-allow-https" - } -} - -resource "aws_security_group" "allow_outbound" { - name = "allow_outbound" - description = "Allow outbound traffic" - vpc_id = aws_vpc.my_vpc.id - - egress { - from_port = 0 - to_port = 0 - protocol = "-1" # -1 means all protocols - cidr_blocks = ["0.0.0.0/0"] # Allows outbound traffic to all IP addresses - } - - tags = { - Name = "${var.environment}-allow-outnound" - } -} - data "template_file" "user_data" { template = file("${path.module}/user_data.tpl") diff --git a/playground/ec2-instance-test/network.tf b/playground/ec2-instance-test/network.tf new file mode 100644 index 0000000..94cd9d9 --- /dev/null +++ b/playground/ec2-instance-test/network.tf @@ -0,0 +1,114 @@ +resource "aws_vpc" "my_vpc" { + cidr_block = "10.0.0.0/16" # Specify your CIDR block + enable_dns_support = true + enable_dns_hostnames = true + + tags = { + Name = "${var.environment}-my-vpc" + } +} + +resource "aws_internet_gateway" "my_igw" { + vpc_id = aws_vpc.my_vpc.id + + tags = { + Name = "${var.environment}-my-igw" + } +} + +resource "aws_subnet" "my_public_subnet" { + vpc_id = aws_vpc.my_vpc.id # Referencing the VPC + cidr_block = "10.0.1.0/24" # Specify your CIDR block for the subnet + availability_zone = "eu-central-1a" # Change to your desired AZ + map_public_ip_on_launch = true + + tags = { + Name = "${var.environment}-my-subnet" + } +} + +resource "aws_route_table" "my_route_table" { + vpc_id = aws_vpc.my_vpc.id + + route { + cidr_block = "0.0.0.0/0" + gateway_id = aws_internet_gateway.my_igw.id + } + + tags = { + Name = "${var.environment}-my-route-table" + } +} + +resource "aws_route_table_association" "a" { + subnet_id = aws_subnet.my_public_subnet.id + route_table_id = aws_route_table.my_route_table.id +} + +resource "aws_security_group" "allow_ssh" { + name = "allow_ssh" + description = "Allow SSH inbound traffic" + vpc_id = aws_vpc.my_vpc.id + + ingress { + from_port = 22 + to_port = 22 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + + tags = { + Name = "${var.environment}-allow-ssh" + } +} + +resource "aws_security_group" "allow_http" { + name = "allow_http" + description = "Allow HTTP inbound traffic" + vpc_id = aws_vpc.my_vpc.id + + ingress { + from_port = 80 + to_port = 80 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + + tags = { + Name = "${var.environment}-allow_http" + } +} + +resource "aws_security_group" "allow_https" { + name = "allow_https" + description = "Allow HTTPS inbound traffic" + vpc_id = aws_vpc.my_vpc.id + + ingress { + from_port = 443 + to_port = 443 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + + tags = { + Name = "${var.environment}-allow-https" + } +} + +resource "aws_security_group" "allow_outbound" { + name = "allow_outbound" + description = "Allow outbound traffic" + vpc_id = aws_vpc.my_vpc.id + + egress { + from_port = 0 + to_port = 0 + protocol = "-1" # -1 means all protocols + cidr_blocks = ["0.0.0.0/0"] # Allows outbound traffic to all IP addresses + } + + tags = { + Name = "${var.environment}-allow-outnound" + } +} diff --git a/playground/fargate/main.tf b/playground/fargate/main.tf new file mode 100644 index 0000000..305871d --- /dev/null +++ b/playground/fargate/main.tf @@ -0,0 +1,160 @@ +terraform { + backend "s3" { + bucket = "org-buetow-tfstate" + key = "playground/fargate/terraform.tfstate" + region = "eu-central-1" + # Optional, if you enabled server-side encryption + encrypt = true + } +} + +provider "aws" { + region = "eu-central-1" # or your preferred AWS region +} + +resource "aws_vpc" "my_vpc" { + cidr_block = "10.0.0.0/16" + enable_dns_support = true + enable_dns_hostnames = true + + tags = { + Name = "my_vpc" + } +} + +resource "aws_internet_gateway" "my_gateway" { + vpc_id = aws_vpc.my_vpc.id +} + +resource "aws_subnet" "my_public_subnet_a" { + vpc_id = aws_vpc.my_vpc.id + cidr_block = "10.0.1.0/24" + availability_zone = "eu-central-1a" + + tags = { + Name = "my_public_subnet_a" + } +} + +resource "aws_subnet" "my_public_subnet_b" { + vpc_id = aws_vpc.my_vpc.id + cidr_block = "10.0.2.0/24" + availability_zone = "eu-central-1b" + + tags = { + Name = "my_public_subnet_b" + } +} + +resource "aws_subnet" "my_public_subnet_c" { + vpc_id = aws_vpc.my_vpc.id + cidr_block = "10.0.3.0/24" + availability_zone = "eu-central-1c" + + tags = { + Name = "my_public_subnet_c" + } +} + +resource "aws_route_table" "public_route_table" { + vpc_id = aws_vpc.my_vpc.id + + route { + cidr_block = "0.0.0.0/0" + gateway_id = aws_internet_gateway.my_gateway.id + } +} + +resource "aws_route_table_association" "public_route_table_assoc_a" { + subnet_id = aws_subnet.my_public_subnet_a.id + route_table_id = aws_route_table.public_route_table.id +} + +resource "aws_route_table_association" "public_route_table_assoc_b" { + subnet_id = aws_subnet.my_public_subnet_b.id + route_table_id = aws_route_table.public_route_table.id +} + +resource "aws_route_table_association" "public_route_table_assoc_c" { + subnet_id = aws_subnet.my_public_subnet_c.id + route_table_id = aws_route_table.public_route_table.id +} + +resource "aws_security_group" "nginx_sg" { + vpc_id = aws_vpc.my_vpc.id + + ingress { + from_port = 80 + to_port = 80 + 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"] + } +} + +resource "aws_ecs_cluster" "my_cluster" { + name = "my-cluster" +} + +resource "aws_ecs_task_definition" "nginx_task" { + family = "nginx" + network_mode = "awsvpc" + requires_compatibilities = ["FARGATE"] + cpu = "256" + memory = "512" + execution_role_arn = aws_iam_role.ecs_execution_role.arn + + container_definitions = jsonencode([{ + name = "nginx", + image = "nginx:latest", + portMappings = [{ + containerPort = 80, + hostPort = 80 + }] + }]) +} + +resource "aws_iam_role" "ecs_execution_role" { + name = "ecs_execution_role" + + assume_role_policy = jsonencode({ + Version = "2012-10-17", + Statement = [{ + Action = "sts:AssumeRole", + Effect = "Allow", + Principal = { + Service = "ecs-tasks.amazonaws.com" + }, + }] + }) +} + +resource "aws_iam_role_policy_attachment" "ecs_execution_role_policy_attach" { + role = aws_iam_role.ecs_execution_role.name + policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy" +} + +resource "aws_ecs_service" "nginx_service" { + name = "nginx-service" + cluster = aws_ecs_cluster.my_cluster.id + task_definition = aws_ecs_task_definition.nginx_task.arn + launch_type = "FARGATE" + desired_count = 1 + + network_configuration { + subnets = [ + aws_subnet.my_public_subnet_a.id, + aws_subnet.my_public_subnet_b.id, + aws_subnet.my_public_subnet_c.id, + ] + security_groups = [aws_security_group.nginx_sg.id] + assign_public_ip = true + } +} + |
