def setUpClass(cls) -> None: cls.STACK_NAME = 'staging' cls.REGION_NAME = 'eu-north-1' cls.WORK_DIR = os.path.join(os.path.dirname(__file__)) cls.FILE_NAME = 'bucket.txt' cls.stack = auto.create_or_select_stack(stack_name=cls.STACK_NAME, work_dir=cls.WORK_DIR) cls.stack.set_config("aws:region", auto.ConfigValue(value=cls.REGION_NAME)) cls.stack.up(on_output=print) cls.outputs = cls.stack.outputs() cls.s3 = boto3.resource('s3')
def handler(event, context): base64_bytes = event['body'].encode('ascii') message_bytes = base64.b64decode(base64_bytes) message = message_bytes.decode('ascii') json_event = json.loads(message) bucket_name = json_event["bucket"] site_title = json_event["title"] site_body = json_event["body"] project_name = "website-builder" # We use a simple stack name here, but recommend using auto.fully_qualified_stack_name for maximum specificity. stack_name = auto.fully_qualified_stack_name("leezen", project_name, bucket_name) def pulumi_program(): create_static_website(bucket_name, site_title, site_body) # create or select a stack matching the specified name and project. # this will set up a workspace with everything necessary to run our inline program (pulumi_program) stack = auto.create_or_select_stack( stack_name=stack_name, project_name=project_name, program=pulumi_program, opts=LocalWorkspaceOptions(pulumi_home="/tmp/pulumi_home")) print("successfully initialized stack") # for inline programs, we must manage plugins ourselves print("installing plugins...") stack.workspace.install_plugin("aws", "v3.26.1") print("plugins installed") # set stack configuration specifying the AWS region to deploy print("setting up config") stack.set_config("aws:region", auto.ConfigValue(value="us-west-2")) print("config set") print("updating stack...") up_res = stack.up(on_output=print) print( f"update summary: \n{json.dumps(up_res.summary.resource_changes, indent=4)}" ) print(f"website url: {up_res.outputs['website_url'].value}") return { 'statusCode': 200, 'body': json.dumps({'url': up_res.outputs['website_url'].value}), }
def create_handler(): """creates new sites""" stack_name = request.json.get('id') content = request.json.get('content') try: def pulumi_program(): return create_pulumi_program(content) # create a new stack, generating our pulumi program on the fly from the POST body stack = auto.create_stack(stack_name=stack_name, project_name=project_name, program=pulumi_program) stack.set_config("aws:region", auto.ConfigValue("us-west-2")) # deploy the stack, tailing the logs to stdout up_res = stack.up(on_output=print) return jsonify(id=stack_name, url=up_res.outputs['website_url'].value) except auto.StackAlreadyExistsError: return make_response(f"stack '{stack_name}' already exists", 409) except Exception as exn: return make_response(str(exn), 500)
def update_handler(id: str): stack_name = id content = request.data.get('content') try: def pulumi_program(): create_pulumi_program(content) stack = auto.select_stack(stack_name=stack_name, project_name=project_name, program=pulumi_program) stack.set_config("aws:region", auto.ConfigValue("us-west-2")) # deploy the stack, tailing the logs to stdout up_res = stack.up(on_output=print) return jsonify(id=stack_name, url=up_res.outputs["website_url"].value) except auto.StackNotFoundError: return make_response(f"stack '{stack_name}' does not exist", 404) except auto.ConcurrentUpdateError: return make_response( f"stack '{stack_name}' already has update in progress", 409) except Exception as exn: return make_response(str(exn), 500)
# To destroy our program, we can run python main.py destroy destroy = False args = sys.argv[1:] if len(args) > 0: if args[0] == "destroy": destroy = True stack_name = "dev" work_dir = os.path.join(os.path.dirname(__file__), "..", "fargate") # Create our stack using a local program in the ../fargate directory stack = auto.create_or_select_stack(stack_name="dev", work_dir=work_dir) print("successfully initialized stack") print("setting up config") stack.set_config("aws:region", auto.ConfigValue(value="us-west-2")) print("config set") print("refreshing stack") stack.refresh(on_output=print) print("refresh complete") if destroy: print("destroying stack...") stack.destroy(on_output=print) print("stack destroy complete") sys.exit() print("updating stack...") up_res = stack.up(on_output=print) print(f"update summary: \n{json.dumps(up_res.summary.resource_changes, indent=4)}")
check=True, cwd=work_dir, capture_output=True) subprocess.run( [os.path.join("venv", "bin", "pip"), "install", "-r", "requirements.txt"], check=True, cwd=work_dir, capture_output=True) print("virtual environment is ready!") # Create our stack using a local program in the ../aws-py-voting-app directory stack = auto.create_or_select_stack(stack_name="dev", work_dir=work_dir) print("successfully initialized stack") print("setting up config") stack.set_config("aws:region", auto.ConfigValue(value="us-west-2")) stack.set_config("voting-app:redis-password", auto.ConfigValue(value="my_password", secret=True)) print("config set") print("refreshing stack") stack.refresh(on_output=print) print("refresh complete") if destroy: print("destroying stack...") stack.destroy(on_output=print) print("stack destroy complete") sys.exit() print("updating stack...")