Yan
Yan

Reputation: 3616

Unable to create CfnSpotFleet using Python in AWS CDK

When running CDK synth I am getting JSII error

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

I created a spot fleet stack and when I am defining CfnSpotFleet I am seeing the 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 Version : aws-cli/1.17.0 Python/3.8.1 Darwin/18.7.0 botocore/1.14.0.
**Framework Version (CDK): 1.30.0 (build 4f54ff7).
**OS : MacOS 10.14.6.
**Language : English.

I also tried to pass an array for 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]))

for that I received a different error:
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:{........

I am looking through the python library and see that CfnSpotFleet doesn't have the JSII annotations that CfnSpotFleetProps has. I am not exactly that it cause this issue though.

@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"):

Please let me know if any other information is required.

===== Update ====

Thank you to @dmahapatro to point out the actual property that is being set in correctly. I was able to get the stack working. Here is the solution if anyone else would be creating SpotFleet via CDK

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)

Upvotes: 1

Views: 1776

Answers (1)

dmahapatro
dmahapatro

Reputation: 50245

If you take a look at CloudFormation documentation in SpotFleetRequestConfigData, you will see that LaunchSpecifications expects an array of SpotFleetLaunchSpecification

Not sure what is the value of batch_workers_launch_specification because it has not been shown in the question, but if it does not represent an array then below should fix it:

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

Upvotes: 2

Related Questions