我最近在 CDK github 帐户上提出了一个功能请求,并指出了 Core.Token 的方向,因为它几乎正是我正在寻找的功能。我现在在实现它时遇到一些问题并收到类似的错误,这是我之前提出的功能请求:https://github.com/aws/aws-cdk/issues/3800
所以我当前的代码看起来像这样:
fargate_service = ecs_patterns.LoadBalancedFargateService(
self, "Fargate",
cluster = cluster,
memory_limit_mib = core.Token.as_number(ssm.StringParameter.value_from_lookup(self, parameter_name='template-service-memory_limit')),
execution_role=fargate_iam_role,
container_port=core.Token.as_number(ssm.StringParameter.value_from_lookup(self, parameter_name='port')),
cpu = core.Token.as_number(ssm.StringParameter.value_from_lookup(self, parameter_name='template-service-container_cpu')),
image=ecs.ContainerImage.from_registry(ecrRepo)
)
当我尝试综合此代码时,出现以下错误:
jsii.errors.JavaScriptError:
Error: Resolution error: Supplied properties not correct for "CfnSecurityGroupEgressProps"
fromPort: "dummy-value-for-template-service-container_port" should be a number
toPort: "dummy-value-for-template-service-container_port" should be a number.
Object creation stack:
对我来说,它似乎已经通过了验证,需要将一个数字传递到 FargateService 验证中,但是当它尝试在此之后创建资源(“CfnSecurityGroupEgressProps”)时,它无法将虚拟字符串解析为数字。我非常感谢任何有关解决此问题的帮助或从 AWS 系统参数传递值的替代建议(我认为可以通过在构建管道期间从 S3 提取的文件或类似的方式将值解析到此处,但这看起来很老套)。
最佳答案
在一些帮助下,我想我们已经解决了这个问题!
问题是我传递“ssm.StringParameter.value_from_lookup”,解决方案是为 token 提供“ssm.StringParameter.value_for_string_parameter”,当合成它时,它存储 token ,然后在部署时存储在系统中的值参数存储被替换。
(我们还提出了另一种方法来实现类似的效果,我们可能会在 SSM 方法上使用它,如果您感兴趣的话,我在代码片段下面详细介绍了)
请参阅下面的完整代码:
from aws_cdk import (
aws_ec2 as ec2,
aws_ssm as ssm,
aws_iam as iam,
aws_ecs as ecs,
aws_ecs_patterns as ecs_patterns,
core,
)
class GenericFargateService(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
containerPort = core.Token.as_number(ssm.StringParameter.value_for_string_parameter(
self, 'template-service-container_port'))
vpc = ec2.Vpc(
self, "cdk-test-vpc",
max_azs=2
)
cluster = ecs.Cluster(
self, 'cluster',
vpc=vpc
)
fargate_iam_role = iam.Role(self,"execution_role",
assumed_by = iam.ServicePrincipal("ecs-tasks"),
managed_policies=[iam.ManagedPolicy.from_aws_managed_policy_name("AmazonEC2ContainerRegistryFullAccess")]
)
fargate_service = ecs_patterns.LoadBalancedFargateService(
self, "Fargate",
cluster = cluster,
memory_limit_mib = 1024,
execution_role=fargate_iam_role,
container_port=containerPort,
cpu = 512,
image=ecs.ContainerImage.from_registry("000000000000.dkr.ecr.eu-west-1.amazonaws.com/template-service-ecr")
)
fargate_service.target_group.configure_health_check(path=self.node.try_get_context("health_check_path"), port="9000")
app = core.App()
GenericFargateService(app, "generic-fargate-service", env={'account':'000000000000', 'region': 'eu-west-1'})
app.synth()
问题的解决方案就像公共(public)汽车,显然你要花很长时间等待一辆,然后两辆一起到达。我认为这辆新巴士是我们可能会选择的选择。
计划是让开发人员使用其代码存储库提供 cdk.json 文件的覆盖,然后将其解析到将合成通用代码的 CDK 管道中。该文件将包含一些“上下文”,然后将在 CDK 中使用该上下文来设置 LoadBalancedFargate 服务的变量。
我包含了一些用于设置 cdk.json 文件的代码片段,然后在下面的代码中使用其值。
CDK.json 示例:
{
"app": "python3 app.py",
"context": {
"container_name":"template-service",
"memory_limit":1024,
"container_cpu":512,
"health_check_path": "/gb/template/v1/status",
"ecr_repo": "000000000000.dkr.ecr.eu-west-1.amazonaws.com/template-service-ecr"
}
}
为变量分配上下文的 Python 示例:
memoryLimitMib = self.node.try_get_context("memory_limit")
我相信,如果开发人员未在其 CDK.json 文件中提供这些默认值,我们也可以使用 Try/Catch block 为其分配一些默认值。
我希望这篇文章为那些寻找创建部署 CDK 代码的通用模板的方法的人提供了一些有用的信息!我不知道我们在这里做的事情是否正确,但这个工具太新了,感觉一些常见模式还不存在。
关于python - 使用 core.Token 将字符串参数作为数字传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57770301/