AWS API Gateway集成CloudWatch的正确服务名称是什么?CDK配置求助
问题描述
我正尝试使用AWS CDK/CloudFormation创建与CloudWatch集成的AWS API Gateway,但遇到错误提示"不支持cloudwatch类型的AWS服务"。使用CloudWatch Logs时可正常工作,但仅集成CloudWatch时失败。
原代码示例:
new AwsIntegrationProps { Region = copilotFoundationalInfrastructure.Region, Options = new IntegrationOptions { PassthroughBehavior = PassthroughBehavior.WHEN_NO_TEMPLATES, CredentialsRole = Role.FromRoleArn(this,"CloudWatchAccessRole", "arn:aws:iam::800524210815:role/APIGatewayCloudWatchRole"), RequestParameters = new Dictionary<string, string>() { { "integration.request.header.Content-Encoding", "'amz-1.0'" }, { "integration.request.header.Content-Type", "'application/json'" }, { "integration.request.header.X-Amz-Target", "'GraniteServiceVersion20100801.PutMetricData'" }, }, }, IntegrationHttpMethod = "POST", Service = "cloudwatch", // 该值在s3和logs场景下正常工作 Action = "PutMetricData" }
请问调用PutMetricData时,CloudWatch对应的正确Service参数值是什么?
待修正代码:
new AwsIntegrationProps { Region = copilotFoundationalInfrastructure.Region, Options = new IntegrationOptions { PassthroughBehavior = PassthroughBehavior.WHEN_NO_TEMPLATES, CredentialsRole = Role.FromRoleArn(this,"CloudWatchAccessRole", "arn:aws:iam::800524210815:role/APIGatewayCloudWatchRole"), RequestParameters = new Dictionary<string, string>() { { "integration.request.header.Content-Encoding", "'amz-1.0'" }, { "integration.request.header.Content-Type", "'application/json'" }, { "integration.request.header.X-Amz-Target", "'GraniteServiceVersion20100801.PutMetricData'" }, }, }, IntegrationHttpMethod = "POST", Service = "", // CloudWatch对应的正确值是什么? Action = "PutMetricData" }
解决方案
调用CloudWatch的PutMetricData接口时,正确的Service参数值是monitoring,而非cloudwatch。
API Gateway的AWS集成对不同AWS服务使用特定的标识符:
- CloudWatch核心监控服务对应的标识符是
monitoring - CloudWatch Logs对应的标识符是
logs
修正后的代码示例:
new AwsIntegrationProps { Region = copilotFoundationalInfrastructure.Region, Options = new IntegrationOptions { PassthroughBehavior = PassthroughBehavior.WHEN_NO_TEMPLATES, CredentialsRole = Role.FromRoleArn(this,"CloudWatchAccessRole", "arn:aws:iam::800524210815:role/APIGatewayCloudWatchRole"), RequestParameters = new Dictionary<string, string>() { { "integration.request.header.Content-Encoding", "'amz-1.0'" }, { "integration.request.header.Content-Type", "'application/json'" }, { "integration.request.header.X-Amz-Target", "'GraniteServiceVersion20100801.PutMetricData'" }, }, }, IntegrationHttpMethod = "POST", Service = "monitoring", // 正确的CloudWatch服务标识符 Action = "PutMetricData" }
内容的提问来源于stack exchange,提问作者Shripati Ukirde
相关产品推荐
相关产品推荐

