-
Notifications
You must be signed in to change notification settings - Fork 10
/
bastion_host_jenkins_kops.tf
128 lines (124 loc) · 2.75 KB
/
bastion_host_jenkins_kops.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
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}
provider "aws" {
profile = "default"
region = "us-east-1"
}
variable "env" {
default = "bastion-host"
}
variable "amis" {
type = map
default = {
"bastion-host" = "ami-06b263d6ceff0b3dd"
}
}
resource "aws_key_pair" "bastion-host" {
key_name = var.env
public_key = file("id_rsa.pub")
}
#Default VPC
resource "aws_default_vpc" "default" {}
#Port 22 for SSH and port 8080 for jenkins
resource "aws_security_group" "bastion-host-sg" {
name = "Allow SSH and Jenkins HTTP(S)"
description = "Allow Jenkins/SSH inbound traffic"
vpc_id = aws_default_vpc.default.id
ingress {
description = "Allow SSH from 0.0.0.0"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "Allow Jenkins HTTPS from 0.0.0.0"
from_port = 8443
to_port = 8443
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"]
}
tags = {
Name = var.env
}
}
resource "aws_iam_instance_profile" "kops_profile" {
name = "kops_profile"
role = aws_iam_role.kops.name
}
resource "aws_iam_role" "kops" {
name = "kops"
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" "kops_policy" {
name = "kops"
role = aws_iam_role.kops.id
policy = <<-EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iam:*",
"s3:*",
"route53:*",
"ec2:*",
"elasticloadbalancing:*",
"autoscaling:*"
],
"Resource": "*"
}
]
}
EOF
}
resource "aws_instance" "bastion-host" {
ami = var.amis[var.env]
instance_type = "t2.micro"
associate_public_ip_address = true
vpc_security_group_ids = [aws_security_group.bastion-host-sg.id]
key_name = aws_key_pair.bastion-host.id
iam_instance_profile = aws_iam_instance_profile.kops_profile.name
tags = {
Name = var.env
}
connection {
type = "ssh"
user = "ec2-user"
private_key = file("id_rsa.pub")
host = self.public_ip
}
provisioner "local-exec" {
command = "echo ${aws_instance.bastion-host.public_ip} > public_ip.txt"
}
lifecycle {
create_before_destroy = true
}
user_data = file("install_jenkins_kops.sh")
}