-
Notifications
You must be signed in to change notification settings - Fork 3
/
aws.tf
113 lines (97 loc) · 2.73 KB
/
aws.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
resource "aws_vpc" "example" {
cidr_block = "10.0.0.0/16"
}
resource "aws_subnet" "my_subnet" {
vpc_id = aws_vpc.example.id
cidr_block = var.vpc_cidr
availability_zone = var.az
}
resource "aws_ec2_transit_gateway_vpc_attachment" "example" {
subnet_ids = [aws_subnet.my_subnet.id]
transit_gateway_id = aws_ec2_transit_gateway.example.id
vpc_id = aws_vpc.example.id
depends_on = [
aws_ec2_transit_gateway.example,
]
}
resource "aws_internet_gateway" "vpc-igw" {
vpc_id = "${aws_vpc.example.id}"
}
resource "aws_main_route_table_association" "main-vpc" {
vpc_id = "${aws_vpc.example.id}"
route_table_id = "${aws_route_table.main-rt.id}"
}
resource "aws_route_table" "main-rt" {
vpc_id = "${aws_vpc.example.id}"
route {
cidr_block = var.hvn_cidr
transit_gateway_id = "${aws_ec2_transit_gateway.example.id}"
}
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.vpc-igw.id}"
}
depends_on = [
aws_ec2_transit_gateway.example,
aws_ec2_transit_gateway_vpc_attachment.example,
]
}
resource "aws_security_group" "main-vpc-sg" {
name = "${var.Name}-main-vpc-sg"
vpc_id = "${aws_vpc.example.id}"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 8 # the ICMP type number for 'Echo'
to_port = 0 # the ICMP code
protocol = "icmp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 0 # the ICMP type number for 'Echo Reply'
to_port = 0 # the ICMP code
protocol = "icmp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
}
}
## Fetching AMI info
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
resource "aws_instance" "test-instance" {
ami = "${data.aws_ami.ubuntu.id}"
instance_type = "t2.micro"
subnet_id = "${aws_subnet.my_subnet.id}"
vpc_security_group_ids = [ "${aws_security_group.main-vpc-sg.id}" ]
key_name = "${aws_key_pair.test-tgw-keypair.key_name}"
associate_public_ip_address = true
user_data = "${data.template_file.init.rendered}"
}
data "template_file" "init" {
template = "${file("${path.module}/init.tpl")}"
}
resource "aws_key_pair" "test-tgw-keypair" {
key_name = "${var.Name}-keypair"
public_key = "${var.public_key}"
}