AWS CDK ssm.CfnAssociation定义参数

时间:2019-10-10 17:24:52

标签: python amazon-web-services aws-cdk ssm

由于我还是AWS CDK和CloudFormation的新手,因此我坚持创建ssm.CfnAssociation。我正在尝试通过ssm.CfnAssociation创建AWS Systems Manager状态管理器任务(AWS-RunAnsiblePlaybook),但是我误会了如何定义参数?我想在参数url中将s3设置为剧本。从CDK文档开始,它应该是: parameters (Union[IResolvable, None, Mapping[str, Union[IResolvable, Forwardref]]]) – AWS::SSM::Association.Parameters.

通过AWS停靠站Type: Map of ParameterValues-> { "ParameterValues" : [ String, ... ] }

我尝试为参数定义各种类型,但是我总是遇到错误:Value did not match any type in union: Expected object reference, got {"plybook":"s3-url"},Value did not match any type in union: Expected object reference, got "s3-url",Expected object reference, got "s3-url" 如果我使用ssm.CfnAssociation.ParameterValuesProperty来匹配键playbookurl,则在部署步骤上遇到了错误:SSMAssociation/SSMAssociation (SSMAssociation6148DA19) Value of {Parameters} must be a map where each value is a list of {String}

您能帮助我吗,因为不知道什么类型以及参数应该如何适合? 谢谢。

class SSMAssociation(core.Construct):

def __init__(self, scope: core.Construct, id: str, 
ssm_association_name: str, **kwargs) -> None:
    super().__init__(scope, id, **kwargs)

    ssm_param_values = ssm.CfnAssociation.ParameterValuesProperty(
        parameter_values=["s3://test-ansible-test1-pl1/playbook1.yml"],
    )

    ssm_tartgets = ssm.CfnAssociation.TargetProperty(
        key="CDK-Type",
        values="EC2Instance",
    ),

    ssm_association = ssm.CfnAssociation(
        self, "SSMAssociation",
        name=ssm_association_name,
        output_location=None,
        parameters={
            "playbookurl": ssm_param_values,
        },

        targets=None,
    )

2 个答案:

答案 0 :(得分:0)

目前解决此问题的方法是使用CfnInclude而不是CfnAssociation的一种方法。 但我认为最好以适当的方式使用CfnAssociation。

class SSMAssociationConstruct(core.Construct):

def __init__(self, scope: core.Construct, id: str, 
             playbook_url: str,
             ec2_tag_key: str,
             ec2_tag_value: str,
             **kwargs) -> None:
    super().__init__(scope, id, **kwargs)

    if playbook_url is not None:
        cfn_include = core.CfnInclude(
            self, "CfnInclude",
            template={
                "Resources": {
                    "SSMAssociation": {
                        "Type" : "AWS::SSM::Association",
                        "Properties" : {
                            "AssociationName" : "SSMRunAnsible" ,
                            "Name" : "AWS-RunAnsiblePlaybook",
                            "ScheduleExpression": "cron(0 0/30 * * * ? *)",
                            "Parameters" : {
                                "playbookurl":[playbook_url],
                            },
                            "Targets" : [{
                                "Key": f"tag:{ec2_tag_key}",
                                "Values": [f"{ec2_tag_value}"]
                            }]
                          }
                    }
                }
            }
        )

答案 1 :(得分:0)

如上所述,根据python docs,参数为(Union[IResolvable, None, Mapping[str, Union[IResolvable, Forwardref]]]) – AWS::SSM::Association.Parameters,所以您所做的是正确的

我刚刚验证cdk synth接受:

    ssm_param_values = ssm.CfnAssociation.ParameterValuesProperty(
        parameter_values=["s3://test-ansible-test1-pl1/playbook1.yml"],
    )
    ssm_association = ssm.CfnAssociation(
        self, "SSMAssociation",
        name=ssm_association_name,
        output_location=None,
        parameters={
            "playbookurl": ssm_param_values,
        },

        targets=None,
    )

在以下版本上

  

Python 3.7.4

     

aws-cdk.aws-events == 1.18.0

     

aws-cdk.aws-iam == 1.18.0

     

aws-cdk.aws-kms == 1.18.0

     

aws-cdk.aws-s3 == 1.18.0

     

aws-cdk.aws-ssm == 1.18.0

     

aws-cdk.core == 1.18.0

     

aws-cdk.cx-api == 1.18.0

     

aws-cdk.region-info == 1.18.0

但是,部署问题仍然存在,似乎应该使用ssm_param_values.parameter_values,但CDK不接受该问题

提起issue on CDK,尽管可能是CF错误。

CF documentation当然是令人误解的,据反馈:

  • 语法说Parameters is just a key: value pair map
  • [Parameters][4]指定[ParameterValues][5]的映射,匹配CDK行为