aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.5k stars 3.84k forks source link

(aws-ec2): Need to be able to freely configure network interfaces in L2 LaunchTemplate #30891

Open chenhe95 opened 1 month ago

chenhe95 commented 1 month ago

Describe the feature

I'd like it if I could be able to just pass in the networkInterfaces to the underlying L1 construct from the L2 constructor, instead of the L2 construct creating the networkInterfaces for me, which doesn't have all of the things I'd like to put in.

The current launch template behavior of deciding the network interface doesn't really work for me https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk-lib/aws-ec2/lib/launch-template.ts#L734-L736

    const networkInterfaces = props.associatePublicIpAddress !== undefined
      ? [{ deviceIndex: 0, associatePublicIpAddress: props.associatePublicIpAddress, groups: securityGroupsToken }]
      : undefined;

Use Case

To not be limited to only network interfaces with a device index of 0 with a configured associatePublicIpAddress. To be able to use the entirety of NetworkInterfaceProperty for my L2 LaunchTemplate construct

Proposed Solution

Change the LaunchTemplate behavior as follows

  1. Add an optional networkInterfaces? field to the LaunchTemplateProps, same as the L1 version
  2. If networkInterfaces is provided, do not create the network interfaces on this line
    const networkInterfaces = props.associatePublicIpAddress !== undefined
      ? [{ deviceIndex: 0, associatePublicIpAddress: props.associatePublicIpAddress, groups: securityGroupsToken }]
      : undefined;

    And also do not create the securityGroupsToken

    const securityGroupsToken = Lazy.list({
      produce: () => {
        if (this._connections && this._connections.securityGroups.length > 0) {
          return this._connections.securityGroups.map(sg => sg.securityGroupId);
        }
        return undefined;
      },
    });

    And also pass in the provided props networkInterfaces to CfnLaunchTemplate and pass in undefined to securityGroupIds

  3. If associatePublicIpAddress is provided through the props but networkInterfaces is not provided through props, retain the current behavior as if associatePublicIpAddress is provided.
  4. If neither are provided through props, retain the current behavior as if associatePublicIpAddress is not provided
  5. If both associatePublicIpAddress and networkInterfaces are provided through props, then there is a conflict as there are 2 sources of truth. We could throw an error throw new Error('You cannot provide both a networkInterfaces and an associatePublicIpAddress');

Other Information

No response

Acknowledgements

CDK version used

1.148.0

Environment details (OS name and version, etc.)

Mac OS Sonoma 14.5

chenhe95 commented 1 month ago

Something I just realized is that there might not be a L2 abstraction for the launch template network interfaces https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-launchtemplate-networkinterface.html

Note: This is different than EC2 network interfaces https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-networkinterface.html

khushail commented 1 month ago

Hi @chenhe95 ,thanks for reaching out.

You could always use the Escape hatches to pass the desired properties to Cloudformation L1 construct directly,you could use CfnInstance and set the netowrkinterface the way you would like.

Please let me know if that would solve your usecase. If it doesn't, and you would still like to propose a L2 construct, we have a set procedure for that. You would have to submit an RFC and get the approval on design and after review and acceptance by the Core team, you could proceed with contribution. However this whole contribution process is currently under review and being reassessed. Please feel free to check this ReadMe on L2 construct submission.. Hope that helps!

github-actions[bot] commented 1 month ago

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

chenhe95 commented 1 month ago

Thanks, I'll look into the L2 construct submission

chenhe95 commented 1 month ago

@khushail Is this actually a L2 construct or is this just a regular typescript interface to satisfy the expected params of a L1 construct?
My understanding is that the data structure needed for LaunchTemplate's NetworkInterface is not actually anything that extends, uses, or relates to the actual class Construct but would be more of an interface like

/**
 * Interface for the Spot market instance options provided in a LaunchTemplate.
 */
export interface LaunchTemplateSpotOptions {
  /**
   * Spot Instances with a defined duration (also known as Spot blocks) are designed not to be interrupted and will run continuously for the duration you select.
   * You can use a duration of 1, 2, 3, 4, 5, or 6 hours.
   *
   * @see https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-requests.html#fixed-duration-spot-instances
   *
   * @default Requested spot instances do not have a pre-defined duration.
   */
  readonly blockDuration?: Duration;

  /**
   * The behavior when a Spot Instance is interrupted.
   *
   * @default Spot instances will terminate when interrupted.
   */
  readonly interruptionBehavior?: SpotInstanceInterruption;

  /**
   * Maximum hourly price you're willing to pay for each Spot instance. The value is given
   * in dollars. ex: 0.01 for 1 cent per hour, or 0.001 for one-tenth of a cent per hour.
   *
   * @default Maximum hourly price will default to the on-demand price for the instance type.
   */
  readonly maxPrice?: number;

  /**
   * The Spot Instance request type.
   *
   * If you are using Spot Instances with an Auto Scaling group, use one-time requests, as the
   * Amazon EC2 Auto Scaling service handles requesting new Spot Instances whenever the group is
   * below its desired capacity.
   *
   * @default One-time spot request.
   */
  readonly requestType?: SpotRequestType;

  /**
   * The end date of the request. For a one-time request, the request remains active until all instances
   * launch, the request is canceled, or this date is reached. If the request is persistent, it remains
   * active until it is canceled or this date and time is reached.
   *
   * @default The default end date is 7 days from the current date.
   */
  readonly validUntil?: Expiration;
};
chenhe95 commented 1 month ago

I'm reading through the RFC documents and the LaunchTemplate NetworkInterface doesn't seem to actually match the definition of a L2 construct https://github.com/aws/aws-cdk/blob/e4fdb0217edd7ecccdd4cbc20de958e3ba1a2349/docs/DESIGN_GUIDELINES.md?plain=1#L139-L147 because the launch template network interface does not actually represent an AWS resource and only represents the formatting an input parameter for an AWS L1 resource

chenhe95 commented 1 month ago

Would LaunchTemplateNetworkInterfaceOptions be a good name for this network interface abstraction? Or is that too verbose?

khushail commented 1 month ago

@chenhe95 , found a PR for the same -https://github.com/aws/aws-cdk/pull/29875 and another one having overlapping concepts - https://github.com/aws/aws-cdk/pull/28901, Could you confirm if this PR addresses your request.

chenhe95 commented 1 month ago

Thanks! https://github.com/aws/aws-cdk/pull/29875 basically solves my issue if it would be merged