微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 CDK 部署上重新创建 EC2

如何解决在 CDK 部署上重新创建 EC2

我们所有的堆栈资源都在 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(亚马逊机器映像)。 因此,每次发布新版本的镜像时,都会在部署时重新创建实例。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。