cnf-init выдает ошибку Python в ubuntu ami - PullRequest
0 голосов
/ 21 июня 2020

Я использую приведенный ниже шаблон облачной информации aws, чтобы развернуть группу автомасштабирования, и он выдает ошибку python, когда я пытаюсь запустить cfn-init. Я перепробовал все, что мог придумать, чтобы решить эту проблему, но теперь я полностью озадачен. Я получаю следующее сообщение об ошибке:

Error occurred during build: list indices must be integers, not unicode

Это мой шаблон Cloudformation:

Description: 'Autoscaling groups'

Parameters:
  DeploymentProfileARN: 
    Description: The ARN of the iam group with deployment permissions.
    Type: String
  PublicSecurityGroup: 
    Description: The security group for use with ec2.
    Type: String
  TargetGroup:
    Description: The load balancer target group
    Type: String
  Subnets: 
    Description: The load balancer target group
    Type: 'List<AWS::EC2::Subnet::Id>'
  DomainUrl:
    Description: The url of the site excluding any sub domains eg. "example.com"
    Type: String
  EnvironmentName:
    Description: An environment name that will be prefixed to resource names
    Type: String
    Default: Dev
    AllowedValues: [Prod, Uat, Dev]
  MasterDataBaseAddress:
    Description: The address of the master database
    Type: String

Resources:
  AuthScalingLaunchConfig:
    Type: AWS::AutoScaling::LaunchConfiguration
    Metadata: 
      AWS::CloudFormation::Init:
        nginx:
          packages:
            apt: 
              - "nginx-core"
          files:
            '/tmp/test':
              content: !Sub |
                test
            '/etc/nginx/sites-enabled/default':
              content: !Sub |
                  upstream phoenix {
                    server 127.0.0.1:4000 max_fails=5 fail_timeout=60s;
                  }
                  server {
                    listen 80 default_server;
                    server_name .${DomainUrl};
                    location / {
                      allow all;
                      proxy_http_version 1.1;
                      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                      proxy_set_header Host $http_host;
                      proxy_set_header X-Cluster-Client-Ip $remote_addr;
                      proxy_set_header Upgrade $http_upgrade;
                      proxy_set_header Connection "upgrade";
                      proxy_pass http://phoenix;
                    }
                  }
          commands:
            01-restartNginx:
              command: service nginx restart
        configSets:
          setup:
            - "nginx"
    Properties:
      ImageId: "ami-0701e7be9b2a77600"
      InstanceType: t1.micro
      KeyName: "main"
      IamInstanceProfile: !Ref DeploymentProfileARN
      SecurityGroups:
        - !Ref PublicSecurityGroup
      UserData:
        Fn::Base64: !Sub |
          #!/bin/bash

          # install cfn
          apt update
          apt install -y python-pip 
          mkdir -p /opt/aws/bin
          pip install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gpip

          cfn-init --stack ${AWS::StackName} --resource AuthScalingLaunchConfig --region ${AWS::Region} --configsets setup
          service cfn-hup start
  AutoScalingGroup:
    Type: AWS::AutoScaling::AutoScalingGroup
    Properties: 
      AvailabilityZones: 
        - !Select [ 0, !GetAZs "" ]
        - !Select [ 1, !GetAZs "" ]
      LaunchConfigurationName: !Ref AuthScalingLaunchConfig
      TargetGroupARNs:
        - !Ref TargetGroup
      HealthCheckGracePeriod: "60"
      HealthCheckType: EC2
      VPCZoneIdentifier: !Ref Subnets
      MaxSize: "10"
      DesiredCapacity: "1"
      MinSize: "1"

Любая помощь или направление в правильном направлении было бы замечательно. Я пробовал использовать разные версии aws -cfn- bootstrap, но все они вызывали у меня одну и ту же проблему.

Ответы [ 2 ]

1 голос
/ 22 июня 2020

Мне удалось заставить это работать. Для будущих поколений это сработало:

Description: 'Autoscaling groups'

Parameters:
  DeploymentProfileARN: 
    Description: The ARN of the iam group with deployment permissions.
    Type: String
  PublicSecurityGroup: 
    Description: The security group for use with ec2.
    Type: String
  TargetGroup:
    Description: The load balancer target group
    Type: String
  Subnets: 
    Description: The load balancer target group
    Type: 'List<AWS::EC2::Subnet::Id>'
  DomainUrl:
    Description: The url of the site excluding any sub domains eg. "example.com"
    Type: String
  EnvironmentName:
    Description: An environment name that will be prefixed to resource names
    Type: String
    Default: Dev
    AllowedValues: [Prod, Uat, Dev]
  MasterDataBaseAddress:
    Description: The address of the master database
    Type: String

Resources:
  AuthScalingLaunchConfig:
    Type: AWS::AutoScaling::LaunchConfiguration
    Metadata: 
      AWS::CloudFormation::Init:
        nginx:
          packages:
            apt:
              'nginx-core': []
          files:
            '/etc/nginx/sites-enabled/default':
              content: !Sub |
                  upstream phoenix {
                    server 127.0.0.1:4000 max_fails=5 fail_timeout=60s;
                  }
                  server {
                    listen 80 default_server;
                    server_name .${DomainUrl};
                    location / {
                      allow all;
                      proxy_http_version 1.1;
                      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                      proxy_set_header Host $http_host;
                      proxy_set_header X-Cluster-Client-Ip $remote_addr;
                      proxy_set_header Upgrade $http_upgrade;
                      proxy_set_header Connection "upgrade";
                      proxy_pass http://phoenix;
                    }
                  }
          commands:
            01-restartNginx:
              command: service nginx restart
        configSets:
          setup:
            - "nginx"
    Properties:
      ImageId: "ami-0701e7be9b2a77600"
      InstanceType: t1.micro
      KeyName: "main"
      IamInstanceProfile: !Ref DeploymentProfileARN
      SecurityGroups:
        - !Ref PublicSecurityGroup
      UserData:
        Fn::Base64: !Sub |
          #!/bin/bash

          # install cfn
          apt-get -y update
          apt-get -y install python-pip
          pip install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz
          cp /usr/local/init/ubuntu/cfn-hup /etc/init.d/cfn-hup 
          chmod +x /etc/init.d/cfn-hup 
          update-rc.d cfn-hup defaults
          service cfn-hup starservice cfn-hup start

          # install code deploy
          cd /home/ubuntu
          wget https://aws-codedeploy-eu-west-1.s3.amazonaws.com/latest/install
          chmod +x ./install
          ./install auto

          cfn-init --stack ${AWS::StackName} --resource AuthScalingLaunchConfig --region ${AWS::Region} --configsets setup
  RibbonAutoScalingGroup:
    Type: AWS::AutoScaling::AutoScalingGroup
    Properties: 
      AvailabilityZones: 
        - !Select [ 0, !GetAZs "" ]
        - !Select [ 1, !GetAZs "" ]
      LaunchConfigurationName: !Ref AuthScalingLaunchConfig
      TargetGroupARNs:
        - !Ref TargetGroup
      HealthCheckGracePeriod: "60"
      HealthCheckType: EC2
      VPCZoneIdentifier: !Ref Subnets
      MaxSize: "10"
      DesiredCapacity: "1"
      MinSize: "1"
0 голосов
/ 21 июня 2020

Я сделал следующее изменение для UserData, которое получит последнюю версию cfn-init.

Description: 'Autoscaling groups'

Parameters:
  DeploymentProfileARN: 
    Description: The ARN of the iam group with deployment permissions.
    Type: String
  PublicSecurityGroup: 
    Description: The security group for use with ec2.
    Type: String
  TargetGroup:
    Description: The load balancer target group
    Type: String
  Subnets: 
    Description: The load balancer target group
    Type: 'List<AWS::EC2::Subnet::Id>'
  DomainUrl:
    Description: The url of the site excluding any sub domains eg. "example.com"
    Type: String
  EnvironmentName:
    Description: An environment name that will be prefixed to resource names
    Type: String
    Default: Dev
    AllowedValues: [Prod, Uat, Dev]
  MasterDataBaseAddress:
    Description: The address of the master database
    Type: String

Resources:
  AuthScalingLaunchConfig:
    Type: AWS::AutoScaling::LaunchConfiguration
    Metadata: 
      AWS::CloudFormation::Init:
        nginx:
          packages:
            apt: 
              - "nginx-core"
          files:
            '/tmp/test':
              content: !Sub |
                test
            '/etc/nginx/sites-enabled/default':
              content: !Sub |
                  upstream phoenix {
                    server 127.0.0.1:4000 max_fails=5 fail_timeout=60s;
                  }
                  server {
                    listen 80 default_server;
                    server_name .${DomainUrl};
                    location / {
                      allow all;
                      proxy_http_version 1.1;
                      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                      proxy_set_header Host $http_host;
                      proxy_set_header X-Cluster-Client-Ip $remote_addr;
                      proxy_set_header Upgrade $http_upgrade;
                      proxy_set_header Connection "upgrade";
                      proxy_pass http://phoenix;
                    }
                  }
          commands:
            01-restartNginx:
              command: service nginx restart
        configSets:
          setup:
            - "nginx"
    Properties:
      ImageId: "ami-0701e7be9b2a77600"
      InstanceType: t1.micro
      KeyName: "main"
      IamInstanceProfile: !Ref DeploymentProfileARN
      SecurityGroups:
        - !Ref PublicSecurityGroup
      UserData:
        Fn::Base64:
          !Sub
            - |
                #!/bin/bash
                # install cfn
                apt-get update -y
                apt-get install -y python-pip curl gzip python3-pip
                pip install awscli --upgrade
                pip install --upgrade pip
                python3 -m pip install --upgrade pip
                python3 -m pip install --upgrade awscli
                cd ~
                if [ ! -d ~/aws-cfn-bootstrap-latest ]; then
                  apt-get install -y python-setuptools jq
                  # Get the latest CloudFormation package
                  cd ~
                  mkdir aws-cfn-bootstrap-latest
                  curl https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz | tar xz -C aws-cfn-bootstrap-latest --strip-components 1
                  easy_install aws-cfn-bootstrap-latest
                fi
                # Run CFN-Init
                echo "cfn-init starting" 2>&1;
                /usr/local/bin/cfn-init -s ${AWS::StackId} -r AuthScalingLaunchConfig --region ${AWS::Region} -c setup
                echo "cfn-init complete" 2>&1;
  AutoScalingGroup:
    Type: AWS::AutoScaling::AutoScalingGroup
    Properties: 
      AvailabilityZones: 
        - !Select [ 0, !GetAZs "" ]
        - !Select [ 1, !GetAZs "" ]
      LaunchConfigurationName: !Ref AuthScalingLaunchConfig
      TargetGroupARNs:
        - !Ref TargetGroup
      HealthCheckGracePeriod: "60"
      HealthCheckType: EC2
      VPCZoneIdentifier: !Ref Subnets
      MaxSize: "10"
      DesiredCapacity: "1"
      MinSize: "1"
...