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

thousands-balloon-4359

08/02/2023, 3:40 PM
as we were discussing in this thread, i created a custom aws provider and am using that to attempt to create: • s3 bucket • ec2 instance (w/ its parent as the s3 bucket, because i need the bucket.arn inside the ec2 instance) when i use the custom provider, the stack resolution (to generate preview) fails with:
Copy code
NameError: name 'bucket' is not defined
although the bucket create logic is right above it:
Copy code
bucket = aws.s3.Bucket(
    'my-bucket',
    opts=pulumi.ResourceOptions(parent=bucket, provider=aws_provider))

# 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=pulumi.Output.all(bucket_arn=bucket.arn).apply(lambda args: """#!/bin/bash
touch /etc/environment && S3_BUCKET_ARN=%s
""" % f"{args['bucket_arn']}/*"),
    ami=ami,
    tags={"Name": "debo-testing-pulumi"},
    opts=pulumi.ResourceOptions(parent=bucket, provider=aws_provider)
)
any clues on how i can debug this?
full code:
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)

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

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

aws_provider = aws.Provider(
    "regional_provider", 
    region=region
)

# 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,
    opts=pulumi.InvokeOptions(provider=aws_provider),
).id

# Create an AWS resource (S3 Bucket)
bucket = aws.s3.Bucket(
    'my-bucket',
    opts=pulumi.ResourceOptions(parent=bucket, provider=aws_provider))

# 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=pulumi.Output.all(bucket_arn=bucket.arn).apply(lambda args: """#!/bin/bash
touch /etc/environment && S3_BUCKET_ARN=%s
""" % f"{args['bucket_arn']}/*"),
    ami=ami,
    tags={"Name": "debo-testing-pulumi"},
    opts=pulumi.ResourceOptions(parent=bucket, provider=aws_provider)
)

# # Export the name of the bucket
pulumi.export('bucket_name', bucket.id)
i don't think the stackref code is relevant(?) but here's that just in case!
Copy code
import pulumi
import pulumi_aws as aws

# Get some configuration values or set default values.
config = pulumi.Config()
vpc_network_cidr = config.get("vpcNetworkCidr")
aws_config = pulumi.Config("aws")
region = aws_config.get("region")

# Create VPC.
vpc = aws.ec2.Vpc("vpc",
    cidr_block=vpc_network_cidr,
    enable_dns_hostnames=True,
    enable_dns_support=True)

# Create an internet gateway.
gateway = aws.ec2.InternetGateway("gateway", vpc_id=vpc.id)

# Create a subnet that automatically assigns new instances a public IP address.
subnet = aws.ec2.Subnet("subnet",
    vpc_id=vpc.id,
    cidr_block="10.0.1.0/24",
    map_public_ip_on_launch=True)

# Create a route table.
route_table = aws.ec2.RouteTable("routeTable",
    vpc_id=vpc.id,
    routes=[aws.ec2.RouteTableRouteArgs(
        cidr_block="0.0.0.0/0",
        gateway_id=gateway.id,
    )])

# Associate the route table with the public subnet.
route_table_association = aws.ec2.RouteTableAssociation("routeTableAssociation",
    subnet_id=subnet.id,
    route_table_id=route_table.id)

# Create a security group allowing inbound access over port 80 and outbound
# access to anywhere.
sec_group = aws.ec2.SecurityGroup("secGroup",
    description="Enable HTTP access",
    vpc_id=vpc.id,
    ingress=[aws.ec2.SecurityGroupIngressArgs(
        from_port=80,
        to_port=80,
        protocol="tcp",
        cidr_blocks=["0.0.0.0/0"],
    )],
    egress=[aws.ec2.SecurityGroupEgressArgs(
        from_port=0,
        to_port=0,
        protocol="-1",
        cidr_blocks=["0.0.0.0/0"],
    )])

# Export network stack info
pulumi.export("region", region)
pulumi.export("subnet_id", subnet.id)
pulumi.export("vpc_security_group_ids", [sec_group.id])
nvm! i have a bad typo in there that i just noticed after 2h of tinkering around..
parent=bucket
in there shouldn't be there
Copy code
bucket = aws.s3.Bucket(
    'my-bucket',
    opts=pulumi.ResourceOptions(parent=bucket, provider=aws_provider))
sometime i wish i could use my vscode debugger to step thru the pulumi code
4 Views