Thread
#python
    c

    creamy-whale-55909

    3 months ago
    I have created a component resource that makes a call to
    get_caller_identity()
    in order to grab the current account id of the locally assumed aws role so it can be used to define an arn in a policy doc: (code snippet has been truncated to save space)
    import pulumi_aws as aws
    
    # gets account id
    current = aws.get_caller_identity()
    account_id = current.account_id
    
    # passes account id when defining policy
    self.lambda_policy = aws.iam.RolePolicy(
                "lambdaPolicy",
                role=self.lambda_role.name,
                policy=self.my_lambda.name.apply(
                    lambda physical_name: json.dumps(
                        {
                            "Version": "2012-10-17",
                            "Statement": [
                                {
                                    "Effect": "Allow",
                                    "Action": "logs:CreateLogGroup",
                                    "Resource": f"arn:aws:logs:us-east-1:{account_id}:*"
    
    ...
    When I instantiate this component resource in my project
    __main__.py
    , it works as expected. However, when I run
    pytest
    , I get the following error:
    ________________________________ ERROR collecting tests/test_ip_scan.py _________________________________
    tests/test_ip_scan.py:18: in <module>
        test_args = ip_scan.MonitorAvailableIpsArgs(
    <string>:9: in __init__
        ???
    components/ip_scan.py:28: in __post_init__
        account_id = aws.get_caller_identity().account_id
    .venv/lib/python3.10/site-packages/pulumi_aws/get_caller_identity.py:106: in get_caller_identity
        account_id=__ret__.account_id,
    E   AttributeError: 'NoneType' object has no attribute 'account_id'
    Any and all feedback is greatly appreciated!
    Ok I figured this one out. I had to mock the function call as follows:
    class MyMocks(pulumi.runtime.Mocks):
        def new_resource(self, args: pulumi.runtime.MockResourceArgs):
            return [args.name + "_id", args.inputs]
    
        def call(self, args: pulumi.runtime.MockCallArgs):
            if args.token == "aws:index/getCallerIdentity:getCallerIdentity":
                return {
                    "accountId": "123456789",
                    "arn": "myarn",
                    "id": "myid",
                    "userId": "achang3"
                }
            return {}