a

    agreeable-window-77899

    2 months ago
    Hello folks, I need some advice on the right way to use Pulumi
    StackReference
    My scenario is as follows:1. I have a base infrastructure project where I have created some common AWS resources like VPC, RDS, Fargate cluster etc. Resource creation is working fine in this base project. The fargate cluster is created like this
    // Create new fargate cluster for running containers
    export const fargateCluster = new awsx.ecs.Cluster(
      `foo-bar`,
      {
        vpc: vpc,
        securityGroups: [fargateSecurityGroup],
      }
    );
    2. Now i want to import this Fargate cluster in another Pulumi project so that I can create new Fargate services and task definitions on top this common cluster. I'm doing this to accomplish that
    // Get resources from infra-base
    const baseInfra = new pulumi.StackReference(
      `blah/blah/blah`
    );
    
    // Get fargate cluster from base infra
    const fargateCluster = baseInfra.getOutput("fargateCluster");
    
    // Define the fargate service and task definitions
    const service = new awsx.ecs.FargateService(`blah blah`, {
      cluster: (fargateCluster as unknown as awsx.ecs.Cluster) || undefined,
      name: `blah blah`,
      desiredCount: 1,
      taskDefinitionArgs: { ... },
      },
    });
    When i try to do
    pulumi up
    I get this error
    TypeError: this.cluster.autoScalingGroups.map is not a function
            at new Service (/Users/amal/rasa/tooling/infra-app/node_modules/@pulumi/ecs/service.ts:60:55)
            at new FargateService (/Users/amal/rasa/tooling/infra-app/node_modules/@pulumi/ecs/fargateService.ts:216:9)
            at Object.<anonymous> (/Users/amal/rasa/tooling/infra-app/index.ts:103:17)
            at Module._compile (node:internal/modules/cjs/loader:1103:14)
            at Module.m._compile (/Users/amal/rasa/tooling/infra-app/node_modules/ts-node/src/index.ts:439:23)
            at Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
            at Object.require.extensions.<computed> [as .ts] (/Users/amal/rasa/tooling/infra-app/node_modules/ts-node/src/index.ts:442:12)
            at Module.load (node:internal/modules/cjs/loader:981:32)
            at Function.Module._load (node:internal/modules/cjs/loader:822:12)
            at Module.require (node:internal/modules/cjs/loader:1005:19)
    What is the right way to do this? Any help is much appreciated.
    a

    ancient-car-89914

    2 months ago
    you're exporting the whole cluster object, which won't work because stack outputs are only primitives like strings - but luckily the FargateService constructor's cluster argument is only supported to be the arn, which is a string
    export const cluster = fargateCluster.arn
    should work
    a

    agreeable-window-77899

    2 months ago
    Hi @ancient-car-89914 Thanks for your response. The
    cluster
    argument which is part of the
    awsx.ecs.FargateService
    is looking for a value of type
    awsx.ecs.Cluster
    and thus the
    arn
    value is not being accepted. I guess your suggestion will work with the classic aws api
    aws.ecs.Service
    since it is only looking for
    string
    for the
    cluster
    argument.
    a

    ancient-car-89914

    2 months ago
    ah, my mistake - I haven't yet worked with the native providers, I guess you would need to get the cluster by its arn somehow
    a

    agreeable-window-77899

    2 months ago
    Unfortunately i could not find any method in
    awsx.ecs
    to fetch the cluster which returns the type of
    awsx.ecs.Cluster
    😢 Will probably have to create the service using
    aws.ecs.Service
    i guess
    c

    clever-sunset-76585

    2 months ago
    Just as @ancient-car-89914 said above, you cannot export the whole cluster object.You could export its
    name
    instead. I am assuming you are using AWSX
    0.x
    ? In that, the
    awsx.ecs.Cluster
    accepts a
    cluster
    arg which can either be an
    aws.ecs.Cluster
    or an
    Input<string>
    (the existing cluster's name): https://github.com/pulumi/pulumi-awsx/blob/066dcd47e78198a9a56ea36a38b4615ac50c747e/awsx-classic/ecs/cluster.ts#L176. So use the
    name
    from your other stack to "re-create" the Cluster resource. Note that when you do this, you are essentially bringing the cluster resource into this stack's state. This is because AWSX does a
    .get()
    when you pass the name thereby bringing the cluster into the state. So it's sort of undoing the separation that you are likely looking to have.
    Also, if you are using AWSX
    1.x-beta
    which is based on the native AWS provider. There it seems the
    FargateService
    itself has a
    cluster
    arg that simply seems to accept a string value which I am assuming is either the name or the ARN of the cluster. https://github.com/pulumi/pulumi-awsx/blob/master/awsx/schema-types.ts#L205
    a

    agreeable-window-77899

    2 months ago
    Thanks for the input @clever-sunset-76585 It will give these a shot and see if it solves my issue
    I tried with the
    1.x-beta
    package but found out that the API have changed significantly there and it was breaking a lot of other stuff. So I did not go with that option. Thus I finally created a
    default VPC
    and let the
    awsx.ecs.FargateService
    create a
    default cluster
    on the default VPC so that the cluster is auto created and i don't have to manage that separately and pass around the value for deployments.
    Update: Pulumi was creating new fargate
    default cluster
    per stack. Thus I finally ended up with create a new Fargate cluster per stack and decided not to share a single cluster across stacks.