Skip to content
This repository was archived by the owner on Oct 7, 2025. It is now read-only.

Commit 7092dfa

Browse files
authored
Merge pull request #2549 from codeenigma/Updating-aws_admin_tools-role-with-new-option-and-setting-up-general-s3-bucket-name-PR-2.x
Updating-aws_admin_tools-role-with-new-option-and-setting-up-general-…
2 parents c9a701c + c6718ef commit 7092dfa

15 files changed

Lines changed: 142 additions & 48 deletions

File tree

roles/aws/aws_admin_tools/defaults/main.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,10 @@ aws_admin_tools:
4545
resource: "*"
4646
action:
4747
- "wafv2:GetWebACL"
48+
- name: "get_infra_data_from_s3" # This is the bit that sets up the API Gateway to pull and share infra data over API. No Lambda function needed here though. How do we do this? See docs
49+
resource: s3
50+
type: GET
51+
bucket: "codeenigma-{{ _aws_profile }}-general-storage-{{ _aws_region }}"
52+
object: "lambda-results/infra-info"
53+
url_params:
54+
- date

roles/aws/aws_admin_tools/tasks/create.yml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,26 @@
3737
- _api_index | length > 0
3838
- _old_api_res_list is defined
3939

40-
- name: Extract the scheduled functions from the list.
40+
- name: Extract the API Gateway functions from the list.
4141
ansible.builtin.set_fact:
4242
_api_functions: "{{ aws_admin_tools.functions | selectattr('resource', 'eq', 'api') | list }}"
4343

44-
- name: Extract the API Gateway functions from the list.
44+
- name: Extract the scheduled functions from the list.
4545
ansible.builtin.set_fact:
4646
_schedule_functions: "{{ aws_admin_tools.functions | selectattr('resource', 'eq', 'schedule') | list }}"
4747

48+
- name: Extract the API Gateway with S3 from the list.
49+
ansible.builtin.set_fact:
50+
_api_s3: "{{ aws_admin_tools.functions | selectattr('resource', 'eq', 's3') | list }}"
51+
52+
- name: Remove old resources if API Gateway exists.
53+
ansible.builtin.include_tasks: remove_old_reources.yml
54+
loop: "{{ _api_functions + _api_s3 }}"
55+
when: _api_index | length > 0
56+
4857
- name: Create resources and set methods on API Gateway.
4958
ansible.builtin.include_tasks: create_methods.yml
50-
loop: "{{ _api_functions }}"
59+
loop: "{{ _api_functions + _api_s3 }}"
5160

5261
- name: Create Event Bridge schedule.
5362
ansible.builtin.include_tasks: create_schedule.yml
@@ -113,3 +122,5 @@
113122
--source-arn arn:aws:execute-api:{{ _aws_region }}:{{ _acc_id }}:{{ _api_gate.id }}/*/{{ item.type }}/{{ item.name }}
114123
--region {{ _aws_region }}
115124
loop: "{{ _api_functions }}"
125+
126+
# TODO Add scheduled triggers

roles/aws/aws_admin_tools/tasks/create_methods.yml

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,3 @@
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 root 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-
251
- name: Create resource on API gateway.
262
ansible.builtin.command: >-
273
aws apigateway create-resource
@@ -64,17 +40,59 @@
6440
--region "{{ _aws_region }}"
6541
when: item.url_params is defined and item.url_params | length > 0
6642

67-
- name: Add Lambda for method without URL params.
43+
- name: Build arn for S3 bucket.
44+
ansible.builtin.set_fact:
45+
_target_arn: "arn:aws:apigateway:{{ _aws_region }}:s3:path/{{ item.bucket }}/{{ item.object }}"
46+
when: item.resource == "s3"
47+
48+
- name: List Objects in S3 bucket.
49+
amazon.aws.s3_object:
50+
bucket: "{{ item.bucket }}"
51+
mode: list
52+
register: _objects_in_s3_bucket
53+
when: item.resource == "s3"
54+
55+
- name: Set _object_missing to false if object exist.
56+
ansible.builtin.set_fact:
57+
_object_missing: false
58+
when: item.resource == "s3" and item.object in _objects_in_s3_bucket.s3_keys
59+
60+
- name: Set _object_missing to true if object doesn't exist.
61+
ansible.builtin.set_fact:
62+
_object_missing: true
63+
when: item.resource == "s3" and item.object not in _objects_in_s3_bucket.s3_keys
64+
65+
- name: Create default file for S3.
66+
ansible.builtin.template:
67+
src: default_s3_object.j2
68+
dest: default_s3_object
69+
when: _object_missing is defined and _object_missing
70+
71+
- name: Add default file to S3 bucket if it doesn't exist.
72+
amazon.aws.s3_object:
73+
bucket: "{{ item.bucket }}"
74+
object: "{{ item.object }}"
75+
src: "default_s3_object"
76+
mode: put
77+
when: _object_missing is defined and _object_missing
78+
79+
- name: Build arn for Lambda.
80+
ansible.builtin.set_fact:
81+
_target_arn: "arn:aws:apigateway:{{ _aws_region }}:lambda:path/2015-03-31/functions/arn:aws:lambda:{{ _aws_region }}:{{ _acc_id }}:function:api_{{ item.name }}/invocations"
82+
when: item.resource == "api"
83+
84+
- name: Add Service for method without URL params.
6885
ansible.builtin.command: >-
6986
aws apigateway put-integration
7087
--rest-api-id "{{ _api_gate.id }}"
7188
--resource-id "{{ _api_resource.id }}"
7289
--http-method "{{ item.type }}"
7390
--type AWS
7491
--content-handling CONVERT_TO_TEXT
75-
--integration-http-method POST
76-
--uri "arn:aws:apigateway:{{ _aws_region }}:lambda:path/2015-03-31/functions/arn:aws:lambda:{{ _aws_region }}:{{ _acc_id }}:function:api_{{ item.name }}/invocations"
92+
--integration-http-method {{ 'GET' if item.resource == 's3' else 'POST' }}
93+
--uri "{{ _target_arn }}"
7794
--region {{ _aws_region }}
95+
{{ '--credentials "arn:aws:iam::' + _acc_id + ':role/api_get_s3"' if item.resource == 's3' else '' }}
7896
when: item.url_params is not defined or item.url_params | length == 0
7997

8098
- name: Generate template parts for each param
@@ -93,19 +111,20 @@
93111
dest: integration_template
94112
when: item.url_params is defined and item.url_params | length > 0
95113

96-
- name: Add Lambda for method with URL params.
114+
- name: Add Service for method with URL params.
97115
ansible.builtin.command: >-
98116
aws apigateway put-integration
99117
--rest-api-id "{{ _api_gate.id }}"
100118
--resource-id "{{ _api_resource.id }}"
101119
--http-method "{{ item.type }}"
102120
--type AWS
103121
--content-handling CONVERT_TO_TEXT
104-
--integration-http-method POST
122+
--integration-http-method {{ 'GET' if item.resource == 's3' else 'POST' }}
105123
--passthrough-behavior WHEN_NO_TEMPLATES
106124
--request-template file://integration_template
107-
--uri "arn:aws:apigateway:{{ _aws_region }}:lambda:path/2015-03-31/functions/arn:aws:lambda:{{ _aws_region }}:{{ _acc_id }}:function:api_{{ item.name }}/invocations"
125+
--uri "{{ _target_arn }}"
108126
--region {{ _aws_region }}
127+
{{ '--credentials "arn:aws:iam::' + _acc_id + ':role/api_get_s3"' if item.resource == 's3' else '' }}
109128
when: item.url_params is defined and item.url_params | length > 0
110129

111130
- name: Add method response.

roles/aws/aws_admin_tools/tasks/lambda_functions.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
role: "{{ aws_iam_role._result[item.resource + '_' + item.name] }}"
1515
runtime: "{{ aws_admin_tools.runtime }}"
1616
function_file: "{{ lookup('template', item.resource + '_' + item.name + '.py.j2') }}"
17-
s3_bucket: "ce-{{ _aws_profile }}-lambda-functions"
17+
s3_bucket: "codeenigma-{{ _aws_profile }}-general-storage-{{ _aws_region }}"
18+
s3_bucket_prefix: "lambda-functions"
1819
tags:
1920
Name: "{{ item.resource }}_{{ item.name }}"

roles/aws/aws_admin_tools/tasks/lambda_iam.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
aws_profile: "{{ _aws_profile }}"
1212
managed_policies: "{{ _policies }}"
1313
inline_policies: "{{ item.inline_policies | default(omit) }}"
14-
policy_document: "{{ lookup('template', 'trusted_entitites.j2') }}"
14+
policy_document: "{{ lookup('template', 'trust_lambda.j2') }}"

roles/aws/aws_admin_tools/tasks/main.yml

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
_api_gate: "{{ _api_gate.stdout | from_json }}"
3434
when: _api_index | length == 0
3535

36-
- name: Setting _api_index if API already exists.
36+
- name: Setting _api_gate if API already exists.
3737
ansible.builtin.set_fact:
3838
_api_gate: "{{ _api_gate_list.rest_apis[_api_index[0]] }}"
3939
when: _api_index | length > 0
@@ -72,13 +72,33 @@
7272
ansible.builtin.set_fact:
7373
_main_api_deploy: "{{ _main_api_deploy_tmp['items'] | last }}"
7474

75+
- name: Extract the API Gateway without s3 for resource.
76+
ansible.builtin.set_fact:
77+
_api_without_s3: "{{ aws_admin_tools.functions | rejectattr('resource', 'eq', 's3') | list }}"
78+
79+
- name: Set CloudWatch logs full access for apigateway.
80+
ansible.builtin.set_fact:
81+
_policies: "{{ ['arn:aws:iam::aws:policy/CloudWatchLogsFullAccess'] }}"
82+
83+
- name: Create a policy for API Gateway to assume role.
84+
ansible.builtin.include_role:
85+
name: aws/aws_iam_role
86+
vars:
87+
aws_iam_role:
88+
name: "api_get_s3"
89+
aws_profile: "{{ _aws_profile }}"
90+
managed_policies: "{{ _policies }}"
91+
inline_policies:
92+
- "s3:GetObject"
93+
policy_document: "{{ lookup('template', 'trust_apigateway.j2') }}"
94+
7595
- name: Configure Lambda IAM policies.
7696
ansible.builtin.include_tasks: lambda_iam.yml
77-
loop: "{{ aws_admin_tools.functions }}"
97+
loop: "{{ _api_without_s3 }}"
7898

7999
- name: Configure Lambda functions.
80100
ansible.builtin.include_tasks: lambda_functions.yml
81-
loop: "{{ aws_admin_tools.functions }}"
101+
loop: "{{ _api_without_s3 }}"
82102

83103
- name: Create WAF for API Gateway.
84104
ansible.builtin.include_role:
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 root 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
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello from S3!

roles/aws/aws_admin_tools/templates/get_infra_data_from_s3.py.j2

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"Version": "2012-10-17",
3+
"Statement": [
4+
{
5+
"Effect": "Allow",
6+
"Principal": {
7+
"Service": "apigateway.amazonaws.com"
8+
},
9+
"Action": "sts:AssumeRole"
10+
}
11+
]
12+
}

0 commit comments

Comments
 (0)