amazon-web-services - 如何访问CDK创建的EC2实例?

标签 amazon-web-services aws-cdk

我正在使用此CDK类设置基础结构:

// imports

export interface AppStackProps extends cdk.StackProps {
    repository: ecr.Repository
}

export class AppStack extends cdk.Stack {
    public readonly service: ecs.BaseService

    constructor(app: cdk.App, id: string, props: AppStackProps) {
        super(app, id, props)

        const vpc = new ec2.Vpc(this, 'main', { maxAzs: 2 })

        const cluster = new ecs.Cluster(this, 'x-workers', { vpc })
        cluster.addCapacity('x-workers-asg', {
            instanceType: ec2.InstanceType.of(ec2.InstanceClass.T2, ec2.InstanceSize.MICRO)
        })

        const logging = new ecs.AwsLogDriver({ streamPrefix: "x-logs", logRetention: logs.RetentionDays.ONE_DAY })

        const taskDef = new ecs.Ec2TaskDefinition(this, "x-task")
        taskDef.addContainer("x-container", {
            image: ecs.ContainerImage.fromEcrRepository(props.repository),
            memoryLimitMiB: 512,
            logging,
        })

        this.service = new ecs.Ec2Service(this, "x-service", {
            cluster,
            taskDefinition: taskDef,
        })
    }
}

当我检查AWS面板时,我看到创建的实例没有 key 对。

在这种情况下,如何访问实例?

最佳答案

您可以转到您的AWS账户> EC2> key 对,创建一个新的 key 对,然后可以将 key 名称传递给正在创建的集群:

const cluster = new ecs.Cluster(this, 'x-workers', { vpc })
        cluster.addCapacity('x-workers-asg', {
            instanceType: ec2.InstanceType.of(ec2.InstanceClass.T2, ec2.InstanceSize.MICRO),
            keyName: "x" // this can be a parameter if you prefer
        })

我通过在CloudFormation上阅读this article得到了这个主意。

关于amazon-web-services - 如何访问CDK创建的EC2实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57572065/

相关文章:

amazon-web-services - 当 AWS 节点组没有出现在 CloudFormation 中并且仅在尝试删除父集群或创建新集群时似乎存在时,如何销毁 AWS 节点组?

python - 如何创建在Python CDK中引用自身的API网关资源策略?

typescript - 如何使用 Typescript 的 CDK 配置发送(apigateway)和获取(lambda)请求查询参数

aws-cloudformation - 外部依赖项的 AWS CDK 模式

aws-cdk - 如何测试 CDK 上下文键是否存在?

amazon-web-services - 如何从 Serverless(Cloud Formation)迁移到 AWS CDK(云开发工具包)

ios - 如何使我的 iOS 应用程序安全地与 AWS S3 一起工作

amazon-web-services - 在AWS Beanstalk上部署docker仅提供html文件,不提供其他文件

aws-cloudformation - 如何使用 cdk 升级自定义 eks 节点组版本?

amazon-web-services - AWS S3 : An error occurred (AccessDenied) when calling the GetObject operation: Access Denied