How to correctly log in into AWS EC2 instance created by CloudFormation

I'm just starting to play with AWS services, more specific with CloudFormation. I'm following some of the AWS tutorials, and so far so good, but I'm stuck at one thing: once I generate the template as follows:

AWSTemplateFormatVersion: 2010-09-09
Resources:
 Ec2Instance:
  Type: AWS::EC2::Instance
  Properties:
    InstanceType:
      Ref: InstanceTypeParameter1
    ImageId: ami-07d02ee1eeb0c996c
Parameters:
  InstanceTypeParameter1:
    Type: String
    Default: t2.micro
    AllowedValues:
      - t2.micro
      - m1.small
      - m1.large
    Description: Choose the ec2 instance type
  myKeyPair: 
    Description: Amazon EC2 Key Pair
    Type: "AWS::EC2::KeyPair::KeyName"

Once I upload the template, and click on Next, I can choose the Amazon EC2 Key Pair as you can see in the pict:

Inputting the ssh key

And then I go to the EC2 console, I look the instance the CloudFromation just created, but I can't log-in using SSH protocol. As you can see in the image

I would like to log in in my EC2 instances created by CloudFormation, I thought that using the myKeyPair parameter:

  myKeyPair: 
    Description: Amazon EC2 Key Pair
    Type: "AWS::EC2::KeyPair::KeyName"

Would do the magic, but it doesn't. So: how can I?


Solution 1:

Well, I find what did the trick:

As in the previously showed .yaml, in the Parameters section you need to ask for the user to select a key to use, with:

Parameters:
.........
  myKeyPair: 
    Description: Amazon EC2 Key Pair
    Type: "AWS::EC2::KeyPair::KeyName"

What in fact led me to, when creating the Stack, the webpage prompt asking me for select one of the saved keys: Select one of the saved keys

But I wasn't linking the collected key in the Parameters section with the Resources section... What I need to do is simply make a reference in the Properties section inside Resource, as follows:

Parameters:
.........
  myKeyPair: 
    Description: Amazon EC2 Key Pair
    Type: "AWS::EC2::KeyPair::KeyName"

Resources:
.......
  EC2Instance:
    Type: AWS::EC2::Instance
    Properties:
       ................
      KeyName:
        Ref: KeyName
       ................

That did the trick!!!