Struggling to grok the output futures. I want to ...
# general
s
Struggling to grok the output futures. I want to do the following • I'm using python • Create a vpc using awsx • In each public subnet start an EC2 instance ◦ This requires iterating on the (future) values of public_subnet_ids ◦ I got it working by spawning the ec2 instances with a lambda using vpc.public_subnet_ids.apply(....) ▪︎ This is not recommended due to it messing up the resource dependency graph. Which predictably resulted in down not working ◦ All other attempts broadly results in ▪︎ " 'Output' object is not iterable, consider iterating the underlying value inside an 'apply'" So my question is what is the correct way to do this?
e
If you add the subnet as an explicit dependency to the ec2 instances I think down will then work correctly. There are times (iteration being a big one) where you do have to create things inside an apply.
s
As in use depends on?
e
yeh
l
Also, it's best not to iterate on a future value. Rather than have AWSX figure out that there will be two subnets, you'd be better off to define a constant value 2 somewhere, and use that to set up both the subnets (as a parameter to the VPC constructor) and the EC2 instances.
s
@little-cartoon-10569 I'm not sure that makes a difference if you need to iterate on the future subnet ids.
Sub-question (perhaps for @echoing-dinner-19531). Is there a way to synchronise after resources are created inside an apply. Or at any point really. Use-case: • Iterate over each region • Create VPC in each region • Create SG in each region • Iterate over subnets and launch EC2 Instances • Add IP addresses of instances to SG of other region At this point I'm trying to do it during the Instance.public_ip_address.apply, but in this case I find that the SG in other regions hasn't been created.
I'm trying to re-work it (without success) in the following wayMake the SG in 1 region dependent on the VPC of another region. However it keeps saying the vpc object is not a resource. nvm. Saw my mistake
So in the end I got it working by successfully making the SG in 1 region dependent on the VPC of another region.
e
Yeh depends_on, or linking outputs from those things into the output you apply on will order things
l
@swift-crayon-9802 It can make the difference. It allows you to unconditionally construct the objects, and have all the calls to
apply()
etc. be as parameters to the constructor, in the normal way. Moving the future-knowledge from outside the constructor to a parameter like that is the goal.