Skip to content

Commit 27cbcaf

Browse files
matej5Matej Stajduhar
andauthored
n10-Creating-new-role-for-administration (#2350)
* n10-Creating-new-role-for-administration * Adding-role-in-meta-tasks * Moving-swag-file * Moving-swag-file-2 * Changing-from-swagger-file-to-text * Adding-API-lookup-prior-to-creation * Adding-API-lookup-prior-to-creation-2 * Adding-API-lookup-prior-to-creation-3 * Adding-API-lookup-prior-to-creation-4 * Adding-API-lookup-prior-to-creation-5 * Adding-API-lookup-prior-to-creation-6 * Adding-API-lookup-prior-to-creation-7 * Updating-tasks * Updating-tasks * Updating-tasks-2 * Updating-tasks-3 * Updating-tasks-4 * Updating-tasks-4 * Updating-tasks-5 * Updating-tasks-6 * Updating-tasks-7 * Adding-for-loop-for-lambda-functions * Adding-for-loop-for-lambda-functions-2 * Adding-for-loop-for-lambda-functions-3 * Adding-for-loop-for-lambda-functions-4 * Adding-for-loop-for-lambda-functions-5 * Adding-for-loop-for-lambda-functions-6 * Adding-for-loop-for-lambda-functions-7 * Adding-for-loop-for-lambda-functions-8 * Adding-for-loop-for-lambda-functions-9 * Adding-for-loop-for-lambda-functions-10 * Adding-for-loop-for-lambda-functions-11 * Switching-role-to-use-aws-cli * Switching-role-to-use-aws-cli-2 * Switching-role-to-use-aws-cli-3 * Switching-role-to-use-aws-cli-4 * Switching-role-to-use-aws-cli-5 * New-admin-tools-role * New-admin-tools-role-2 * New-admin-tools-role-3 * New-admin-tools-role-4 * New-admin-tools-role * New-admin-tools-role-2 * New-admin-tools-role-3 * New-admin-tools-role-4 * New-api_admin_tools-role * Updating-defaults --------- Co-authored-by: Matej Stajduhar <[email protected]>
1 parent 278831f commit 27cbcaf

File tree

26 files changed

+742
-182
lines changed

26 files changed

+742
-182
lines changed

ce-dev/ansible/vars/provision/galaxy-requirements.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
roles:
33
- name: geerlingguy.solr
44
- name: geerlingguy.java
5-
- name: cloudalchemy.process_exporter
6-
- name: cloudalchemy.grafana
5+
- name: prometheus.prometheus.process_exporter
6+
- name: grafana.grafana.grafana
77
collections:
88
- name: community.grafana
99
- name: prometheus.prometheus

roles/_meta/aws_region/meta/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ dependencies:
88
- role: aws/aws_cloudwatch_log_group
99
- role: aws/aws_backup
1010
- role: aws/aws_backup_sns
11+
- role: aws/aws_admin_tools

roles/aws/aws_acl/defaults/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ aws_acl:
66
region: "us-east-1"
77
tags: "{{ _aws_tags }}"
88
recreate: false # set to true to creating the ACL
9+
default_action: "Allow" # Default action if no rules are triggered, can be Block
910
rules:
1011
rate_limit:
1112
value: 600 # set to 0 to skip rate limit rule, set to a value to set how many requests to allow in period before blocking

roles/aws/aws_acl/tasks/create_acl.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
description: "{{ _acl.description }}"
9393
scope: "{{ _acl.scope }}"
9494
region: "{{ _acl.region }}"
95-
default_action: Allow # or "Block"
95+
default_action: "{{ _acl.default_action }}" # or "Block"
9696
sampled_requests: false
9797
cloudwatch_metrics: true # or "false" to disable metrics
9898
metric_name: test-metric-name # not sure about this name, since each rule also has it's own metrics name (maybe log group name)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
aws_admin_tools:
2+
runtime: "python3.12"
3+
timeout: 20
4+
allowed_ips:
5+
- 192.168.1.1/32 # Ip of server with access to API-s
6+
functions:
7+
- name: "GetForecastedCosts"
8+
type: GET
9+
policies:
10+
- "arn:aws:iam::{{ _acc_id }}:policy/CEBillingPolicy" # Custom policy
11+
- name: "ChangeASGScaling"
12+
type: POST
13+
policies:
14+
- arn:aws:iam::aws:policy/AmazonEC2FullAccess
15+
- name: "GetListOfEC2"
16+
type: GET
17+
policies:
18+
- arn:aws:iam::aws:policy/AmazonEC2FullAccess
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
- name: Create stage on API gateway.
2+
ansible.builtin.command: >-
3+
aws apigateway create-stage
4+
--rest-api-id "{{ _api_gate.id }}"
5+
--stage-name "prod"
6+
--deployment-id "{{ _main_api_deploy.id }}"
7+
--region "{{ _aws_region }}"
8+
register: _main_api_stage
9+
when: _api_index | length == 0
10+
11+
- name: Create resources and set methods on API Gateway.
12+
ansible.builtin.include_tasks: create_methods.yml
13+
loop: "{{ aws_admin_tools.functions }}"
14+
15+
- name: Obtain all information for a single WAF.
16+
community.aws.wafv2_web_acl_info:
17+
name: "{{ _aws_profile }}_admin_tools"
18+
scope: "REGIONAL"
19+
region: "{{ _aws_region }}"
20+
register: _main_waf
21+
22+
- name: Get list of API gateway resources.
23+
ansible.builtin.command: >-
24+
aws apigateway get-resources
25+
--region "{{ _aws_region }}"
26+
--rest-api-id "{{ _api_gate.id }}"
27+
register: _api_res_list
28+
29+
- name: Setting previous command output into variable.
30+
ansible.builtin.set_fact:
31+
_api_res_list: "{{ _api_res_list.stdout | from_json | json_query('items') }}"
32+
33+
- name: Get index of DelMe resource from API gateway.
34+
ansible.builtin.set_fact:
35+
_api_res_index_list: "{{ lookup('ansible.utils.index_of', _api_res_list, 'eq', '/DelMe', 'path', wantlist=True) }}"
36+
when: _api_index | length == 0
37+
38+
- name: Delete the initial resource.
39+
ansible.builtin.command: >-
40+
aws apigateway delete-resource
41+
--rest-api-id "{{ _api_gate.id }}"
42+
--resource-id "{{ _api_res_list[_api_res_index_list[0]].id }}"
43+
--region "{{ _aws_region }}"
44+
when: _api_index | length == 0
45+
46+
- name: Deploy API gateway prior to attaching WAF.
47+
ansible.builtin.command: >-
48+
aws apigateway create-deployment
49+
--rest-api-id "{{ _api_gate.id }}"
50+
--stage-name "prod"
51+
--region "{{ _aws_region }}"
52+
53+
- name: Add API gateway to waf.
54+
community.aws.wafv2_resources:
55+
name: "{{ _aws_profile }}_admin_tools"
56+
scope: REGIONAL
57+
state: present
58+
region: "{{ _aws_region }}"
59+
arn: "arn:aws:apigateway:{{ _aws_region }}::/restapis/{{ _api_gate.id }}/stages/prod"
60+
61+
- name: Generate unique string.
62+
ansible.builtin.set_fact:
63+
_rand_str: "{{ lookup('community.general.random_string', length=8, special=false, min_lower=2, min_numeric=2, min_upper=2) }}"
64+
65+
- name: Update Lambda triggers.
66+
ansible.builtin.command: >-
67+
aws lambda add-permission
68+
--function-name "API_{{ item.name }}"
69+
--statement-id "{{ item.name }}_{{ _rand_str }}"
70+
--action "lambda:InvokeFunction"
71+
--principal apigateway.amazonaws.com
72+
--source-arn arn:aws:execute-api:{{ _aws_region }}:{{ _acc_id }}:{{ _api_gate.id }}/*/{{ item.type }}/{{ item.name }}
73+
--region {{ _aws_region }}
74+
loop: "{{ aws_admin_tools.functions }}"
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
- name: Get resources.
2+
ansible.builtin.command: >-
3+
aws apigateway get-resources
4+
--rest-api-id "{{ _api_gate.id }}"
5+
--region "{{ _aws_region }}"
6+
register: _api_old_resource
7+
8+
- name: Setting previous command output into variable.
9+
ansible.builtin.set_fact:
10+
_api_old_resource: "{{ _api_old_resource.stdout | from_json }}"
11+
12+
- name: Find the index of existing resource.
13+
ansible.builtin.set_fact:
14+
_api_old_resource_index: "{{ lookup('ansible.utils.index_of', _api_old_resource['items'], 'eq', '/' + item.name, 'path', wantlist=True) }}"
15+
16+
- name: Delete resource.
17+
ansible.builtin.command: >-
18+
aws apigateway delete-resource
19+
--rest-api-id "{{ _api_gate.id }}"
20+
--resource-id "{{ _api_old_resource['items'][_api_old_resource_index[0]].id }}"
21+
--region "{{ _aws_region }}"
22+
register: _api_old_resource
23+
when: _api_old_resource_index | length > 0
24+
25+
- name: Create resource on API gateway.
26+
ansible.builtin.command: >-
27+
aws apigateway create-resource
28+
--rest-api-id "{{ _api_gate.id }}"
29+
--parent-id "{{ _api_res_list[_api_res_index_list[0]].id }}"
30+
--path-part "{{ item.name }}"
31+
--region "{{ _aws_region }}"
32+
register: _api_resource
33+
34+
- name: Setting previous command output into variable.
35+
ansible.builtin.set_fact:
36+
_api_resource: "{{ _api_resource.stdout | from_json }}"
37+
38+
- name: Put method on API gateway
39+
ansible.builtin.command: >-
40+
aws apigateway put-method
41+
--rest-api-id "{{ _api_gate.id }}"
42+
--resource-id "{{ _api_resource.id }}"
43+
--http-method "{{ item.type }}"
44+
--authorization-type "NONE"
45+
--no-api-key-required
46+
--region "{{ _aws_region }}"
47+
48+
- name: Add Lambda for method.
49+
ansible.builtin.command: >-
50+
aws apigateway put-integration
51+
--rest-api-id "{{ _api_gate.id }}"
52+
--resource-id "{{ _api_resource.id }}"
53+
--http-method "{{ item.type }}"
54+
--type AWS
55+
--content-handling CONVERT_TO_TEXT
56+
--request-templates '{ "application/json": "{\"statusCode\": 200}" }'
57+
--integration-http-method POST
58+
--uri "arn:aws:apigateway:{{ _aws_region }}:lambda:path/2015-03-31/functions/arn:aws:lambda:{{ _aws_region }}:{{ _acc_id }}:function:API_{{ item.name }}/invocations"
59+
--region {{ _aws_region }}
60+
61+
- name: Add method response.
62+
ansible.builtin.command: >-
63+
aws apigateway put-method-response
64+
--rest-api-id "{{ _api_gate.id }}"
65+
--resource-id "{{ _api_resource.id }}"
66+
--http-method "{{ item.type }}"
67+
--status-code "200"
68+
--response-models '{"application/json":"Empty"}'
69+
--region {{ _aws_region }}
70+
71+
- name: Add integration response.
72+
ansible.builtin.command: >-
73+
aws apigateway put-integration-response
74+
--rest-api-id "{{ _api_gate.id }}"
75+
--resource-id "{{ _api_resource.id }}"
76+
--http-method "{{ item.type }}"
77+
--status-code "200"
78+
--selection-pattern ""
79+
--content-handling "CONVERT_TO_TEXT"
80+
--region {{ _aws_region }}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
- name: Create MOCK resource on API gateway.
2+
ansible.builtin.command: >-
3+
aws apigateway create-resource
4+
--rest-api-id "{{ _api_gate.id }}"
5+
--parent-id "{{ _api_res_list[_api_res_index_list[0]].id }}"
6+
--path-part "DelMe"
7+
--region "{{ _aws_region }}"
8+
register: _api_resource
9+
10+
- name: Setting command output into variable.
11+
ansible.builtin.set_fact:
12+
_api_resource: "{{ _api_resource.stdout | from_json }}"
13+
14+
- name: Put method on API gateway.
15+
ansible.builtin.command: >-
16+
aws apigateway put-method
17+
--rest-api-id "{{ _api_gate.id }}"
18+
--resource-id "{{ _api_resource.id }}"
19+
--http-method "GET"
20+
--authorization-type "NONE"
21+
--no-api-key-required
22+
--region "{{ _aws_region }}"
23+
24+
- name: Add mock integration.
25+
ansible.builtin.command: >-
26+
aws apigateway put-integration
27+
--rest-api-id "{{ _api_gate.id }}"
28+
--resource-id "{{ _api_resource.id }}"
29+
--http-method GET
30+
--type MOCK
31+
--region {{ _aws_region }}
32+
33+
- name: Create initial deployent for API gateway.
34+
ansible.builtin.command: >-
35+
aws apigateway create-deployment
36+
--rest-api-id "{{ _api_gate.id }}"
37+
--region "{{ _aws_region }}"
38+
register: _main_api_deploy
39+
40+
- name: Setting command output into variable.
41+
ansible.builtin.set_fact:
42+
_main_api_deploy: "{{ _main_api_deploy.stdout | from_json }}"
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
- name: Create S3 bucket for lambda functions.
2+
amazon.aws.s3_bucket:
3+
name: "{{ _aws_profile }}-lambda-api-functions"
4+
region: "{{ _aws_region }}"
5+
state: present
6+
7+
- name: Check and clean any previous python files.
8+
ansible.builtin.file:
9+
path: "/tmp/{{ item.name }}.py"
10+
state: absent
11+
12+
- name: Write Lambda functions.
13+
ansible.builtin.template:
14+
src: "API_{{ item.name }}.py.j2"
15+
dest: "/tmp/API_{{ item.name }}.py"
16+
17+
- name: Create a zip archive of Lambda functions.
18+
community.general.archive:
19+
path: "/tmp/API_{{ item.name }}.py"
20+
dest: "/tmp/API_{{ item.name }}.zip"
21+
format: zip
22+
23+
- name: Place Lambda functions in S3 bucket.
24+
amazon.aws.s3_object:
25+
bucket: "{{ _aws_profile }}-lambda-api-functions"
26+
object: "lambda-functions/API-{{ item.name }}.zip"
27+
src: "/tmp/API_{{ item.name }}.zip"
28+
mode: put
29+
30+
- name: Get appropriate IAM role for Lambda.
31+
amazon.aws.iam_role_info:
32+
name: "API_{{ item.name }}"
33+
register: _iam_api_lambda
34+
35+
- name: Create Lambda functions.
36+
amazon.aws.lambda:
37+
name: "API_{{ item.name }}"
38+
description: "Lambda function for {{ item.name }}"
39+
region: "{{ _aws_region }}"
40+
timeout: "{{ aws_admin_tools.timeout }}"
41+
s3_bucket: "{{ _aws_profile }}-lambda-api-functions"
42+
s3_key: "lambda-functions/API-{{ item.name }}.zip"
43+
state: present
44+
runtime: "{{ aws_admin_tools.runtime }}"
45+
role: "{{ _iam_api_lambda.iam_roles[0].arn }}"
46+
handler: "API_{{ item.name }}.lambda_handler"
47+
tags:
48+
Name: "API_{{ item.name }}"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
- name: Attach CloudWatch policy.
2+
ansible.builtin.set_fact:
3+
_policies: "{{ item.policies + ['arn:aws:iam::aws:policy/CloudWatchLogsFullAccess'] }}"
4+
5+
- name: Create a role and attach policies.
6+
amazon.aws.iam_role:
7+
name: "API_{{ item.name }}"
8+
assume_role_policy_document: "{{ lookup('template', 'trusted_entitites.j2') }}"
9+
managed_policies: "{{ _policies }}"

0 commit comments

Comments
 (0)