1+ <?php
2+
3+ use DivineOmega \HCLParser \HCLParser ;
4+
5+ require __DIR__ .'/vendor/autoload.php ' ;
6+
7+ $ hcl = '
8+ # Specify the provider and access details
9+ provider "aws" {
10+ region = "${var.aws_region}"
11+ }
12+
13+ # Create a VPC to launch our instances into
14+ resource "aws_vpc" "default" {
15+ cidr_block = "10.0.0.0/16"
16+ }
17+
18+ # Create an internet gateway to give our subnet access to the outside world
19+ resource "aws_internet_gateway" "default" {
20+ vpc_id = "${aws_vpc.default.id}"
21+ }
22+
23+ # Grant the VPC internet access on its main route table
24+ resource "aws_route" "internet_access" {
25+ route_table_id = "${aws_vpc.default.main_route_table_id}"
26+ destination_cidr_block = "0.0.0.0/0"
27+ gateway_id = "${aws_internet_gateway.default.id}"
28+ }
29+
30+ # Create a subnet to launch our instances into
31+ resource "aws_subnet" "default" {
32+ vpc_id = "${aws_vpc.default.id}"
33+ cidr_block = "10.0.1.0/24"
34+ map_public_ip_on_launch = true
35+ }
36+
37+ # A security group for the ELB so it is accessible via the web
38+ resource "aws_security_group" "elb" {
39+ name = "terraform_example_elb"
40+ description = "Used in the terraform"
41+ vpc_id = "${aws_vpc.default.id}"
42+
43+ # HTTP access from anywhere
44+ ingress {
45+ from_port = 80
46+ to_port = 80
47+ protocol = "tcp"
48+ cidr_blocks = ["0.0.0.0/0"]
49+ }
50+
51+ # outbound internet access
52+ egress {
53+ from_port = 0
54+ to_port = 0
55+ protocol = "-1"
56+ cidr_blocks = ["0.0.0.0/0"]
57+ }
58+ }
59+
60+ # Our default security group to access
61+ # the instances over SSH and HTTP
62+ resource "aws_security_group" "default" {
63+ name = "terraform_example"
64+ description = "Used in the terraform"
65+ vpc_id = "${aws_vpc.default.id}"
66+
67+ # SSH access from anywhere
68+ ingress {
69+ from_port = 22
70+ to_port = 22
71+ protocol = "tcp"
72+ cidr_blocks = ["0.0.0.0/0"]
73+ }
74+
75+ # HTTP access from the VPC
76+ ingress {
77+ from_port = 80
78+ to_port = 80
79+ protocol = "tcp"
80+ cidr_blocks = ["10.0.0.0/16"]
81+ }
82+
83+ # outbound internet access
84+ egress {
85+ from_port = 0
86+ to_port = 0
87+ protocol = "-1"
88+ cidr_blocks = ["0.0.0.0/0"]
89+ }
90+ }
91+
92+ resource "aws_elb" "web" {
93+ name = "terraform-example-elb"
94+
95+ subnets = ["${aws_subnet.default.id}"]
96+ security_groups = ["${aws_security_group.elb.id}"]
97+ instances = ["${aws_instance.web.id}"]
98+
99+ listener {
100+ instance_port = 80
101+ instance_protocol = "http"
102+ lb_port = 80
103+ lb_protocol = "http"
104+ }
105+ }
106+
107+ resource "aws_key_pair" "auth" {
108+ key_name = "${var.key_name}"
109+ public_key = "${file(var.public_key_path)}"
110+ }
111+
112+ resource "aws_instance" "web" {
113+ # The connection block tells our provisioner how to
114+ # communicate with the resource (instance)
115+ connection {
116+ # The default username for our AMI
117+ user = "ubuntu"
118+
119+ # The connection will use the local SSH agent for authentication.
120+ }
121+
122+ instance_type = "t2.micro"
123+
124+ # Lookup the correct AMI based on the region
125+ # we specified
126+ ami = "${lookup(var.aws_amis, var.aws_region)}"
127+
128+ # The name of our SSH keypair we created above.
129+ key_name = "${aws_key_pair.auth.id}"
130+
131+ # Our Security group to allow HTTP and SSH access
132+ vpc_security_group_ids = ["${aws_security_group.default.id}"]
133+
134+ # We \'re going to launch into the same subnet as our ELB. In a production
135+ # environment it \'s more common to have a separate private subnet for
136+ # backend instances.
137+ subnet_id = "${aws_subnet.default.id}"
138+
139+ # We run a remote provisioner on the instance after creating it.
140+ # In this case, we just install nginx and start it. By default,
141+ # this should be on port 80
142+ provisioner "remote-exec" {
143+ inline = [
144+ "sudo apt-get -y update",
145+ "sudo apt-get -y install nginx",
146+ "sudo service nginx start",
147+ ]
148+ }
149+ }
150+ ' ;
151+
152+ $ object = (new HCLParser ($ hcl ))->parse ();
153+
154+ var_dump ($ object );
0 commit comments