Hello… does anyone know regarding case below. I wa...
# general
a
Hello… does anyone know regarding case below. I want to run resource based on that stack, but I just want to write in one repo. So I only declare the import module inside the condition. are there any docs/blog/vlog about this?, Thank you.
Copy code
if (stackName === "development") {
    import "vpc-development"
} else if (stackName === "staging") {
    import vpc-staging
}
s
What is the difference between your dev and staging VPC? Rather than importing a module conditionally, I would consider whether it might be better to config values to each stack and call the same VPC module with different params. https://www.pulumi.com/docs/intro/concepts/config/#setting-and-getting-configuration-values
a
Thanks @stocky-restaurant-98004, the difference is in the staging vpc has connection peering with other vpc and in dev is not, So I have to type different code and using decision flow in code.
s
Could you separate the VPC peering from the rest of the VPC resources, and then do the following:
Copy code
const createPeering = config.requireBoolean("vpc-peering");
if (createPeering) {
 // etc.
a
okay, thank you for your help @stocky-restaurant-98004. I’ll try first
oh yeah, I have one problem when implement that way in create ec2 case. I created code like this
Copy code
const production = config.requireBoolean("environment-production");
const development = config.requireBoolean("environment-development");
if (production) {
const serverProd = new aws.ec2.instance("serverProd", {
    ...
}) 

export const serverIP = serverProd.ip (is not working)
}

if (development) {
const serverDev = new aws.ec2.instance("serverDev", {
    ...
}) 
export const serverIP = serverDev.ip (is not working)
}
I can’t sent output of ip public/private from the server, because the export can’t working inside the condition. How I can export the ip to another file/resource? thank you
s
Rather than creating config params, you can get the stack name programmatically: https://www.pulumi.com/docs/intro/concepts/stack/#getting-the-current-stack-programmatically
If you are creating an EC2 instance in every environment, try varying the args instead, e.g.:
Copy code
const stackName = pulumi.getStack();
const ec2Args = getEc2Args(stackName); // returns e.g. a t2 micro if stackname == dev, m5g.medium in prod (or whatever)

const server = new aws.ec2.instance("server", ec2Args);
a
Okay, thank you for your advice @stocky-restaurant-98004. Really appreciated.🙏
s
You're most welcome! Good luck!