无法在AWS CDK中使用Python创建CfnSpotFleet

时间:2020-03-20 13:22:41

标签: aws-cdk

运行CDK合成器时,出现JSII错误

jsii.errors.JSIIError: Value did not match any type in union: Value did not match any type in union: Expected array type, got {"$jsii.struct":{"fqn":"@aws-cdk/aws-ec2.CfnSpotFleet.SpotFleetLaunchSpecificationProperty",

我创建了一个现货车队堆栈,当我定义CfnSpotFleet时,我看到了JSIIError

batch_workers_request_config_data = ec2.CfnSpotFleet.SpotFleetRequestConfigDataProperty(
            iam_fleet_role=spotfleet_role.role_arn,
            target_capacity=num_of_workers,
            instance_interruption_behavior='terminate',
            launch_specifications=batch_workers_launch_specification
        )
        ec2.CfnSpotFleetProps(spot_fleet_request_config_data=batch_workers_request_config_data)

        ec2.CfnSpotFleet(self, id=f'{id}-spot-fleet',
                         spot_fleet_request_config_data=batch_workers_request_config_data)

** CLI版本:aws-cli / 1.17.0 Python / 3.8.1 Darwin / 18.7.0 botocore / 1.14.0。
**框架版本(CDK):1.30.0(内部版本4f54ff7)。
** OS:MacOS 10.14.6。
**英语语言。

我还尝试为spot_fleet_request_config_data传递数组

ec2.CfnSpotFleet(self, id=f'{id}-spot-fleet',
                         spot_fleet_request_config_data=ec2.CfnSpotFleetProps(
                             spot_fleet_request_config_data=[batch_workers_request_config_data]))

为此,我收到了另一个错误:
jsii.errors.JSIIError: Value did not match any type in union: Got an array where a @aws-cdk/aws-ec2.CfnSpotFleet.SpotFleetRequestConfigDataProperty was expected. Did you mean to pass a variable number of arguments?,Expected object reference, got [{"$jsii.struct":{"fqn":"@aws-cdk/aws-ec2.CfnSpotFleet.SpotFleetRequestConfigDataProperty","data:{........

我正在浏览python库,发现CfnSpotFleet没有CfnSpotFleetProps拥有的JSII注释。我不完全是这会导致此问题。

@jsii.data_type(jsii_type="@aws-cdk/aws-ec2.CfnSpotFleetProps", jsii_struct_bases=[], name_mapping={'spot_fleet_request_config_data': 'spotFleetRequestConfigData'})

@jsii.implements(aws_cdk.core.IInspectable)
class CfnSpotFleet(aws_cdk.core.CfnResource, metaclass=jsii.JSIIMeta, jsii_type="@aws-cdk/aws-ec2.CfnSpotFleet"):

请让我知道是否需要其他信息。

=====更新====

感谢@dmahapatro指出正确设置的实际属性。我能够使堆栈正常工作。如果有人通过CDK创建SpotFleet,这就是解决方案

ec2.AmazonLinuxImage()    
ec2_instance_profile = iam.CfnInstanceProfile(self, f'{id}-spot-ec2-profile', roles=[batch_iam_role.role_name])
batch_workers_tags = ec2.CfnSpotFleet.SpotFleetTagSpecificationProperty(resource_type='instance',
                                                                        tags=[
                                                                            core.CfnTag(key='Name',
                                                                                        value=f'{id}-worker-ec2'),
                                                                            core.CfnTag(key='batch.role',
                                                                                        value=f'{id}-worker'),                                                                  
                                                                        ])

block_device_mapping = ec2.CfnSpotFleet.BlockDeviceMappingProperty(device_name='/dev/sda1',
                                                                   ebs=ec2.CfnSpotFleet.EbsBlockDeviceProperty(
                                                                       volume_size=64))

batchworker_spotfleet_iam_role = iam.Role(
    self,
    'AmazonEC2SpotFleetRole',
    assumed_by=iam.ServicePrincipal('spotfleet.amazonaws.com'),
    managed_policies=[
        iam.ManagedPolicy.from_aws_managed_policy_name('service-role/AmazonEC2SpotFleetTaggingRole')
    ]
)

batchworker_iam_profile_spec = ec2.CfnSpotFleet.IamInstanceProfileSpecificationProperty(
    arn=ec2_instance_profile.attr_arn)

batch_workers_launch_specification = ec2.CfnSpotFleet.SpotFleetLaunchSpecificationProperty(
    image_id=ec2_image_id,
    instance_type=ec2_instance_type,
    key_name=ec2_ssh_key,
    block_device_mappings=[block_device_mapping],
    iam_instance_profile=batchworker_iam_profile_spec,
    tag_specifications=[batch_workers_tags],
    security_groups=[ec2.CfnSpotFleet.GroupIdentifierProperty(group_id=security_group.security_group_id)],
)

batch_workers_request_config_data = ec2.CfnSpotFleet.SpotFleetRequestConfigDataProperty(
    iam_fleet_role=batchworker_spotfleet_iam_role.role_arn,
    target_capacity=num_of_workers,
    instance_interruption_behavior='terminate',
    launch_specifications=[batch_workers_launch_specification]
)

ec2.CfnSpotFleet(self, id=f'{id}-spot-fleet',
                 spot_fleet_request_config_data=batch_workers_request_config_data)

1 个答案:

答案 0 :(得分:1)

如果您查看SpotFleetRequestConfigData中的CloudFormation文档,将会发现LaunchSpecifications期望包含SpotFleetLaunchSpecification的数组

不确定batch_workers_launch_specification的值是什么,因为它没有在问题中显示,但是如果它不表示数组,则下面应该解决它:

batch_workers_request_config_data = 
ec2.CfnSpotFleet.SpotFleetRequestConfigDataProperty(
    iam_fleet_role=spotfleet_role.role_arn,
    target_capacity=num_of_workers,
    instance_interruption_behavior='terminate',
    launch_specifications=[batch_workers_launch_specification]
)