AWS CDK自定义资源创建

时间:2020-01-31 15:22:55

标签: typescript amazon-web-services cloud aws-cdk

我有一个AWS YAML格式的自定义资源,我想为其创建AWS CDK代码。我可以使用AWS CDK CfnCustomResource添加条件和ServiceToken,但不能添加属性。

YAML模板

class http_monitoring (
  Array[Struct[{
    url                 => String,
    name                => String,
    runbook             => String,
    Optional[comment]   => String,
    Optional[secure]    => Boolean,
    Optional[warning]   => Integer,
    Optional[critical]  => Integer,
  }]]
  $checks = [{
    secure   => true,
    warning  => 30,
    critical => 15,
  }]
){
  $check_defaults = {
    secure   => true,
    warning  => 30,
    critical => 15,
  }

  # Checks with defaults
  $checks_with_defaults_added = $checks.map | Hash $next_check | {
    $check_defaults + $next_check
  }


  # $checks.each | Hash $check | {
  #   notify {"${check}":}
  # }

  $checks_with_defaults_added.each | Hash $check | {
    $check_output = String($check)
    notify { $check_output: }
  }
}

class website {
  class { 'http_monitoring':
    checks => [
      {url => 'https://example.com',  name => 'example'  ,runbook => 'https://link-to-docs.com/'},
      {url => 'https://example2.com', name => 'example2' ,runbook => 'https://link-to-docs.com/', warning => 5, critical => 10},
    ]
  }
}

include website

正在运行的相应AWS CDK打字稿代码。

MyAmi:
Condition: UseGI
Properties:
  ServiceToken:
    Fn::ImportValue: !Join ['', [!If [ MyProd, '', 'qa-'], Prod-LookupAmiFunction]]
  AMI: {Ref: AMI}      
  appId: {Ref: AppId}
  envType: {Ref: EnvType}
  osType: {Ref: OSType}
Type: Custom::MyAmi

我想添加YAML模板的属性,该怎么办。

1 个答案:

答案 0 :(得分:0)

我明白了。实际上,我应该使用cdk.cfnResource而不是cfn.CfnCustomResource(它最可能与lambda函数或SNS主题一起使用)。 cdk.cfnResource将让我定义任何自定义属性。

const MyAmi= new cdk.CfnResource(this, 'MyAmi', {
 type : "Custom::MyAmi",
 properties : {

  ServiceToken : cdk.Fn.importValue(cdk.Fn.join('',[cdk.Fn.conditionIf('MyProd','','qa-').toString(),'Prod-LookupAmiFunction'])),
  bizUnit: BizUnit,
  AMI: AMI,      
  appId: AppId,
  envType: EnvType

 });LookupAmi.cfnOptions.condition = UsemE