Skip to content

Updating cloud front role pr 2.x #2592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions roles/aws/aws_cloudfront_distribution/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
---
aws_cloudfront_distribution:
functions:
- name: "example-cf-function"
type: "cf" # This can be cf or lambda
description: "This is example function."
runtime: "cloudfront-js-2.0" # Can be either cloudfront-js-2.0 or cloudfront-js-1.0
kvs: "" # arn of KeyValueStore
code: "function.js"
- name: "example-lambda-edge-function"
type: "lambda" # This can be cf or lambda
description: "This is example lambda function."
timeout: 5
runtime: "nodejs22.x" # Lambda runtimes are defined here https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html
kvs: "" # arn of KeyValueStore
code: "function.js"
aws_profile: "{{ _aws_profile }}"
region: "{{ _aws_region }}"
tags: {}
Expand Down
24 changes: 24 additions & 0 deletions roles/aws/aws_cloudfront_distribution/tasks/create_function.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
- name: Create config for CloudFront function.
ansible.builtin.template:
src: config.j2
dest: cf_config
owner: controller
group: controller
mode: 0644
when: _funct.type == cf

- name: Create CF function.
ansible.builtin.command:
cmd: "aws cloudfront create-function --function-config file://cf_config --name new-funct --profile dummy --function-code fileb://{{ _ce_provision_build_dir }}/files/{{ _funct.code }}"
when: _funct.type == cf
register: _cf_function

- name: Setting previous command output into variable.
ansible.builtin.set_fact:
_cf_function: "{{ _cf_function.stdout | from_json }}"
when: _funct.type == cf

- name: Register aws_lambda results.
ansible.builtin.set_fact:
_function_results: "{{ _function_results + [_cf_function] }}"
when: _funct.type == cf
11 changes: 11 additions & 0 deletions roles/aws/aws_cloudfront_distribution/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@
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: create_function.yml
loop: "{{ aws_cloudfront_distribution.functions }}"
loop_control:
loop_var: _funct
when: aws_cloudfront_distribution.functions is defined

- name: Create a CloudFront distribution.
community.aws.cloudfront_distribution:
profile: "{{ aws_cloudfront_distribution.aws_profile }}"
Expand Down
Empty file.
14 changes: 14 additions & 0 deletions roles/aws/aws_cloudfront_distribution/templates/config.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Comment": "{{ _funct.description }}",
"Runtime": "{{ _funct.runtime }}",
{% if _funct.kvs %}
"KeyValueStoreAssociations": {
"Quantity": 1,
"Items": [
{
"KeyValueStoreARN": {{_funct.kvs }}
}
]
}
{% endif %}
}
1 change: 1 addition & 0 deletions roles/aws/aws_lambda/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
aws_lambda:
name: "lambda_function_name"
description: "Description for AWS Lambda function"
region: "{{ _aws_region }}"
timeout: "20" # Maximum number of seconds before function times out
handler: "lambda_handler" # Name of main function
s3_bucket: "codeenigma-{{ _aws_profile }}-general-storage-{{ _aws_region }}"
Expand Down
29 changes: 27 additions & 2 deletions roles/aws/aws_lambda/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,51 @@
vars:
aws_s3_bucket:
profile: "{{ _aws_profile }}"
region: "{{ _aws_region }}"
region: "{{ aws_lambda.region }}"
name: "{{ aws_lambda.s3_bucket }}"
tags: []
state: "present"

- name: Check string type using regex
ansible.builtin.set_fact:
string_type: >-
{%- if input_string | regex_search('^https?://') -%}
url
{%- elif input_string | regex_search('\.zip$', ignorecase=True) -%}
zip
{%- else -%}
single
{%- endif -%}
vars:
input_string: "{{ aws_lambda.function_file }}"

- name: Check and clean previous Lambda function.
ansible.builtin.file:
path: "{{ _ce_provision_build_dir }}/{{ aws_lambda.name }}.py"
state: absent
when: string_type == 'single'

- name: Write Lambda function.
ansible.builtin.copy:
content: "{{ aws_lambda.function_file }}"
dest: "{{ _ce_provision_build_dir }}/{{ aws_lambda.name }}.py"
when: string_type == 'single'

- name: Create a zip archive of Lambda function.
community.general.archive:
path: "{{ _ce_provision_build_dir }}/{{ aws_lambda.name }}.py"
dest: "{{ _ce_provision_build_dir }}/{{ aws_lambda.name }}.zip"
format: zip
when: string_type == 'single'

- name: Copy a zip archive of Lambda function.
ansible.builtin.copy:
src: "{{ aws_lambda.function_file }}"
dest: "{{ _ce_provision_build_dir }}/{{ aws_lambda.name }}.zip"
owner: controller
group: controller
mode: '0644'
when: string_type == 'zip'

- name: Place Lambda function in S3 bucket.
amazon.aws.s3_object:
Expand All @@ -36,7 +61,7 @@
amazon.aws.lambda:
name: "{{ aws_lambda.name }}"
description: "{{ aws_lambda.description }}"
region: "{{ _aws_region }}"
region: "{{ aws_lambda.region }}"
timeout: "{{ aws_lambda.timeout }}"
s3_bucket: "{{ aws_lambda.s3_bucket }}"
s3_key: "{{ aws_lambda.s3_bucket_prefix }}/{{ aws_lambda.name }}.zip"
Expand Down
Loading