-
Notifications
You must be signed in to change notification settings - Fork 0
/
TerraformAWSProvisionEC2
70 lines (58 loc) · 1.62 KB
/
TerraformAWSProvisionEC2
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
Terraform code to provision EC2 instance in AWS
# Configure the AWS provider
provider "aws" {
region = "us-west-2"
}
# Create a VPC
resource "aws_vpc" "example" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
}
# Create a VPN subnet
resource "aws_subnet" "vpn_subnet" {
vpc_id = aws_vpc.example.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-west-2a"
}
# Create an Internet Gateway
resource "aws_internet_gateway" "example" {
vpc_id = aws_vpc.example.id
}
# Create a Route Table
resource "aws_route_table" "example" {
vpc_id = aws_vpc.example.id
}
# Associate the Route Table with the VPN subnet
resource "aws_route_table_association" "example" {
subnet_id = aws_subnet.vpn_subnet.id
route_table_id = aws_route_table.example.id
}
# Create a route to the Internet Gateway
resource "aws_route" "example" {
route_table_id = aws_route_table.example.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.example.id
}
# Create a security group
resource "aws_security_group" "example" {
name = "example-security-group"
description = "Example security group"
vpc_id = aws_vpc.example.id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
# Create an EC2 instance
resource "aws_instance" "example" {
ami = "ami-0c94855ba95c71c99"
instance_type = "t2.micro"
key_name = "your_key_pair_name"
subnet_id = aws_subnet.vpn_subnet.id
vpc_security_group_ids = [aws_security_group.example.id]
tags = {
Name = "ExampleInstance"
}
}