Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ec2): added support for network interfaces on ec2 instances by providing an associatePublicIpAddress property #25441

Merged
merged 21 commits into from May 23, 2023

Conversation

colifran
Copy link
Contributor

@colifran colifran commented May 4, 2023

Motivation

When creating and launching an EC2 instance, a public IPv4 address will be assigned by default for any instances being launched into a default public subnet. Conversely, any EC2 instance being launched into a nondefault public subnet will not be automatically assigned a public IPv4 address. The decision to automatically assign or not assign a public IPv4 address is based on a subnet property which is true by default for default public subnets and false by default for nondefault public subnets. This property can be controlled by specifying that the 'associatePublicIpAddress' be true for an EC2 instance. This property can be exposed via the 'networkInterfaces' property on the underlying L1 CfnInstance construct. Furthermore, any network interface that has an 'associatePublicIpAddress' set to true must also be the primary network interface for the EC2 instance and a primary network interface will always have a device index of 0. The work in this PR will allow a user to automatically have a public IPv4 address assigned to an EC2 instance that they are launching into a nondefault public subnet or stop the default subnet behavior of automatically assigning a public IPv4 address.

Important Changes

The changes made in this PR start by exposing the 'networkInterfaces' property on the underlying L1 CfnInstance. Next, I added 'associatePublicIpAddress' as an optional boolean property that is part of the 'InstanceProps' interface. Importantly, if 'associatePublicIpAddress' is set to true or false, then this means we need to launch the EC2 instance with a configured primary network interface. If 'associatePublicIpAddress' is set to true or false, a network interfaces array is created with the specified network interface configuration for the primary network interface. The subnetId and securityGroupIds are also configured for the network interface since they must be defined on the network interface level when launching an EC2 instance with a configured network interface. I updated the L1 CfnInstance to set subnetId and securityGroupIds to undefined in the event that the network interfaces array is defined.

Closes #17127


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

colifran added 11 commits May 3, 2023 11:41
Signed-off-by: Francis <colifran@amazon.com>
…ctor

Signed-off-by: Francis <colifran@amazon.com>
…ork interface of an EC2 instance

Signed-off-by: Francis <colifran@amazon.com>
… subnet

Signed-off-by: Francis <colifran@amazon.com>
…ddress associated

Signed-off-by: Francis <colifran@amazon.com>
…s on primary network interface if it is specified on instance creation

Signed-off-by: Francis <colifran@amazon.com>
…stance - added clarifying comment in instance.ts about logic in L1 construct

Signed-off-by: Francis <colifran@amazon.com>
Signed-off-by: Francis <colifran@amazon.com>
Signed-off-by: Francis <colifran@amazon.com>
… README - added an additional unit test for associatePublicIpAddress set to false

Signed-off-by: Francis <colifran@amazon.com>
@gitpod-io
Copy link

gitpod-io bot commented May 4, 2023

@aws-cdk-automation aws-cdk-automation requested a review from a team May 4, 2023 17:18
@github-actions github-actions bot added effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. p2 labels May 4, 2023
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label May 4, 2023
@corymhall corymhall added the @aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud label May 8, 2023
@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label May 8, 2023
Copy link
Contributor

@corymhall corymhall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@colifran looks great! Just a couple of minor comments/questions.

associatePublicIpAddress: true,
});

instance.node.addDependency(vpc);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you may be able to accomplish in this PR this by using the internetConnectivityEstablished dependable.

example

const internetConnected = instanceProps.vpc.selectSubnets(instanceProps.vpcSubnets).internetConnectivityEstablished;

this.instance.node.addDependency(internetConnected);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion. I've added this into the instance constructor and updated the README to remove instance.node.addDependency(vpc).

@@ -398,14 +407,22 @@ export class Instance extends Resource implements IInstance {
});
}

// network interfaces array is set to configure the primary network interface if associatePublicIpAddress is true or false
const networkInterfaces = props.associatePublicIpAddress !== undefined
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should add DeleteOnTermination. The docs don't state what the default is so I'm not sure. What do you think?

Copy link
Contributor Author

@colifran colifran May 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good point. I just launched an instance via the console and the primary network interface has DeleteOnTermination set to true by default. It should be okay to leave things as is, as this will provide the same experience as the console.

@@ -1547,6 +1547,36 @@ const aspect = new ec2.InstanceRequireImdsv2Aspect();
Aspects.of(this).add(aspect);
```

### Associating a Public IP Address with an Instance

All subnets have an attribute that determines whether instances launched into that subnet are assigned a public IPv4 address. This attribute is set to true by default for default public subnets. Thus, an EC2 instance launched into a default public subnet will be assigned a public IPv4 address. Nondefault public subnets have this attribute set to false by default and any EC2 instance launched into a nondefault public subnet will not be assigned a public IPv4 address automatically. To automatically assign a public IPv4 address to an instance launched into a nondefault public subnet, you can set the `associatePublicIpAddress` property on the `Instance` construct to true. Alternatively, to not automatically assign a public IPv4 address to an instance launched into a default public subnet, you can set `associatePublicIpAddress` to false. Importantly, if you set `associatePublicIpAddress` and there is a VPC defined in the same stack, you must make sure the `Instance` has a dependency on the VPC to declare a dependency on the VPC-gateway attachment.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a note here about what happens if you want to switch this value for an existing instance?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated this section in the README to include a note about what happens when associatePublicIpAddress is included, removed, or updated on an existing instance.

Signed-off-by: Francis <colifran@amazon.com>
Signed-off-by: Francis <colifran@amazon.com>
Signed-off-by: Francis <colifran@amazon.com>
…icIpAddress is added, removed, or updated on an existing instance

Signed-off-by: Francis <colifran@amazon.com>
Signed-off-by: Francis <colifran@amazon.com>
Signed-off-by: Francis <colifran@amazon.com>
…dress is set to true

Signed-off-by: Francis <colifran@amazon.com>
…associatePublicIpAddress is true

Signed-off-by: Francis <colifran@amazon.com>
Signed-off-by: Francis <colifran@amazon.com>
@aws-cdk-automation aws-cdk-automation removed the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label May 23, 2023
Copy link
Contributor

@corymhall corymhall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@mergify
Copy link
Contributor

mergify bot commented May 23, 2023

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 60d5f90
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify mergify bot merged commit d43834d into main May 23, 2023
7 checks passed
@mergify mergify bot deleted the colifran/associate-public-ip-address branch May 23, 2023 12:43
@mergify
Copy link
Contributor

mergify bot commented May 23, 2023

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud contribution/core This is a PR that came from AWS. effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. p2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[aws-ec2]: Instance - support for NetworkInterfaces
3 participants