英文:
AWS cdk stack hangs at last steps without error message, but works anyway. I don't know why
问题
我是新手cdk/aws,对它了解不多。我只是在构建一个node-express应用程序来使用ecr,然后部署它。这似乎运行正常,我可以访问端点和一切,但是cdk卡住了。无论我是从管道部署还是在本地使用cdk部署都无关紧要。
import * as cdk from 'aws-cdk-lib';
import { CfnOutput, Duration } from 'aws-cdk-lib';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as ecr from 'aws-cdk-lib/aws-ecr';
import * as ecs from 'aws-cdk-lib/aws-ecs';
import * as elbv2 from 'aws-cdk-lib/aws-elasticloadbalancingv2';
import * as iam from 'aws-cdk-lib/aws-iam';
import { Construct } from 'constructs';
import { Networking } from '../imports/Networking';
interface DatabaseProps extends cdk.StackProps {
databaseName: string,
databasePass: string
}
export class WebApiStack extends cdk.Stack {
constructor(scope: Construct, id: string, props: DatabaseProps) {
super(scope, id, props);
const vpc = Networking.createVpc(this);
const executionRolePolicy = new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
resources: ['*'],
actions: [
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
});
const repository = ecr.Repository.fromRepositoryName(this, 'nodejs-repo', 'api')
//Find a way to make it a generic object
const cluster = new ecs.Cluster(this, 'Cluster', { vpc });
// Add capacity to it
cluster.addCapacity('DefaultAutoScalingGroupCapacity', {
instanceType: new ec2.InstanceType("t2.micro"),
});
const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDef');
const container = taskDefinition.addContainer('fullstack', {
image: new ecs.EcrImage(repository, 'latest'),
cpu: 128,
memoryReservationMiB: 256,
logging: ecs.LogDrivers.awsLogs({ streamPrefix: 'nodejs' }),
environment: {
DatabasePassword: props.databasePass,
DatabaseName: props.databaseName,
ME_CONFIG_MONGODB_URL: "mongodb+srv://fake.mongodb.net/",
SECRET_API:"fakeszzz",
SECRET_COOKIE:'fakezz'
}
});
// Instantiate an Amazon ECS Service
const ecsService = new ecs.Ec2Service(this, 'Service', {
cluster,
taskDefinition,
});
taskDefinition.addToExecutionRolePolicy(executionRolePolicy);
container.addPortMappings({
containerPort: 3000
});
// Setup AutoScaling policy
const scaling = ecsService.autoScaleTaskCount({ maxCapacity: 3, minCapacity: 1 });
scaling.scaleOnCpuUtilization('CpuScaling', {
targetUtilizationPercent: 90,
scaleInCooldown: Duration.seconds(60),
scaleOutCooldown: Duration.seconds(60)
});
const lb = new elbv2.ApplicationLoadBalancer(this, 'ALB', {
vpc,
internetFacing: true
});
const listener = lb.addListener('Listener', {
port: 80,
});
listener.addTargets('Target', {
port: 80,
targets: [ecsService],
healthCheck: { path: '/api/' }
});
listener.connections.allowDefaultPortFromAnyIpv4('Open to the world');
new CfnOutput(this, 'webApi', { value: container.imageName });
}
}
它在没有错误的情况下失败,显示以下信息:
WebApiStack | 48/53 | 3:27:26 AM | CREATE_COMPLETE | AWS::SNS::Subscription | Cluster/DefaultAutoScalingGroupCapacity/DrainECSHook/Function/Topic (ClusterDefaultAutoScalingGroupCapacityDrainECSHookFunctionTopicC6667F16)
WebApiStack | 49/53 | 3:27:35 AM | CREATE_COMPLETE | AWS::Lambda::Permission | Cluster/DefaultAutoScalingGroupCapacity/DrainECSHook/Function/AllowInvoke:WebApiStackClusterDefaultAutoScalingGroupCapacityLifecycleHookDrainHookTopic27A34C51 (ClusterDefaultAutoScalingGroupCapacityDrainECSHookFunctionAllowInvokeWebApiStackClusterDefaultAutoScalingGroupCapacityLifecycleHookDrainHookTopic27A34C51E27DA75C)
49/53 Currently in progress: WebApiStack, ServiceD69D759B
英文:
I'm new to cdk/aws and don't really know much about it. I'm just building a node-express app to ecr, and then deploying it. This appears to work fine and I can access to endpoints and everything, however cdk is getting stuck. Doesn't matter if I deploy from pipeline or locally using cdk.
import * as cdk from 'aws-cdk-lib';
import { CfnOutput, Duration } from 'aws-cdk-lib';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as ecr from 'aws-cdk-lib/aws-ecr';
import * as ecs from 'aws-cdk-lib/aws-ecs';
import * as elbv2 from 'aws-cdk-lib/aws-elasticloadbalancingv2';
import * as iam from 'aws-cdk-lib/aws-iam';
import { Construct } from 'constructs';
import { Networking } from '../imports/Networking';
interface DatabaseProps extends cdk.StackProps {
databaseName: string,
databasePass: string
}
export class WebApiStack extends cdk.Stack {
constructor(scope: Construct, id: string, props: DatabaseProps) {
super(scope, id, props);
const vpc = Networking.createVpc(this);
const executionRolePolicy = new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
resources: ['*'],
actions: [
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
});
const repository = ecr.Repository.fromRepositoryName(this, 'nodejs-repo', 'api')
//Find a way to make it a generic object
const cluster = new ecs.Cluster(this, 'Cluster', { vpc });
// Add capacity to it
cluster.addCapacity('DefaultAutoScalingGroupCapacity', {
instanceType: new ec2.InstanceType("t2.micro"),
});
const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDef');
const container = taskDefinition.addContainer('fullstack', {
image: new ecs.EcrImage(repository, 'latest'),
cpu: 128,
memoryReservationMiB: 256,
logging: ecs.LogDrivers.awsLogs({ streamPrefix: 'nodejs' }),
environment: {
DatabasePassword: props.databasePass,
DatabaseName: props.databaseName,
ME_CONFIG_MONGODB_URL: "mongodb+srv://fake.mongodb.net/",
SECRET_API:"fakeszzz",
SECRET_COOKIE:'fakezz'
}
});
// Instantiate an Amazon ECS Service
const ecsService = new ecs.Ec2Service(this, 'Service', {
cluster,
taskDefinition,
});
taskDefinition.addToExecutionRolePolicy(executionRolePolicy);
container.addPortMappings({
containerPort: 3000
});
// Setup AutoScaling policy
const scaling = ecsService.autoScaleTaskCount({ maxCapacity: 3, minCapacity: 1 });
scaling.scaleOnCpuUtilization('CpuScaling', {
targetUtilizationPercent: 90,
scaleInCooldown: Duration.seconds(60),
scaleOutCooldown: Duration.seconds(60)
});
const lb = new elbv2.ApplicationLoadBalancer(this, 'ALB', {
vpc,
internetFacing: true
});
const listener = lb.addListener('Listener', {
port: 80,
});
listener.addTargets('Target', {
port: 80,
targets: [ecsService],
healthCheck: { path: '/api/' }
});
listener.connections.allowDefaultPortFromAnyIpv4('Open to the world');
new CfnOutput(this, 'webApi', { value: container.imageName });
}
}
It fails with the following, without errors.
WebApiStack | 48/53 | 3:27:26 AM | CREATE_COMPLETE | AWS::SNS::Subscription | Cluster/DefaultAutoScalingGroupCapacity/DrainECSHook/Function/Topic (ClusterDefaultAutoScalingGroupCapacityDrainECSHookFunctionTopicC6667F16)
WebApiStack | 49/53 | 3:27:35 AM | CREATE_COMPLETE | AWS::Lambda::Permission | Cluster/DefaultAutoScalingGroupCapacity/DrainECSHook/Function/AllowInvoke:WebApiStackClusterDefaultAutoScalingGroupCapacityLifecycleHookDrainHookTopic27A34C51 (ClusterDefaultAutoScalingGroupCapacityDrainECSHookFunctionAllowInvokeWebApiStackClusterDefaultAutoScalingGroupCapacityLifecycleHookDrainHookTopic27A34C51E27DA75C)
49/53 Currently in progress: WebApiStack, ServiceD69D759B
答案1
得分: 0
已通过删除ecsService上的healthcheck路径来解决该问题,因为该端点不存在。
英文:
Solved it by removing the healthcheck path on that ecsService since that endpoint doesnt exist.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论