Last active
December 3, 2021 14:08
-
-
Save jackl0phty/240939a59c6b8e016c69 to your computer and use it in GitHub Desktop.
Use Ansible to create AWS Security Group, Create Name tag, Open TCP port 80, and Only Allow Access From Your Home External Ip Address.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: localhost | |
connection: local | |
gather_facts: no | |
vars: | |
region: us-east-1 | |
route_prefix: /32 | |
tasks: | |
- command: curl --silent --fail http://checkip.amazonaws.com/ | |
register: external_ip | |
- set_fact: routable_ip="{{external_ip.stdout}}{{ route_prefix }}" | |
- name: Create test ansible security group. | |
local_action: | |
module: ec2_group | |
name: "{{ item.sg_name }}" | |
region: "{{ region }}" | |
name: http_port_80_home | |
description: HTTP From Home Ip | |
state: present | |
rules: | |
- proto: tcp | |
from_port: 80 | |
to_port: 80 | |
cidr_ip: "{{ routable_ip }}" | |
rules_egress: | |
- proto: all | |
cidr_ip: 0.0.0.0/0 | |
register: aws_sg | |
- name: Tag security group http from home Ip | |
local_action: | |
module: ec2_tag | |
resource: "{{aws_sg.group_id}}" | |
region: "{{ region }}" | |
state: present | |
tags: | |
Name: HTTP_port_80_home | |
The above Ansible play will require the following minimal access in AWS. | |
# Create & modify AWS security group. | |
{ | |
"Version": "2012-10-17", | |
"Statement": [ | |
{ | |
"Effect": "Allow", | |
"Action": [ | |
"ec2:CreateSecurityGroups", | |
"ec2:*SecurityGroups*", | |
"ec2:Describe*", | |
"ec2:CreateSecurity*", | |
"ec2:RevokeSecurity*", | |
"ec2:AuthorizeSecurity*" | |
], | |
"Resource": "*" | |
} | |
] | |
} | |
# Create & modify ec2 tags | |
{ | |
"Version": "2012-10-17", | |
"Statement": [ | |
{ | |
"Effect": "Allow", | |
"Action": [ | |
"ec2:CreateTags", | |
"ec2:*CreateTags*", | |
"ec2:Describe*", | |
"ec2:RevokeTags*", | |
"ec2:AuthorizeTags*" | |
], | |
"Resource": "*" | |
} | |
] | |
} | |
# AWS provided policy "AmazonEC2ReadOnlyAccess". | |
{ | |
"Version": "2012-10-17", | |
"Statement": [ | |
{ | |
"Effect": "Allow", | |
"Action": "ec2:Describe*", | |
"Resource": "*" | |
}, | |
{ | |
"Effect": "Allow", | |
"Action": "elasticloadbalancing:Describe*", | |
"Resource": "*" | |
}, | |
{ | |
"Effect": "Allow", | |
"Action": [ | |
"cloudwatch:ListMetrics", | |
"cloudwatch:GetMetricStatistics", | |
"cloudwatch:Describe*" | |
], | |
"Resource": "*" | |
}, | |
{ | |
"Effect": "Allow", | |
"Action": "autoscaling:Describe*", | |
"Resource": "*" | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment