Guillaume Coré
2018-08-31 3e2a8d22546870750fafc39a3f56c6e3253360c7
use s3 when CloudFormation template is too big

When template is > 51kbyte, push it to s3
2 files modified
85 ■■■■■ changed files
ansible/roles/infra-ec2-template-create/tasks/main.yml 33 ●●●●● patch | view | raw | blame | history
ansible/roles/infra-ec2-template-generate/tasks/main.yml 52 ●●●●● patch | view | raw | blame | history
ansible/roles/infra-ec2-template-create/tasks/main.yml
@@ -21,7 +21,7 @@
        - cloudformation_out is defined
        - cloudformation_out is failed
    - name: Launch CloudFormation template
    - name: Launch CloudFormation template (local)
      # environment:
      #   AWS_ACCESS_KEY_ID: "{{aws_access_key_id}}"
      #   AWS_SECRET_ACCESS_KEY: "{{aws_secret_access_key}}"
@@ -48,6 +48,37 @@
        )
      retries: "{{ cloudformation_retries | default(3) }}"
      delay: "{{ cloudformation_retry_delay | default(30) }}"
      when: stat_template.stat.size <= 51200
      ignore_errors: yes
    - name: Launch CloudFormation template (from S3)
      # environment:
      #   AWS_ACCESS_KEY_ID: "{{aws_access_key_id}}"
      #   AWS_SECRET_ACCESS_KEY: "{{aws_secret_access_key}}"
      #   AWS_DEFAULT_REGION: "{{aws_region_loop|d(aws_region)}}"
      cloudformation:
        aws_access_key: "{{ aws_access_key_id }}"
        aws_secret_key: "{{ aws_secret_access_key }}"
        stack_name: "{{ project_tag }}"
        state: "present"
        region: "{{ aws_region_loop | d(aws_region) | d(region) | d('us-east-1')}}"
        # rollback is unreliable, it can make this task hang forever.
        disable_rollback: true
        template_url: "https://s3.amazonaws.com/redhat-gpe-cloudformation-templates/{{env_type}}.{{guid}}.{{cloud_provider}}_cloud_template"
        tags: "{{ cf_tags | combine(cloud_tags_final)}}"
      tags:
        - aws_infrastructure_deployment
        - provision_cf_template
      register: cloudformation_out
      until: >-
        cloudformation_out is succeeded
        or (
          'output' in cloudformation_out
          and cloudformation_out.output in ["Stack CREATE complete", "Stack is already up-to-date."]
        )
      retries: "{{ cloudformation_retries | default(3) }}"
      delay: "{{ cloudformation_retry_delay | default(30) }}"
      when: stat_template.stat.size > 51200
      ignore_errors: yes
    - name: debug cloudformation
ansible/roles/infra-ec2-template-generate/tasks/main.yml
@@ -24,9 +24,40 @@
    - gen_cf_template
    - minify_template
######################### Copy CF Template to S3 if too big
- name: Stat CloudFormation template
  stat:
    path: "{{ANSIBLE_REPO_PATH}}/workdir/{{ env_type }}.{{ guid }}.{{cloud_provider}}_cloud_template"
  register: stat_template
  tags:
    - aws_infrastructure_deployment
    - gen_cf_template
- when:
    stat_template.stat.size > 51200
  tags:
    - aws_infrastructure_deployment
    - gen_cf_template
  environment:
    AWS_ACCESS_KEY_ID: "{{aws_access_key_id}}"
    AWS_SECRET_ACCESS_KEY: "{{aws_secret_access_key}}"
    AWS_DEFAULT_REGION: "{{aws_region_final|d(aws_region)}}"
  block:
    - name: Create bucket
      s3_bucket:
        name: redhat-gpe-cloudformation-templates
    - name: Copy Template to S3
      aws_s3:
        bucket: redhat-gpe-cloudformation-templates
        object: "{{ env_type }}.{{ guid }}.{{cloud_provider}}_cloud_template"
        src: "{{ANSIBLE_REPO_PATH}}/workdir/{{ env_type }}.{{ guid }}.{{cloud_provider}}_cloud_template"
        mode: put
######################### Validate CF Template
- name: validate cloudformation template
- name: validate cloudformation template (local)
  environment:
    AWS_ACCESS_KEY_ID: "{{aws_access_key_id}}"
    AWS_SECRET_ACCESS_KEY: "{{aws_secret_access_key}}"
@@ -42,5 +73,24 @@
  tags:
    - aws_infrastructure_deployment
    - validate_cf_template
  when: stat_template.stat.size <= 51200
- name: validate cloudformation template (S3)
  environment:
    AWS_ACCESS_KEY_ID: "{{aws_access_key_id}}"
    AWS_SECRET_ACCESS_KEY: "{{aws_secret_access_key}}"
    AWS_DEFAULT_REGION: "{{aws_region_final|d(aws_region)}}"
  command: >-
    aws cloudformation validate-template
    --region {{ aws_region_final | d(aws_region) | default(region) | default('us-east-1')}}
    --template-url https://s3.amazonaws.com/redhat-gpe-cloudformation-templates/{{env_type}}.{{guid}}.{{cloud_provider}}_cloud_template
  changed_when: false
  register: cloudformation_validation
  until: cloudformation_validation is succeeded
  delay: 20
  tags:
    - aws_infrastructure_deployment
    - validate_cf_template
  when: stat_template.stat.size > 51200
######################### Launch CF Template