Skip to content

Commit 93ba584

Browse files
committed
fix(ci): replace EC2 Instance Connect with cloud-init SSH key injection
Tests are often failing due to the SSH access to the instance. EC2 Instance Connect push the temporary SSH key which is then available only for 60 seconds. Recently, errors often occur when the SSH key is sent to the instance, resulting in a timeout. We replace runtime SSH key injection via EC2 Instance Connect API with cloud-init configuration to add the SSH public key during instance initialization. Note that we are still using EC2 Instance Connect to create the SSH key pair, but we are not using it to push the key to the instance.
1 parent defc662 commit 93ba584

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

testinfra/test_ami_nix.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from ec2instanceconnectcli.EC2InstanceConnectLogger import EC2InstanceConnectLogger
1010
from ec2instanceconnectcli.EC2InstanceConnectKey import EC2InstanceConnectKey
1111
from time import sleep
12-
import subprocess
1312
import paramiko
1413

1514
# if GITHUB_RUN_ID is not set, use a default value that includes the user and hostname
@@ -233,6 +232,10 @@ def host():
233232
def gzip_then_base64_encode(s: str) -> str:
234233
return base64.b64encode(gzip.compress(s.encode())).decode()
235234

235+
# Create temporary SSH key pair
236+
ec2logger = EC2InstanceConnectLogger(debug=False)
237+
temp_key = EC2InstanceConnectKey(ec2logger.get_logger())
238+
236239
instance = list(
237240
ec2.create_instances(
238241
BlockDeviceMappings=[
@@ -279,6 +282,10 @@ def gzip_then_base64_encode(s: str) -> str:
279282
- 'bash init.sh "staging"'
280283
- 'touch /var/lib/init-complete'
281284
- 'rm -rf /tmp/*'
285+
users:
286+
- name: ubuntu
287+
ssh_authorized_keys:
288+
- {temp_key.get_pub_key()}
282289
""",
283290
TagSpecifications=[
284291
{
@@ -297,16 +304,6 @@ def gzip_then_base64_encode(s: str) -> str:
297304
# Increase wait time before starting health checks
298305
sleep(30) # Wait for 30 seconds to allow services to start
299306

300-
ec2logger = EC2InstanceConnectLogger(debug=False)
301-
temp_key = EC2InstanceConnectKey(ec2logger.get_logger())
302-
ec2ic = boto3.client("ec2-instance-connect", region_name="ap-southeast-1")
303-
response = ec2ic.send_ssh_public_key(
304-
InstanceId=instance.id,
305-
InstanceOSUser="ubuntu",
306-
SSHPublicKey=temp_key.get_pub_key(),
307-
)
308-
assert response["Success"]
309-
310307
# Wait for instance to have public IP
311308
while not instance.public_ip_address:
312309
logger.warning("waiting for ip to be available")

0 commit comments

Comments
 (0)