Skip to content

Commit d44eec6

Browse files
authored
cfn: Update ec2 key ref to be conditional (#415)
* cfn: Update ec2 key ref to be conditional Signed-off-by: Dan Sover <[email protected]> * fix: Use condition for ec2 key name * apply: Use String type for ec2 key name Signed-off-by: Dan Sover <[email protected]> --------- Signed-off-by: Dan Sover <[email protected]>
1 parent c09c566 commit d44eec6

File tree

3 files changed

+63
-15
lines changed

3 files changed

+63
-15
lines changed

avalanche-ops/src/aws/cfn-templates/asg_ubuntu.yaml

+18-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ Parameters:
4242
Description: S3 bucket name.
4343

4444
Ec2KeyPairName:
45-
Type: AWS::EC2::KeyPair::KeyName
45+
Type: String
4646
Description: EC2 SSH key name
47+
Default: ""
4748

4849
InstanceProfileArn:
4950
Type: String
@@ -201,6 +202,12 @@ Parameters:
201202
MaxValue: 100
202203
Description: 0 for Spot only. 100 for On-Demand only.
203204

205+
SshEnabled:
206+
Type: String
207+
AllowedValues: [true, false]
208+
Default: false
209+
Description: true to enable SSH access to nodes
210+
204211
NlbEnabled:
205212
Type: String
206213
AllowedValues: [true, false]
@@ -410,6 +417,11 @@ Conditions:
410417
- Ref: NlbEnabled
411418
- "true"
412419

420+
SshEnabledTrue:
421+
Fn::Equals:
422+
- Ref: SshEnabled
423+
- "true"
424+
413425
NlbTargetGroupArnEmpty:
414426
Fn::Equals:
415427
- Ref: NlbTargetGroupArn
@@ -526,7 +538,11 @@ Resources:
526538
- HasImageId
527539
- !Ref ImageId
528540
- !Ref ImageIdSsmParameter
529-
KeyName: !Ref Ec2KeyPairName
541+
KeyName:
542+
Fn::If:
543+
- SshEnabledTrue
544+
- !Ref Ec2KeyPairName
545+
- !Ref AWS::NoValue
530546

531547
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/device_naming.html
532548
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-launchtemplate-blockdevicemapping.html

avalanche-ops/src/dev-machines/cfn-templates/asg_ubuntu.yaml

+18-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ Parameters:
2929
Description: S3 bucket name.
3030

3131
Ec2KeyPairName:
32-
Type: AWS::EC2::KeyPair::KeyName
32+
Type: String
3333
Description: EC2 SSH key name
34+
Default: ""
3435

3536
InstanceProfileArn:
3637
Type: String
@@ -187,6 +188,12 @@ Parameters:
187188
MaxValue: 100
188189
Description: 0 for Spot only. 100 for On-Demand only.
189190

191+
SshEnabled:
192+
Type: String
193+
AllowedValues: [true, false]
194+
Default: false
195+
Description: true to enable SSH access to nodes
196+
190197
Conditions:
191198
HasImageId:
192199
Fn::Not:
@@ -355,6 +362,11 @@ Conditions:
355362
- Ref: InstanceTypesCount
356363
- 10
357364

365+
SshEnabledTrue:
366+
Fn::Equals:
367+
- Ref: SshEnabled
368+
- "true"
369+
358370
Resources:
359371
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-launchtemplate-launchtemplatedata.html
360372
AsgLaunchTemplate:
@@ -370,7 +382,11 @@ Resources:
370382
- HasImageId
371383
- !Ref ImageId
372384
- !Ref ImageIdSsmParameter
373-
KeyName: !Ref Ec2KeyPairName
385+
KeyName:
386+
Fn::If:
387+
- SshEnabledTrue
388+
- !Ref Ec2KeyPairName
389+
- !Ref AWS::NoValue
374390

375391
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/device_naming.html
376392
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-launchtemplate-blockdevicemapping.html

avalancheup-aws/src/apply/mod.rs

+27-11
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ pub async fn execute(log_level: &str, spec_file_path: &str, skip_prompt: bool) -
826826
build_param("AadTag", &spec.aad_tag),
827827
build_param("S3Region", &spec.resource.regions[0]),
828828
build_param("S3BucketName", &spec.resource.s3_bucket),
829-
build_param("Ec2KeyPairName", &regional_resource.ec2_key_name),
829+
build_param("SshEnabled", &spec.enable_ssh.to_string()),
830830
build_param(
831831
"InstanceProfileArn",
832832
&regional_resource
@@ -878,6 +878,12 @@ pub async fn execute(log_level: &str, spec_file_path: &str, skip_prompt: bool) -
878878
.unwrap(),
879879
));
880880
}
881+
if spec.enable_ssh {
882+
common_asg_params.push(build_param(
883+
"Ec2KeyPairName",
884+
&regional_resource.ec2_key_name,
885+
));
886+
}
881887

882888
// just copy the regional machine params, and later overwrite if 'create-dev-machine' is true
883889
let mut common_dev_machine_params = BTreeMap::new();
@@ -897,10 +903,13 @@ pub async fn execute(log_level: &str, spec_file_path: &str, skip_prompt: bool) -
897903
common_dev_machine_params.insert("AadTag".to_string(), spec.aad_tag.clone());
898904
common_dev_machine_params
899905
.insert("S3BucketName".to_string(), spec.resource.s3_bucket.clone());
900-
common_dev_machine_params.insert(
901-
"Ec2KeyPairName".to_string(),
902-
regional_resource.ec2_key_name.clone(),
903-
);
906+
907+
if spec.enable_ssh {
908+
common_dev_machine_params.insert(
909+
"Ec2KeyPairName".to_string(),
910+
regional_resource.ec2_key_name.clone(),
911+
);
912+
}
904913
common_dev_machine_params.insert(
905914
"InstanceProfileArn".to_string(),
906915
regional_resource
@@ -1346,8 +1355,10 @@ pub async fn execute(log_level: &str, spec_file_path: &str, skip_prompt: bool) -
13461355
}
13471356
}
13481357

1349-
let f = File::open(&regional_resource.ec2_key_path).unwrap();
1350-
f.set_permissions(PermissionsExt::from_mode(0o444)).unwrap();
1358+
if spec.enable_ssh {
1359+
let f = File::open(&regional_resource.ec2_key_path).unwrap();
1360+
f.set_permissions(PermissionsExt::from_mode(0o444)).unwrap();
1361+
}
13511362

13521363
println!();
13531364
let mut ssh_commands = Vec::new();
@@ -1379,7 +1390,7 @@ pub async fn execute(log_level: &str, spec_file_path: &str, skip_prompt: bool) -
13791390
},
13801391
};
13811392
if spec.enable_ssh {
1382-
println!("\n{}\n", ssh_command.to_string());
1393+
println!("\n{}\n", ssh_command);
13831394
} else {
13841395
println!("\n{}\n", ssh_command.ssm_start_session_command());
13851396
}
@@ -1855,8 +1866,10 @@ pub async fn execute(log_level: &str, spec_file_path: &str, skip_prompt: bool) -
18551866
}
18561867
}
18571868

1858-
let f = File::open(&regional_resource.ec2_key_path).unwrap();
1859-
f.set_permissions(PermissionsExt::from_mode(0o444)).unwrap();
1869+
if spec.enable_ssh {
1870+
let f = File::open(&regional_resource.ec2_key_path).unwrap();
1871+
f.set_permissions(PermissionsExt::from_mode(0o444)).unwrap();
1872+
}
18601873

18611874
println!();
18621875
let mut ssh_commands = Vec::new();
@@ -1889,7 +1902,7 @@ pub async fn execute(log_level: &str, spec_file_path: &str, skip_prompt: bool) -
18891902
},
18901903
};
18911904
if spec.enable_ssh {
1892-
println!("\n{}\n", ssh_command.to_string());
1905+
println!("\n{}\n", ssh_command);
18931906
} else {
18941907
println!("\n{}\n", ssh_command.ssm_start_session_command());
18951908
}
@@ -2740,6 +2753,9 @@ default-spec --log-level=info --funded-keys={funded_keys} --region={region} --up
27402753
regional_common_dev_machine_asg_params
27412754
.insert("SshKeyEmail".to_string(), email.clone());
27422755
};
2756+
// SSH keys for dev machine
2757+
regional_common_dev_machine_asg_params
2758+
.insert("SshEnabled".to_string(), spec.enable_ssh.to_string());
27432759

27442760
if !dev_machine.instance_types.is_empty() {
27452761
let instance_types = dev_machine.instance_types.clone();

0 commit comments

Comments
 (0)