thousands-balloon-4359
08/02/2023, 7:41 AM"""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)
touch /etc/environment && S3_BUCKET_ID=Calling __str__ on an Output[T] is not supported.
cuddly-computer-18851
08/02/2023, 7:46 AMthousands-balloon-4359
08/02/2023, 7:55 AMpulumi.Output
cuddly-computer-18851
08/02/2023, 8:13 AM__str__
method on objects, but 🤷thousands-balloon-4359
08/02/2023, 8:14 AMaws: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
?cuddly-computer-18851
08/02/2023, 8:17 AMconfig
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
thousands-balloon-4359
08/02/2023, 8:19 AMpulumi_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?cuddly-computer-18851
08/02/2023, 8:38 AMaws:region
, it'll need to specify that in the provider explicitly instead of relying on inferring it via the configthousands-balloon-4359
08/02/2023, 8:58 AMcuddly-computer-18851
08/02/2023, 9:07 AMregion
with however you want to read in this string
https://www.pulumi.com/registry/packages/aws/api-docs/provider/
so nothing crazy, just one parameterthousands-balloon-4359
08/02/2023, 9:20 AM