Skip to content
This repository was archived by the owner on Dec 18, 2025. It is now read-only.

Commit c9e137e

Browse files
committed
Make bastion an optional component.
1 parent 80c31db commit c9e137e

File tree

5 files changed

+613
-20
lines changed

5 files changed

+613
-20
lines changed

cmd.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
aws cloudformation create-stack --region=us-east-1 --stack-name startupkit-eb --template-body file://vpc-eb-rds.cfn.yml --capabilities CAPABILITY_IAM --parameters ParameterKey=DatabasePassword,ParameterValue=aquasush ParameterKey=TemplateBucket,ParameterValue=gjg-startupkit-templates ParameterKey=AppS3Bucket,ParameterValue=gjg-startupkit-testing ParameterKey=AppS3Key,ParameterValue=demoapp/1.zip ParameterKey=AvailabilityZone1,ParameterValue=us-east-1a ParameterKey=AvailabilityZone2,ParameterValue=us-east-1b ParameterKey=StackType,ParameterValue=rails

templates/bastion.cfn.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ Parameters:
1919

2020
KeyName:
2121
Description: EC2 key pair name for bastion host SSH access
22-
Type: AWS::EC2::KeyPair::KeyName
22+
Type: String
23+
Default: ''
2324

2425
LogRetentionInDays:
2526
Description: Number of days you would like your CloudWatch Logs to be retained
@@ -36,6 +37,9 @@ Parameters:
3637
- true
3738
- false
3839

40+
Conditions:
41+
HasKeyName: !Not [ !Equals [ !Ref KeyName, '' ]]
42+
3943
Mappings:
4044

4145
# Amazon Linux AMI - https://aws.amazon.com/amazon-linux-ami/
@@ -211,7 +215,7 @@ Resources:
211215

212216
Properties:
213217
InstanceType: t2.micro
214-
KeyName: !Ref KeyName
218+
KeyName: !If [ HasKeyName, !Ref KeyName, !Ref "AWS::NoValue" ]
215219
NetworkInterfaces:
216220
- NetworkInterfaceId: !Ref BastionNetworkInterface
217221
DeviceIndex: 0

templates/elastic-beanstalk.cfn.yml

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ Parameters:
4343
MaxLength: 255
4444
AllowedPattern: "^[a-zA-Z][-a-zA-Z0-9]*$"
4545

46+
Bastion:
47+
Description: Flag on whether to allow SSH access to instances via bastion instead of Session Manager
48+
Type: String
49+
Default: false
50+
AllowedValues:
51+
- true
52+
- false
53+
4654
DatabaseStackName:
4755
Description: Name of an active CloudFormation stack of database resources
4856
Type: String
@@ -80,7 +88,8 @@ Parameters:
8088

8189
EC2KeyPairName:
8290
Description: EC2 key pair name for SSH access
83-
Type: AWS::EC2::KeyPair::KeyName
91+
Type: String
92+
Default: ''
8493

8594
DevInstanceType:
8695
Description: The instance type for the dev environment
@@ -122,16 +131,19 @@ Parameters:
122131
Conditions:
123132

124133
CreateProdEnv: !Equals [ !Ref EnvironmentName, prod ]
134+
HasKeyName: !Not [ !Equals [ !Ref EC2KeyPairName, '' ]]
125135

126136
TlsEnabled: !Not [ !Equals [ !Ref SSLCertificateArn, "" ] ]
127137

138+
CreateBastion: !Equals [ !Ref Bastion, true ]
139+
128140
Mappings:
129141
# Maps stack type parameter to solution stack name string
130142
StackMap:
131143
node:
132144
stackName: 64bit Amazon Linux 2018.03 v4.5.3 running Node.js
133145
rails:
134-
stackName: 64bit Amazon Linux 2018.03 v2.8.3 running Ruby 2.4 (Puma)
146+
stackName: 64bit Amazon Linux 2018.03 v2.8.7 running Ruby 2.4 (Puma)
135147
spring:
136148
stackName: 64bit Amazon Linux 2018.03 v3.0.3 running Tomcat 8 Java 8
137149
python:
@@ -213,18 +225,20 @@ Resources:
213225
Value: !Ref AutoScalingMaxInstanceCount
214226

215227
- Namespace: aws:autoscaling:launchconfiguration
216-
OptionName: SecurityGroups
217-
Value:
218-
Fn::ImportValue: !Sub "${NetworkStackName}-AppSecurityGroupID"
228+
OptionName: SSHSourceRestriction
229+
Value: !If
230+
- CreateBastion
231+
- "Fn::Join":
232+
- ','
233+
- - 'tcp, 22, 22'
234+
- !ImportValue
235+
"Fn::Sub": "${NetworkStackName}-BastionGroupID"
236+
- 'tcp, 22, 22, 127.0.0.1/32'
219237

220238
- Namespace: aws:autoscaling:launchconfiguration
221-
OptionName: SSHSourceRestriction
239+
OptionName: SecurityGroups
222240
Value:
223-
"Fn::Join":
224-
- ','
225-
- - 'tcp, 22, 22'
226-
- !ImportValue
227-
"Fn::Sub": "${NetworkStackName}-BastionGroupID"
241+
Fn::ImportValue: !Sub "${NetworkStackName}-AppSecurityGroupID"
228242

229243
- Namespace: aws:autoscaling:launchconfiguration
230244
OptionName: InstanceType
@@ -236,7 +250,7 @@ Resources:
236250

237251
- Namespace: aws:autoscaling:launchconfiguration
238252
OptionName: EC2KeyName
239-
Value: !Ref EC2KeyPairName
253+
Value: !If [ HasKeyName, !Ref EC2KeyPairName, !Ref "AWS::NoValue" ]
240254

241255
- Namespace: aws:autoscaling:updatepolicy:rollingupdate
242256
OptionName: RollingUpdateEnabled

templates/vpc.cfn.yml

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ AWSTemplateFormatVersion: 2010-09-09
44
Description: SASKV5N VPC
55

66
# This VPC stack should be created first before any other
7-
# CloudFormation stacks, such as a bastion stack, database
8-
# stack and application stack
7+
# CloudFormation stacks, such as a database stack
8+
# and application stack
99
Parameters:
1010

1111
AvailabilityZone1:
@@ -18,6 +18,14 @@ Parameters:
1818
Type: AWS::EC2::AvailabilityZone::Name
1919
ConstraintDescription: Must be a valid availability zone
2020

21+
Bastion:
22+
Description: Flag on whether to allow SSH access to instances via bastion instead of Session Manager
23+
Type: String
24+
Default: false
25+
AllowedValues:
26+
- true
27+
- false
28+
2129
SSHFrom:
2230
Description: Limit SSH access to bastion hosts to a CIDR IP block
2331
Type: String
@@ -76,6 +84,7 @@ Metadata:
7684
Conditions:
7785
CreateSingleNatGateway: !Equals [ !Ref SingleNatGateway, true ]
7886
CreateMultipleNatGateways: !Not [ Condition: CreateSingleNatGateway ]
87+
CreateBastion: !Equals [ !Ref Bastion, true ]
7988

8089
Mappings:
8190

@@ -230,10 +239,6 @@ Resources:
230239
IpProtocol: tcp
231240
ToPort: !Ref AppIngressPort
232241
FromPort: !Ref AppIngressPort
233-
- SourceSecurityGroupId: !Ref BastionSecurityGroup
234-
IpProtocol: tcp
235-
ToPort: 22
236-
FromPort: 22
237242
Tags:
238243
- Key: Name
239244
Value: !Sub "${AWS::StackName}-AppSecurityGroup"
@@ -248,6 +253,7 @@ Resources:
248253
SourceSecurityGroupId: !Ref ELBSecurityGroup
249254

250255
AppSecurityGroupFromBastionIngress:
256+
Condition: CreateBastion
251257
Type: AWS::EC2::SecurityGroupIngress # prevent security group circular references
252258
Properties:
253259
GroupId: !Ref AppSecurityGroup
@@ -257,6 +263,7 @@ Resources:
257263
SourceSecurityGroupId: !Ref BastionSecurityGroup
258264

259265
BastionSecurityGroup:
266+
Condition: CreateBastion
260267
Type: AWS::EC2::SecurityGroup
261268
Properties:
262269
GroupDescription: Enable access to the bastion host
@@ -284,6 +291,7 @@ Resources:
284291
Value: !Sub "${AWS::StackName}-BastionSecurityGroup"
285292

286293
BastionSecurityGroupToAppEgress:
294+
Condition: CreateBastion
287295
Type: AWS::EC2::SecurityGroupEgress # prevent security group circular references
288296
Properties:
289297
GroupId: !Ref BastionSecurityGroup
@@ -293,6 +301,7 @@ Resources:
293301
DestinationSecurityGroupId: !Ref AppSecurityGroup
294302

295303
BastionSecurityGroupToDbEgress:
304+
Condition: CreateBastion
296305
Type: AWS::EC2::SecurityGroupEgress # prevent security group circular references
297306
Properties:
298307
GroupId: !Ref BastionSecurityGroup
@@ -320,6 +329,7 @@ Resources:
320329
Value: !Sub "${AWS::StackName}-DbSecurityGroup"
321330

322331
DbSecurityGroupFromBastionIngress:
332+
Condition: CreateBastion
323333
Type: AWS::EC2::SecurityGroupIngress # prevent security group circular references
324334
Properties:
325335
GroupId: !Ref DbSecurityGroup
@@ -482,6 +492,7 @@ Outputs:
482492

483493
BastionSecurityGroup:
484494
Description: Security group ID for bastion host
495+
Condition: CreateBastion
485496
Value: !GetAtt BastionSecurityGroup.GroupId
486497
Export:
487498
Name: !Sub "${AWS::StackName}-BastionGroupID"

0 commit comments

Comments
 (0)