Ansible EC2 Dynamic inventory minimum IAM policies
Asked Answered
Y

4

10

Has someone figured out the minimum IAM policies required to run the EC2 dynamic inventory script (ec2.py) on ansible via an IAM role?

So far, I haven't seen a concrete reference in this matter other than specifying credentials for boto library in the official documentation of ansible, however, on production environments, I rarely use key pairs for access to AWS services from EC2 instances, instead I have embraced the use of IAM roles for that case scenario.

I have tried policies allowing ec2:Describe* actions but it doesn't seem to be enough for the script as it always exits with Unauthorized operation.

Could you help me out?

Yep answered 29/5, 2015 at 0:57 Comment(0)
I
6

I just created a demo policy, created a new role and used that new policy, and then created a new instance that used that new role.

Demo Policy:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Demo201505282045",
            "Effect": "Allow",
            "Action": [
                "ec2:Describe*",
                "route53:ListHostedZones",
                "route53:ListResourceRecordSets"
            ],
            "Resource": "*"
        }
    ]
}

I had to add route53 as I use the route53 option (route53 = true in the ec2.ini) but other than that it worked fine.

If you are still having problems, try running ec2.py from the commandline (./ec2.py) as that does usually give reasonable error messages when run directly.

Inebriant answered 29/5, 2015 at 2:41 Comment(2)
If you have enabled RDS in ec2.ini, you will also need rds:Describe I guessUsurer
Your contributions were precisely what I needed to know, thus, I think this should be specified along with the Ansible documentation. Thanks a lot!.Yep
D
9

The script also looks at RDS and elasticache. They can be disabled in ec2.ini, but if you don't, the following policy seems to be enough to run the dynamic inventory.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Demo201505282045",
            "Effect": "Allow",
            "Action": [
                "ec2:Describe*",
                "route53:ListHostedZones",
                "route53:ListResourceRecordSets",
                "rds:Describe*",
                "elasticache:Describe*"
            ],
            "Resource": "*"
        }
    ]
}
Descartes answered 9/7, 2015 at 13:20 Comment(0)
I
6

I just created a demo policy, created a new role and used that new policy, and then created a new instance that used that new role.

Demo Policy:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Demo201505282045",
            "Effect": "Allow",
            "Action": [
                "ec2:Describe*",
                "route53:ListHostedZones",
                "route53:ListResourceRecordSets"
            ],
            "Resource": "*"
        }
    ]
}

I had to add route53 as I use the route53 option (route53 = true in the ec2.ini) but other than that it worked fine.

If you are still having problems, try running ec2.py from the commandline (./ec2.py) as that does usually give reasonable error messages when run directly.

Inebriant answered 29/5, 2015 at 2:41 Comment(2)
If you have enabled RDS in ec2.ini, you will also need rds:Describe I guessUsurer
Your contributions were precisely what I needed to know, thus, I think this should be specified along with the Ansible documentation. Thanks a lot!.Yep
A
1

The script checks also for Route53, RDS and ElastiCache configurations, so it will require access to ec2:Describe*, route53:ListHostedZones, route53:ListResourceRecordSets, rds:Describe* and elasticache:Describe*.

Still, if you don't use all these services you can selectively disable their check in the ec2.ini file by setting to False the values of the associated group_by_* variables: this will skip the fetching of those configurations, both allowing you to minimize the actions allowed for the role (eg: ec2:Describe* only) and reducing the overall query time of the script.

Azotic answered 5/9, 2018 at 15:8 Comment(0)
F
0

These are the permissions that I identified as required by ec2.py after checking CloudTrail:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "elasticache:DescribeReplicationGroups",
                "ec2:DescribeInstances",
                "ec2:DescribeTags",
                "rds:DescribeDBInstances",
                "elasticache:DescribeCacheClusters"
            ],
            "Resource": "*"
        }
    ]
}
Filiform answered 3/5, 2021 at 18:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.