Somehow, I had to get second eth1 private ip address of ec2 instances using python3. I can get only ec2 instance-id. I managed to do it,but it is not good code I think.
I tried filters but failed. Instead I use like below.
ec2 = boto3.client('ec2')
def get_sec_ip(instance_id):
res=ec2.describe_instances(InstanceIds=[instance_id])
print(res['Reservations'][0]['Instances'][0]['NetworkInterfaces'][1]['PrivateIpAddress'])```
instance_id = 'i-03a0d15992b7bf'
get_sec_ip(instance_id)
Is there anybody who did this result using ec2.describe_instances' filters?
An other possibility is to use Instance object:
ec2 = boto3.resource('ec2')
instance = ec2.Instance(instance_id)
print(instance.network_interfaces[1].private_ip_address)
The filter is not used for limiting output, but limiting number of instance descriptions return. For example, the describe_instances can return 10 instances (not in your case, but in general), and Filters can limit it to only 2 based on some criteria, e.g. instances only in a given subnet.
Related
I want to build a 3 tier VPC using only AZ1a and AZ1b. But I want to have a range of IP addresses and use them for each tier but need them to always fall in AZ1a or AZ1b.
I have 3 app tiers (web, app, db):
Using only two AZs.
availability_zones = ["us-east-1a", "us-east-1b"]
I have put together a list of the subnets I want to use. SoI want web to grab the first two and put into each of the AZs, then I want app to grab the next two and put into each of the AZs, and so on on.
private_subnets_cidr_intapp = ["10.211.130.0/28", "10.211.130.16/28", "10.211.130.32", "10.211.130.48", "10.211.130.64/28", "10.211.130.80/28"]
How do you go about this, and an example would help. Should I be using a map?
I am new in terraform, I have problem while creating ec2 instance, my condition is, I am passing private_Ips values from variable file (like IP pool) and creating two ec2 instance so when I run terraform apply first time it creates two ec2 instances, but when second time I run terraform apply it says IP already in use, its not going to take 3rd and 4th IP. I want on 2nd run it for it to take 3rd and 4th IPs. Below are my definitions. Could you please suggest:
var.tf
variable "private_ips" {
default = {
"0" = "x.x.x.x"
"1" = "x.x.x.x"
"2" = "x.x.x.x"
"3" = "x.x.x.x"
}
}
main.tf
private_ip = "${lookup(var.private_ips,count.index)}"
Terraform is a stateful tool. So, whenever it creates some pieces of infrastructure it keeps track of them (in what is called Terraform state). The whole idea is that if you run terraform apply the first time it creates some infrastructure. And any subsequent run just updates whatever has been created before (with whatever changes were applied in .tf files).
You might want to read up on workspaces whose idea is about using the same configuration (.tf files) against multiple independent copies of target infrastructure. Typically used for dev/test/prod kind of setups. It might be what you are after.
I am wondering if there is a way to pass either a host, fqdn, or IP address to DNSPython and have it perform the proper lookup (forward for hosts and fqdns and reverse for ips). Also, I want to know what kind of address (host, fqdn, or ip) was sent to the method originally.
Thanks in advance
To my knowledge, there's not a single function that will do what you're looking for. However, it wouldn't be too hard to implement. Here's how I'd probably do it.
First, I'd check if the input was an IP address
import ipaddress
def is_ipaddress(string):
try:
ipaddress.ip_address(string)
return True
except ValueError:
return False
If it is an IP address, then I'd call dns.query.reverse_query(). This is assuming I have installed the latest development version of dnspython from Github because reverse_query() was only recently added (see https://github.com/rthalley/dnspython/commit/7c105cce64699e1a221176f98f7cb9e682aba1e0).
If it's not an IP address, then I'd prepare my query with dns.message.make_query(name, rdtype) and then send it with dns.query.udp().
If you wanted to use the value of search in /etc/reolv.conf, you might consider creating a dns.resolver.Resolver, which currently does search processing by default.
import dns.resolver
import dns.rdatatype
resolver = dns.resolver.Resolver()
response = resolver.query('my-computer', dns.rdatatype.A)
I want to get for each instance family and type on AWS their properties and tags. For example how many cores, RAM, bandwidth etc is available at each instance that AWS provides e.g. x2.2xlarge, t1.micro etc. Is there any way I can do this with python?
The closest I have got is to use boto3 like this:
import boto3
ec2 = boto3.resource('ec2')
instance = ec2.Instance('id') #how to change this, brute force?
Any help regarding this problem will be highly appreciated, thanks!
If you want to print all the attributes of a given instance, you can perform a describe call with a filter for that particular instance. Please note this is a 'client' call, not a 'resource' one. Meaning, you will get a JSON output, not an object. It is quite good to get to know all the attributes of an instance.
import boto3
from pprint import pprint
instance_id = 'i-xxxxxxxxx'
ec2_client = boto3.client('ec2')
pprint(ec2_client.describe_instances(Filters=[{'Name': 'instance-id', 'Values': [instance_id]}]))
The documentation has all the properties listed, including the methods, collections, etc.
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#instance
Here is the example for instance id & instance state. you can add more parameters into it.
import boto3
ec2 = boto3.resource('ec2')
for instance in ec2.instances.all():
print (instance.id , instance.state)
Using ServiceStack version 4.0.40.
I am trying get RedisSentinel to use the RedisManagerPool instead of the
PooledRedisClientManager so it will allow clients above the client pool size.
I see this in the docs to set this...
sentinel.RedisManagerFactory = (master,slaves) => new RedisManagerPool(master);
I'm not sure how to use this. Do I pass in the master host name? What if I don't know which is master because of a previous failover? I can't sentinel.start() to find out which is master because it will start with the PooledRedisClientManager, which isn't what I want.
Or, do I pass in the sentinel hosts? RedisManagerPool takes a list of hosts, I can pass in the sentinel hosts, but I cannot set it to sentinel.RedisManagerFactory as RedisManagerFactory is not convertible to RedisManagerPool.
I think I am missing something simple here. Any help appreciated.
UPDATE
As per mythz's comment below, this isn't available in version 4.0.40 of ServiceStack. But you can use;
sential.RedisManagerFactory.FactoryFn = (master, slaves) => new RedisManagerPool(master);
Thanks
This is literally the config you need to use to change RedisSentinel to use RedisManagerPool:
sentinel.RedisManagerFactory = (master,slaves) =>
new RedisManagerPool(master);
You don’t need to pass anything else, the master host argument uses the lambda argument.