<commit_before>module Barcelona
  module Network
    class AutoscalingBuilder < CloudFormation::Builder
      # http://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-optimized_AMI.html
      # amzn2-ami-ecs-hvm-2.0
      ECS_OPTIMIZED_AMI_IDS = {
        "us-east-1"      => "ami-0669eafef622afea1",
        "us-east-2"      => "ami-09e6907b7bcde92d5",
        "us-west-1"      => "ami-08683f0adcda2044b",
        "us-west-2"      => "ami-05d33de432484ef34",
        "eu-west-1"      => "ami-02e8b3843287bc013",
        "eu-west-2"      => "ami-07d7061189d56c1eb",
        "eu-west-3"      => "ami-0591b048d04681f04",
        "eu-central-1"      => "ami-08c1d0b4f39f110d4",
        "ap-northeast-1"      => "ami-0fa2ad3f51711653a",
        "ap-northeast-2"      => "ami-0cc0a1657e6978861",
        "ap-southeast-1"      => "ami-01308997d4554a2f3",
        "ap-southeast-2"      => "ami-091469e4fdf00e0ab",
        "ca-central-1"      => "ami-0b30ea7d35c930001",
        "ap-south-1"      => "ami-0382cce5fd7023271",
        "sa-east-1"      => "ami-04625c1460c9a9d32",
      }

      def ebs_optimized_by_default?
        # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSOptimized.html
        !!(instance_type =~ /\A(a1|c4|c5.?|d2|f1|g3.?|h1|i3|m4|m5.?|p2|p3(dn)?|r4|r5.?|t3|u-.*|x1.?|z1d)\..*\z/)
      end

      def build_resources
        add_resource("AWS::AutoScaling::LaunchConfiguration",
                     "ContainerInstanceLaunchConfiguration") do |j|

          j.IamInstanceProfile ref("ECSInstanceProfile")
          j.ImageId ECS_OPTIMIZED_AMI_IDS[stack.district.region]
          j.InstanceType instance_type
          j.SecurityGroups [ref("InstanceSecurityGroup")]
          j.UserData instance_user_data
          j.EbsOptimized ebs_optimized_by_default?
          j.BlockDeviceMappings [
            # Root volume
            # https://docs.aws.amazon.com/AmazonECS/latest/developerguide/al2ami-storage-config.html
            {
              "DeviceName" => "/dev/xvda",
              "Ebs" => {
                "DeleteOnTermination" => true,
                "VolumeSize" => 100,
                "VolumeType" => "gp2"
              }
            }
          ]
        end<commit_msg>Update AMI to 2.0.20201028<commit_after>module Barcelona
  module Network
    class AutoscalingBuilder < CloudFormation::Builder
      # http://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-optimized_AMI.html
      # amzn2-ami-ecs-hvm-2.0
      ECS_OPTIMIZED_AMI_IDS = {
        "us-east-1"      => "ami-09bee01cc997a78a6",
        "us-east-2"      => "ami-0a9e12068cb98a01d",
        "us-west-1"      => "ami-0fa6c8d131a220017",
        "us-west-2"      => "ami-078c97cf1cefd1b38",
        "eu-west-1"      => "ami-0c9ef930279337028",
        "eu-west-2"      => "ami-0ce75da72db51b0f0",
        "eu-west-3"      => "ami-015011bcdf1c3377a",
        "eu-central-1"      => "ami-065c1e34da68f2b02",
        "ap-northeast-1"      => "ami-02265963d1614d04d",
        "ap-northeast-2"      => "ami-043f04a0533062573",
        "ap-southeast-1"      => "ami-0b68661b29b9e058c",
        "ap-southeast-2"      => "ami-00e4b147599c13588",
        "ca-central-1"      => "ami-0223c41fb4b45f725",
        "ap-south-1"      => "ami-0fa0107c4a8501039",
        "sa-east-1"      => "ami-0e598e30f638c5363",
      }

      def ebs_optimized_by_default?
        # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSOptimized.html
        !!(instance_type =~ /\A(a1|c4|c5.?|d2|f1|g3.?|h1|i3|m4|m5.?|p2|p3(dn)?|r4|r5.?|t3|u-.*|x1.?|z1d)\..*\z/)
      end

      def build_resources
        add_resource("AWS::AutoScaling::LaunchConfiguration",
                     "ContainerInstanceLaunchConfiguration") do |j|

          j.IamInstanceProfile ref("ECSInstanceProfile")
          j.ImageId ECS_OPTIMIZED_AMI_IDS[stack.district.region]
          j.InstanceType instance_type
          j.SecurityGroups [ref("InstanceSecurityGroup")]
          j.UserData instance_user_data
          j.EbsOptimized ebs_optimized_by_default?
          j.BlockDeviceMappings [
            # Root volume
            # https://docs.aws.amazon.com/AmazonECS/latest/developerguide/al2ami-storage-config.html
            {
              "DeviceName" => "/dev/xvda",
              "Ebs" => {
                "DeleteOnTermination" => true,
                "VolumeSize" => 100,
                "VolumeType" => "gp2"
              }
            }
          ]
        end
{'fraction_non_alphanumeric': 0.14615557049988792, 'fraction_numerical': 0.10020174848688634, 'mean_word_length': 2.010796221322537, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 43, 'https://': 4, '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': '403815', 'n_tokens_mistral': 1938, 'n_tokens_neox': 1699, 'n_words': 158}