Home > Back-end >  Unable to create EC2 Key Pair through CloudFormation
Unable to create EC2 Key Pair through CloudFormation

Time:06-24

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-keypair.html

Based on this documentation, you should be able to create a new key pair when launching an instance instead of creating one before hand and referencing it through parameters.

AWS even provides an example:

Resources:
  NewKeyPair:
    Type: 'AWS::EC2::KeyPair'
    Properties:
      KeyName: MyKeyPair
  Ec2Instance:
    Type: 'AWS::EC2::Instance'
    Properties:
      ImageId: ami-02b92c281a4d3dc79
      KeyName: !Ref NewKeyPair

The template is valid, but after I launch the stack I get this error:

NewKeyPair - Resource handler returned message: "null" (RequestToken: 6068026b-63b2-c71b-6cbb-f76f09fe599e, HandlerErrorCode: AlreadyExists)

EC2-SecurityKey - The following resource(s) failed to create: [NewKeyPair]. Rollback requested by user.

Anyone know why this rollback occurs and if I am missing something?

CodePudding user response:

The error means that you already have a key pair which has the same name as !Ref NewKeyPair. You have to ensure that your new key pair has new name, or delete pre-existing key pair.

CodePudding user response:

Check Your Keypairs from the ec2 console this error is about you already created "MyKeyPair" named key that's why this error is coming. if you want to create a key pair change a keypair name in this script or delete the old key pair named "MyKeyPair". this might be solve this error

  • Related