From a00906d22317103156acacc597623aafa660acbb Mon Sep 17 00:00:00 2001 From: Ian Alford Date: Wed, 18 Dec 2019 02:32:10 -0800 Subject: [PATCH] feat(ec2): add `privateIpAddress` to Instance Add an optional privateIpAddress prop to the Instance class to allow the specification of a Private IP address Fixes #4004 --- packages/@aws-cdk/aws-ec2/lib/instance.ts | 10 +++++++++ .../@aws-cdk/aws-ec2/test/test.instance.ts | 21 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/packages/@aws-cdk/aws-ec2/lib/instance.ts b/packages/@aws-cdk/aws-ec2/lib/instance.ts index 75878d86eab75..d86d047438c47 100644 --- a/packages/@aws-cdk/aws-ec2/lib/instance.ts +++ b/packages/@aws-cdk/aws-ec2/lib/instance.ts @@ -167,6 +167,15 @@ export interface InstanceProps { * @default true */ readonly sourceDestCheck?: boolean; + + /** + * Defines a private IP address to associate with an instance. + * + * Private IP should be available within the VPC that the instance is build within. + * + * @default - no association + */ + readonly privateIpAddress?: string } /** @@ -284,6 +293,7 @@ export class Instance extends Resource implements IInstance { subnetId: subnet.subnetId, availabilityZone: subnet.availabilityZone, sourceDestCheck: props.sourceDestCheck, + privateIpAddress: props.privateIpAddress }); this.instance.node.addDependency(this.role); diff --git a/packages/@aws-cdk/aws-ec2/test/test.instance.ts b/packages/@aws-cdk/aws-ec2/test/test.instance.ts index bbaea12bc9299..66513308c9fd5 100644 --- a/packages/@aws-cdk/aws-ec2/test/test.instance.ts +++ b/packages/@aws-cdk/aws-ec2/test/test.instance.ts @@ -100,6 +100,27 @@ export = { }, })); + test.done(); + }, + 'instance can be created with Private IP Address'(test: Test) { + // GIVEN + const stack = new Stack(); + const vpc = new Vpc(stack, 'VPC'); + + // WHEN + new Instance(stack, 'Instance', { + vpc, + machineImage: new AmazonLinuxImage(), + instanceType: InstanceType.of(InstanceClass.T3, InstanceSize.LARGE), + privateIpAddress: "10.0.0.2" + }); + + // THEN + expect(stack).to(haveResource('AWS::EC2::Instance', { + InstanceType: 't3.large', + PrivateIpAddress: '10.0.0.2' + })); + test.done(); }, };