Connect to an ec2 Instance created through cdk using .pem file

0

I have created an infrastructure containing a VPC and Subnet and Security group and auto scaling group. Then, I created a Key Pair in the aws management console. then I referenced this Key-Pair in my CDK code.

 const keyPair = ec2.KeyPair.fromKeyPairAttributes(this, 'KeyPair', {
      keyPairName: 'sample-key-name',
      type: ec2.KeyPairType.RSA,
    })

and passed it this value to the Autoscaling group definition

 const asg= new autoscaling.AutoScalingGroup(this,'my-autoscaling',{
      keyName:keyPair.keyPairName,
    ....... //other props
    })

  }

Now when i try to ssh to this instance , the connection times out and i have double checked that I allow all ssh connections to this instance. also I can see that after deploying my cdk app , Elastic IP address has been created , but when I check the instance details i can see that Elastic Ip address field is empty and am using this elastic Ip address to ssh with. what am i doing wrong ?

2 Answers
0

Hello.

Instead of attaching ElasticIP to EC2 launched in an AutoScaling group, how about automatically assigning a public IPv4 address as shown below?
I think the timeout error is occurring because the public IP address is not attached to EC2 that has been launched in the AutoScaling group.
There is a parameter called "associatePublicIpAddress", so if you set this to "True", you can attach a public IPv4 address to EC2 launched in the AutoScaling group.
https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_autoscaling.AutoScalingGroup.html

I think the CDK code would be as follows.

 const asg= new autoscaling.AutoScalingGroup(this,'my-autoscaling',{
      keyName:keyPair.keyPairName,
      associatePublicIpAddress: True,
    ....... //other props
    })

  }

In addition to the above, the subnet where EC2 starts must also be a public subnet.

profile picture
EXPERT
answered a month ago
  • Thank you a lot , I will update my CDK code with what you suggested. furthermore, may i know if the approach of creating a .pem file and then refering to it in the cdk code is valid or there is another approach for attaching .pem files for EC2 instances that are not created through the aws management console ? i.e. Instances launched through CDK

  • You can create a key pair from CDK using "CfnKeyPair" as shown below. The key pair you create is stored in the Systems Manager Parameter Store. https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.CfnKeyPair.html

0

Hello , I have added the following block of code

const asg= new autoscaling.AutoScalingGroup(this,'my-autoscaling',{
      keyName:keyPair.keyPairName,
     ** associatePublicIpAddress: True,**
    ....... //other props
    })

  }

but unfortunately, I still can't see a public Ip address got created for the instance , i made sure to attach the ec2 instance to the public subnet as mentioned in your answer

const asg= new autoscaling.AutoScalingGroup(this,'autoscaling-cdk',{
      ....
      vpc
      associatePublicIpAddress:true,
      ................
      keyName:cfnKeyPair.keyName,
      securityGroup,
      **vpcSubnets:{subnetType:ec2.SubnetType.PUBLIC},**
    
    })
    
  }

Note :

1- I can see that two elastic IPs are created once i deploy my cdk code , I don't specify creating elastic Ip address anywhere in my cdk code .

2- I created a new Key pair as mentioned here https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.CfnKeyPair.html and i got its value from parameter store and added it to a .pem file to ssh with along with both of the elastic Ips but still connection times out.

Mahmoud
answered a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions