Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Poller persistence #27

Open
GuidoNebiolo opened this issue Nov 19, 2019 · 0 comments
Open

Poller persistence #27

GuidoNebiolo opened this issue Nov 19, 2019 · 0 comments

Comments

@GuidoNebiolo
Copy link

Hi,

I'm trying to create a Custom resource to create an account inside AWS Organization.
This action is asynchronous because you have to call two different boto3 method: first you need to call the CreateAccount API, this provide you a request id which needs to be used polling the DescribeCreateAccountStatus API until the create status is SUCCEEDED.

I would like to persist the RequestId putting it in the ChHelperData, but calling this is not enought
helper.Data.update({"CreateAccountRequestId": create_account_request_id})
I don't find anything inside the event inside the ChHelperData during the next invocation.

Here the code I'm developing:

@helper.poll_create
def create_account(event, _):
    organizations_client = boto3.client('organizations')
    create_account_request_id = helper.Data.get("CreateAccountRequestId")
    if not create_account_request_id:
        account_name = event.get("ResourceProperties", {}).get('AccountName')
        if not account_name:
            raise ValueError("AccountName is not specified")
        account_email = f"{account_name}@{INFO_DOMAIN}"
        response = organizations_client.create_account(
            Email=account_email,
            AccountName=account_name,
        )
        create_account_request_id = response['CreateAccountStatus']['Id']
        if not create_account_request_id:
            print(response)
            raise ValueError("CreateAccountRequestId not found")
        helper.Data.update({"CreateAccountRequestId": create_account_request_id})
    else:
        response = organizations_client.describe_create_account_status(
            CreateAccountRequestId=create_account_request_id,
        )
        status = response['CreateAccountStatus']['State']
        print(f"Account creation status: {status}")
        if status == 'FAILED':
            raise RuntimeError(response['CreateAccountStatus']['FailureReason'])
        if status == 'SUCCEEDED':
            account_id = response['CreateAccountStatus']['AccountId']
            helper.Data.update({
                "AccountId": account_id,
                "AccessRole": f"arn:aws:iam::{account_id}:role/OrganizationAccountAccessRole"
            })
            return account_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant