-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge1.tf
186 lines (170 loc) · 4.08 KB
/
challenge1.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# --------------------------------------------------------------------------------------------------
# Terraform scenary for ADV-it challenge 1
# Author: Dmitry Lavrukhin
#
# Version Date Name Info
# 0.1 19-Nov-2019 Dmitry Lavrukhin Initial Version
#
# Requirements:
# please set this environment variables before apply:
# ---windows example:
# set AWS_ACCESS_KEY_ID=REPLACE_ME
# set AWS_SECRET_ACCESS_KEY=REPLACE_ME
# ---windows PowerShell example:
# $env:AWS_ACCESS_KEY_ID="REPLACE_ME"
# $env:AWS_SECRET_ACCESS_KEY="REPLACE_ME"
# ---linux example:
# export AWS_ACCESS_KEY_ID=REPLACE_ME
# export AWS_SECRET_ACCESS_KEY=REPLACE_ME
#
# also set variable parameters in file terraform.tfvars.example and rename it to terraform.tfvars
# --------------------------------------------------------------------------------------------------
provider "aws" {
region = var.region
}
data "template_file" "myuserdata" {
template = "${file("${path.cwd}/myuserdata.tpl")}"
}
variable "region" {
type = string
}
variable "keypair_name" {
type = string
}
variable "instance_type" {
type = string
}
variable "vpc_id" {
type = string
}
variable "subnets" {
type = list(string)
}
resource "aws_eip" "advit" {
vpc = true
tags = {
Owner = "Dmitry Lavrukhin"
Project = "ADV-IT challenge1"
}
}
resource "aws_iam_instance_profile" "profile" {
name = "ec2allowattacheip"
role = aws_iam_role.role.name
}
resource "aws_iam_role" "role" {
name = "ec2allowattacheip"
path = "/"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_role_policy" "policy" {
name = "ec2allowattacheip"
role = aws_iam_role.role.id
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ec2:AssociateAddress",
"ec2:DescribeAddresses",
"ec2:DescribeTags",
"ec2:DescribeInstances"
],
"Resource": "*"
}
]
}
EOF
}
resource "aws_security_group" "sshonly" {
name = "allow_ssh_only"
description = "Allow only ssh inbound traffic"
vpc_id = var.vpc_id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_launch_template" "advit" {
name = "advit-challenge1-template"
description = "launch template for running amazon linux autoscaling group ADV-IT challenge 1"
image_id = "ami-0f3a43fbf2d3899f7"
instance_type = var.instance_type
key_name = var.keypair_name
iam_instance_profile {
name = aws_iam_instance_profile.profile.name
}
network_interfaces {
associate_public_ip_address = true
security_groups = [aws_security_group.sshonly.id]
delete_on_termination = true
}
tags = {
Name = "advit-challenge1-template"
Owner = "Dmitry Lavrukhin"
Project = "ADV-IT challenge1"
}
tag_specifications {
resource_type = "instance"
tags = {
Name = "adv-it challenge1 instance"
Owner = "Dmitry Lavrukhin"
Project = "ADV-IT challenge1"
}
}
user_data = base64encode(data.template_file.myuserdata.template)
}
resource "aws_autoscaling_group" "advit" {
name = "ADV-IT challenge1 group"
availability_zones = ["eu-central-1a"]
desired_capacity = 1
max_size = 1
min_size = 1
health_check_grace_period = 180
default_cooldown = 180
vpc_zone_identifier = var.subnets
launch_template {
id = aws_launch_template.advit.id
version = "$Latest"
}
tags = [
{
key = "Name"
value = "advit challenge1"
propagate_at_launch = false
},
{
key = "Owner"
value = "Dmitry Lavrukhin"
propagate_at_launch = true
},
{
key = "Project"
value = "ADV-IT challenge1"
propagate_at_launch = true
}
]
}