EC2: get instance id from within the instance itself [duplicate] - amazon-ec2

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Find out the instance id from within an ec2 machine
I am looking for the simplest way to find the id of an instance from within this instance itself.
I looked at the shell variables: no luck.
How can I get it ?
My only solution right now is to write to the java api (1) to describe instances and (2) to finding the one matching ip address or dns and get its id. Anything simpler ?
thanks
didier

You can use the Instance Metadata API to retrieve the instance ID. Simply make a HTTP GET request to http://169.254.169.254/latest/meta-data/instance-id from within the instance.

Related

Automatically saving a document from a link to a specific folder in Windows [duplicate]

This question already has answers here:
Windows batch file file download from a URL
(20 answers)
Closed 1 year ago.
First I apologize if this is not a good place to ask this question.
So I will specify the problem:
Firm A is sending me an HTTP link that holds an excel document with price changes from the competition which I have to save to a specific folder so our Software can implement the same document into our database. Basically what we get is our current price and price of 5-8 of our competitors that are stored into the database and can be seen for each product in the database.
My question is it possible to automate saving a file from a HTTP link to a specific folder and do it few times a day so our software can load it n amount of times. Also since the name of the file is the same it needs to overwrite each time it saves the document.
The software works in a way that it is able to import excel files and update database fields properly. API in any way is not possible.
Everything needs to be done in Windows 10, for what i need to use i'm open to suggestions.
I thought about FTP access but since is outside our local network im not qualified to be absolutely sure that the connection is safe.
Thanks in advance.
So what i did is use powershell:
Invoke-WebRequest -Uri "HTTP LINK" -OutFile "WhereToPlaceit\nameofthe.file"
And run task scheduler, more info here: https://community.spiceworks.com/how_to/17736-run-powershell-scripts-from-task-scheduler
Place it on the server, and the command repeats itself each hour overwriting the existing file.

Access playbook variables from custom module in Ansible [duplicate]

This question already has answers here:
Ansible: Access host/group vars from within custom module
(3 answers)
Closed 7 years ago.
I'm writing a custom module in Ansible, specific to a Playbook. Is it possible to directly access a playbook variable, without needing to pass it to the task as a parameter?
It is not possible, because the module is executed remotely and all the variables are not available unless explicitly passed.
I had the same question a while ago and Pruce P offered an interesting workaround in his answer.
Though I had another idea in the meantime, but this is only theoretical and never tested: Beside normal modules, Ansible has a special kind of module: action plugins. (...not documented) They are used exactly like modules but are executed locally. Since they run locally they do have access to Ansibles runner object which holds all the group/host vars etc.
The cool thing is, you can call a module programmatically from the action plugin. So you could have a small wrapper action plugin which then calls the actual module and passes all (required) vars to it. Since it is undocumented you need to look at the available plugins. For instance here is one which calls a module: assemble.
I have written something here which interacts with vars from the runner object.

Ansible and AWS EC2 inventory

I'd like to get EC2 instances metadata with Ansible and do something with those instances based on the metadata. However, ec2_facts wants to SSH into instances in order to get the metadata.
I believe it should be possible to obtain the instances metadata without SSH connections.
Could you help me with that please?
Thank you.
There is information you can retrieve about instances using the aws API but ec_facts does not use it. What that Ansible module does specifically is fetch metadata via http://169.254.169.254/latest/meta-data/ which can only be done from the instance itself.
Some more information about what instance data you wish to fetch would be helpful to know. At this time there is no aws cloud module in core that will retrieve general information about an instance but Ansible makes it easy to write one.
Here is an example of a module that returns information about instances that match a set of tags - https://github.com/edx/configuration/blob/master/playbooks/library/ec2_lookup

EC2 init.d script - what's the best practice

I'm creating an init.d script that will run a couple of tasks when the instance starts up.
it will create a new volume with our code repository and mount it if it doesn't exist already.
it will tag the instance
The tasks above being complete will be crucial for our site (i.e. without the code repository mounted the site won't work). How can I make sure that the server doesn't end up being publicly visible? Should I start my init.d script with de-registering the instance from the ELB (I'm not even sure if it will be registered at that point), and then register it again when all the tasks finished successfully?
What is the best practice?
Thanks!
You should have a health check on your ELB. So your server shouldn't get in unless it reports as happy. And it shouldn't report happy if the boot script errors out.
(Also, you should look into using cloud-init. That way you can change the boot script without making a new AMI.)
I suggest you use CloudFormation instead. You can bring up a full stack of your system by representing it in a JSON format template.
For example, you can create an autoscale group that has an instances with unique tags and the instances have another volume attached (which presumably has your code)
Here's a sample JSON template attaching an EBS volume to an instance:
https://s3.amazonaws.com/cloudformation-templates-us-east-1/EC2WithEBSSample.template
And here many other JSON templates that you can use for your guidance and deploy your specific Stack and Application.
http://aws.amazon.com/cloudformation/aws-cloudformation-templates/
Of course you can accomplish the same using init.d script or using the rc.local file in your instance but I believe CloudFormation is a cleaner solution from the outside (not inside your instance)
You can also write your own script that brings up your stack from the outside by why reinvent the wheel.
Hope this helps.

Is there anyway to get the user data of a running EC2 instance via AWS SDK?

I've tried to use DescribeInstances but I've found the response result does not contain user data. Is there any way to retrieve this user data?
My usage case is I've trying to request spot instances and assign different user data to each EC2 instance for some kind of automation and then I want to tag the name of each instance according to this user data. Based on my understanding, creating a tag request requires InstanceId, which is not available at the time when I make a request to reserve a spot instance.
So I'm wondering whether there is any way to get the user data of a running instance instead of SSHing the instance...
The DescribeInstanceAttributes endpoint will provide you with user data.
http://docs.aws.amazon.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeInstanceAttribute.html

Resources