https://pulumi.com logo
#python
Title
# python
a

ancient-yacht-19759

09/02/2020, 9:03 PM
is there somewhere i can find the correct python syntax for creating and selecting AWS resource providers to create resources in multiple regions ?
g

gentle-diamond-70147

09/02/2020, 9:10 PM
a

ancient-yacht-19759

09/02/2020, 9:18 PM
my objective is to create a stack with an s3 bucket in every region with a common prefix. is creating providers fro each region and switching them with `
Copy code
pulumi.ResourceOptions(provider=
a valid method to achieve this ?
i tried just setting region but this fails
getting results like :-
Copy code
error: Error creating S3 bucket: AuthorizationHeaderMalformed: The authorization header is malformed; the region 'us-east-1' is wrong; expecting 'ca-central-1'
g

gentle-diamond-70147

09/02/2020, 9:30 PM
Can you share your full code?
a

ancient-yacht-19759

09/02/2020, 9:31 PM
i can share a subset
from an object that looks like this :-
Copy code
{
     'mc-static-files': {'bucket': 'mc-static-files',
                              'versioning': {'enabled': True}},
     'atl-tableau': {'bucket': 'atl-tableau'},
     'atl-tableau-backups': {'bucket': 'atl-tableau-backups'},
     'backmac-lambda': {'bucket': 'backmac-lambda'},
     'myteam-ansible-cfn-templates': {'acl': 'private',
                                      'bucket': 'myteam-ansible-cfn-templates'},
}
i do:
Copy code
# Create AWS resources (S3 Bucket)
for bucket_name, bucket_options in allbucket_dict.items():
    bucket = s3.Bucket(bucket_name, **bucket_options, opts=pulumi.ResourceOptions(provider=aws[bucket_options['region']]))
well thats what im tryin gto do now... before i wasnt passing opt
i will write it up with a sanitised subset and present back here shortly
g

gentle-diamond-70147

09/02/2020, 9:46 PM
Here's a working example:
Copy code
"""An AWS Python Pulumi program"""

import pulumi
import pulumi_aws as aws

regions = ["us-east-1", "us-west-2", "eu-west-1"]

for region in regions:
    region_provider = aws.Provider(region,region=region)
    bucket = aws.s3.Bucket(f'my-bucket-{region}', opts=pulumi.ResourceOptions(provider=region_provider))
a

ancient-yacht-19759

09/02/2020, 9:47 PM
awesome, thanks
ok im getting a stack trace in my implementation that seems unclear
Copy code
'''An AWS Python Pulumi program'''

import pulumi
from pprint import pprint
from copy import deepcopy
import pulumi_aws as aws

pulumi.runtime.settings._set_test_mode_enabled(True)

allbucket_dict = {
 'my-regional-999205377999-ap-northeast-1': {'acl': 'private', 'server_side_encryption_configuration': {
     'rule': {'applyServerSideEncryptionByDefault': {'sseAlgorithm': 'AES256'}}},
                                             'bucket': 'my-regional-999205377999-ap-northeast-1',
                                             'region': 'ap-northeast-1', 'tags': {'created_by': 'pulumi'}},
 'my-regional-999205377999-ap-northeast-2': {'acl': 'private', 'server_side_encryption_configuration': {
     'rule': {'applyServerSideEncryptionByDefault': {'sseAlgorithm': 'AES256'}}},
                                             'bucket': 'my-regional-999205377999-ap-northeast-2',
                                             'region': 'ap-northeast-2', 'tags': {'created_by': 'pulumi'}},
 'my-regional-999205377999-ap-south-1': {'acl': 'private', 'server_side_encryption_configuration': {
     'rule': {'applyServerSideEncryptionByDefault': {'sseAlgorithm': 'AES256'}}},
                                         'bucket': 'my-regional-999205377999-ap-south-1', 'region': 'ap-south-1',
                                         'tags': {'created_by': 'pulumi'}},
 'my-regional-999205377999-ap-southeast-1': {'acl': 'private', 'server_side_encryption_configuration': {
     'rule': {'applyServerSideEncryptionByDefault': {'sseAlgorithm': 'AES256'}}},
                                             'bucket': 'my-regional-999205377999-ap-southeast-1',
                                             'region': 'ap-southeast-1', 'tags': {'created_by': 'pulumi'}},
 'my-regional-999205377999-ap-southeast-2': {'acl': 'private', 'server_side_encryption_configuration': {
     'rule': {'applyServerSideEncryptionByDefault': {'sseAlgorithm': 'AES256'}}},
                                             'bucket': 'my-regional-999205377999-ap-southeast-2',
                                             'region': 'ap-southeast-2', 'tags': {'created_by': 'pulumi'}},
 'my-regional-999205377999-ca-central-1': {'acl': 'private', 'server_side_encryption_configuration': {
     'rule': {'applyServerSideEncryptionByDefault': {'sseAlgorithm': 'AES256'}}},
                                           'bucket': 'my-regional-999205377999-ca-central-1', 'region': 'ca-central-1',
                                           'tags': {'created_by': 'pulumi'}},
 'my-regional-999205377999-eu-central-1': {'acl': 'private', 'server_side_encryption_configuration': {
     'rule': {'applyServerSideEncryptionByDefault': {'sseAlgorithm': 'AES256'}}},
                                           'bucket': 'my-regional-999205377999-eu-central-1', 'region': 'eu-central-1',
                                           'tags': {'created_by': 'pulumi'}},
 'my-regional-999205377999-eu-north-1': {'acl': 'private', 'server_side_encryption_configuration': {
     'rule': {'applyServerSideEncryptionByDefault': {'sseAlgorithm': 'AES256'}}},
                                         'bucket': 'my-regional-999205377999-eu-north-1', 'region': 'eu-north-1',
                                         'tags': {'created_by': 'pulumi'}},
 'my-regional-999205377999-eu-west-1': {'acl': 'private', 'server_side_encryption_configuration': {
     'rule': {'applyServerSideEncryptionByDefault': {'sseAlgorithm': 'AES256'}}},
                                        'bucket': 'my-regional-999205377999-eu-west-1', 'region': 'eu-west-1',
                                        'tags': {'created_by': 'pulumi'}},
 'my-regional-999205377999-eu-west-2': {'acl': 'private', 'server_side_encryption_configuration': {
     'rule': {'applyServerSideEncryptionByDefault': {'sseAlgorithm': 'AES256'}}},
                                        'bucket': 'my-regional-999205377999-eu-west-2', 'region': 'eu-west-2',
                                        'tags': {'created_by': 'pulumi'}},
 'my-regional-999205377999-eu-west-3': {'acl': 'private', 'server_side_encryption_configuration': {
     'rule': {'applyServerSideEncryptionByDefault': {'sseAlgorithm': 'AES256'}}},
                                        'bucket': 'my-regional-999205377999-eu-west-3', 'region': 'eu-west-3',
                                        'tags': {'created_by': 'pulumi'}},
 'my-regional-999205377999-sa-east-1': {'acl': 'private', 'server_side_encryption_configuration': {
     'rule': {'applyServerSideEncryptionByDefault': {'sseAlgorithm': 'AES256'}}},
                                        'bucket': 'my-regional-999205377999-sa-east-1', 'region': 'sa-east-1',
                                        'tags': {'created_by': 'pulumi'}},
 'my-regional-999205377999-us-east-1': {'acl': 'private', 'server_side_encryption_configuration': {
     'rule': {'applyServerSideEncryptionByDefault': {'sseAlgorithm': 'AES256'}}},
                                        'bucket': 'my-regional-999205377999-us-east-1', 'region': 'us-east-1',
                                        'tags': {'created_by': 'pulumi'}},
 'my-regional-999205377999-us-east-2': {'acl': 'private', 'server_side_encryption_configuration': {
     'rule': {'applyServerSideEncryptionByDefault': {'sseAlgorithm': 'AES256'}}},
                                        'bucket': 'my-regional-999205377999-us-east-2', 'region': 'us-east-2',
                                        'tags': {'created_by': 'pulumi'}},
 'my-regional-999205377999-us-west-1': {'acl': 'private', 'server_side_encryption_configuration': {
     'rule': {'applyServerSideEncryptionByDefault': {'sseAlgorithm': 'AES256'}}},
                                        'bucket': 'my-regional-999205377999-us-west-1', 'region': 'us-west-1',
                                        'tags': {'created_by': 'pulumi'}},
 'my-regional-999205377999-us-west-2': {'acl': 'private', 'server_side_encryption_configuration': {
     'rule': {'applyServerSideEncryptionByDefault': {'sseAlgorithm': 'AES256'}}},
                                        'bucket': 'my-regional-999205377999-us-west-2', 'region': 'us-west-2',
                                        'tags': {'created_by': 'pulumi'}}
}

for region in itplat_allregions_list:
    region_provider = aws.Provider(region,region=region)


# Create AWS resources (S3 Bucket)
for bucket_name, bucket_options in allbucket_dict.items():
    bucket = aws.s3.Bucket(bucket_name, **bucket_options, opts=pulumi.ResourceOptions(provider=bucket_options['region']))
results in
Copy code
error: Program
failed
with an unhandled exception:
    error: Traceback(most
    recent
    call
    last):
    File
    "/usr/local/bin/pulumi-language-python-exec", line
    85, in < module >
    loop.run_until_complete(coro)
    File
    "/Users/myuser/.pyenv/versions/3.7.3/lib/python3.7/asyncio/base_events.py", line
    584, in run_until_complete
    return future.result()
    File
    "/Users/myuser/repos/myteam-pulumi-infrastructure/venv/lib/python3.7/site-packages/pulumi/runtime/stack.py", line
    83, in run_in_stack
    await run_pulumi_func(lambda: Stack(func))
    File
    "/Users/myuser/repos/myteam-pulumi-infrastructure/venv/lib/python3.7/site-packages/pulumi/runtime/stack.py", line
    51, in run_pulumi_func
    await RPC_MANAGER.rpcs.pop()
    File
    "/Users/myuser/repos/myteam-pulumi-infrastructure/venv/lib/python3.7/site-packages/pulumi/runtime/rpc_manager.py", line
    67, in rpc_wrapper
    result = await rpc
    File
    "/Users/myuser/repos/myteam-pulumi-infrastructure/venv/lib/python3.7/site-packages/pulumi/output.py", line
    88, in is_value_known
    return await is_known and not contains_unknowns(await future)
    File
    "/Users/myuser/repos/myteam-pulumi-infrastructure/venv/lib/python3.7/site-packages/pulumi/output.py", line
    88, in is_value_known
    return await is_known and not contains_unknowns(await future)
    File
    "/Users/myuser/repos/myteam-pulumi-infrastructure/venv/lib/python3.7/site-packages/pulumi/output.py", line
    88, in is_value_known
    return await is_known and not contains_unknowns(await future)
    [Previous line repeated 23 more times]
    File
    "/Users/myuser/repos/myteam-pulumi-infrastructure/venv/lib/python3.7/site-packages/pulumi/runtime/resource.py", line
    320, in do_register
    resolver = await prepare_resource(res, ty, custom, props, opts)
    File
    "/Users/myuser/repos/myteam-pulumi-infrastructure/venv/lib/python3.7/site-packages/pulumi/runtime/resource.py", line
    106, in prepare_resource
    provider_urn = await provider.urn.future()
    AttributeError: 'str'
    object
    has
    no
    attribute
    'urn'
    error: an
    unhandled
    error
    occurred: Program
    exited
    with non - zero exit code: 1
seems to be coming from the loop creating the provider
oh nm i see it
made providers a dict like :-
Copy code
for region in itplat_allregions_list:
    providers[region] = aws.Provider(region,region=region)
now referencing like :-
Copy code
bucket = aws.s3.Bucket(bucket_name, **bucket_options, opts=pulumi.ResourceOptions(provider=providers[bucket_options['region']]))
thanks for the assist @gentle-diamond-70147 . . . this is working 100% now
🙌 1
👍 1
3 Views