# Getting aws account id

**URL:** <https://forum.ansible.com/t/getting-aws-account-id/21466>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [May 18, 2016, 3:16am UTC](https://forum.ansible.com/t/getting-aws-account-id/21466 "2016-05-18T03:16:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Darvin\_Denmian](https://avatars.discourse-cdn.com/v4/letter/d/c68b51/32.png) [@Darvin\_Denmian](https://forum.ansible.com/u/Darvin_Denmian)\
**Post date:** [May 18, 2016, 3:16am UTC](https://forum.ansible.com/t/getting-aws-account-id/21466/1 "2016-05-18T03:16:02Z")

</div>

Hi guys,

does anyone have a good option to get the my aws account id using Ansible ?

Thanks !

---

<div class="post-metadata">

**Author:** ![Allen\_Sanabria1](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@Allen\_Sanabria1](https://forum.ansible.com/u/Allen_Sanabria1)\
**Post date:** [May 18, 2016, 3:32am UTC](https://forum.ansible.com/t/getting-aws-account-id/21466/2 "2016-05-18T03:32:39Z")

</div>

You can use a Ansible Filter Plugin.

I have a repo with all of my filter plugins. Line 8 has the function you need.

`

```auto
def get_account_id(region):
    client = boto3.client('iam', region_name=region)
    try:
        account_id = client.list_users()['Users'][0]['Arn'].split(':')[4]
        return account_id
    except Exception:
        raise errors.AnsibleFilterError(
            "Failed to retrieve account id"
        )

```

`

[https://github.com/linuxdynasty/ld-ansible-filters/blob/master/filter\_plugins/aws.py#L8](https://github.com/linuxdynasty/ld-ansible-filters/blob/master/filter_plugins/aws.py#L8)

---

<div class="post-metadata">

**Author:** ![Erick\_Vitor\_de\_Barro](https://avatars.discourse-cdn.com/v4/letter/e/ee7513/32.png) [@Erick\_Vitor\_de\_Barro](https://forum.ansible.com/u/Erick_Vitor_de_Barro)\
**Post date:** [May 18, 2016, 3:35am UTC](https://forum.ansible.com/t/getting-aws-account-id/21466/3 "2016-05-18T03:35:06Z")

</div>

Thanks Allen, I’ll give it a try !
