I'm trying to SSH into an EC2 instance using a command like this:
ssh -i ~/.ssh/mykey.pem ubuntu#<ec2_public_DNS_name>
But, the PublicDnsName field is showing up blank on the command line after I create the instance. I have already tried to set DNS Hostnames to yes in the VPC dashboard (and then terminated and created another instance). I have also checked the subnet and Auto-Assign public IP is set to yes.
Where can I find the public DNS name?
From what you've described, your instance has probably been stopped. If you're using AWS Command-Line Interface (CLI), you can query your instance details including its public DNS hostname if you know your instance ID:
aws ec2 describe-instances --instance-ids i-XXXXXXXX
Or, if you only know the AMI ID your instance was created from:
aws ec2 describe-instances --filters "Name=image-id,Values=ami-XXXXXXXX"
You should also be able to review all instances owned by your AWS account by visiting AWS EC2 dashboard from a browser.
Related
I have deployed an auto scaling EC2 and has associated an Elastic IP address with it. I'm not using a load balancer, because the total number of users doesn't exceed 20. Therefore, my current settings are to have 1 minimum and 1 maximum servers.
If the EC2 server fails, another one is created instead, which is what i'm trying to do. However, the elastic IP is not automatically remapped to the newly created server.
How can i assign the elastic IP automatically to the newly created EC2 instance? Is there a workaround this issue?
UPDATE:
I've added the following to User Data, but the new EC2 is created without a public ip still.
#!/bin/bash
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
aws ec2 associate-address --instance-id $INSTANCE_ID --allocation-id=eipalloc-**.***.***.***
Without an ELB to manage your Elastic IPs, you'll need to use the User Data field on your EC2 instance to call the aws ec2 associate-address API endpoint upon instance creation:
aws ec2 associate-address --instance-id <instance id> --allocation-id <eip-alloc-id>
The EIP allocation ID can be found using the AWS Console. You can obtain the Instance ID by making this call in the User Data:
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
With EC2 & Auto scaling, You need using user data in EC2 to Auto Attach Elastic IP to EC2 Instance For Auto scaling
#!/bin/bash
aws configure set aws_access_key_id "XYZ..."
aws configure set aws_secret_access_key "ABC..."
aws configure set region "ap-..."
aws ec2 associate-address --instance-id "$(curl -X GET "http://169.254.169.254/latest/meta-data/instance-id")" --public-ip your_elastic_IP
Note: you should create new user & IAM have only permission associate-address to create/get aws key
Hope it be help you :)
I need to update a config file in a shared EFS drive with all of the private IP addresses of the current autoscaling group.
The approach I'm thinking is to run a user data script that queries the ASG for the private IP addresses then echo that into the config file. To do that the ec2 needs to have AWS CLI credentials and appropriate read-only access. Ideally, I don't want to store any credentials on this ec2.
Is there another way? Possibly VPC Endpoint or something?
Thanks!
You are asking two questions.
How do I provide credentials securely to an EC2 instance?
You use IAM Roles and assign the role to your EC2 instances. Then use the instance credentials in your code. The CLI examples below will automatically pick up these credentials.
Using an IAM Role to Grant Permissions to Applications Running on Amazon EC2 Instances
How do I get the private IP address of EC2 instances in an Auto Scaling Group (ASG)?
You need to get a list of instances attached to your ASG.
For each instance in your ASG call the describe API and extract the private IP address.
Example commands:
aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name my-auto-scaling-group
aws ec2 describe-instances --instance-ids i-1234567890abcdef0
You can filter the command output. For example add the following to the second command to just display the private IP address:
--query 'Reservations[*].Instances[*].PrivateIpAddress'
Recommendation:
I would use the Python SDK and write a simple program that provides these features and updates your config file.
I want to stop and restart my AWS EC2 instance daily. I can stop it through the API command line interface, but to reconnect I need to get the new DNS information so that I can connect through Remote Desktop. Is there a way to reconnect that doesn't involve going through the EC2 Management Console?
Option 1
Assign an Elastic IP address to the instance, and always connect via that IP.
You can also then setup a DNS record with a friendly name (e.g. myinstance.mydomain.com) pointing to that elastic IP address.
Note that while your instance is stopped, having a reserved elastic IP address assigned to it will cost a small hourly charge - see https://aws.amazon.com/ec2/pricing/on-demand/#Elastic_IP_Addresses for more information.
Option 2
If you're using route53 for DNS management (or some other DNS hosting service which has an API you can use), you could write a script that runs at instance startup which detects its current IP address, and uses the route53 api to update a DNS record with the instances new IP address. You'd need to take into account the DNS propagation time if doing this, so I'd definitely recommend the Elastic IP method over this if possible.
Option 3
Use the AWS CLI with the following commands to get the public IP address of your instance. Be sure to change the instance-id parameter to match your own instance.
aws ec2 describe-instances --instance-id i-0a3bd317964ca45543 --query 'Reservations[0].Instances[0].PublicIpAddress'
For example, combining that with an SSH command might look like this:
ssh ec2-user#`aws ec2 describe-instances --instance-id i-0a3bd317964ca45543 --query 'Reservations[0].Instances[0].PublicIpAddress' --output text` -i ~/my-key.pem
Is there a way to set a Public (Elastic) IP for a machine in AWS? I'm using Packer from w/in a corp network. We have to explicitly whitelist IPs as SSH targets. Is there a way to, when Packer starts an EC2 instance for image build, have it get a specific Elastic IP address?
I assume you already have the elastic IPs allocated and you want to assign one of the IPs from that pool. There is no way to assign the IP when starting. Instead upload a script using FileProvisioner. The script will execute a AWS CLI command to assign the IP of your choice to your instance.
aws ec2 associate-address --instance-id <your-instance-id> --public-ip <your-elastic-IP>
Then use ShellProvisioner to execute that script.
I can able to create an instace with follwoing command
aws ec2 run-instances --image-id $AMI_ID --count 1 --instance-type ${INSTANCE_TYPE} --key-name KEY_NAME --region us-east-1 --security-groups MYSECURITY_GROUP
But I did not find any option to attach elastic IP address to it. Is it possible to attach a Elastic IP during bootstrapping? Or post bootstrapping?
You can use --user-data (string) option to run-instances. The user data that you pass will contain the CLI to associate the elastic IP. The CLI command is given below. To get the instance-id in user data, use the metadata server:
curl instance-data/latest/meta-data/instance-id
You can also attach an elastic IP after you launch. Use associate-address to attach an elastic IP.
More examples in: associate-address
This example uses the new style (longer) instance id.
aws ec2 associate-address --instance-id i-0b263919b6498b123 --allocation-id eipalloc-64d5890a
You can get the allocation id from
aws ec2 describe-addresses
describe-addresses
The desire I read in the question is "how to start an instance with a given known IP address (from an elastic IP pool,) without first starting it with another temporary IP address."
The way to do this that I've found, is to first allocate a NetworkInterface, and then allocate the IP address, and then bind the IP address to the NetworkInterface, and then bind the pre-allocated NetworkInterface to the eth0 interface as part of the NetworkInterface launch parameters. Yup, four steps, just because you want your instance to start out with an IP address that won't change for the duration of its lifetime!
For "infrastructure as cattle" behind a NAT/load balancer of some sort, this doesn't matter of course. But for "cloud developer hosts" that you SSH to or "open remote" to from your IDE, keeping the IP address the same all the way from the beginning is a pretty important requirement.