sst / ion

SST v3
https://sst.dev
MIT License
1.84k stars 220 forks source link

$transform not applying to created functions #189

Closed trinitrotoluene closed 5 months ago

trinitrotoluene commented 5 months ago

cc @thdxr

// async run()
  const envVars = { foo: "bar" }
  const executionRole = new aws.iam.Role("LambdaExecutionRole", {
    assumeRolePolicy: aws.iam.assumeRolePolicyForPrincipal({
      Service: "lambda.amazonaws.com",
    }),
    managedPolicyArns: [
      "arn:aws:iam::aws:policy/service-role/AWSLambdaSQSQueueExecutionRole",
    ],
  });

  const backgroundQueue = new sst.aws.Queue("SomeQueue", {
    fifo: true,
    transform: {
      queue: (args) => {
        args.visibilityTimeoutSeconds = 300;
      },
    },
  });

  const secrets = [
    new sst.Secret("Secret1"),
    new sst.Secret("Secret2"),
  ];

  $transform(sst.aws.Function, (args) => {
    args.runtime = "nodejs18.x";
    args.role = executionRole.arn;
    args.environment = {
      ...envVars,
      BACKGROUND_QUEUE_URL: backgroundQueue.url,
    };
    args.link = secrets;
  });

  backgroundQueue.subscribe({
    handler: "src/handlers/index.handler",
    timeout: "5 minutes",
  });

  const someCron = new sst.aws.Cron("SomeCron", {
    schedule: "some schedule",
    job: {
      handler: "src/jobs/index.handler",
      timeout: "30 seconds",
    },
  });

If I stick a console.log in the $transform handler I can see it get invoked, but there is no effect on any of the created functions. The secrets are not linked, nothing other than the default environment variables are set on the created lambda, and the runtime/role settings aren't respected either.

sst version is 0.0.270

trinitrotoluene commented 5 months ago
    $transform(sst.aws.Function, (args) => {
      // @ts-ignore
      console.log("$transform", sst.aws.Function.__pulumiType);

results in the below - so it definitely does get registered correctly 👍

image

I've tried both mutating args and returning new args from the callback - same result.

trinitrotoluene commented 5 months ago

I had a crack at debugging the resource creation process to see where it's losing the information and it looks like it's here:

thdxr commented 5 months ago

talking to the pulumi team about this - thanks for finding it! a work around for now is to $transform non-components so the raw underlying resources eg aws.lambda.Function - that should still work

unfortunately it won't work with link but we can probably fix our components to work with $transform - just have to get guidance from the pulumi team first

thdxr commented 5 months ago

i made a temporary fix that should be in v0.0.281