我创建了一个 ECS 集群,如下所示:
this.cluster = new ecs.Cluster(this, 'Cluster', {
containerInsights: true,
vpc: ec2.Vpc.fromLookup(this, props.stage + 'Vpc', {isDefault: false})
});
Run Code Online (Sandbox Code Playgroud)
我想根据我的集群创建一个 CW 警报,如下所示:
const CPUHigh = new cw.Alarm(this, "CPUHigh", {
metric: this.cluster.metric("CPUUtilized"),
threshold: 50,
evaluationPeriods: 3,
period: cdk.Duration.seconds(60),
comparisonOperator: cw.ComparisonOperator.GREATER_THAN_THRESHOLD
})
Run Code Online (Sandbox Code Playgroud)
但即使该指标与 Container Insights 创建的指标相匹配,似乎也无法以这种方式引用。
有谁知道它应该如何引用?
我有一个代码,我需要将发送消息授予现有的 sqs 队列。
我在 aws-cdk 中有这段代码。但这是行不通的。没有添加访问权限。
const sqsQ = sqs.Queue.fromQueueArn(this, "some-id", "arn:aws:sqs:us-east-2:SOME-ACCOUNT:QUEUE-NAME");
sqsQ.grantSendMessages(new iam.ServicePrincipal("events.amazonaws.com"));
Run Code Online (Sandbox Code Playgroud) 如何使用 Secrets Manager 创建的 L2密钥来解析为 L1 Cfn 属性值?
from aws_cdk import (
core,
aws_secretsmanager as secretsmanager,
aws_elasticache as elasticache
)
class MyStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
redis_password = secretsmanager.Secret(
self, "RedisPassword",
description="Redis auth",
generate_secret_string=secretsmanager.SecretStringGenerator(
exclude_characters='/"@'
)
)
self.redis = elasticache.CfnReplicationGroup(self, 'RedisCluster',
auth_token=redis_password.secret_value,
# other properties
)
Run Code Online (Sandbox Code Playgroud)
这给出了错误
jsii.errors.JSIIError: Object of type @aws-cdk/aws-secretsmanager.Secret is not convertible to @aws-cdk/core.CfnElement
Run Code Online (Sandbox Code Playgroud)
在 Cloudformation 中解决秘密时我会使用类似的东西
jsii.errors.JSIIError: Object of type @aws-cdk/aws-secretsmanager.Secret is not convertible to @aws-cdk/core.CfnElement …Run Code Online (Sandbox Code Playgroud) python amazon-web-services aws-cloudformation aws-secrets-manager aws-cdk
我的 CDK 堆栈包含太多需要在命令行中指定的参数(子网 id、api url)。所以我想将它们保存在单独的文件中,例如 dev.properties 或 prod.json。cdk.json 中的上下文值可能是这样,但我不知道如何保留多个并行版本。有没有办法应用文件中的参数,例如 cdk deploy --parameters file:///dev.json?
我正在尝试使用带有 S3 和 S3 部署模块的 AWS CDK 将静态网页上传到 s3。问题是部署进展顺利,直到我收到一条错误,指出上传的文件必须是非空 zip。文档表明我应该能够使用目录,但我也尝试过使用 zip,但仍然存在相同的错误。不知道如何继续。
import * as CDK from "@aws-cdk/core";
import * as S3 from "@aws-cdk/aws-s3";
import * as S3Deployment from "@aws-cdk/aws-s3-deployment";
const path = "../website.zip";
export class WebsiteStack extends CDK.Stack {
constructor(app: CDK.App, id: string, props?: CDK.StackProps) {
super(app, id, props);
const bucket = new S3.Bucket(this, "Files", {
websiteIndexDocument: "index.html",
publicReadAccess: true,
});
new S3Deployment.BucketDeployment(this, "Deployment", {
sources: [S3Deployment.Source.asset(path)],
destinationBucket: bucket,
});
new CDK.CfnOutput(this, "BucketDomain", {
value: bucket.bucketWebsiteDomainName,
});
Run Code Online (Sandbox Code Playgroud)
} }
我正在将 AWS CDK 用于基础设施即代码(下面是 C# .NET 中的 CDK 代码片段)。
我创建了一个 VPC,如下所示:
// vpc (testing)
this.vpc = new Vpc(this, "vpc", new VpcProps() {
MaxAzs = 1,
NatGateways = 1
});
Run Code Online (Sandbox Code Playgroud)
我创建了一个集群,如下所示:
// cluster
this.cluster = new Cluster(this, "cluster", new ClusterProps() {
Vpc = this.vpc
});
Run Code Online (Sandbox Code Playgroud)
我对 Fargate 任务有一个工作人员定义,如下所示:
// worker
this.workerDefinition = new FargateTaskDefinition(this,
"worker-definition",
new FargateTaskDefinitionProps() {
Cpu = 256,
MemoryLimitMiB = 512
}
);
Run Code Online (Sandbox Code Playgroud)
我有 Fargate 工作线程服务,如下所示,它通过实例计数和任务定义将工作线程服务与集群联系起来:
var worker = new FargateService(this, "worker", new FargateServiceProps() {
Cluster = this.cluster, …Run Code Online (Sandbox Code Playgroud) 学习AWS CDK(来自terraform)。我目前正在努力,如何编写一段代码将允许我创建重定向,如下图所示:

到目前为止我拥有的代码:
class LoadBalancer(core.Construct):
def __init__(self, scope: core.Construct, construct_id: str, props):
super().__init__(scope, construct_id)
self.props = props
def load_balancer(self, security_group, fargate_service, arn_certificates_list):
load_balancer = alb.ApplicationLoadBalancer(
self,
"TestFargateALB",
vpc=self.props["vpc"],
deletion_protection=True if self.props["environment"].lower() == "prod" else False,
http2_enabled=True,
internet_facing=True,
security_group=security_group
)
load_balancer.set_attribute(key="routing.http.drop_invalid_header_fields.enabled", value="true")
http_load_balancer_listener = load_balancer.add_listener(
'http_listener', port=80, open=False
)
https_load_balancer_listener = load_balancer.add_listener(
"https_listener", port=443, open=False, certificate_arns=arn_certificates_list
)
https_target_group = https_load_balancer_listener.add_targets(
'ecs_target_group',
targets=[fargate_service],
port=80,
protocol=alb.ApplicationProtocol.HTTP,
deregistration_delay=core.Duration.seconds(amount=10)
)
https_target_group.configure_health_check(
healthy_http_codes="200,301,302",
healthy_threshold_count=3,
interval=core.Duration.seconds(10),
timeout=core.Duration.seconds(5),
unhealthy_threshold_count=5,
path="/"
)
http_load_balancer_listener.add_action(
'DefaultAction',
action=alb.ListenerAction(action_json=alb.CfnListener.ActionProperty(
type="redirect",
redirect_config=alb.CfnListener.RedirectConfigProperty(
status_code="HTTP_301",
host="#{host}", …Run Code Online (Sandbox Code Playgroud) 我想知道是否可以做这样的事情:
如果我将包括打字稿文件的完整 CDK 项目上传到 S3 存储桶,我可以设法完成这三个步骤。所以我的问题是是否有解决方法,这样您只需要 cdk.out 文件夹,而不需要 typescript 脚本本身。我的意思是该项目已经综合了?
当我尝试从 cdk.out 文件夹执行此操作时,它抱怨需要 cdk.json 文件。如果我使用 cdk.out 文件夹上传该文件,则会抱怨找不到打字稿文件。我认为这是因为我在 cdk.json 中有一个“app”:“npx ts-node bin/app.ts”。我不确定如何解决这个问题,以及是否可能。
如果不可能,我只需上传完整的项目,而不仅仅是 cdk.out 文件夹...
我有一个在不同堆栈中定义/管理的存储桶。该存储桶由 KMS 中管理的密钥加密。在我自己的堆栈中,我尝试创建一个角色,并分别授予该角色对存储桶和密钥的读取和解密权限。
我按如下方式引用存储桶和密钥:
const otherBucket = Bucket.fromBucketName(this, 'otherBucket', '<BucketName>');
const otherKeyArn = otherBucket.encryptionKey?.keyArn || '';
Run Code Online (Sandbox Code Playgroud)
我使用密钥 arn 为我的角色创建策略语句,并且它始终返回为 ''。我在堆栈中创建了另一个存储桶,当我尝试访问该存储桶的加密密钥时,我得到了该存储桶的正确密钥 arn。
fromBucketName导致此问题的方法是否存在错误?我目前必须将键的字符串 arn 作为硬编码值存储在我的常量文件中,有更好的方法吗?
Terraform 通过记录良好的插件拥有远程堆栈,即 terraform.backend.s3 https://www.terraform.io/docs/language/settings/backends/s3.html
aws cdk 可以为堆栈提供远程状态吗?我在文档中找不到。 https://docs.aws.amazon.com/cdk/latest/guide/awscdk.pdfstack
我询问有关 aws cdk 的问题,因为我找到了有关 aws cdktf 的纯文档。发现cloudcloudfront生成了很多json文件并且使用了它。是否包含状态?

amazon-web-services aws-cloudformation terraform terraform-provider-aws aws-cdk
aws-cdk ×10
amazon-s3 ×2
aws-fargate ×2
python ×2
amazon-ec2 ×1
amazon-ecs ×1
amazon-kms ×1
amazon-sqs ×1
aws-application-load-balancer ×1
javascript ×1
terraform ×1
typescript ×1