winglang / wing

A programming language for the cloud ☁️ A unified programming model, combining infrastructure and runtime code into one language ⚡
https://winglang.io
Other
5.05k stars 198 forks source link

Referencing API url in other handlers will cause cyclic dependency #4906

Closed skorfmann closed 8 months ago

skorfmann commented 1 year ago

I tried this:

For a scenario like the following

bring cloud;
bring http;

let api = new cloud.Api();
let queue = new cloud.Queue();

api.get("/api/v1/xxx", inflight (req) => {    
    return {
        body: "hello world",
        status: 200
    };
});

api.get("/api/v2/xxx", inflight (req) => {    
    return {
        body: "hello world",
        status: 200
    };
});

queue.setConsumer(inflight (msg) => {
    http.get("${api.url}/api/v2/xxx");
});

see in playground

the preflight value of the api gets referenced in the inflight consumer of the queue. This is slimmed down example extracted from a real application.

This happened:

Trying to remove one the handlers causes issues for the tf-aws target, for an iterative deployment with existing state. Terraform generates a cyclic dependency as described here https://github.com/hashicorp/terraform-provider-aws/issues/11344#issuecomment-1521831630

I expected this:

Being able to deploy the api without manual interaction

Is there a workaround?

see https://github.com/hashicorp/terraform-provider-aws/issues/11344#issuecomment-1521831630

Component

SDK

Wing Version

0.45

Node.js Version

18.17

Platform(s)

MacOS

Anything else?

The value lifting for inflight handlers is done via env, e.g. for the queue consumer above that's like

  "environment": {
    "variables": {
      "WING_FUNCTION_NAME": "cloud-Queue-SetConsumer-3fc9280c-c8180aab",
      "WING_TARGET": "tf-aws",
      "WING_TOKEN_HTTPS_TFTOKEN_TOKEN_8_EXECUTE_API_TFTOKEN_TOKEN_1_AMAZONAWS_COM_TFTOKEN_TOKEN_9": "${jsonencode(\"https://${aws_api_gateway_rest_api.cloudApi_api_2B334D75.id}.execute-api.${data.aws_region.Region.name}.amazonaws.com/${aws_api_gateway_stage.cloudApi_api_stage_BBB283E4.stage_name}\")}"
    }
  },

And this beautifully named env WING_TOKEN_HTTPS_TFTOKEN_TOKEN_8_EXECUTE_API_TFTOKEN_TOKEN_1_AMAZONAWS_COM_TFTOKEN_TOKEN_9 is the offender in this case.

Community Notes

skorfmann commented 1 year ago

On thing I thought about, perhaps it would be worthwhile thinking about other ways of value lifting. Something based on SSM parameters for example. Values could be written to an SSM key path based on some convention. When these values are only read at the inflight stage, this would effectively decouple the resources in the preflight stage.

As a first step, doing this manually would be good enough already. Perhaps this could helpful here as well https://github.com/winglang/wing/issues/2726

Chriscbr commented 1 year ago

Was this fixed by https://github.com/winglang/wing/pull/4879? The example seems to compile in the playground now

skorfmann commented 1 year ago

No, not really. It's essentially this case described over here https://github.com/hashicorp/terraform-provider-aws/issues/11344#issuecomment-1521831630

Just built a simple Parameter (along the lines of https://github.com/winglang/wing/issues/2726) resource internally to decouple the API url from other consumers, which then really fixes it

ekeren commented 1 year ago

On thing I thought about, perhaps it would be worthwhile thinking about other ways of value lifting. Something based on SSM parameters for example. Values could be written to an SSM key path based on some convention. When these values are only read at the inflight stage, this would effectively decouple the resources in the preflight stage.

As a first step, doing this manually would be good enough already. Perhaps this could helpful here as well https://github.com/winglang/wing/issues/2726

@skorfmann this is only relevant for tokens, right?

skorfmann commented 1 year ago

This particular issue is only relevant for terraform dependencies, no direct relation to tokens, cdktf or wing.

github-actions[bot] commented 10 months ago

Hi,

This issue hasn't seen activity in 60 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days. Feel free to re-open this issue when there's an update or relevant information to be added. Thanks!

Chriscbr commented 8 months ago

Closing as I believe this was fixed by https://github.com/winglang/wing/pull/5637. Please feel free to re-open if I'm mistaken.