在添加ec2实例时使用现有的vpc和安全组 [英] use existing vpc and security group when adding an ec2 instance

查看:65
本文介绍了在添加ec2实例时使用现有的vpc和安全组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多示例代码,但是快速改进的cdk包并不能帮助我找到一些(我认为)简单的事情的可行示例.例如,即使我在示例中找到的导入也会失败:

There is lots of example code, but the rapidly improving cdk package isn't helping me find working examples of some (I thought) simple things. eg., even an import I found in an example fails:

import { VpcNetworkRef } from '@aws-cdk/aws-ec2';
 error TS2724: Module '"../node_modules/@aws-cdk/aws-ec2/lib"' has no exported member 'VpcNetworkRef'. Did you mean 'IVpcNetwork'?

为什么示例ec2代码未显示原始ec2实例的创建?

Why does the example ec2 code not show creation of raw ec2 instances?

有用的cdk代码示例将使用硬编码的VpcId和SecurityGroupId(我将它们作为上下文值传递)来创建一对新的子网(即,每个可用区为1个),并在其中放置一对EC2实例的数量.

WHAT would help is example cdk code that uses hardcoded VpcId and SecurityGroupId (I'll pass these in as context values) to create a pair of new subnets (ie., 1 for each availability zone) into which we place a pair of EC2 instances.

同样,实例的目标VPC和SecurityGroup已经存在.在添加新的EC2实例集时,我们只是(今天)创建了新的子网.

Again, the target VPC and SecurityGroup for the instances already exist. We just (today) create new subnets as we add new sets of EC2 instances.

我们有很多不同的环境(aws基础架构集),这些环境当前共享一个帐户,VPC和安全组.这将改变,但是我当前的目标是查看是否可以使用云开发工具包在此现有模型中创建新的独特环境.今天我们有一个CF模板.

We have lots of distinct environments (sets of aws infrastructure) that currently share a single account, VPC, and security group. This will change, but my current goal is to see if we can use the cloud dev kit to create new distinct environments in this existing model. We have a CF template today.

我不知道从哪里开始.未引用现有VPC的示例.

I can't tell where to start. The examples for referencing existing VPCs aren't compiling.

import { VpcNetworkRef } from '@aws-cdk/aws-ec2';
const vpc = VpcNetworkRef.import(this, 'unused', {vpcId, availabilityZones: ['unused']});

同样,实例的目标VPC和SecurityGroup已经存在.在添加新的EC2实例集时,我们只是(今天)创建了新的子网.

Again, the target VPC and SecurityGroup for the instances already exist. We just (today) create new subnets as we add new sets of EC2 instances.

-----编辑-------->

-----edit-------->

有关gitter的讨论帮助我回答了这一问题以及如何添加裸实例

Discussions on gitter helped me answer this and how to add a bare Instance

const vpc - ec2.VpcNetwork.import(this, 'YOUR-VPC-NAME', {
    vpcId: 'your-vpc-id',
    availabilityZones: ['list', 'some', 'zones'],
    publicSubnetIds: ['list', 'some', 'subnets'],
    privateSubnetIds: ['list', 'some', 'more'],
});

const sg = ec2.SecurityGroup.import(this, 'YOUR-SG-NAME', {
    securityGroupId: 'your-sg-id'
});

// can add subnets to existing..
const newSubnet = new ec2.VpcSubnet(this, "a name", {
    availablityZone: "us-west-2b",
    cidrBlock: "a.b.c.d/e",
    vpcId: vpc.vpcId
});

// add bare instance
new ec2.CfnInstance(this, "instance name", {
    imageId: "an ami",
    securityGroupIds: [sg.securityGroupId],
    subnetId: newSubnet.subnetId,
    instanceType: "an instance type",
    tags: [{ key: "key", value: "value"}]
});

不需要进一步的答案...对我来说

No further answers needed... for me.

推荐答案

import ec2 = require('@aws-cdk/aws-ec2');

// looking up a VPC by its name
const vpc = ec2.Vpc.fromLookup(this, 'VPC', {
  vpcName: 'VPC-Name'
});


// looking up an SG by its ID
const sg = ec2.SecurityGroup.fromSecurityGroupId(this, 'SG', 'SG-ID')


// creating the EC2 instance
const instance = new ec2.Instance(this, 'Instance', {
  vpc: vpc,
  securityGroup: sg,
  instanceType: new ec2.InstanceType('m4.large'),
  machineImage: new ec2.GenericLinuxImage({
    'us-east-1': 'ami-abcdef' // <- add your ami-region mapping here
   }),
});

这篇关于在添加ec2实例时使用现有的vpc和安全组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆