| 1 |
--- |
| 2 |
- name: Integration testing for lambda_policy |
| 3 |
module_defaults: |
| 4 |
group/aws: |
| 5 |
access_key: "{{ aws_access_key }}" |
| 6 |
secret_key: "{{ aws_secret_key }}" |
| 7 |
session_token: "{{ security_token | default(omit) }}" |
| 8 |
region: "{{ aws_region }}" |
| 9 |
block: |
| 10 |
- name: Create minimal lambda role |
| 11 |
amazon.aws.iam_role: |
| 12 |
name: "{{ lambda_role_name }}" |
| 13 |
assume_role_policy_document: '{{ lookup("file", "minimal_trust_policy.json") }}' |
| 14 |
create_instance_profile: false |
| 15 |
managed_policies: |
| 16 |
- arn:aws:iam::aws:policy/AWSXrayWriteOnlyAccess |
| 17 |
register: iam_role |
| 18 |
- name: Wait 10 seconds for role to become available |
| 19 |
ansible.builtin.pause: |
| 20 |
seconds: 10 |
| 21 |
|
| 22 |
- name: Test with no parameters |
| 23 |
amazon.aws.lambda_policy: |
| 24 |
register: result |
| 25 |
ignore_errors: true |
| 26 |
- name: Assert failure when called with no parameters |
| 27 |
ansible.builtin.assert: |
| 28 |
that: |
| 29 |
- result.failed |
| 30 |
- 'result.msg.startswith("missing required arguments: ")' |
| 31 |
- '"action" in result.msg' |
| 32 |
- '"function_name" in result.msg' |
| 33 |
- '"principal" in result.msg' |
| 34 |
- '"statement_id" in result.msg' |
| 35 |
|
| 36 |
- name: Move lambda into place for archive module |
| 37 |
ansible.builtin.copy: |
| 38 |
src: mini_http_lambda.py |
| 39 |
dest: "{{ output_dir }}/mini_http_lambda.py" |
| 40 |
mode: preserve |
| 41 |
- name: Bundle lambda into a zip |
| 42 |
register: zip_res |
| 43 |
community.general.archive: |
| 44 |
format: zip |
| 45 |
path: "{{ output_dir }}/mini_http_lambda.py" |
| 46 |
dest: "{{ output_dir }}/mini_http_lambda.zip" |
| 47 |
mode: "0644" |
| 48 |
- name: Create minimal lambda role |
| 49 |
amazon.aws.iam_role: |
| 50 |
name: ansible_lambda_role |
| 51 |
assume_role_policy_document: "{{ lookup('file', 'minimal_trust_policy.json', convert_data=False) }}" |
| 52 |
create_instance_profile: false |
| 53 |
register: iam_role |
| 54 |
- name: Wait 10 seconds for role to become available |
| 55 |
ansible.builtin.pause: |
| 56 |
seconds: 10 |
| 57 |
- name: Test state=present - upload the lambda |
| 58 |
amazon.aws.lambda: |
| 59 |
name: "{{ lambda_function_name }}" |
| 60 |
runtime: python3.9 |
| 61 |
handler: mini_http_lambda.handler |
| 62 |
role: "{{ lambda_role_name }}" |
| 63 |
zip_file: "{{ zip_res.dest }}" |
| 64 |
register: lambda_result |
| 65 |
- name: Get the aws account ID for use in future commands |
| 66 |
amazon.aws.aws_caller_info: {} |
| 67 |
register: aws_caller_info |
| 68 |
- name: Register lambda uri for use in template |
| 69 |
ansible.builtin.set_fact: |
| 70 |
mini_lambda_uri: arn:aws:apigateway:{{ aws_region }}:lambda:path/2015-03-31/functions/arn:aws:lambda:{{ aws_region }}:{{ aws_caller_info.account }}:function:{{ |
| 71 |
lambda_result.configuration.function_name }}/invocations |
| 72 |
- name: Build API file |
| 73 |
ansible.builtin.template: |
| 74 |
src: endpoint-test-swagger-api.yml.j2 |
| 75 |
dest: "{{ output_dir }}/endpoint-test-swagger-api.yml.j2" |
| 76 |
mode: "0644" |
| 77 |
- name: Deploy new API |
| 78 |
community.aws.api_gateway: |
| 79 |
api_file: "{{ output_dir }}/endpoint-test-swagger-api.yml.j2" |
| 80 |
stage: lambdabased |
| 81 |
register: create_result |
| 82 |
- name: Register api id for later |
| 83 |
ansible.builtin.set_fact: |
| 84 |
api_id: "{{ create_result.api_id }}" |
| 85 |
- name: Check API fails with permissions failure |
| 86 |
ansible.builtin.uri: |
| 87 |
url: https://{{ create_result.api_id }}.execute-api.{{ aws_region }}.amazonaws.com/lambdabased/mini/Mr_Ansible_Tester |
| 88 |
register: unauth_uri_result |
| 89 |
ignore_errors: true |
| 90 |
- name: Assert internal server error due to permissions |
| 91 |
ansible.builtin.assert: |
| 92 |
that: |
| 93 |
- unauth_uri_result is failed |
| 94 |
- unauth_uri_result.status == 500 |
| 95 |
- name: Give api gateway execute permissions on lambda |
| 96 |
amazon.aws.lambda_policy: |
| 97 |
function_name: "{{ lambda_function_name }}" |
| 98 |
state: present |
| 99 |
statement_id: api-gateway-invoke-lambdas |
| 100 |
action: lambda:InvokeFunction |
| 101 |
principal: apigateway.amazonaws.com |
| 102 |
source_arn: arn:aws:execute-api:{{ aws_region }}:{{ aws_caller_info.account }}:*/* |
| 103 |
- name: Try again but with ARN |
| 104 |
amazon.aws.lambda_policy: |
| 105 |
function_name: "{{ lambda_result.configuration.function_arn }}" |
| 106 |
state: present |
| 107 |
statement_id: api-gateway-invoke-lambdas |
| 108 |
action: lambda:InvokeFunction |
| 109 |
principal: apigateway.amazonaws.com |
| 110 |
source_arn: arn:aws:execute-api:{{ aws_region }}:{{ aws_caller_info.account }}:*/* |
| 111 |
- name: Wait for permissions to propagate |
| 112 |
ansible.builtin.pause: |
| 113 |
seconds: 5 |
| 114 |
- name: Check API works with execute permissions |
| 115 |
ansible.builtin.uri: |
| 116 |
url: https://{{ create_result.api_id }}.execute-api.{{ aws_region }}.amazonaws.com/lambdabased/mini/Mr_Ansible_Tester |
| 117 |
register: uri_result |
| 118 |
- name: Assert API works success |
| 119 |
ansible.builtin.assert: |
| 120 |
that: |
| 121 |
- uri_result != {} |
| 122 |
- name: Deploy new API |
| 123 |
community.aws.api_gateway: |
| 124 |
api_file: "{{ output_dir }}/endpoint-test-swagger-api.yml.j2" |
| 125 |
stage: lambdabased |
| 126 |
register: create_result |
| 127 |
ignore_errors: true |
| 128 |
always: |
| 129 |
- name: Destroy lambda for test cleanup if created |
| 130 |
amazon.aws.lambda: |
| 131 |
name: "{{ lambda_function_name }}" |
| 132 |
state: absent |
| 133 |
register: result |
| 134 |
ignore_errors: true |
| 135 |
- name: Destroy API for test cleanup if created |
| 136 |
community.aws.api_gateway: |
| 137 |
state: absent |
| 138 |
api_id: "{{ api_id }}" |
| 139 |
register: destroy_result |
| 140 |
ignore_errors: true |
| 141 |
- name: Clean up test role |
| 142 |
amazon.aws.iam_role: |
| 143 |
name: "{{ lambda_role_name }}" |
| 144 |
state: absent |
| 145 |
ignore_errors: true |