This repository was archived by the owner on Oct 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Updating cloud front role pr devel 2.x #2593
Closed
Closed
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
3f986be
Merge branch '2.x' of github.com:codeenigma/ce-provision into r71691-…
nfawbert 061ac44
Merge branch 'r71691-init-linotp-db-after-upgrade' into r71691-init-l…
nfawbert 91d671e
Merge pull request #2591 from codeenigma/sync-disabled-ldap-users-git…
nfawbert c6f60ad
Merge pull request #2583 from codeenigma/r71691-init-linotp-db-after-…
drazenCE e94a9d9
Adding-tasks-for-cf-function
ced6e4f
Merge branch 'Updating-CloudFront-role' into Updating-CloudFront-role…
07d27cc
Removing-.vscode-folder
acbd52a
Merge branch 'Updating-CloudFront-role' into Updating-CloudFront-role…
240f686
Updating-path-to-function-file
c6d18e4
Merge branch 'Updating-CloudFront-role' into Updating-CloudFront-role…
68f893e
Updating-path-to-function-file-2
5533c79
Merge branch 'Updating-CloudFront-role' into Updating-CloudFront-role…
cb17698
Updating-path-to-function-file-3
f581c69
Merge branch 'Updating-CloudFront-role' into Updating-CloudFront-role…
2ddef0e
Adding-region-to-lambda
fca5982
Merge branch 'Updating-CloudFront-role' into Updating-CloudFront-role…
f4fec93
Adding-option-for-adding-zip-file-for-lambda
632b224
Merge branch 'Updating-CloudFront-role' into Updating-CloudFront-role…
555cd66
Changing-module-name-for-copy-task
6793335
Merge branch 'Updating-CloudFront-role' into Updating-CloudFront-role…
96d97d6
Changing-module-name-for-copy-task-2
a8612f4
Merge branch 'Updating-CloudFront-role' into Updating-CloudFront-role…
6dd7b66
Changing-module-name-for-copy-task-3
c4d1f62
Merge branch 'Updating-CloudFront-role' into Updating-CloudFront-role…
7cd944a
Changing-module-name-for-copy-task-4
dadda26
Merge branch 'Updating-CloudFront-role' into Updating-CloudFront-role…
cc192ba
Finished-lambda-edge-t-cloudfront-function
a892d68
Merge branch 'Updating-CloudFront-role' into Updating-CloudFront-role…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,4 +61,3 @@ aws_cloudfront_distribution: | |
| cache_behaviors: [] # A list of cache behaviors same as default_cache_behavior with additional path_pattern var required. | ||
| enabled: true | ||
| purge_existing: true # Set to false to append entries instead of replacing them. | ||
| web_acl: false # Set to true to create Web ACL for WAF. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please put this line back:) |
||
16 changes: 16 additions & 0 deletions
16
roles/aws/aws_cloudfront_distribution/tasks/add_function.yml
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| - name: Set empty list. | ||
| ansible.builtin.set_fact: | ||
| _lambda_funct_list: [] | ||
|
|
||
| - name: Create dict. | ||
| ansible.builtin.set_fact: | ||
| _lambda_funct_list: "{{ _lambda_funct_list + [{'event_type': item.event_type, 'lambda_function_arn': aws_lambda._result[item.lambda_name].configuration.function_arn}] }}" | ||
| loop: "{{ _behavior.lambda_functions }}" | ||
|
|
||
| - name: Remove lambda_functions and add lambda_function_associations. | ||
| ansible.builtin.set_fact: | ||
| _processed_behavior: "{{ (_behavior | dict2items | rejectattr('key', 'equalto', 'lambda_functions') | items2dict) | combine({'lambda_function_associations': _lambda_funct_list}) }}" | ||
|
|
||
| - name: Append behaviour to list. | ||
| ansible.builtin.set_fact: | ||
| _processed_cache_behaviors: "{{ _processed_cache_behaviors + [_processed_behavior] }}" |
7 changes: 7 additions & 0 deletions
7
roles/aws/aws_cloudfront_distribution/tasks/check_function.yml
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| - name: Create CloudFront function. | ||
| ansible.builtin.include_tasks: create_cf_function.yml | ||
| when: _funct.type == 'cf' | ||
|
|
||
| - name: Create Lambda function. | ||
| ansible.builtin.include_tasks: create_lambda_function.yml | ||
| when: _funct.type == 'lambda' |
48 changes: 48 additions & 0 deletions
48
roles/aws/aws_cloudfront_distribution/tasks/create_cf_function.yml
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| - name: Check if CF function exists. | ||
| ansible.builtin.command: "aws cloudfront list-functions --query \"FunctionList.Items[?Name=='{{ _funct.name }}']\"" | ||
| register: _cf_funct_list_result | ||
|
|
||
| - name: Get details of CloudFront function | ||
| ansible.builtin.command: "aws cloudfront get-function --name {{ _funct.name }} /tmp/cf_funct" | ||
| register: _cf_funct_result | ||
| when: (_cf_funct_list_result.stdout | from_json) | length > 0 | ||
|
|
||
| - name: Setting command output into variable. | ||
| ansible.builtin.set_fact: | ||
| _cf_funct_result: "{{ _cf_funct_result.stdout | from_json }}" | ||
| when: (_cf_funct_list_result.stdout | from_json) | length > 0 | ||
|
|
||
| - name: Create config for CloudFront function. | ||
| ansible.builtin.template: | ||
| src: "config.j2" | ||
| dest: "/tmp/cf_config" | ||
| owner: controller | ||
| group: controller | ||
| mode: 0644 | ||
|
|
||
| - name: Create CF function. | ||
| ansible.builtin.command: "aws cloudfront create-function --function-config file:///tmp/cf_config --name {{ _funct.name }} --function-code fileb://{{ _ce_provision_build_dir }}/files/{{ _funct.code }}" | ||
| register: _cf_function | ||
| when: (_cf_funct_list_result.stdout | from_json) | length == 0 | ||
|
|
||
| - name: Setting previous command output into variable. | ||
| ansible.builtin.set_fact: | ||
| _cf_function: "{{ _cf_function.stdout | from_json }}" | ||
| when: (_cf_funct_list_result.stdout | from_json) | length == 0 | ||
|
|
||
| - name: Update CloudFront function. | ||
| ansible.builtin.command: "aws cloudfront update-function --name {{ _funct.name }} --if-match {{ _cf_funct_result.ETag }} --function-config file:///tmp/cf_config --function-code fileb://{{ _ce_provision_build_dir }}/files/{{ _funct.code }}" | ||
| register: _cf_function | ||
| when: (_cf_funct_list_result.stdout | from_json) | length > 0 | ||
|
|
||
| - name: Setting previous command output into variable. | ||
| ansible.builtin.set_fact: | ||
| _cf_function: "{{ _cf_function.stdout | from_json }}" | ||
| when: (_cf_funct_list_result.stdout | from_json) | length > 0 | ||
|
|
||
| - name: Update CloudFront function. | ||
| ansible.builtin.command: "aws cloudfront publish-function --name {{ _funct.name }} --if-match {{ _cf_function.ETag }}" | ||
|
|
||
| - name: Register aws_lambda results. | ||
| ansible.builtin.set_fact: | ||
| _function_results: "{{ _function_results + [_cf_function] }}" |
32 changes: 32 additions & 0 deletions
32
roles/aws/aws_cloudfront_distribution/tasks/create_lambda_function.yml
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| - name: Attach CloudWatch policy. | ||
| ansible.builtin.set_fact: | ||
| _policies: "{{ ['arn:aws:iam::aws:policy/AWSLambda_FullAccess'] + ['arn:aws:iam::aws:policy/CloudWatchLogsFullAccess'] }}" | ||
|
|
||
| - name: Create a Lambda@Edge role. | ||
| ansible.builtin.include_role: | ||
| name: aws/aws_iam_role | ||
| vars: | ||
| aws_iam_role: | ||
| name: "lambda_edge" | ||
| aws_profile: "{{ _aws_profile }}" | ||
| managed_policies: "{{ _policies }}" | ||
| inline_policies: | ||
| - "lambda:GetFunction" | ||
| policy_document: "{{ lookup('template', 'lambda_policy.json') }}" | ||
|
|
||
| - name: Create Lambda function. | ||
| ansible.builtin.include_role: | ||
| name: aws/aws_lambda | ||
| vars: | ||
| aws_lambda: | ||
| name: "{{ _funct.name }}" | ||
| region: "us-east-1" | ||
| description: "Lambda function for {{ _funct.name }}" | ||
| timeout: "5" | ||
| role: "{{ aws_iam_role._result['lambda_edge'] }}" | ||
| runtime: "{{ _funct.runtime }}" | ||
| function_file: "{{ _ce_provision_build_dir }}/files/{{ _funct.code }}" | ||
| s3_bucket: "codeenigma-{{ _aws_profile }}-general-storage-us-east-1" | ||
| s3_bucket_prefix: "lambda-functions" | ||
| tags: | ||
| Name: "{{ _funct.name }}" |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |
| ansible.builtin.include_role: | ||
| name: aws/aws_acl | ||
| when: | ||
| - aws_cloudfront_distribution.web_acl | ||
| # - aws_cloudfront_distribution.web_acl | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please put this condition back:) |
||
| - aws_acl.name is defined | ||
| - aws_acl.scope is defined | ||
| - aws_acl.scope == 'CLOUDFRONT' | ||
|
|
@@ -29,6 +29,93 @@ | |
| include_cookies: false # Set true to add cookies in logs | ||
| prefix: "cf-logging/" # Prefix for S3 object names | ||
|
|
||
| - name: Set empty list for function results. | ||
| ansible.builtin.set_fact: | ||
| _function_results: [] | ||
|
|
||
| - name: Create CloudFront function if defined. | ||
| ansible.builtin.include_tasks: check_function.yml | ||
| loop: "{{ aws_cloudfront_distribution.functions }}" | ||
| loop_control: | ||
| loop_var: _funct | ||
| when: aws_cloudfront_distribution.functions is defined | ||
|
|
||
| - name: Initialize processed cache behaviors list. | ||
| ansible.builtin.set_fact: | ||
| _processed_cache_behaviors: [] | ||
|
|
||
| - name: Process default cache behavior if it has lambda functions | ||
| ansible.builtin.include_tasks: add_function.yml | ||
| vars: | ||
| _behavior: "{{ aws_cloudfront_distribution.default_cache_behavior }}" | ||
| register: default_result | ||
| when: aws_cloudfront_distribution.default_cache_behavior.lambda_functions is defined | ||
|
|
||
| - name: Update default cache behavior | ||
| ansible.builtin.set_fact: | ||
| _default_cache_string: "{{ _processed_behavior }}" | ||
| when: aws_cloudfront_distribution.default_cache_behavior.lambda_functions is defined | ||
|
|
||
| - name: Create dict for default cache behavior | ||
| ansible.builtin.set_fact: | ||
| _default_cache: "{{ _default_cache_string | from_yaml }}" | ||
| when: aws_cloudfront_distribution.default_cache_behavior.lambda_functions is defined | ||
|
|
||
| - name: Create dict for default cache behavior | ||
| ansible.builtin.set_fact: | ||
| _default_cache: "{{ aws_cloudfront_distribution.default_cache_behavior }}" | ||
| when: aws_cloudfront_distribution.default_cache_behavior.lambda_functions is not defined | ||
|
|
||
| - name: Initialize processed cache behaviors list. | ||
| ansible.builtin.set_fact: | ||
| _processed_cache_behaviors: [] | ||
|
|
||
| - name: Extract the cache behaviors with Lambda function. | ||
| ansible.builtin.set_fact: | ||
| _cache_behavior_with_lambda: "{{ aws_cloudfront_distribution.cache_behaviors | selectattr('lambda_functions', 'defined') | list }}" | ||
|
|
||
| - name: Extract the cache behaviors with Lambda function. | ||
| ansible.builtin.set_fact: | ||
| _cache_behavior_without_lambda: "{{ aws_cloudfront_distribution.cache_behaviors | selectattr('lambda_functions', 'undefined') | list }}" | ||
|
|
||
| # Process behaviors with lambda functions | ||
| - name: Process behavior with lambda functions | ||
| ansible.builtin.include_tasks: add_function.yml | ||
| loop: "{{ _cache_behavior_with_lambda | default([]) }}" | ||
| loop_control: | ||
| loop_var: _behavior | ||
| when: _cache_behavior_with_lambda | length > 0 | ||
|
|
||
| # Add each behavior to the final list (processed or original) | ||
| - name: Add behavior to final list | ||
| ansible.builtin.set_fact: | ||
| _processed_cache_behaviors: "{{ _processed_cache_behaviors + _cache_behavior_without_lambda }}" | ||
|
|
||
| - name: Update cache behaviors in aws_cloudfront_distribution | ||
| ansible.builtin.set_fact: | ||
| _cache_behavior_with_lambda: "{{ {'cache_behaviors': _processed_cache_behaviors} }}" | ||
|
|
||
| - name: Extract the Lambda functios. | ||
| ansible.builtin.set_fact: | ||
| _lambda_functions: "{{ aws_cloudfront_distribution.functions | selectattr('type', '==', 'lambda') | list }}" | ||
|
|
||
| - name: Wait for Lambda function to be Active. | ||
| ansible.builtin.command: > | ||
| aws lambda get-function | ||
| --function-name {{ item.name }} | ||
| --query 'Configuration.[State, LastUpdateStatus]' | ||
| --region us-east-1 | ||
| register: lambda_status | ||
| until: (lambda_status.stdout | from_json)[0] == "Active" | ||
| retries: 30 | ||
| delay: 10 | ||
| changed_when: false | ||
| loop: "{{ _lambda_functions }}" | ||
|
|
||
| - name: Wait for 10 seconds. | ||
| ansible.builtin.wait_for: | ||
| timeout: 10 | ||
|
|
||
| - name: Create a CloudFront distribution. | ||
| community.aws.cloudfront_distribution: | ||
| profile: "{{ aws_cloudfront_distribution.aws_profile }}" | ||
|
|
@@ -38,8 +125,8 @@ | |
| aliases: "{{ aws_cloudfront_distribution.aliases }}" | ||
| origins: "{{ aws_cloudfront_distribution.origins }}" | ||
| web_acl_id: "{{ _created_acl.arn | default(omit) }}" | ||
| default_cache_behavior: "{{ aws_cloudfront_distribution.default_cache_behavior }}" | ||
| cache_behaviors: "{{ aws_cloudfront_distribution.cache_behaviors }}" | ||
| default_cache_behavior: "{{ _default_cache }}" | ||
| cache_behaviors: "{{ _processed_cache_behaviors }}" | ||
| validate_certs: "{{ aws_cloudfront_distribution.validate_certs }}" | ||
| viewer_certificate: "{{ aws_cloudfront_distribution.viewer_certificate }}" | ||
| purge_origins: "{{ aws_cloudfront_distribution.purge_existing }}" | ||
|
|
||
28 changes: 28 additions & 0 deletions
28
roles/aws/aws_cloudfront_distribution/tasks/process_all_behaviors.yml
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| - name: Process default cache behavior lambda functions. | ||
| when: aws_cloudfront_distribution.default_cache_behavior.lambda_functions is defined | ||
| block: | ||
| - name: Set empty list for default behavior. | ||
| ansible.builtin.set_fact: | ||
| _lambda_funct_list: [] | ||
|
|
||
| - name: Create lambda function associations for default behavior. | ||
| ansible.builtin.set_fact: | ||
| _lambda_funct_list: "{{ _lambda_funct_list + [{'event_type': item.event_type, 'lambda_function_arn': aws_lambda._result[item.lambda_name].configuration.function_arn}] }}" | ||
| loop: "{{ aws_cloudfront_distribution.default_cache_behavior.lambda_functions }}" | ||
|
|
||
| - name: Update default cache behavior. | ||
| ansible.builtin.set_fact: | ||
| aws_cloudfront_distribution: "{{ aws_cloudfront_distribution | combine({'default_cache_behavior': (aws_cloudfront_distribution.default_cache_behavior | dict2items | rejectattr('key', 'equalto', 'lambda_functions') | items2dict) | combine({'lambda_function_associations': _lambda_funct_list})}) }}" | ||
|
|
||
| - name: Process cache behaviors lambda functions. | ||
| ansible.builtin.set_fact: | ||
| updated_cache_behaviors: "{{ updated_cache_behaviors | default([]) + [processed_behavior] }}" | ||
| vars: | ||
| lambda_list: "{{ item.lambda_functions | map('combine', {'lambda_function_arn': aws_lambda._result[item.lambda_name].configuration.function_arn}) | map('dict2items') | map('rejectattr', 'key', 'equalto', 'lambda_name') | map('items2dict') | list if item.lambda_functions is defined else [] }}" | ||
| processed_behavior: "{{ (item | dict2items | rejectattr('key', 'equalto', 'lambda_functions') | items2dict) | combine({'lambda_function_associations': lambda_list}) if item.lambda_functions is defined else item }}" | ||
| loop: "{{ aws_cloudfront_distribution.cache_behaviors | default([]) }}" | ||
|
|
||
| - name: Update cache behaviors in main structure. | ||
| ansible.builtin.set_fact: | ||
| aws_cloudfront_distribution: "{{ aws_cloudfront_distribution | combine({'cache_behaviors': updated_cache_behaviors}) }}" | ||
| when: updated_cache_behaviors is defined |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| { | ||
| "Comment": "{{ _funct.description }}", | ||
| "Runtime": "{{ _funct.runtime }}"{% if _funct.kvs %}, | ||
| "KeyValueStoreAssociations": { | ||
| "Quantity": 1, | ||
| "Items": [ | ||
| { | ||
| "KeyValueStoreARN": "{{_funct.kvs }}" | ||
| } | ||
| ] | ||
| } | ||
| {% endif %} | ||
| } |
15 changes: 15 additions & 0 deletions
15
roles/aws/aws_cloudfront_distribution/templates/lambda_policy.json
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "Version": "2012-10-17", | ||
| "Statement": [ | ||
| { | ||
| "Effect": "Allow", | ||
| "Principal": { | ||
| "Service": [ | ||
| "lambda.amazonaws.com", | ||
| "edgelambda.amazonaws.com" | ||
| ] | ||
| }, | ||
| "Action": "sts:AssumeRole" | ||
| } | ||
| ] | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please put this line back:)