Created
December 23, 2020 12:03
-
-
Save pahud/e38e71494ffd57fef40e453a65432f2b to your computer and use it in GitHub Desktop.
cdk eni attach instance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { App, CfnOutput, Construct, Stack, StackProps } from '@aws-cdk/core'; | |
import * as ec2 from '@aws-cdk/aws-ec2'; | |
export class MyStack extends Stack { | |
constructor(scope: Construct, id: string, props: StackProps = {}) { | |
super(scope, id, props); | |
const vpc = ec2.Vpc.fromLookup(this, 'Vpc', { isDefault: true }); | |
const subnetId = vpc.privateSubnets[0].subnetId; | |
const eni = new ec2.CfnNetworkInterface(this, 'ENI', { | |
subnetId, | |
}) | |
const instance = new ec2.Instance(this, 'Instance', { | |
vpc, | |
vpcSubnets: { | |
subnets: [ ec2.Subnet.fromSubnetAttributes(this, 'Subnet', { | |
subnetId, | |
availabilityZone: vpc.privateSubnets[0].availabilityZone, | |
})] | |
}, | |
instanceType: new ec2.InstanceType('t3.large'), | |
machineImage: new ec2.AmazonLinuxImage({ | |
generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2, | |
}), | |
}) | |
// attachment | |
new ec2.CfnNetworkInterfaceAttachment(this, 'Attach', { | |
instanceId: instance.instanceId, | |
deviceIndex:'1', | |
networkInterfaceId: eni.ref, | |
}) | |
new CfnOutput(this, 'IP', { value: eni.attrPrimaryPrivateIpAddress }) | |
} | |
} | |
// for development, use account/region from cdk cli | |
const devEnv = { | |
account: process.env.CDK_DEFAULT_ACCOUNT, | |
region: process.env.CDK_DEFAULT_REGION, | |
}; | |
const app = new App(); | |
new MyStack(app, 'my-stack-dev', { env: devEnv }); | |
// new MyStack(app, 'my-stack-prod', { env: prodEnv }); | |
app.synth(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment