Last active
August 19, 2021 13:08
-
-
Save elena-vi/032b3f4aba12a8e1d0ed8e7cb05fd66e to your computer and use it in GitHub Desktop.
An easy ec2 instance with ssh access, inspired by https://bit.ly/3mbgc35
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
variable "ami_name" { | |
default = "Amazon Linux 2 AMI " | |
} | |
variable "ami_id" { | |
default = "ami-0d26eb3972b7f8c96" | |
} | |
variable "ami_key_pair_name" { | |
default = "el-test" | |
} | |
//subnets.tf | |
resource "aws_subnet" "subnet-uno" { | |
cidr_block = "${cidrsubnet(aws_vpc.test-env.cidr_block, 3, 1)}" | |
vpc_id = "${aws_vpc.test-env.id}" | |
availability_zone = "eu-west-2a" | |
} | |
//security.tf | |
resource "aws_security_group" "ingress-all-test" { | |
name = "allow-all-sg" | |
vpc_id = "${aws_vpc.test-env.id}" | |
ingress { | |
cidr_blocks = [ | |
"0.0.0.0/0" | |
] | |
from_port = 22 | |
to_port = 22 | |
protocol = "tcp" | |
} | |
ingress { | |
cidr_blocks = [ | |
"0.0.0.0/0" | |
] | |
from_port = 0 | |
to_port = 0 | |
protocol = "-1" | |
} | |
// Terraform removes the default rule | |
egress { | |
from_port = 0 | |
to_port = 0 | |
protocol = "-1" | |
cidr_blocks = ["0.0.0.0/0"] | |
} | |
} | |
//servers.tf | |
resource "aws_instance" "test-ec2-instance" { | |
ami = "${var.ami_id}" | |
instance_type = "t2.medium" | |
key_name = "${var.ami_key_pair_name}" | |
security_groups = ["${aws_security_group.ingress-all-test.id}"] | |
subnet_id = "${aws_subnet.subnet-uno.id}" | |
tags = { | |
Name = "${local.identifier_prefix}-Data-hub" | |
} | |
} | |
resource "aws_vpc" "test-env" { | |
cidr_block = "10.0.0.0/16" | |
enable_dns_hostnames = true | |
enable_dns_support = true | |
} | |
resource "aws_eip" "ip-test-env" { | |
instance = "${aws_instance.test-ec2-instance.id}" | |
vpc = true | |
} | |
//gateways.tf | |
resource "aws_internet_gateway" "test-env-gw" { | |
vpc_id = "${aws_vpc.test-env.id}" | |
} | |
//subnets.tf | |
resource "aws_route_table" "route-table-test-env" { | |
vpc_id = "${aws_vpc.test-env.id}" | |
route { | |
cidr_block = "0.0.0.0/0" | |
gateway_id = "${aws_internet_gateway.test-env-gw.id}" | |
} | |
} | |
resource "aws_route_table_association" "subnet-association" { | |
subnet_id = "${aws_subnet.subnet-uno.id}" | |
route_table_id = "${aws_route_table.route-table-test-env.id}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment