add terraform and fix docker compose and update gitignore

This commit is contained in:
Mona Lisa 2023-11-17 13:38:49 -05:00
parent 0f31944be5
commit 7048b7419c
13 changed files with 328 additions and 2 deletions

35
.gitignore vendored
View file

@ -58,3 +58,38 @@ coverage
typechain
cache
artifacts
# Local .terraform directories
**/.terraform/*
# .tfstate files
*.tfstate
*.tfstate.*
# Crash log files
crash.log
crash.*.log
# Exclude all .tfvars files, which are likely to contain sensitive data, such as
# password, private keys, and other secrets. These should not be part of version
# control as they are data points which are potentially sensitive and subject
# to change depending on the environment.
*.tfvars
*.tfvars.json
# Ignore override files as they are usually used to override resources locally and so
# are not checked in
override.tf
override.tf.json
*_override.tf
*_override.tf.json
# Include override files you do wish to add to version control using negated pattern
# !example_override.tf
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*
# Ignore CLI configuration files
.terraformrc
terraform.rc

View file

@ -5,4 +5,3 @@ services:
command: --dev --http --http.addr 0.0.0.0
ports:
- 8545:8545
- 30303:30303

View file

@ -0,0 +1,12 @@
provider "aws" {
region = "us-east-1"
}
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.56"
}
}
}

View file

@ -0,0 +1,10 @@
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "main"
}
}

View file

@ -0,0 +1,6 @@
resource "aws_eks_addon" "csi_driver" {
cluster_name = aws_eks_cluster.demo.name
addon_name = "aws-ebs-csi-driver"
addon_version = "v1.16.0-eksbuild.1"
service_account_role_arn = aws_iam_role.eks_ebs_csi_driver.arn
}

View file

@ -0,0 +1,7 @@
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.main.id
tags = {
Name = "igw"
}
}

View file

@ -0,0 +1,49 @@
resource "aws_subnet" "private_us_east_1a" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.0.0/19"
availability_zone = "us-east-1a"
tags = {
"Name" = "private-us-east-1a"
"kubernetes.io/role/internal-elb" = "1"
"kubernetes.io/cluster/demo" = "owned"
}
}
resource "aws_subnet" "private_us_east_1b" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.32.0/19"
availability_zone = "us-east-1b"
tags = {
"Name" = "private-us-east-1b"
"kubernetes.io/role/internal-elb" = "1"
"kubernetes.io/cluster/demo" = "owned"
}
}
resource "aws_subnet" "public_us_east_1a" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.64.0/19"
availability_zone = "us-east-1a"
map_public_ip_on_launch = true
tags = {
"Name" = "public-us-east-1a"
"kubernetes.io/role/elb" = "1"
"kubernetes.io/cluster/demo" = "owned"
}
}
resource "aws_subnet" "public_us_east_1b" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.96.0/19"
availability_zone = "us-east-1b"
map_public_ip_on_launch = true
tags = {
"Name" = "public-us-east-1b"
"kubernetes.io/role/elb" = "1"
"kubernetes.io/cluster/demo" = "owned"
}
}

View file

@ -0,0 +1,18 @@
resource "aws_eip" "nat" {
vpc = true
tags = {
Name = "nat"
}
}
resource "aws_nat_gateway" "nat" {
allocation_id = aws_eip.nat.id
subnet_id = aws_subnet.public_us_east_1a.id
tags = {
Name = "nat"
}
depends_on = [aws_internet_gateway.igw]
}

View file

@ -0,0 +1,45 @@
resource "aws_route_table" "private" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.nat.id
}
tags = {
Name = "private"
}
}
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
tags = {
Name = "public"
}
}
resource "aws_route_table_association" "private_us_east_1a" {
subnet_id = aws_subnet.private_us_east_1a.id
route_table_id = aws_route_table.private.id
}
resource "aws_route_table_association" "private_us_east_1b" {
subnet_id = aws_subnet.private_us_east_1b.id
route_table_id = aws_route_table.private.id
}
resource "aws_route_table_association" "public_us_east_1a" {
subnet_id = aws_subnet.public_us_east_1a.id
route_table_id = aws_route_table.public.id
}
resource "aws_route_table_association" "public_us_east_1b" {
subnet_id = aws_subnet.public_us_east_1b.id
route_table_id = aws_route_table.public.id
}

View file

@ -0,0 +1,40 @@
resource "aws_iam_role" "demo" {
name = "eks-cluster-demo"
assume_role_policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "eks.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
POLICY
}
resource "aws_iam_role_policy_attachment" "demo_amazon_eks_cluster_policy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSClusterPolicy"
role = aws_iam_role.demo.name
}
resource "aws_eks_cluster" "demo" {
name = "demo"
version = "1.27"
role_arn = aws_iam_role.demo.arn
vpc_config {
subnet_ids = [
aws_subnet.private_us_east_1a.id,
aws_subnet.private_us_east_1b.id,
aws_subnet.public_us_east_1a.id,
aws_subnet.public_us_east_1b.id
]
}
depends_on = [aws_iam_role_policy_attachment.demo_amazon_eks_cluster_policy]
}

View file

@ -0,0 +1,69 @@
resource "aws_iam_role" "nodes" {
name = "eks-node-group-nodes"
assume_role_policy = jsonencode({
Statement = [{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "ec2.amazonaws.com"
}
}]
Version = "2012-10-17"
})
}
resource "aws_iam_role_policy_attachment" "nodes_amazon_eks_worker_node_policy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy"
role = aws_iam_role.nodes.name
}
resource "aws_iam_role_policy_attachment" "nodes_amazon_eks_cni_policy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy"
role = aws_iam_role.nodes.name
}
resource "aws_iam_role_policy_attachment" "nodes_amazon_ec2_container_registry_read_only" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
role = aws_iam_role.nodes.name
}
# Optional: only if you want to "SSH" to your EKS nodes.
resource "aws_iam_role_policy_attachment" "amazon_ssm_managed_instance_core" {
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
role = aws_iam_role.nodes.name
}
resource "aws_eks_node_group" "private_nodes" {
cluster_name = aws_eks_cluster.demo.name
node_group_name = "private-nodes"
node_role_arn = aws_iam_role.nodes.arn
# Single subnet to avoid data transfer charges while testing.
subnet_ids = [
aws_subnet.private_us_east_1a.id
]
capacity_type = "ON_DEMAND"
instance_types = ["t2.large"]
scaling_config {
desired_size = 1
max_size = 2
min_size = 0
}
update_config {
max_unavailable = 1
}
labels = {
role = "general"
}
depends_on = [
aws_iam_role_policy_attachment.nodes_amazon_eks_worker_node_policy,
aws_iam_role_policy_attachment.nodes_amazon_eks_cni_policy,
aws_iam_role_policy_attachment.nodes_amazon_ec2_container_registry_read_only,
]
}

View file

@ -0,0 +1,9 @@
data "tls_certificate" "eks" {
url = aws_eks_cluster.demo.identity[0].oidc[0].issuer
}
resource "aws_iam_openid_connect_provider" "eks" {
client_id_list = ["sts.amazonaws.com"]
thumbprint_list = [data.tls_certificate.eks.certificates[0].sha1_fingerprint]
url = aws_eks_cluster.demo.identity[0].oidc[0].issuer
}

View file

@ -0,0 +1,27 @@
data "aws_iam_policy_document" "csi" {
statement {
actions = ["sts:AssumeRoleWithWebIdentity"]
effect = "Allow"
condition {
test = "StringEquals"
variable = "${replace(aws_iam_openid_connect_provider.eks.url, "https://", "")}:sub"
values = ["system:serviceaccount:kube-system:ebs-csi-controller-sa"]
}
principals {
identifiers = [aws_iam_openid_connect_provider.eks.arn]
type = "Federated"
}
}
}
resource "aws_iam_role" "eks_ebs_csi_driver" {
assume_role_policy = data.aws_iam_policy_document.csi.json
name = "eks-ebs-csi-driver"
}
resource "aws_iam_role_policy_attachment" "amazon_ebs_csi_driver" {
role = aws_iam_role.eks_ebs_csi_driver.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEBSCSIDriverPolicy"
}