我正在使用AWS CDK创建一个应该是简单的基础设施:
我可以将其全部启动并运行,但无法访问why服务器(例如,通过在浏览器中访问EC2实例的IP,或使用wget),而且我也不知道为什么无法访问它。
我尝试过/发现的事情:
AWS_VPC和BRIDGE网络模式,结果相同(一切都在运行,但无法连接到服务器)AWS_VPC模式,我将得到与EC2实例相关联的两个网络接口。即使我确保两个安全组都允许传入端口-80通信量,我仍然不能直接连接到EC2实例。容器之外的所有内容似乎都是为了能够通过web与EC2实例对话而设置的,所以我假设它是关于任务/容器本身的。我最好的猜测是,这与任务的网络模式有关,但我还没有找到一个能让我找到答案的配置或文档。
有人知道为什么我不能访问这个EC2实例吗?或者有任何类似CDK脚本的例子可供参考?
下面是我希望得到一个可访问的webserver (但不是)的最小CDK脚本,它使用演示nginx容器作为hello-world:
import {
Stack,
StackProps,
aws_ec2 as ec2,
aws_ecs as ecs,
} from 'aws-cdk-lib';
import { Construct } from 'constructs';
export class MyStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
// VPC
const vpc = new ec2.Vpc(this, 'VPC', {
enableDnsSupport: true,
enableDnsHostnames: true,
subnetConfiguration: [{ name: 'PublicSubnet', subnetType: ec2.SubnetType.PUBLIC }],
});
// ECS
const cluster = new ecs.Cluster(this, 'Cluster', {
vpc,
capacity: {
instanceType: ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.NANO),
machineImage: ecs.EcsOptimizedImage.amazonLinux(),
desiredCapacity: 1,
},
});
cluster.connections.allowFromAnyIpv4(ec2.Port.tcp(80));
// TASK DEFINITION
const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDef', {
networkMode: ecs.NetworkMode.AWS_VPC,
});
const container = taskDefinition.addContainer('HelloWorldContainer', {
image: ecs.ContainerImage.fromRegistry('nginxdemos/hello'),
memoryReservationMiB: 256,
portMappings: [
{
containerPort: 80,
protocol: ecs.Protocol.TCP,
},
],
});
const service = new ecs.Ec2Service(this, 'Service', {
cluster,
taskDefinition,
});
service.connections.allowFromAnyIpv4(ec2.Port.tcp(80));
}
}https://stackoverflow.com/questions/74483430
复制相似问题