默认 API 网关响应采用以下形式:{"message":$context.error.messageString}。通过控制台更改此模板,"error"例如将其包装在对象中,效果很好。但是,当我在 CDK 中尝试时,在部署过程中出现错误Invalid mapping expression specified
代码(注意这实际上是默认模板):
webServiceApi.addGatewayResponse('4XX', {
type: apigw.ResponseType.DEFAULT_4XX,
responseHeaders: COMMON_HEADERS,
templates: {
'application/json': '{"message":$context.error.messageString}',
},
});
Run Code Online (Sandbox Code Playgroud)
任何更改都会发生这种情况,包括我能找到的所有 AWS 编写的示例。AWS 中的各个地方都没有充分记录:CDK、SDK、API Gateway 和 CloudFormation,因此我很难找出那里出了什么问题?
云形成错误
Resource handler returned message: "Invalid mapping expression specified: Validation Result: warnings : [], errors : [Invalid mapping expression specified: *] (Service: ApiGateway, Status Code: 400, Request ID: [TRUNCATED], Extended Request ID: null)" (RequestToken: [TRUNCATED], HandlerErrorCode: InvalidRequest)
Run Code Online (Sandbox Code Playgroud) amazon-web-services aws-cloudformation aws-lambda aws-api-gateway aws-cdk
我有一个创建了 S3Bucket 的 ApplicationStack:
export class ApplicationStack extends Cdk.Stack {
public readonly websiteBucket : S3.Bucket;
constructor(scope: Construct, id: string, props: ApplicationStackProps) {
super(scope, id, props);
// Amazon S3 bucket to host the store website artifact
this.websiteBucket = new S3.Bucket(this, "eCommerceWebsite", {
bucketName: `${props.websiteDomain}-${account}-${region}`,
websiteIndexDocument: "index.html",
websiteErrorDocument: "error.html",
removalPolicy: Cdk.RemovalPolicy.DESTROY,
autoDeleteObjects: true,
accessControl: S3.BucketAccessControl.PRIVATE,
encryption: S3.BucketEncryption.S3_MANAGED,
publicReadAccess: false,
blockPublicAccess: S3.BlockPublicAccess.BLOCK_ALL,
});
// Create a dummy export.
// https://www.endoflineblog.com/cdk-tips-03-how-to-unblock-cross-stack-references
this.exportValue(this.websiteBucket.bucketArn);
...
...
...
}
}
Run Code Online (Sandbox Code Playgroud)
我还定义了ApplicationStage上面包含的内容ApplicationStack
export class ApplicationStage …Run Code Online (Sandbox Code Playgroud) amazon-web-services aws-cloudformation aws-codepipeline aws-cdk
我有一个 Cloudformation 模板,可以创建 WAFv2 以及 Cloudwatch Logging。我在尝试设置 LoggingConfiguration 时遇到问题。我得到的实际错误看起来像这样:
Resource handler returned message: "Error reason: The ARN isn't valid. A valid ARN begins with arn: and includes other information separated by colons or slashes., field: LOG_DESTINATION, parameter: arn:aws:logs:us-east-1:xxxxx:log-group:aws-waf-bar-foo:*
Run Code Online (Sandbox Code Playgroud)
我的 LoggingConfiguration 看起来像这样:
"webAcllogging": {
"Type": "AWS::WAFv2::LoggingConfiguration",
"Properties": {
"ResourceArn": {
"Fn::GetAtt": [
"webAcl",
"Arn"
]
},
"LogDestinationConfigs": [
{
"Fn::Sub": "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:aws-waf-bar-foo:*"
}
],
"RedactedFields": [
{
"SingleHeader": {
"Name": "password"
}
}
]
}
},
Run Code Online (Sandbox Code Playgroud)
我尝试更改一些内容,但仍然遇到此错误。有人知道为什么吗?
我试图让我的CloudFormation堆栈在完成时删除自身。当我在模板中尝试以下代码时,日志显示未找到文件或命令。
当我使用runuser执行其他AWS CLI命令时,我没有问题(只要该命令不需要以“-”开头的选项)。
我正在使用基本的AWS IAM。
"06_delete_stack": { "command": { "Fn::Join": [ "", [
"runuser -u fhwa 'aws cloudformation delete-stack --stack-name ", { "Ref": "StackName" }, "'"
] ] },
"cwd": "/var/log"}
Run Code Online (Sandbox Code Playgroud) 我不想创建新的子网和VPC,已经创建了它们,并且希望我的cloudformation模板使用它们。
我在哪个参数中指定此参数,或者我对此工作方式感到困惑?
当我查看“ AWS :: EC2 :: VPC”和“ AWS :: EC2 :: Subnet”的文档时,似乎这些资源仅用于创建新的 VPC,并且子网正确吗?
我是否应该仅将实例资源直接指向我要使用的现有VPC和子网?
编辑:
例如,如果我的模板中有一个实例资源,并且将其直接指向这样的现有子网:
{
"Resources": {
"MyServer": {
"Type": "AWS::EC2::Instance",
"Properties": {
"InstanceType": {
"Ref": "InstanceType"
},
"SubnetId": {
"Ref": "subnet-abc123"
},
...
Run Code Online (Sandbox Code Playgroud)
验证模板时出现此错误:
Template contains errors.: Template format error: Unresolved resource dependencies [subnet-abc123] in the Resources block of the template
Run Code Online (Sandbox Code Playgroud)
我尝试通过映射执行此操作,但仍然出现错误。我在映射中有这个:
"Mappings": {
"SubnetID": {
"TopKey": {
"Default": "subnet-abc123"
}
}
Run Code Online (Sandbox Code Playgroud)
而这在实例资源中:
"SubnetId": {
"Fn::FindInMap": [
"SubnetID",
{
"Ref": "TopKey"
},
"Default"
]
} …Run Code Online (Sandbox Code Playgroud) amazon-ec2 amazon-web-services aws-cloudformation amazon-vpc
AWS文档无济于事,它只是说说属性为CognitoEventsis String: String。我在GitHub上找到了一个暗示它的主题Event: Lambda ARN,但没有具体说明事件可能是什么(我可能会猜中syncTrigger)。
知道什么事件会填充模板的CognitoEvents属性AWS::Cognito::IdentityPool吗?
在CloudFormation堆栈创建期间,我得到了CREATE_FAILEDAurora 的错误DB Instance quota exceeded.
我认为这意味着有太多的数据库实例正在运行(我在一个公司帐户上有一个联合登录,而不是唯一一个创建堆栈和启动实例).
我如何解决它?是否需要删除一些正在运行的实例?另外,我如何找出允许的配额是多少?
堆栈:AWS
服务:云形成
我要实现的目标:尝试使用Cloud形成方式来形成VPC
更多细节 :
1,尝试着云的形成
2.在使用云形成[JSON]构建VPC方面具有逐步的方法。
面临的问题:使用第一个模板,仅VPC成功创建。当我尝试使用Internet网关更新堆栈并附加到VPC时,开始出现错误“模板验证错误:无效的模板资源属性'VPCID'。
JSON模板如下
{
"Parameters": {
"CIDRRange": {
"Description": "VPCCIDR Range (will be a /16 block)",
"Type": "String",
"Default": "10.251.0.0",
"AllowedValues": ["10.250.0.0","10.251.0.0"]
}
},
"Resources": {
"VPCBase": {
"Type": "AWS::EC2::VPC",
"Properties": {
"CidrBlock": { "Fn::Join" : ["", [{ "Ref" : "CIDRRange" }, "/16"]] },
"EnableDnsSupport": "True",
"EnableDnsHostnames": "True",
"Tags": [{ "Key": "Name", "Value": { "Fn::Join" : ["", [{ "Ref" : "AWS::StackName" }, "-VPC"]] } }]
}
},
"IGWBase" : …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用AWS cloudformation创建具有ALB和ECS服务的堆栈,但是我在CREATE_FAILED上看到了AWS::ECS::Service,这是elb name longer than 32。
我不明白为什么当ALB本身处于CREATE_COMPLETE状态时ECS服务抱怨ELB名称…
这是与我发送到cloudformation的ALB创建相关的JSON:
"loadBalancer" : {
"Type": "AWS::ElasticLoadBalancingV2::LoadBalancer",
"Properties": {
"Name": "test-alb",
"Scheme" : "internal",
"Subnets" : [
"subnet-b8217295",
"subnet-ddaad2b8",
"subnet-6d71fb51"
],
"LoadBalancerAttributes" : [
{ "Key" : "idle_timeout.timeout_seconds", "Value" : "50" }
],
"SecurityGroups": [
{ "Ref": "InstanceSecurityGroupOpenWeb" },
{ "Ref" : "InstanceSecurityGroupOpenFull" }
],
"Tags" : [
{ "Key" : "key", "Value" : "value" },
{ "Key" : "key2", "Value" : "value2" }
]
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个防止CloudFormation删除表的AWS角色。例如,我创建了如下表:
UsersDynamoDBTable:
Type: AWS::DynamoDB::Table
Description: Users DynamoDB Table
Properties:
AttributeDefinitions:
- AttributeName: hashKey
AttributeType: S
- AttributeName: rangeKey
AttributeType: S
KeySchema:
- AttributeName: hashKey
KeyType: HASH
- AttributeName: rangeKey
KeyType: RANGE
BillingMode: PAY_PER_REQUEST
GlobalSecondaryIndexes:
- IndexName: index-rangeKey
KeySchema:
- AttributeName: rangeKey
KeyType: HASH
- AttributeName: hashKey
KeyType: RANGE
Projection:
ProjectionType: ALL
Run Code Online (Sandbox Code Playgroud)
现在,假设开发人员意外删除了这些行并更新了堆栈。这样,表及其所有数据将被删除。因此,我想创建一个防止CloudFormation删除DynamoDB表的角色。我的第一个尝试是在下面创建“角色”,但是没有用。
PreventCloudFormationDeleteTableIAMRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- cloudformation.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: PreventTableDeletePolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: …Run Code Online (Sandbox Code Playgroud) amazon-vpc ×2
aws-cdk ×2
amazon-ec2 ×1
amazon-ecs ×1
amazon-iam ×1
amazon-rds ×1
aws-cli ×1
aws-iam ×1
aws-lambda ×1