Skip to content

Commit

Permalink
Do not ignore errors when deleting api gateway on failure
Browse files Browse the repository at this point in the history
  • Loading branch information
abikouo committed Aug 14, 2023
1 parent 85404d0 commit 6fbd416
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# awsconfig_apigateway_with_lambda_integration

A role to create/delete an API gateway with lambda function integration.
the role produces a variable **awsconfig_apigateway_with_lambda_integration\_\_invoke_url** that contains the URL to invoke API gateway.
the role produces variables **awsconfig_apigateway_with_lambda_integration\_\_invoke_url** that contains the URL to invoke API gateway and **awsconfig_apigateway_with_lambda_integration\_\_id** that contains the id of the API gateway created.

## Requirements

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

- name: Define API gateway id as variable
ansible.builtin.set_fact:
awsconfig_apigateway_with_lambda_integration_id: "{{ awsconfig_apigateway_with_lambda_integration__create_apigateway.api_id }}"
awsconfig_apigateway_with_lambda_integration__id: "{{ awsconfig_apigateway_with_lambda_integration__create_apigateway.api_id }}"

- name: Give API gateway permission to invoke lambda function
amazon.aws.lambda_policy:
Expand All @@ -40,11 +40,11 @@
statement_id: "AllowExecutionFromAPIGateway"
action: "lambda:InvokeFunction"
principal: "apigateway.amazonaws.com"
source_arn: "arn:aws:execute-api:{{ awsconfig_apigateway_with_lambda_integration__awsregion }}:{{ awsconfig_apigateway_with_lambda_integration__caller_info.account }}:{{ awsconfig_apigateway_with_lambda_integration_id }}/*/*"
source_arn: "arn:aws:execute-api:{{ awsconfig_apigateway_with_lambda_integration__awsregion }}:{{ awsconfig_apigateway_with_lambda_integration__caller_info.account }}:{{ awsconfig_apigateway_with_lambda_integration__id }}/*/*"

- name: Define API gateway invoke url
- name: Define API gateway invoke url and API gateway identifier
ansible.builtin.set_fact:
awsconfig_apigateway_with_lambda_integration__invoke_url: "https://{{ awsconfig_apigateway_with_lambda_integration_id }}.execute-api.{{ awsconfig_apigateway_with_lambda_integration__awsregion }}.amazonaws.com/{{ awsconfig_apigateway_with_lambda_integration_stage_name }}"
awsconfig_apigateway_with_lambda_integration__invoke_url: "https://{{ awsconfig_apigateway_with_lambda_integration__id }}.execute-api.{{ awsconfig_apigateway_with_lambda_integration__awsregion }}.amazonaws.com/{{ awsconfig_apigateway_with_lambda_integration_stage_name }}"

always:
- name: Delete temporary file
Expand All @@ -64,11 +64,22 @@
- awsconfig_apigateway_with_lambda_integration_id is not defined
- awsconfig_apigateway_with_lambda_integration_tags is not defined

- name: Delete API gateway
- name: Delete API Gateway using identifier
when: awsconfig_apigateway_with_lambda_integration_id is defined
block:
- name: Get API gateway info
community.aws.api_gateway_info:
register: awsconfig_apigateway_with_lambda_integration__rest_apis

- name: Delete API gateway using identifier
community.aws.api_gateway:
state: absent
api_id: "{{ awsconfig_apigateway_with_lambda_integration_id }}"
when: awsconfig_apigateway_with_lambda_integration__rest_apis.rest_apis | selectattr('id', 'equalto', awsconfig_apigateway_with_lambda_integration_id) | list | length > 0

- name: Delete API gateway using tags
community.aws.api_gateway:
state: absent
api_id: "{{ awsconfig_apigateway_with_lambda_integration_id | default(omit) }}"
name: "{{ awsconfig_apigateway_with_lambda_integration_api_name | default(awsconfig_apigateway_with_lambda_integration_default_api_name) }}"
tags: "{{ awsconfig_apigateway_with_lambda_integration_tags | default(omit) }}"
lookup: "{{ awsconfig_apigateway_with_lambda_integration_id is defined | ternary('id', 'tag') }}"
ignore_errors: true
tags: "{{ awsconfig_apigateway_with_lambda_integration_tags }}"
lookup: "tag"
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,6 @@
ansible.builtin.set_fact:
awsconfig_apigateway_with_lambda_integration__lambda_arn: "{{ awsconfig_apigateway_with_lambda_integration__updload_lambda.configuration.function_arn }}"

rescue:
- name: Delete Lambda function
amazon.aws.lambda:
name: "{{ awsconfig_apigateway_with_lambda_integration__lambda_name }}"
state: absent

- name: Delete IAM role name
community.aws.iam_role:
name: "{{ awsconfig_apigateway_with_lambda_integration__iam_role_name }}"
state: absent

- name: Exist as an error has occurred
ansible.builtin.fail:
msg: "Something went wrong while trying to create lambda function"

always:
- name: Delete temporary file
ansible.builtin.file:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@
register: __uri_result
failed_when: __uri_result is successful

rescue:
always:
- name: Delete API Gateway
ansible.builtin.include_role:
name: cloud.aws_ops.awsconfig_apigateway_with_lambda_integration
vars:
awsconfig_apigateway_with_lambda_integration_operation: delete
awsconfig_apigateway_with_lambda_integration_id: "{{ awsconfig_apigateway_with_lambda_integration__id }}"
when: awsconfig_apigateway_with_lambda_integration__id is defined

always:
- name: Delete temporary directory
ansible.builtin.file:
path: "{{ __tempdir.path }}"
Expand Down

0 comments on commit 6fbd416

Please sign in to comment.