pulumi / pulumi-dotnet

.NET support for Pulumi
Apache License 2.0
28 stars 25 forks source link

Functions inside an apply do not execute #139

Closed jaxxstorm closed 1 year ago

jaxxstorm commented 1 year ago

What happened?

I am creating an example that provisions a lambda function and then uses the resolved outputs to run a function that deletes old lambda versions.

The code inside the apply gets swallowed and never executes.

Expected Behavior

I would expect the code inside the apply to run correctly.

Steps to reproduce

See this example: https://github.com/pierskarsenbarg/lee-pulumi-examples/blob/6cfa63026c41c085aa0860cad21853744bf7b32d/dotnet/aws/lambda_delete_old_versions/infrastructure/MyStack.cs#LL96C3-L96C3

Output of pulumi about

N/A

Additional context

No response

Contributing

Vote on this issue by adding a 👍 reaction. To contribute a fix for this issue, leave a comment (and link to your pull request, if you've opened one already).

jaxxstorm commented 1 year ago

The same process works flawlessly in Python:

import pulumi
import pulumi_aws as aws
import json
import boto3

role = aws.iam.Role(
    "delete-lambda-versions",
    assume_role_policy=json.dumps({
        "Version": "2008-10-17",
        "Statement": [{
            "Sid": "",
            "Effect": "Allow",
            "Principal": {
                "Service": "lambda.amazonaws.com"
            },
            "Action": "sts:AssumeRole"
        }]
    }),
    managed_policy_arns=["arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"]
)

func = aws.lambda_.Function("delete-lambda-versions",
    role=role.arn,
    runtime="python3.9",
    handler="hello.handler",
    code=pulumi.AssetArchive({
        '.': pulumi.FileArchive('./hello_lambda')
    }),
    publish=True,
)

def delete_old_lambda_versions(function_name: str):
    client = boto3.client('lambda')

    # List all the versions of the function
    response = client.list_versions_by_function(FunctionName=function_name)

    # Extract the version numbers, excluding the $LATEST
    versions = [x['Version'] for x in response['Versions'] if x['Version'] != '$LATEST']

    # Sort the versions in descending order
    versions.sort(key=int, reverse=True)

    # Exclude the last 3 versions
    versions_to_delete = versions[3:]

    for version in versions_to_delete:
        try:
            client.delete_function(FunctionName=function_name, Qualifier=version)
            pulumi.log.info(f"Deleted version {version}", resource=func)

        except client.exceptions.TypeError:
            pulumi.log.error(f"Deleted version {version}", resource=func)

pulumi.Output.all(func.arn).apply(lambda args: delete_old_lambda_versions(args[0]))
dixler commented 1 year ago

Got internal feedback that I'm going to post here.

I suspect less alarming than it sounds. Most applys must be running otherwise we'd have been seeing test failures all over the place. I suspect this is a race issue for un-awaited applys. Not good, but not scary bad.