Skip to content

Commit f10b70c

Browse files
committed
Add Terraform configuration for AWS infrastructure setup including VPC, subnets, security groups, and EC2 instance
1 parent 906063b commit f10b70c

File tree

1 file changed

+220
-0
lines changed
  • codebundles/aws-c7n-tag-compliance/.test/terraform

1 file changed

+220
-0
lines changed
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
# Provider Configuration
2+
provider "aws" {
3+
region = "us-west-2"
4+
}
5+
6+
# Local SSH Key Generation
7+
resource "tls_private_key" "ssh_key" {
8+
algorithm = "RSA"
9+
rsa_bits = 2048
10+
}
11+
12+
# EC2 Key Pair
13+
resource "aws_key_pair" "my_ec2_key" {
14+
key_name = "my-ec2-key"
15+
public_key = tls_private_key.ssh_key.public_key_openssh
16+
}
17+
18+
# Create a new VPC
19+
resource "aws_vpc" "new_vpc" {
20+
cidr_block = "10.0.0.0/16"
21+
enable_dns_support = true
22+
enable_dns_hostnames = false
23+
24+
tags = {
25+
Name = "private-vpc"
26+
}
27+
}
28+
29+
# Create Private Subnet
30+
resource "aws_subnet" "private_subnet" {
31+
vpc_id = aws_vpc.new_vpc.id
32+
cidr_block = "10.0.1.0/24"
33+
availability_zone = "us-west-2a"
34+
35+
tags = {
36+
Name = "private-subnet"
37+
}
38+
}
39+
40+
resource "aws_subnet" "public_subnet" {
41+
vpc_id = aws_vpc.new_vpc.id
42+
cidr_block = "10.0.2.0/24"
43+
availability_zone = "us-west-2a"
44+
45+
tags = {
46+
Name = "public-subnet"
47+
}
48+
}
49+
50+
resource "aws_subnet" "public_subnet_2" {
51+
vpc_id = aws_vpc.new_vpc.id
52+
cidr_block = "10.0.3.0/24"
53+
availability_zone = "us-west-2b"
54+
55+
tags = {
56+
Name = "public-subnet"
57+
}
58+
}
59+
60+
# Create an Internet Gateway
61+
resource "aws_internet_gateway" "igw" {
62+
vpc_id = aws_vpc.new_vpc.id
63+
64+
tags = {
65+
Name = "example-igw"
66+
}
67+
}
68+
69+
# Add a Route to the Internet Gateway
70+
resource "aws_route" "internet_access" {
71+
route_table_id = aws_route_table.public_route_table.id
72+
destination_cidr_block = "0.0.0.0/0"
73+
gateway_id = aws_internet_gateway.igw.id
74+
}
75+
76+
# Associate the Route Table with Public Subnets
77+
resource "aws_route_table_association" "public_subnet" {
78+
subnet_id = aws_subnet.public_subnet.id
79+
route_table_id = aws_route_table.public_route_table.id
80+
}
81+
82+
resource "aws_route_table_association" "public_subnet_2" {
83+
subnet_id = aws_subnet.public_subnet_2.id
84+
route_table_id = aws_route_table.public_route_table.id
85+
}
86+
87+
# Create a Route Table
88+
resource "aws_route_table" "public_route_table" {
89+
vpc_id = aws_vpc.new_vpc.id
90+
91+
tags = {
92+
Name = "public-route-table"
93+
}
94+
}
95+
96+
# Amazon Linux 2 AMI
97+
data "aws_ami" "amazon_linux_2" {
98+
most_recent = true
99+
owners = ["amazon"]
100+
101+
filter {
102+
name = "name"
103+
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
104+
}
105+
}
106+
107+
# Security Group
108+
resource "aws_security_group" "instance_sg" {
109+
name = "test-instance-sg"
110+
description = "Security group for test EC2 instance"
111+
vpc_id = aws_vpc.new_vpc.id
112+
113+
ingress {
114+
from_port = 22
115+
to_port = 22
116+
protocol = "tcp"
117+
cidr_blocks = ["0.0.0.0/0"]
118+
}
119+
120+
egress {
121+
from_port = 0
122+
to_port = 0
123+
protocol = "-1"
124+
cidr_blocks = ["0.0.0.0/0"]
125+
}
126+
127+
tags = {
128+
Name = "test-instance-sg"
129+
}
130+
}
131+
132+
resource "aws_launch_template" "example" {
133+
name = "example-launch-template"
134+
image_id = data.aws_ami.amazon_linux_2.id
135+
instance_type = "t2.micro"
136+
key_name = aws_key_pair.my_ec2_key.key_name
137+
network_interfaces {
138+
subnet_id = aws_subnet.private_subnet.id
139+
}
140+
141+
tag_specifications {
142+
resource_type = "instance"
143+
144+
tags = {
145+
Name = "example-asg-instance"
146+
}
147+
}
148+
}
149+
150+
# Create an Auto Scaling Group
151+
resource "aws_autoscaling_group" "example" {
152+
name = "example-asg"
153+
max_size = 1
154+
min_size = 1
155+
desired_capacity = 1
156+
vpc_zone_identifier = [aws_subnet.private_subnet.id]
157+
launch_template {
158+
id = aws_launch_template.example.id
159+
version = "$Latest"
160+
}
161+
162+
health_check_type = "EC2"
163+
health_check_grace_period = 300
164+
165+
tag {
166+
key = "Name"
167+
value = "example-asg-instance"
168+
propagate_at_launch = true
169+
}
170+
171+
}
172+
173+
# EC2 Instance
174+
resource "aws_instance" "test_instance" {
175+
ami = data.aws_ami.amazon_linux_2.id
176+
instance_type = "t2.micro"
177+
178+
# Use the private subnet
179+
subnet_id = aws_subnet.private_subnet.id
180+
181+
# Associate the security group
182+
vpc_security_group_ids = [aws_security_group.instance_sg.id]
183+
184+
# Use the generated key pair
185+
key_name = aws_key_pair.my_ec2_key.key_name
186+
187+
tags = {
188+
Name = "TestInstance"
189+
}
190+
}
191+
192+
# Outputs
193+
output "instance_id" {
194+
value = aws_instance.test_instance.id
195+
}
196+
197+
output "instance_private_ip" {
198+
value = aws_instance.test_instance.private_ip
199+
}
200+
201+
output "vpc_id" {
202+
value = aws_vpc.new_vpc.id
203+
}
204+
205+
output "subnet_id" {
206+
value = aws_subnet.private_subnet.id
207+
}
208+
209+
output "private_key" {
210+
value = tls_private_key.ssh_key.private_key_pem
211+
sensitive = true
212+
}
213+
214+
output "public_key" {
215+
value = tls_private_key.ssh_key.public_key_openssh
216+
}
217+
218+
output "launch_template" {
219+
value = aws_autoscaling_group.example.launch_configuration
220+
}

0 commit comments

Comments
 (0)