I am running my application in VPN having a public ELB over 4 nodes, I want to know the ipAddress of all nodes behind that ELB.
You Can Create a CLI Script.
Using the AWS CLI -
Pass the ELB name to describe the LoadBalancer.
aws elb describe-load-balancers --load-balancer-name sarat-load-balancer
It returns a set of instance id running behind the load balancer in Json Format.
Extract the Instance ids and Put them in a Loop.
Pass the Instance ids to describe the Instance.
aws ec2 describe-instances --instance-ids i-xxxxxxxxxx
It returns a set of data with public ips of the instances in Json Format.
You can do the same with SDK.
Hope It Helps.. :)
Related
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
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.)
I have deployed a webservice application in Amazon EC2 and has associated an Elastic IP address with the same. Our mobile interact with this webservice using elastic IP. Now I want to implemented auto scaling on the EC2.
But what I am not sure is how does my single elastic ip be associated with multiple EC2 instances as it scales up? Is this possible. Please guide.
An elastic IP address is only ever associated with a single EC2 instance.
If you want to start auto-scaling your application, then you need to put a load balancer in front of your EC2 instances. That can be AWS Elastic Load Balancer, or some other.
Users would connect to the Load Balancer, and the Load Balancer would forward requests to the underlying EC2 instances.
Assuming you use an Elastic Load Balancer, you'll need to drop the Elastic IP address since ELB cannot use them. Instead, you'll create a CNAME (or Alias if your DNS is using Route 53) to the ELB.
No that's not possible. You probably need to be using an Elastic Load Balancer.
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 :)
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 want to get the ELB name from inside the EC2 instance.
I can do a aws elb describe-load-balancers but this needs the ELB name as input.
I want do it reverse - I know the EC2 instance details from EC2 metadata and want to find the ELB name so that I can append to a file.
The command aws elb describe-load-balancers doesn't need the elb name. It will list all the elbs you have. Then you can parse the output JSON and get which instances are attached to every ELB. I don't know if this is your case, but you can find which elb your instance is attached, and then grab the elb name.
Regards