Search code examples
pulumi

How to call a function when an Object has been created?


I'm trying out Pulumi and creating an Elasticbeanstalk setup which uses an s3 bucket to store the application version artifacts.

Pulumi complains that the release artifact isn't on the s3 bucket when 'ApplicationVersion' is being created

Updating (testing-stack):

     Type                                        Name                        Status                  Info
     pulumi:pulumi:Stack                         webserver-py-testing-stack  **failed**              1 error
 +   └─ aws:elasticbeanstalk:ApplicationVersion  dev-app-name                **creating failed**     1 error

Diagnostics:
  aws:elasticbeanstalk:ApplicationVersion (dev-app-name):
    error: Plan apply failed: InvalidParameterCombination: Unable to download from S3 location (Bucket: app-name-releases  Key: release2.zip). Reason: Not Found
        status code: 400, request id: e7d4a07e-f55b-4a13-a4cf-fe971982a441

  pulumi:pulumi:Stack (webserver-py-testing-stack):
    error: update failed

Resources:
    4 unchanged

Duration: 6s

pulumi python config

releases_bucket = s3.Bucket(
    resource_name=RELEASES_BUCKET,
    bucket=RELEASES_BUCKET,
)

def upload_release_zip(path, bucket_id):
    s3 = boto3.client('s3')
    s3.upload_file(Filename=str(path), Bucket=bucket_id, Key=path.name)


upload_release_zip = partial(upload_release_zip, path=Path('release2.zip'))

releases_bucket.id.apply(upload_release_zip)

application = Application(resource_name=ENV_APP_NAME, name=ENV_APP_NAME)

repository = ecr.Repository(resource_name=APP_NAME, name=APP_NAME)

app_version = ApplicationVersion(
    resource_name=ENV_APP_NAME,
    application=application,
    bucket=releases_bucket.id,
    key='release2.zip',
)
environment = Environment(
    application=application,
    resource_name=ENV_APP_NAME,
    name=ENV_APP_NAME,
    solution_stack_name=STACK,
    settings=BEANSTALK_ENVIRONMENT_SETTINGS,
    wait_for_ready_timeout=BEANSTALK_ENVIRONMENT_TIMEOUT,
    version=app_version,
)

Note releases_bucket.id.apply(upload_release_zip) in the above, I did that to try and call the upload function before ApplicationVersion happens but it doesn't seem to work. The docs https://pulumi.io/reference/programming-model/#outputs don't seem to give a way for me to say 'call this function after s3 bucket is created'.

Does anyone know how to do that? Otherwise I'll go back to Terraform.


Solution

  • Try defining a BucketObject resource instead of uploading the file manually. Something in line with

    # upload_release_zip removed
    
    obj = s3.BucketObject('release2.zip',
        bucket=releases_bucket.id,
        source=FileAsset('./release2.zip'))
    
    app_version = elasticbeanstalk.ApplicationVersion(
        resource_name=ENV_APP_NAME,
        application=application,
        bucket=releases_bucket.id,
        key=obj.key
    )