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
Related
I have an AWS instance that does some network data collection when it boots up. It's important that the collection happen from a fresh public IP address -- I can't get useful data by running the collection again on the same address.
Right now, I can stop and then restart the instance in the EC2 console, and when it restarts, it gets a new IP address and collects more useful data. (Just rebooting the instance doesn't assign a new IP -- I have to stop and then start.)
I know about time-based instance start scheduling, but what I'd like to do instead is schedule a restart from within the instance itself. Sort of like an at job: at now + 5 minutes restart-this-instance ; shutdown -h now.
Alternatively, if there's a way to release and reallocate an instance's public IP, that would work too.
No, this is not possible.
The command to Start the instance needs to be issued when the instance is Stopped. Therefore, the instance cannot issue the command to start itself.
It could, however, trigger something external to cause it to happen, such as creating a CloudWatch Rule or a Lambda function.
I suggest another approach...
The goal is to change Public IP address to measure how caching and repeat traffic from previously-seen IP addresses affects the measured performance of the main system.
Therefore, I would recommend:
Have an Elastic IP address associated with the instance
When a new IP address is desired:
Disassociate the Elastic IP address
Release the Elastic IP address
Allocate a new Elastic IP address
Associate the new Elastic IP address with the instance
You can use this script:
INSTANCE=`curl -s http://169.254.169.254/latest/meta-data/instance-id/`
ALLOC=`aws ec2 describe-addresses --filters Name=instance-id,Values=$INSTANCE --query Addresses[].AllocationId --output text`
aws ec2 release-address --allocation-id $ALLOC
NEW=`aws ec2 allocate-address --domain vpc --query AllocationId --output text`
aws ec2 associate-address --allocation-id $NEW --instance-id $INSTANCE
I just handed over a project which technical responsible person quit.
My client asked me to fix problem on their service they just know which domain but none of server ip and how to access.
Since they uses aws I looked for ip address which digged from domain but can't find in Ec2, load balancer and elastic ip.
By IP search, this ip address is served by amazon aws.
How can I find this server from ip address.
I can access to my customer's aws account.
I can access dns of customer's service domain.
It seems that your situation is:
You have a domain name that points to an IP address
You wish to find the EC2 instance(s) that the IP address points to
An IP address could be associated with:
An EC2 instance IP address
An Elastic IP address, which is then associated with an EC2 instance
Not a load balancer (it uses a DNS Name, not an IP address -- except for the new Network Load Balancer, but it is unlikely they are using this)
An database instance (eg RDS, Redshift, Elasticache) but this is unlikely as you are saying that the IP address is responding with web traffic
Therefore, the best thing to do would be to use the AWS Command-Line Interface (CLI) to list all IP addresses on EC2 instances and Elastic IPs:
aws ec2 describe-instances --query Reservations[*].Instances[*].[InstanceId,PublicIpAddress] --output text
aws ec2 describe-addresses --query Addresses[*].[NetworkInterfaceId,PrivateIpAddress] --output text
Run the above commands in every Region and you should find where that particular IP address is pointing.
First of all go to the billing section of the AWS Account and verify there are EC2 instances running under the AWS account provided to you.
Username in menu bar (top right corner) -> My Billing Dashboard -> Bills -> Details -> Elastic Compute Cloud -> Region of your EC2 instances
If you find instances in a particular region by switching to it and then you should be able to find the EC2 instance and SSH/RDP to it using the Key.pem file given to you(Or need to request it from your customer)
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.
I want to retrieve all the public IP addresses that have been allocated to an Amazon Web Services account.
There are two types of public IP addresses:
Elastic IP addresses (static)
Auto-assigned IP addresses (which might change if an instance is Stopped & Started)
Also, many different services are assigned IP addresses:
Amazon EC2 instances
Amazon RDS instances
Amazon Elasticache instances
Amazon Redshift instances
Amazon EMR master node
Elastic Load Balancer (the IP addresses change and should never be cached/stored)
etc
You would need to perform describe commands against each individual service to retrieve IP address information. There is no command that can retrieve this information across all services.
You might be able to use an AWS Config configuration snapshot -- it might have the information you seek for most services.
Example: Fetching Elastic IP Addresses
The easiest way to obtain a listing of Elastic IP Addresses (which are static addresses assigned to EC2 instances) is to use the AWS Command-Line Interface (CLI), which has a describe-addresses command:
aws ec2 describe-addresses --region ap-southeast-2
To obtain a list of Instance IDs and IP addresses:
$ aws ec2 describe-addresses --region ap-southeast-2 --query 'Addresses[*].[InstanceId,PublicIp]' --output text
i-0c9c9394b3583afdc 54.222.207.37
i-0ef605853622f705e 54.79.149.39
The command would need to be issued for each region separately. (You could create a script that loops through them all.)
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.