3

CloudFormation で ECS クラスターを使用して OpsWorks レイヤーをセットアップする方法がわかりません。以下のエラーが原因でレイヤーの作成が失敗しますが、クラスターをテンプレートのスタックに登録する明確な方法がないようです。スタックとレイヤーの両方に追加しようとしEcsClusterArnましたが、うまくいきませんでした。API にはコマンドがありますが、テンプレートにすべてを含めたいと思います。

エラー:

Attributes - EcsClusterArn: XXX must be registered to the layer's stack first.

テンプレートのスニペット:

"ecsCluster" : {
  "Type" : "AWS::ECS::Cluster"
},
...
"opsworksStack" : {
  "Type" : "AWS::OpsWorks::Stack",
  "Properties" : {
    "Name" : "my-stack",
    "ServiceRoleArn" : {
      "Fn::Join" : [ "", [ "arn:aws:iam::", {
        "Ref" : "AWS::AccountId"
      }, ":role/", {
        "Ref" : "ServiceRole"
      } ] ]
    },
    "DefaultInstanceProfileArn" : {
      "Fn::Join" : [ "", [ "arn:aws:iam::", {
        "Ref" : "AWS::AccountId"
      }, ":instance-profile/", {
        "Ref" : "InstanceRole"
      } ] ]
    },
    "UseOpsworksSecurityGroups" : "false",
    "ChefConfiguration" : {
      "BerkshelfVersion" : "3.3.0",
      "ManageBerkshelf" : "true"
    },
    "ConfigurationManager" : {
      "Name" : "Chef",
      "Version" : "11.10"
    }
  }
},
"opsworksLayer" : {
  "Type" : "AWS::OpsWorks::Layer",
  "DependsOn" : "ecsCluster",
  "Properties" : {
    "StackId" : {
      "Ref" : "opsworksStack"
    },
    "Type" : "ecs-cluster",
    "Name" : "my-layer",
    "Shortname" : "my-layer",
    "Attributes" : {
      "EcsClusterArn" : {
        "Fn::Join" : [ "", [ "arn:aws:ecs:", {
          "Ref" : "AWS::Region"
        }, ":", {
          "Ref" : "AWS::AccountId"
        }, ":cluster/", {
          "Ref" : "ecsCluster"
        } ] ]
      }
    },
    "CustomSecurityGroupIds" : [ {
      "Ref" : "ec2DefaultSecurityGroup"
    } ],
    "EnableAutoHealing" : "true",
    "AutoAssignElasticIps" : "false",
    "AutoAssignPublicIps" : "false",
    "InstallUpdatesOnBoot" : "true"
  }
}

ありがとう、ティエン

4

3 に答える 3

2

私の印象では、スタックへのクラスターの登録に失敗しています。これを回避するために、登録を手動で行うラムダ関数を実装しました。テンプレートの例を github に投稿しました: https://github.com/arjenderijke/aws-cloud-examples/blob/master/cloudformation/opsworks/opsworks-ecs-layer.template

cloudformation テンプレートには、ecs レイヤーとその他の最小限の必要なリソースを備えた opsworks スタックの完全な例が含まれています。ecs クラスターは自動的に登録されないため、スタックは通常、作成に失敗します。これを回避するために、テンプレートは aws ラムダ関数を実行するカスタム リソースを実装します。この関数は、クラスターをスタックに登録します。このカスタム リソースを使用することで、エラーは発生しなくなりました。

 "OpsworksRegisterCluster": {
  "Type": "AWS::Lambda::Function",
  "Properties": {
    "Handler": "index.lambda_handler",
    "Role": { "Fn::GetAtt" : ["LambdaExecutionRole", "Arn"] },
    "Code": {
      "ZipFile":  { "Fn::Join": ["\n", [
        "import boto3",
        "import json",
        "import cfnresponse",
        "ecsclient = boto3.client('ecs')",
        "opsworksclient = boto3.client('opsworks',",
        "  region_name='us-east-1',",
        "  endpoint_url='https://opsworks.us-east-1.amazonaws.com')",
        "def lambda_handler(event, context):",
        "  try:",
        "    if (event['RequestType'] == 'Create'):",
        "      ecscluster = ecsclient.describe_clusters(clusters=[",
        "        event['ResourceProperties']['EcsClusterName']])",
        "      response = opsworksclient.register_ecs_cluster(",
        "        EcsClusterArn=ecscluster['clusters'][0]['clusterArn'],",
        "        StackId=event['ResourceProperties']['OpsworksStackId']",
        "      )",
        "      responseData = {}",
        "      responseData['data'] = response['EcsClusterArn']",
        "      cfnresponse.send(event, context, cfnresponse.SUCCESS,   responseData, \"CustomResourcePhysicalID\")",
        "    else:",
        "      responseData = {}",
        "      cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData, \"CustomResourcePhysicalID\")",
        "  except Exception as e:",
        "    responseData = {}",
        "    responseData['error'] = e.message",
        "    cfnresponse.send(event, context, cfnresponse.FAILED, responseData, \"CustomResourcePhysicalID\")"
      ]]}
    },
    "Runtime": "python2.7",
    "Timeout": "10"
  }
},
于 2016-05-03T09:28:35.693 に答える
1

皆さんの知るところですが、私は数日前に AWS クラウドフォーメーションでこの問題に直面しており、これについて Amazon に訴訟を起こしました。

この問題はAPIにまだ存在しているようです(コメントでThienが指摘したように、このバグは2016年2月に解決されるはずでした)、解決されていません。ネストされたスタックを使用しているため、この問題が存在するかどうかはわかりません(おそらく、単一のテンプレートではそうではありません。私はそれを試していません)。

AWS の人々は、ラムダ関数を使用してこの問題を解決するように私に依頼しました。これは、上記の @Arjen によって提供されたものと非常によく似たラムダ関数です。したがって、この問題に直面している場合は、上記のラムダ関数を使用してください。

ありがとう、マニッシュ

于 2016-06-23T07:28:20.167 に答える
0

セクションAdd an ECS Cluster Layer to a Stackによると、クラスターをスタックに関連付けるには 2 つの操作が必要です。

[...] クラスターをスタックに登録してから、関連するレイヤーを作成します。AWS OpsWorks コンソールはこれらのステップを組み合わせます。レイヤーの作成により、指定されたクラスターが自動的に登録されます。AWS OpsWorks API、CLI、または SDK を使用する場合は、別のオペレーションを使用してクラスターを登録し、関連するレイヤーを作成する必要があります[鉱山を強調]

スタックとレイヤーの両方に追加しようとしEcsClusterArnたがうまくいかなかったと述べていますが、エラー メッセージはAmazon ECSクラスターを最初にレイヤーのスタックに登録する必要があることを確認しており、サンプル テンプレートには実際にこの側面がありませんか? 場合によっては、単に応答を繰り返す必要があるかもしれません。AWS::OpsWorks::Layer内部からの断片AWS::OpsWorks::Stackも:

   "Attributes" : {
      "EcsClusterArn" : {
        "Fn::Join" : [ "", [ "arn:aws:ecs:", {
          "Ref" : "AWS::Region"
        }, ":", {
          "Ref" : "AWS::AccountId"
        }, ":cluster/", {
          "Ref" : "ecsCluster"
        } ] ]
      }
    },
于 2015-08-23T22:42:20.857 に答える