使用 customResource 将 RDS 层注册到 opsworks 堆栈

Using customResource to register RDS layer to opsworks stack

我正在创建一个 cloudformation 模板,用于创建堆栈、层和应用程序。应用程序的数据库配置为 Datasources :

"AdminApp": {
      "Type": "AWS::OpsWorks::App",
      "Properties": {
        "AppSource" : {
          "Type" : "git",
          "Url" : "git://github.com:myrepo/adminapp.git",
          "Revision" : "master"
        },
        "DataSources":[{
          "Arn" : { "Ref" : "RegisterRDStoStack" },
          "DatabaseName" : "fadmin",
          "Type" : "RdsDbInstance"
        }],
        "Description": "Administration",
        "Name" : "admin-api",
        "Shortname" : "admin_api",
        "StackId" : {"Ref": "Stack"},
        "Type" : "php"
      }
    },

属性 数据源中的数据库应首先注册到堆栈,以便应用程序可以访问它。 由于 AWS 尚未使用 Cloudformation 在 opsworks 堆栈中创建 RDS 层,因此我创建了一个 CustomResource 作为解决方法:

"RegisterRDStoStack" : {
  "Type": "Custom::RDSLayer",
  "Version" : "1.0",
  "Properties" : {
    "ServiceToken": {"Ref" : "RDSInstanceARN"},
    "StackId" : {"Ref" : "Stack" },
    "User" : {"Ref" : "UserDB" },
    "Password" : {"Ref" : "PasswordDB" }
  }
},

测试模板时出现此错误:

CREATE_FAILED   Custom::RDSLayer    RegisterRDStoStack  Invalid service token

所以好像有错误,但不知道具体是什么。我正确地提供了数据库的 ARN。请问我应该怎么做才能完成这项工作?有什么想法吗?

您需要提供 Lambda 函数的 ARN(例如,{"Ref": "MyLambdaFunction"})作为 Custom Resource. See the documentation for ServiceTokenServiceToken 参数:

The service token that was given to the template developer by the service provider to access the service, such as an Amazon SNS topic ARN or Lambda function ARN. The service token must be from the same region in which you are creating the stack.