-
Notifications
You must be signed in to change notification settings - Fork 2
/
terraform.tf
155 lines (128 loc) · 3.38 KB
/
terraform.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# main.tf
# Variables
variable "profile" {
description = "The AWS CLI profile to use (default: default)"
default = "default"
}
variable "region" {
description = "The region to deploy resources"
default = "us-east-1"
}
variable "az" {
description = "The AZ you want the instance deployed into (0, 1, 2, etc)"
default = "a"
}
locals {
azs = {
a = 0
b = 1
c = 2
d = 3
e = 4
f = 5
g = 6
}
}
variable "os" {
description = "The OS to deploy"
default = "ubuntu"
}
variable "size" {
description = "The ec2 instance size"
default = "t2.micro"
}
variable "keyfile" {
description = "The local public key file"
default = "~/.ssh/id_rsa.pub"
}
variable "allowed_cidr" {
description = "The cidr address allowed to access the instance"
default = "0.0.0.0/0"
}
# Maps
variable "ami_names" {
type = map(string)
default = {
ubuntu = "ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server*"
aws = "amzn-ami-hvm-*-x86_64-gp2"
aws2 = "amzn2-ami-hvm-2.0.*-x86_64-gp2"
}
}
variable "custom_ami" {
description = "A custom AMI to use"
default = "YOU MUST SPECIFY IF YOU WANT TO USE THIS"
}
variable "os_users" {
type = map(string)
default = {
ubuntu = "ubuntu"
aws = "ec2-user"
aws2 = "ec2-user"
}
}
variable "custom_user" {
description = "The username to use with your custom AMI"
default = "YOU MUST SPECIFY IF YOU WANT TO USE THIS"
}
# Providers
provider "aws" {
profile = var.profile
region = var.region
}
# Data lookups
data "aws_ami" "ami" {
most_recent = true
# self for own account, conical for ubuntu, or amazon for aws linux
owners = ["self", "099720109477", "amazon"]
filter {
name = "name"
# TF-UPGRADE-TODO: In Terraform v0.10 and earlier, it was sometimes necessary to
# force an interpolation expression to be interpreted as a list by wrapping it
# in an extra set of list brackets. That form was supported for compatibilty in
# v0.11, but is no longer supported in Terraform v0.12.
#
# If the expression in the following list itself returns a list, remove the
# brackets to avoid interpretation as a list of lists. If the expression
# returns a single list item then leave it as-is and remove this TODO comment.
values = [lookup(var.ami_names, var.os, var.custom_ami)]
}
}
data "aws_vpc" "default" {
default = "true"
}
# All AZs Available in the current region
data "aws_availability_zones" "available" {
}
# Resources
resource "aws_instance" "instance" {
ami = data.aws_ami.ami.id
instance_type = var.size
availability_zone = data.aws_availability_zones.available.names[local.azs[var.az]]
security_groups = [aws_security_group.sec_grp.name]
key_name = aws_key_pair.key.key_name
tags = {
Name = "temp_tf_instance"
}
}
resource "aws_key_pair" "key" {
key_name = "ssh-key"
public_key = file(var.keyfile)
}
resource "aws_security_group" "sec_grp" {
name_prefix = "ssh-only-sg_"
description = "ssh_only"
vpc_id = data.aws_vpc.default.id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [var.allowed_cidr]
}
}
# outputs
output "instance_id" {
value = aws_instance.instance.id
}
output "ssh_command" {
value = "ssh -i ${var.keyfile} ${lookup(var.os_users, var.os, var.custom_user)}@${aws_instance.instance.public_ip}"
}