简体   繁体   English

AWS CDK ssm.CfnAssociation 定义参数

[英]AWS CDK ssm.CfnAssociation define parameters

I'm stuck with creating ssm.CfnAssociation due to I'm novice in AWS CDK and CloudFormation also.我坚持创建 ssm.CfnAssociation 因为我也是 AWS CDK 和 CloudFormation 的新手。 I'm trying to create AWS Systems Manager State Manager task (AWS-RunAnsiblePlaybook) by ssm.CfnAssociation, but I have misunderstanding how can I define parameters?我正在尝试通过 ssm.CfnAssociation 创建 AWS Systems Manager State 管理器任务(AWS-RunAnsiblePlaybook),但我误解了如何定义参数? I want to set in parameters url to s3 for playbook.我想将参数 url 设置为 s3 以用于剧本。 As from CDK docs it should be: parameters (Union[IResolvable, None, Mapping[str, Union[IResolvable, Forwardref]]]) – AWS::SSM::Association.Parameters.根据 CDK 文档,它应该是: parameters (Union[IResolvable, None, Mapping[str, Union[IResolvable, Forwardref]]]) – AWS::SSM::Association.Parameters.

By AWS docks Type: Map of ParameterValues -> { "ParameterValues": [ String, ... ] }按 AWS 码头Type: Map of ParameterValues -> { "ParameterValues": [ String, ... ] }

I've tried to define various types for parameters, but I always have error: 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" If I'm using ssm.CfnAssociation.ParameterValuesProperty for matching to key playbookurl , I have and error on the deploying step: SSMAssociation/SSMAssociation (SSMAssociation6148DA19) Value of {Parameters} must be a map where each value is a list of {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" union: 预期playbookurl 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 SSMAssociation/SSMAssociation (SSMAssociation6148DA19) Value of {Parameters} must be a map where each value is a list of {String}

Could you please help me with it, because have no idea what type and how should be proper for parameters?你能帮我解决一下吗,因为不知道什么类型以及如何适合参数? Thank you.谢谢你。

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,
    )

At this moment work around for this issue it's a way of using CfnInclude instead of CfnAssociation.目前解决这个问题的方法是使用 CfnInclude 而不是 CfnAssociation。 But in my opinion it would be better to use CfnAssociation in a proper way.但在我看来,以适当的方式使用 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}"]
                            }]
                          }
                    }
                }
            }
        )

As mentioned above according to the python docs , parameters is (Union[IResolvable, None, Mapping[str, Union[IResolvable, Forwardref]]]) – AWS::SSM::Association.Parameters , so what you did is correct如上所述,根据 python 文档,参数是(Union[IResolvable, None, Mapping[str, Union[IResolvable, Forwardref]]]) – AWS::SSM::Association.Parameters ,所以你所做的是正确的

I just verified cdk synth accepts:我刚刚验证了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,
    )

On the following versions在以下版本中

Python 3.7.4 Python 3.7.4

aws-cdk.aws-events==1.18.0 aws-cdk.aws-events==1.18.0

aws-cdk.aws-iam==1.18.0 aws-cdk.aws-iam==1.18.0

aws-cdk.aws-kms==1.18.0 aws-cdk.aws-kms==1.18.0

aws-cdk.aws-s3==1.18.0 aws-cdk.aws-s3==1.18.0

aws-cdk.aws-ssm==1.18.0 aws-cdk.aws-ssm==1.18.0

aws-cdk.core==1.18.0 aws-cdk.core==1.18.0

aws-cdk.cx-api==1.18.0 aws-cdk.cx-api==1.18.0

aws-cdk.region-info==1.18.0 aws-cdk.region-info==1.18.0

However the deploy issue still exists, where it seems that you should be using ssm_param_values.parameter_values , but it's not accepted by CDK但是部署问题仍然存在,您似乎应该使用ssm_param_values.parameter_values ,但 CDK 不接受它

Filed an issue on CDK , although it may be a CF bug.在 CDK 上提交了一个问题,尽管它可能是一个 CF 错误。

The CF documentation is certainly misleading, reported feedback: CF 文档肯定具有误导性,报告的反馈:

  • Syntax says Parameters is just a key: value pair map语法说Parameters is just a key: value pair map
  • [Parameters][4] specifies a Map of [ParameterValues][5] , matching CDK behaviour [Parameters][4]指定[ParameterValues][5]的 Map ,匹配 CDK 行为

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM