https://pulumi.com logo
#general
Title
# general
t

thousands-balloon-4359

08/02/2023, 7:41 AM
hello there, i'm trying to do the following: • create an s3 bucket • create an ec2 instance w/ s3 bucket metadata stored in user-data what I think i'm seeing: bucket and ec2 instances are getting created in parallel when i actually want the bucket to be created first, then the ec2 instance any thoughts on how i can achieve this?
relevant code that i have:
Copy code
"""An AWS Python Pulumi program"""

import os

import boto3
import pulumi
import pulumi_aws as aws
from pulumi import StackReference

def wait_for_instance_running(instance_id, region):
    ec2 = boto3.client('ec2', region=region) 
    
    <http://pulumi.log.info|pulumi.log.info>(
        msg="Waiting for Instance to pass healthchecks",
        ephemeral=True,
    )

    waiter = ec2.get_waiter('instance_status_ok')
    waiter.wait(InstanceIds=[instance_id])
    
    <http://pulumi.log.info|pulumi.log.info>(msg="Instance is running and passed healthchecks", ephemeral=True)

def wait_for_s3_created(bucket_id, region):
    s3 = boto3.client('s3', region=region) 
    
    <http://pulumi.log.info|pulumi.log.info>(
        msg="Waiting for bucket to be ready",
        ephemeral=True,
    )

    # <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3/waiter/BucketExists.html>
    waiter = s3.get_waiter('bucket_exists')
    waiter.wait(Bucket=bucket_id)
    
    <http://pulumi.log.info|pulumi.log.info>(msg="Bucket has been created", ephemeral=True)


config = pulumi.Config()
instance_type = config.get("instanceType")

base_stack = StackReference(f"net-cfg")
region = base_stack.get_output("region")

# TODO: need a way to reference aws:region from a diff stack and use it in this stack
# currently forced to do this manually using CLU before calling `pulumi up`

# Look up the latest Amazon Linux 2 AMI.
ami = aws.ec2.get_ami(
    filters=[aws.ec2.GetAmiFilterArgs(
        name="name", values=["amzn2-ami-hvm-*"],
    )],
    owners=["amazon"], most_recent=True,
).id

# Create an AWS resource (S3 Bucket)
bucket = aws.s3.Bucket('my-bucket')

# try to make the thing wait till bucket ID is ready
bucket.id.apply(lambda bucket_id: wait_for_s3_created(bucket_id))

user_data = """#!/bin/bash
echo "Hello, World from Pulumi!" > index.html
touch /etc/environment && S3_BUCKET_ID=%s
nohup python -m SimpleHTTPServer 80 &
""" % str(bucket.id) # doesn't work

# Create and launch an EC2 instance into the public subnet.
server = aws.ec2.Instance("server",
    instance_type=instance_type,
    subnet_id=base_stack.get_output("subnet_id"),
    vpc_security_group_ids=base_stack.get_output("vpc_security_group_ids"),
    user_data=user_data,
    ami=ami,
    tags={"Name": "debo-testing-pulumi"},
)

# # Export the name of the bucket
pulumi.export('bucket_name', bucket.id)
i am seeing the following in the user-data:
Copy code
touch /etc/environment && S3_BUCKET_ID=Calling __str__ on an Output[T] is not supported.
c

cuddly-computer-18851

08/02/2023, 7:46 AM
You need to handle the Output like the Bucket arn example here: https://www.pulumi.com/docs/concepts/inputs-outputs/#outputs-and-json
t

thousands-balloon-4359

08/02/2023, 7:55 AM
testing
thanks, @cuddly-computer-18851 - that example helped me codify my understanding of the pulumi lifecycle basically, if you want to create a string based on previous stack outputs, use
pulumi.Output
c

cuddly-computer-18851

08/02/2023, 8:13 AM
It's one of my pet peeves w/ pulumi 😃
I would prefer if the Python implementation should do this by default via the
__str__
method on objects, but 🤷
t

thousands-balloon-4359

08/02/2023, 8:14 AM
another comment i had in that thread was for the following: • i have one stack (stack_1) that has config w/
aws:region: us-east-1
• i have a diff stack (stack_2) that takes a dep on that stack using stackref ◦ this stack does not have
aws:region
in its config • when i create aws objects in
stack_2
, how can i dynamically read
aws:region
thru the stack ref dep on
stack_1
?
c

cuddly-computer-18851

08/02/2023, 8:17 AM
Good question - I am not sure of the best pulumi-way to read the
config
of a different stack. Obviously you can just use
pyyaml
to read in the actual config file, or include that variable as a stack
output
t

thousands-balloon-4359

08/02/2023, 8:19 AM
so even tho i exported that variable as output, when the
pulumi_aws
module loads, it seems like its unable to find the
region
config are you suggesting i export the whole `aws_provider`object or the configuration?
c

cuddly-computer-18851

08/02/2023, 8:38 AM
well if Stack B needs Stack A's
aws:region
, it'll need to specify that in the provider explicitly instead of relying on inferring it via the config
t

thousands-balloon-4359

08/02/2023, 8:58 AM
gotcha gotcha
sounds like i gotta build that provider from scratch for this one
which makes total sense
i think i'm trying to do something outside of the super-blessed path
c

cuddly-computer-18851

08/02/2023, 9:07 AM
just specify
region
with however you want to read in this string https://www.pulumi.com/registry/packages/aws/api-docs/provider/ so nothing crazy, just one parameter
t

thousands-balloon-4359

08/02/2023, 9:20 AM
yup yup
ty!