summaryrefslogtreecommitdiff
path: root/playground/ec2-instance-test
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2023-12-16 11:55:13 +0200
committerPaul Buetow <paul@buetow.org>2023-12-16 11:55:13 +0200
commit2d674c21ccc627cb98ff62eedcecbe0097553fa2 (patch)
treea747e10f4131075f0ecc0c194636e10e2d54bbcb /playground/ec2-instance-test
parent75ff2560a50325fb0a0db61a93da8768b1ada1b6 (diff)
use a non-default VPC
Diffstat (limited to 'playground/ec2-instance-test')
-rw-r--r--playground/ec2-instance-test/data.tf6
-rw-r--r--playground/ec2-instance-test/main.tf80
2 files changed, 74 insertions, 12 deletions
diff --git a/playground/ec2-instance-test/data.tf b/playground/ec2-instance-test/data.tf
index a0c4f5d..c34ec20 100644
--- a/playground/ec2-instance-test/data.tf
+++ b/playground/ec2-instance-test/data.tf
@@ -8,3 +8,9 @@ data "aws_ami" "amazon-linux-2" {
}
}
+#data "aws_vpc" "selected" {
+# filter {
+# name = "tag:Name"
+# values = ["YourVPCName"] # Replace with your VPC's name
+# }
+#}
diff --git a/playground/ec2-instance-test/main.tf b/playground/ec2-instance-test/main.tf
index 7d0ed53..7eef733 100644
--- a/playground/ec2-instance-test/main.tf
+++ b/playground/ec2-instance-test/main.tf
@@ -17,9 +17,58 @@ 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 = "my-vpc"
+ }
+}
+
+resource "aws_internet_gateway" "my_igw" {
+ vpc_id = aws_vpc.my_vpc.id
+
+ tags = {
+ Name = "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 = "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 = "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
@@ -32,6 +81,7 @@ resource "aws_security_group" "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
@@ -44,6 +94,7 @@ resource "aws_security_group" "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
@@ -53,24 +104,29 @@ resource "aws_security_group" "allow_https" {
}
}
-# Get latest Amazon Linux 2 AMI
-data "aws_ami" "amazon-linux-2" {
- most_recent = true
- owners = ["amazon"]
- filter {
- name = "name"
- values = ["amzn2-ami-hvm*"]
+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
}
}
resource "aws_instance" "my_instance" {
ami = data.aws_ami.amazon-linux-2.id
- instance_type = "t2.micro"
+ instance_type = "t2.large"
key_name = aws_key_pair.id_rsa_pub.key_name
- security_groups = [
- aws_security_group.allow_ssh.name,
- aws_security_group.allow_http.name,
- aws_security_group.allow_https.name
+ subnet_id = aws_subnet.my_public_subnet.id
+ vpc_security_group_ids = [
+ aws_security_group.allow_ssh.id,
+ aws_security_group.allow_http.id,
+ aws_security_group.allow_https.id,
+ aws_security_group.allow_outbound.id
]
tags = {