Skip to content

Instantly share code, notes, and snippets.

@pulse00
Forked from ErikHarmon/create_aws_api_group.py
Last active February 5, 2016 15:20
Show Gist options
  • Save pulse00/55284e75ee2f32e64d72 to your computer and use it in GitHub Desktop.
Save pulse00/55284e75ee2f32e64d72 to your computer and use it in GitHub Desktop.
Create AWS security group egress rules for AWS API access, using Ansible
#!/usr/bin/python
import json
import sys
# take json from stdin, from source such as https://ip-ranges.amazonaws.com/ip-ranges.json
# and turn it into an AWS security group using Ansible
region = 'eu-west-1'
header_str = """---
- hosts: 127.0.0.1
connection: local
gather_facts: no
tasks:"""
task_str =""" - name: Create AWS {} API access security group
local_action:
module: ec2_group
region: "{{ aws_region }}"
vpc_id: "{{ vpc_id }}"
name: "{{ env }}_aws_route53_health_api"
description: "{{ env }} AWS ROUTE53_HEALTHCHECKS API access"
rules:"""
rule_str = """ - proto: tcp
from_port: 443
to_port: 443
cidr_ip: "{}" """
obj = json.load(sys.stdin)
print header_str
api = 'ROUTE53_HEALTHCHECKS'
print task_str.format(api,api,api)
cnt = 0
for o in obj['prefixes']:
if o['service'] == api:
cnt += 1
if cnt < 50:
print rule_str.format(o['ip_prefix'])
else:
sys.stderr.write('exceeded 50 rules for aws-{}-api\n'.format(api))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment