-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbastion-server.yml
167 lines (167 loc) · 4.66 KB
/
bastion-server.yml
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
Description: >
Damiloju / Udacity
This template deploys a Bastion Server.
Parameters:
EnvironmentName:
Description: An Environment name that will be prefixed to resources
Type: String
NetworkAccessIP:
Description: Source that has SSH access to the bastion host. (i.e. 50.32.108.38/32)
Type: String
BastionHostSSHS3Bucket:
Description: Bucket name for Application
Type: String
BastionHostSSHKeyName:
Description: Bastion Host SSH Key Name
Type: "AWS::EC2::KeyPair::KeyName"
Resources:
IamRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
IamPolicies:
Type: AWS::IAM::Policy
Properties:
PolicyName: root
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action: "s3:*"
Resource: "*"
Roles:
- !Ref IamRole
IamInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: "/"
Roles:
- !Ref IamRole
BastionSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: SSH Access to Bastion Instance
VpcId:
Fn::ImportValue: !Sub "${EnvironmentName}-VPCID"
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: "22"
ToPort: "22"
CidrIp: !Sub ${NetworkAccessIP}
SecurityGroupEgress:
- IpProtocol: "-1"
CidrIp: 0.0.0.0/0
BastionEC2Instance1:
Type: AWS::EC2::Instance
DependsOn:
- BastionSecurityGroup
- IamInstanceProfile
Properties:
IamInstanceProfile: !Ref IamInstanceProfile
ImageId: ami-003634241a8fcdec0
InstanceType: t2.micro
KeyName: !Sub ${BastionHostSSHKeyName}
SecurityGroupIds:
- Ref: BastionSecurityGroup
SubnetId:
Fn::ImportValue: !Sub "${EnvironmentName}-PUB1-SN"
UserData:
Fn::Base64: !Sub |
#!/bin/bash
apt-get update -y
apt-get install unzip awscli -y
cd /home/ubuntu
aws s3 cp s3://${BastionHostSSHS3Bucket}/${BastionHostSSHKeyName}.pem /home/ubuntu
chmod 400 ${BastionHostSSHKeyName}.pem
EIP1:
Type: AWS::EC2::EIP
Properties:
Domain:
Fn::ImportValue: !Sub "${EnvironmentName}-VPCID"
EIPAssociation1:
Type: AWS::EC2::EIPAssociation
DependsOn:
- BastionEC2Instance1
- EIP1
Properties:
InstanceId:
Ref: BastionEC2Instance1
AllocationId:
Fn::GetAtt:
- EIP1
- AllocationId
BastionEC2Instance2:
Type: AWS::EC2::Instance
DependsOn:
- BastionSecurityGroup
- IamInstanceProfile
Properties:
IamInstanceProfile: !Ref IamInstanceProfile
ImageId: ami-003634241a8fcdec0
InstanceType: t2.micro
KeyName: !Sub ${BastionHostSSHKeyName}
SecurityGroupIds:
- Ref: BastionSecurityGroup
SubnetId:
Fn::ImportValue: !Sub "${EnvironmentName}-PUB2-SN"
UserData:
Fn::Base64: !Sub |
#!/bin/bash
apt-get update -y
apt-get install unzip awscli -y
cd /home/ubuntu
aws s3 cp s3://${BastionHostSSHS3Bucket}/${BastionHostSSHKeyName}.pem /home/ubuntu
chmod 400 ${BastionHostSSHKeyName}.pem
EIP2:
Type: AWS::EC2::EIP
Properties:
Domain:
Fn::ImportValue: !Sub "${EnvironmentName}-VPCID"
EIPAssociation2:
Type: AWS::EC2::EIPAssociation
DependsOn:
- BastionEC2Instance2
- EIP2
Properties:
InstanceId:
Ref: BastionEC2Instance2
AllocationId:
Fn::GetAtt:
- EIP2
- AllocationId
Outputs:
BastionHost1CIDR:
Description: A reference to the Bastion Host CIDR
Value: !Join
- ""
- - !GetAtt BastionEC2Instance1.PrivateIp
- !Sub "/32"
Export:
Name: !Sub ${EnvironmentName}-BASTION1-HOST-CIDR
BastionHost2CIDR:
Description: A reference to the Bastion Host CIDR
Value: !Join
- ""
- - !GetAtt BastionEC2Instance2.PrivateIp
- !Sub "/32"
Export:
Name: !Sub ${EnvironmentName}-BASTION2-HOST-CIDR
BastionHostSSHKeyName:
Description: A reference to the Bastion Host SSH Key Name
Value: !Sub ${BastionHostSSHKeyName}
Export:
Name: !Sub ${EnvironmentName}-BASTION-HOST-SSH-KEY-NAME
BastionSecurityGroupName:
Description: A reference to the Bastion Security Name
Value: !Sub ${BastionSecurityGroup}
Export:
Name: !Sub ${EnvironmentName}-BASTION-SECURITY-GROUP-NAME