amazon-web-services - cnf-init 在 ubuntu ami 上抛出 Python 错误

标签 amazon-web-services ubuntu pip aws-cloudformation

我正在使用下面的 aws cloudformation 模板来启动自动缩放组,当我尝试运行 cfn-init 时,它抛出一个 python 错误。我已经尝试了所有我能想到的方法来解决这个问题,但现在我完全陷入了困境。我得到的错误是:

构建期间发生错误:列表索引必须是整数,而不是 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,但它们都给了我同样的问题。

最佳答案

我已经成功地让这个工作了。对于后代来说,这行之有效:

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"

关于amazon-web-services - cnf-init 在 ubuntu ami 上抛出 Python 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62499177/

相关文章:

java - 来自 aws-java-sdk 的 DynamoDB 在 gradle 下不完整

ubuntu - NEXT-AUTH -/api/auth/signin 上的 500 内部服务器错误,无法访问实时站点上的登录页面

windows - gcc 可被 cmd 识别,但不能被 bash 识别

python - 如何在 Windows 中运行使用 pip 安装的程序?

javascript - 通过 AWS Javascript SDK 将 S3 缓冲区保存到文件

amazon-web-services - 在基于 aws appsync 的聊天应用程序中显示在线用户

amazon-web-services - 通过 SSH 端口的 AWS Load Balancer Git 克隆错误

linux - 使用 ssh 启动远程进程并退出

python - Windows 10 pip 安装 'Failed Connection'

python - 使用 django 模板时出错