Key Pair not being created in AWS Cloudshell

232 Views Asked by At

Trying to create a key pair using boto3 a python file in AWS cloudshell but when I run it (and many other variations of it), it is not being created.

def create_key_pair():
    client = boto3.client("ec2", region_name="us-east-1")
    key_pair = client.create_key_pair(KeyName="test-key")

    private_key = key_pair["KeyMaterial"]

    with os.fdopen(os.open("/tmp/test-key.pem", os.O_WRONLY | os.O_CREAT, 0o400), "w+") as handle:
            handle.write(private_key)

The region is correct

The file is not being created with the private key

When I try to launch an instance with this in the KeyName parameter I am told that it does not exist.

I have also tried with 'import boto3' at the start of the document and nothing different happened.

Edit

Slight breakthrough, the .pem file is now being populated with an access denied error, see below.

<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>AccessDenied</Code><Message>Access Denied</Message><RequestId>...</RequestId><HostId>...</HostId$

This comes even when I run the program file with root permissions.

Any help is much appreciated! Thanks in advance!

0

There are 0 best solutions below