- Newest
- Most votes
- Most comments
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.
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.
Relevant content
- asked 5 years ago
- asked 5 years ago
- Accepted Answerasked 19 days ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 7 months 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