<commit_before>Description: "Ufo ECS stack <%= @stack_name %>"
Parameters:
  # required
  Vpc:
    Description: Existing vpc id
    Type: AWS::EC2::VPC::Id
  ElbSubnets:
    Description: Existing subnet ids for ELB
    Type: List<AWS::EC2::Subnet::Id>
  EcsSubnets:
    Description: Existing subnet ids for ECS
    Type: List<AWS::EC2::Subnet::Id>
  EcsSecurityGroups:
    Description: Existing ecs security group ids
    Type: String
    Default: ''
  ElbSecurityGroups:
    Description: Existing elb security group ids. List with commas.
    Type: String
    Default: ''

  ElbTargetGroup:
    Description: Existing target group
    Type: String
    Default: '' # when blank the automatically created TargetGroup is used
  CreateElb:
    Description: Create elb
    Type: String
    Default: true
  EcsDesiredCount:
    Description: Ecs desired count
    Type: String
    Default: 1
  EcsTaskDefinition:
    Description: Ecs task definition arn
    Type: String

  # Using to keep state
  ElbEipIds:
    Description: ELB EIP Allocation ids to use for network load balancer
    Type: String
    Default: ''
  EcsSchedulingStrategy:
    Description: The scheduling strategy to use for the service
    Type: String
    Default: 'REPLICA'
Conditions:
  CreateElbIsTrue: !Equals [ !Ref CreateElb, true ]
  ElbTargetGroupIsBlank: !Equals [ !Ref ElbTargetGroup, '' ]
  CreateTargetGroupIsTrue: !And
  - !Condition CreateElbIsTrue
  - !Condition ElbTargetGroupIsBlank
  ElbSecurityGroupsIsBlank: !Equals [ !Ref ElbSecurityGroups, '' ]
  EcsSecurityGroupsIsBlank: !Equals [ !Ref EcsSecurityGroups, '' ]
  EcsDesiredCountIsBlank: !Equals [ !Ref EcsDesiredCount, '' ]
Resources:
  Elb:
    Type: AWS::ElasticLoadBalancingV2::LoadBalancer
    Condition: CreateElbIsTrue
    Properties:
<% if ENV['UFO_FORCE_ELB'] -%>
# Error: SetSubnets is not supported for load balancers of type 'network'
# Happens: When changing subnets for an ELB
# Solution: Rename the ELB to force a replacement of it
<% random = (0...3).map { (65 + rand(26)).chr }.join.downcase %>
      Name: <%= "#{@stack_name}-#{random}" %>
<% end -%>
      Type: <%= @elb_type %>
      Tags:
      - Key: Name
        Value: <%= @stack_name %>
<% if @elb_type == "application" -%>
      # Add additional extra security groups if parameters set
      SecurityGroups: !Split
        - ','
        - !If
          - ElbSecurityGroupsIsBlank
          - !Ref ElbSecurityGroup
          - !Join [',', [!Ref ElbSecurityGroups, !Ref ElbSecurityGroup]]
<% end -%>
<% if @elb_type == "network" && @subnet_mappings && !@subnet_mappings.empty? -%>
      SubnetMappings:
<% @subnet_mappings.each do |allocation_id, subnet_id| -%>
        - AllocationId: <%= allocation_id %>
          SubnetId: <%= subnet_id %>
<% end -%>
<% else -%>
      Subnets: !Ref ElbSubnets
<% end -%>
<%= custom_properties(:Elb) %>

  TargetGroup:
    Type: AWS::ElasticLoadBalancingV2::TargetGroup
    Condition: CreateTargetGroupIsTrue
    Properties:
      VpcId: !Ref Vpc
      Tags:
      - Key: Name
        Value: <%= @stack_name %>
<% if ENV['UFO_FORCE_TARGET_GROUP'] -%>
# When adding and removing EIPs
# Error: TargetGroup cannot be associated with more than one load balancer
# Solution: https://forums.aws.amazon.com/thread.jspa?threadID=254544
      Name: !Join
      - '-'
      - - <%= @stack_name %>
        - !Select [ 2, !Split [ '-', !GetAtt Elb.LoadBalancerName]]
<% end -%>
      Protocol: <%= @default_target_group_protocol %>
<% if @container[:network_mode] == "awsvpc" -%>
      TargetType: ip
<% end -%>
<% if @elb_type == "network" && @network_mode == "awsvpc" -%>
      # target groups with network load balancers need to check the container
      # port dirtectly and will be using
      HealthCheckPort: <%= @container[:port] %>
<% end -%>
<%= custom_properties(:TargetGroup) %><commit_msg>fix edge case when target group name is too long<commit_after>Description: "Ufo ECS stack <%= @stack_name %>"
Parameters:
  # required
  Vpc:
    Description: Existing vpc id
    Type: AWS::EC2::VPC::Id
  ElbSubnets:
    Description: Existing subnet ids for ELB
    Type: List<AWS::EC2::Subnet::Id>
  EcsSubnets:
    Description: Existing subnet ids for ECS
    Type: List<AWS::EC2::Subnet::Id>
  EcsSecurityGroups:
    Description: Existing ecs security group ids
    Type: String
    Default: ''
  ElbSecurityGroups:
    Description: Existing elb security group ids. List with commas.
    Type: String
    Default: ''

  ElbTargetGroup:
    Description: Existing target group
    Type: String
    Default: '' # when blank the automatically created TargetGroup is used
  CreateElb:
    Description: Create elb
    Type: String
    Default: true
  EcsDesiredCount:
    Description: Ecs desired count
    Type: String
    Default: 1
  EcsTaskDefinition:
    Description: Ecs task definition arn
    Type: String

  # Using to keep state
  ElbEipIds:
    Description: ELB EIP Allocation ids to use for network load balancer
    Type: String
    Default: ''
  EcsSchedulingStrategy:
    Description: The scheduling strategy to use for the service
    Type: String
    Default: 'REPLICA'
Conditions:
  CreateElbIsTrue: !Equals [ !Ref CreateElb, true ]
  ElbTargetGroupIsBlank: !Equals [ !Ref ElbTargetGroup, '' ]
  CreateTargetGroupIsTrue: !And
  - !Condition CreateElbIsTrue
  - !Condition ElbTargetGroupIsBlank
  ElbSecurityGroupsIsBlank: !Equals [ !Ref ElbSecurityGroups, '' ]
  EcsSecurityGroupsIsBlank: !Equals [ !Ref EcsSecurityGroups, '' ]
  EcsDesiredCountIsBlank: !Equals [ !Ref EcsDesiredCount, '' ]
Resources:
  Elb:
    Type: AWS::ElasticLoadBalancingV2::LoadBalancer
    Condition: CreateElbIsTrue
    Properties:
<% if ENV['UFO_FORCE_ELB'] -%>
# Error: SetSubnets is not supported for load balancers of type 'network'
# Happens: When changing subnets for an ELB
# Solution: Rename the ELB to force a replacement of it
<% random = (0...3).map { (65 + rand(26)).chr }.join.downcase %>
      Name: <%= "#{@stack_name}-#{random}" %>
<% end -%>
      Type: <%= @elb_type %>
      Tags:
      - Key: Name
        Value: <%= @stack_name %>
<% if @elb_type == "application" -%>
      # Add additional extra security groups if parameters set
      SecurityGroups: !Split
        - ','
        - !If
          - ElbSecurityGroupsIsBlank
          - !Ref ElbSecurityGroup
          - !Join [',', [!Ref ElbSecurityGroups, !Ref ElbSecurityGroup]]
<% end -%>
<% if @elb_type == "network" && @subnet_mappings && !@subnet_mappings.empty? -%>
      SubnetMappings:
<% @subnet_mappings.each do |allocation_id, subnet_id| -%>
        - AllocationId: <%= allocation_id %>
          SubnetId: <%= subnet_id %>
<% end -%>
<% else -%>
      Subnets: !Ref ElbSubnets
<% end -%>
<%= custom_properties(:Elb) %>

  TargetGroup:
    Type: AWS::ElasticLoadBalancingV2::TargetGroup
    Condition: CreateTargetGroupIsTrue
    Properties:
      VpcId: !Ref Vpc
      Tags:
      - Key: Name
        Value: <%= @stack_name %>
<% if ENV['UFO_FORCE_TARGET_GROUP'] -%>
# When adding and removing EIPs
# Error: TargetGroup cannot be associated with more than one load balancer
# Solution: https://forums.aws.amazon.com/thread.jspa?threadID=254544
# Note: we truncate the stack name because target group names can be only 32 chars long
      Name: !Join
      - '-'
      - - <%= @stack_name[0..-6] %>
        - !Select [ 2, !Split [ '-', !GetAtt Elb.LoadBalancerName]]
<% end -%>
      Protocol: <%= @default_target_group_protocol %>
<% if @container[:network_mode] == "awsvpc" -%>
      TargetType: ip
<% end -%>
<% if @elb_type == "network" && @network_mode == "awsvpc" -%>
      # target groups with network load balancers need to check the container
      # port dirtectly and will be using
      HealthCheckPort: <%= @container[:port] %>
<% end -%>
<%= custom_properties(:TargetGroup) %>
{'fraction_non_alphanumeric': 0.12217948717948718, 'fraction_numerical': 0.005384615384615384, 'mean_word_length': 3.3194905869324476, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 63, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5728273', 'n_tokens_mistral': 2650, 'n_tokens_neox': 2505, 'n_words': 687}