登录
首页 >  Golang >  Go问答

Golang AWS CDK自动缩放未定义类型

来源:stackoverflow

时间:2024-04-13 10:06:32 282浏览 收藏

对于一个Golang开发者来说,牢固扎实的基础是十分重要的,golang学习网就来带大家一点点的掌握基础知识点。今天本篇文章带大家了解《Golang AWS CDK自动缩放未定义类型》,主要介绍了,希望对大家的知识积累有所帮助,快点收藏起来吧,否则需要时就找不到了!

问题内容

我正在使用适用于 golang 的 aws cdk,但收到以下未定义的错误:

cd cdk && cdk deploy
# command-line-arguments
./ecs-run-task.go:54:22: undefined: autoscaling
./ecs-run-task.go:54:54: undefined: this
./ecs-run-task.go:54:81: undefined: autoscalinggroupprops
./ecs-run-task.go:62:69: undefined: cpuutilizationscalingprops
./ecs-run-task.go:66:72: undefined: cpuutilizationscalingprops
./ecs-run-task.go:70:22: undefined: ecs
./ecs-run-task.go:70:49: undefined: this
./ecs-run-task.go:70:92: undefined: asgcapacityproviderprops
./ecs-run-task.go:73:9: cluster.addasgcapacityprovider undefined (type awsecs.cluster has no field or method addasgcapacityprovider, but does have addasgcapacityprovider)
./ecs-run-task.go:114:72: cannot refer to unexported name awsecs.taskdefinitionprops
./ecs-run-task.go:73:9: too many errors

我导入了这些包:

"strings"
"github.com/aws/aws-cdk-go/awscdk"
"github.com/aws/aws-cdk-go/awscdk/awsec2"
"github.com/aws/aws-cdk-go/awscdk/awsecs"
"github.com/aws/aws-cdk-go/awscdk/awsiam"
"github.com/aws/aws-cdk-go/awscdk/awslambda"
"github.com/aws/aws-cdk-go/awscdk/awslambdaeventsources"
"github.com/aws/aws-cdk-go/awscdk/awslambdago"
"github.com/aws/aws-cdk-go/awscdk/awss3"
"github.com/aws/constructs-go/constructs/v3"
"github.com/aws/jsii-runtime-go"

这是我尝试创建并附加自动缩放组的源代码:

// Create the cluster.
cluster := awsecs.NewCluster(stack, jsii.String("ecsCluster"), &awsecs.ClusterProps{
    Vpc: vpc,
    EnableFargateCapacityProviders: newTrue(),
})

//Creating auto scaling group to attach to cluster
autoScalingGroup := autoscaling.NewAutoScalingGroup(this, jsii.String("ASG"), &autoScalingGroupProps{
    vpc: vpc,
    instanceType: ec2.NewInstanceType(jsii.String("t2.micro")),
    machineImage: ecs.ecsOptimizedImage.amazonLinux2(),
    minCapacity: jsii.Number(0),
    maxCapacity: jsii.Number(100),
})

autoScalingGroup.scaleOnCpuUtilization(jsii.String("CpuScaling"), &cpuUtilizationScalingProps{
    targetUtilizationPercent: jsii.Number(90),
})

autoScalingGroup.scaleOnCpuUtilization(jsii.String("MemoryScaling"), &cpuUtilizationScalingProps{
    targetUtilizationPercent: jsii.Number(90),
})

capacityProvider := ecs.NewAsgCapacityProvider(this, jsii.String("AsgCapacityProvider"), &asgCapacityProviderProps{
    autoScalingGroup: autoScalingGroup,
})
cluster.addAsgCapacityProvider(capacityProvider)

有谁知道我需要导入什么或做什么才能访问自动缩放等功能。 aws 上的 cdk 文档和此处 https://pkg.go.dev/github.com/aws/aws-cdk-go/awscdk/v2/awsautoscaling#section-readme 显示了所有这些工具的示例,没有任何问题,但我没有运气好的话,尝试了从 cdk v2 和其他地方多次导入。


正确答案


要导入“github.com/aws/aws-cdk-go/awscdk/awsapplicationautoscaling”的包

// AutoScaling based on memory and cpu usage
        scalabletargetPub := pubService.AutoScaleTaskCount(&awsapplicationautoscaling.EnableScalingProps{
            MinCapacity: &apps.ApplicationVpc.ECS.AppMktNodeCountMin,
            MaxCapacity: &apps.ApplicationVpc.ECS.AppMktNodeCountMax,
        })

        scalabletargetPub.ScaleOnMemoryUtilization(jsii.String("Memory_threshold"), &ecs.MemoryUtilizationScalingProps{
            TargetUtilizationPercent: &apps.ApplicationVpc.ECS.AppMemoryUtilizationThresholdPercent,
        })
        scalabletargetPub.ScaleOnCpuUtilization(jsii.String("Cpu_threshold"), &ecs.CpuUtilizationScalingProps{
            TargetUtilizationPercent: &apps.ApplicationVpc.ECS.AppCPUUtilizationThresholdPercent,
        })

以上就是《Golang AWS CDK自动缩放未定义类型》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>