在 CDK 部署上重新创建 EC2
EC2 re-creation on CDK deploy
我们所有的堆栈资源 运行 都在一个 VPC 中,所以为了访问它们,我们使用堡垒主机,一个简单的 EC2 实例来创建一个进入 VPC 的 SSH 隧道。
然后我们将 SSH 密钥添加到主机,但在部署我们的 CDK 堆栈时,EC2 似乎不时被新密钥替换。然后我们每次都必须重新添加我们的 SSH 密钥。
有没有办法不重新创建 EC2 实例?
我们的代码:
export default class FooStack extends Stack {
constructor(scope: App, id: string, props?: StackProps) {
super(scope, id, props);
const vpc = this.createVPC();
const bastionSecurityGroup = this.createBastionSecurityGroup(vpc);
this.createBastionHost(bastionSecurityGroup, vpc);
}
private createVPC() {
const vpc = new Vpc(this, 'Vpc', { natGateways: 1 });
return vpc;
}
private createBastionSecurityGroup(vpc: Vpc) {
const bastionSecurityGroup = new SecurityGroup(this, 'BastionSecurityGroup', { vpc, allowAllOutbound: true });
bastionSecurityGroup.connections.allowFrom(
bastionSecurityGroup,
Port.allTraffic(),
'Allow inbound traffic to the Bastion Host from its security group',
);
bastionSecurityGroup.addIngressRule(
Peer.anyIpv4(),
Port.tcp(22),
'Allow inbound traffic to the Bastion Host on port 22.',
);
return bastionSecurityGroup;
}
private createBastionHost(securityGroup: SecurityGroup, vpc: Vpc) {
new BastionHostLinux(this, 'BastionHost', {
vpc,
instanceType: InstanceType.of(InstanceClass.T3, InstanceSize.NANO),
securityGroup,
subnetSelection: vpc.selectSubnets({ subnets: [vpc.publicSubnets[0]] }),
});
}
}
问题是我没有指定 AMI(Amazon 机器映像)。
因此,每次发布新版本的图像时,都会在部署时重新创建实例。
我们所有的堆栈资源 运行 都在一个 VPC 中,所以为了访问它们,我们使用堡垒主机,一个简单的 EC2 实例来创建一个进入 VPC 的 SSH 隧道。
然后我们将 SSH 密钥添加到主机,但在部署我们的 CDK 堆栈时,EC2 似乎不时被新密钥替换。然后我们每次都必须重新添加我们的 SSH 密钥。
有没有办法不重新创建 EC2 实例?
我们的代码:
export default class FooStack extends Stack {
constructor(scope: App, id: string, props?: StackProps) {
super(scope, id, props);
const vpc = this.createVPC();
const bastionSecurityGroup = this.createBastionSecurityGroup(vpc);
this.createBastionHost(bastionSecurityGroup, vpc);
}
private createVPC() {
const vpc = new Vpc(this, 'Vpc', { natGateways: 1 });
return vpc;
}
private createBastionSecurityGroup(vpc: Vpc) {
const bastionSecurityGroup = new SecurityGroup(this, 'BastionSecurityGroup', { vpc, allowAllOutbound: true });
bastionSecurityGroup.connections.allowFrom(
bastionSecurityGroup,
Port.allTraffic(),
'Allow inbound traffic to the Bastion Host from its security group',
);
bastionSecurityGroup.addIngressRule(
Peer.anyIpv4(),
Port.tcp(22),
'Allow inbound traffic to the Bastion Host on port 22.',
);
return bastionSecurityGroup;
}
private createBastionHost(securityGroup: SecurityGroup, vpc: Vpc) {
new BastionHostLinux(this, 'BastionHost', {
vpc,
instanceType: InstanceType.of(InstanceClass.T3, InstanceSize.NANO),
securityGroup,
subnetSelection: vpc.selectSubnets({ subnets: [vpc.publicSubnets[0]] }),
});
}
}
问题是我没有指定 AMI(Amazon 机器映像)。 因此,每次发布新版本的图像时,都会在部署时重新创建实例。