当前位置: 首页 > 知识库问答 >
问题:

cnf init在ubuntu ami上抛出Python错误

彭仲卿
2023-03-14

我正在使用下面的aws cloudformation模板来启动一个自动缩放组,当我尝试运行cfn init时,它抛出了一个python错误。我已经想尽一切办法来解决这个问题,但现在我完全被难倒了。我得到的错误是:

在构建过程中发生错误:列表索引必须是整数,而不是Unicode

这是我的云形成模板:

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引导,但它们都给了我相同的问题。

共有2个答案

邵正雅
2023-03-14

我对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"
锺离玮
2023-03-14

我已经设法让它工作了。对后代来说,这是行之有效的:

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 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"
 类似资料:
  • 这是一个完美的位置&android命令也工作得很好。请帮忙。我使用的是cordova 4.3.0

  • 中的这个简单程序会引发错误。可能的原因是什么?此问题在我安装/重新安装 后出现。此外已安装在我的PC上(视窗10机器)。 错误消息: 文件"C:\Python36\lib\subprocess.py",第336行,check_output**kwargs). stdout 文件"C:\Python36\lib\subprocess.py",第403行,以Popen(*popenargs,**kwa

  • 问题内容: 我在项目中使用Gson将JSON字符串反序列化为Java对象。如果我提出请求,我希望服务器发出明确定义的响应。服务器将返回我期望的定义明确的响应,或者将返回一个(也定义为)错误对象。 讲清楚一点:假设我有一个简单的对象,像这样: 和这样的错误对象: 如果我收到类似的服务器响应 一切都按预期进行。 但是如果回应是这样的 我会得到一个对象,那里是和是0!Gson文档(来自Json)明确指出

  • 我创建了一个分支,方法是

  • 我有一个非常简单/基本的Web应用程序。当我跑的时候 mvn部署-e 我在控制台中看到以下错误。 [错误]无法执行目标组织。阿帕奇。专家plugins:maven deploy plugin:2.7:project TestWebApp上的部署(默认部署):部署失败:未在POM inside distributionManagement元素或-DaltDeploymentRepository=id

  • 我已经使用android Studio创建了一个基本的android颤振应用程序。我记得运行过几次这个应用程序,但突然它不运行了。根据一些帮助,尝试将targetSdkVersion和compileSdkVersion从27更改为28,但没有成功。试图了解问题的根源,因为我对Android开发非常陌生。 我注意到的:只有当我在pubspec.yaml中添加地理定位器或位置包并导入它时,才会出现以下